001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.workinprogress; 031 032import java.io.IOException; 033 034import org.openimaj.hardware.serial.SerialDevice; 035import org.openimaj.stream.provider.twitter.TwitterStreamDataset; 036import org.openimaj.util.api.auth.DefaultTokenFactory; 037import org.openimaj.util.api.auth.common.TwitterAPIToken; 038import org.openimaj.util.array.ArrayUtils; 039import org.openimaj.util.function.Operation; 040 041import twitter4j.Status; 042 043public class SignProgrammer { 044 private static final byte CLEAR = (byte) 0x8C; 045 private static final byte SPEED = (byte) 0x8D; 046 public static byte LEFT = (byte) 0x81; 047 public static byte RIGHT = (byte) 0x82; 048 public static byte END = (byte) 0x80; 049 public static byte FLASH = (byte) 0x88; 050 public static byte START = (byte) 0xAA; 051 public static byte ID = (byte) 0xBB; 052 public static byte WAIT = (byte) 0x8F; 053 public static byte JUMP = (byte) 0x85; // instant replace 054 public static byte RANDOM = (byte) 0x8E; 055 056 public static void main(String[] args) throws Exception { 057 final SerialDevice dev = new SerialDevice("/dev/tty.usbserial-FTB3MMNA", 2400, 8, 1, 0); 058 final byte[] init = { 059 START, START, START, START, 060 START, START, START, START, 061 ID, 062 LEFT }; 063 final byte[] end = { END }; 064 065 final byte[] cmd = ArrayUtils.concatenate(init, new byte[] { SPEED, 1, CLEAR }, 066 "Starting twitter display".getBytes(), end); 067 writeMessage(dev, cmd); 068 069 Thread.sleep(10000); 070 071 final TwitterStreamDataset data = new 072 TwitterStreamDataset(DefaultTokenFactory.get(TwitterAPIToken.class)); 073 data.forEach(new Operation<Status>() { 074 @Override 075 public void perform(Status object) { 076 try { 077 if (object.getIsoLanguageCode().equals("en")) { 078 final String tweet = object.getText().replaceAll("[^\\x00-\\x7F]", "") + " "; 079 final byte[] message = tweet.getBytes("US-ASCII"); 080 final byte[] cmd = ArrayUtils.concatenate(init, new byte[] { SPEED, 1, CLEAR }, message, end); 081 082 System.out.println("Sending command " + tweet); 083 writeMessage(dev, cmd); 084 System.out.println("Done"); 085 Thread.sleep(100 * message.length); 086 } 087 } catch (final Exception e) { 088 System.err.println(e); 089 } 090 } 091 }); 092 093 // dev.close(); 094 } 095 096 private static void writeMessage(final SerialDevice dev, final byte[] cmd) throws IOException { 097 for (int i = 0; i < cmd.length; i++) { 098 if (cmd[i] == ' ') { 099 cmd[i] = ':'; 100 } else if (cmd[i] == ':') { 101 cmd[i] = ' '; 102 } else if (cmd[i] == '_') { 103 cmd[i] = '>'; 104 } else if (cmd[i] == '>') { 105 cmd[i] = '_'; 106 } 107 } 108 109 dev.getOutputStream().write(cmd); 110 } 111}