1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.openimaj.workinprogress;
31
32 import java.io.IOException;
33
34 import org.openimaj.hardware.serial.SerialDevice;
35 import org.openimaj.stream.provider.twitter.TwitterStreamDataset;
36 import org.openimaj.util.api.auth.DefaultTokenFactory;
37 import org.openimaj.util.api.auth.common.TwitterAPIToken;
38 import org.openimaj.util.array.ArrayUtils;
39 import org.openimaj.util.function.Operation;
40
41 import twitter4j.Status;
42
43 public class SignProgrammer {
44 private static final byte CLEAR = (byte) 0x8C;
45 private static final byte SPEED = (byte) 0x8D;
46 public static byte LEFT = (byte) 0x81;
47 public static byte RIGHT = (byte) 0x82;
48 public static byte END = (byte) 0x80;
49 public static byte FLASH = (byte) 0x88;
50 public static byte START = (byte) 0xAA;
51 public static byte ID = (byte) 0xBB;
52 public static byte WAIT = (byte) 0x8F;
53 public static byte JUMP = (byte) 0x85;
54 public static byte RANDOM = (byte) 0x8E;
55
56 public static void main(String[] args) throws Exception {
57 final SerialDevice dev = new SerialDevice("/dev/tty.usbserial-FTB3MMNA", 2400, 8, 1, 0);
58 final byte[] init = {
59 START, START, START, START,
60 START, START, START, START,
61 ID,
62 LEFT };
63 final byte[] end = { END };
64
65 final byte[] cmd = ArrayUtils.concatenate(init, new byte[] { SPEED, 1, CLEAR },
66 "Starting twitter display".getBytes(), end);
67 writeMessage(dev, cmd);
68
69 Thread.sleep(10000);
70
71 final TwitterStreamDataset data = new
72 TwitterStreamDataset(DefaultTokenFactory.get(TwitterAPIToken.class));
73 data.forEach(new Operation<Status>() {
74 @Override
75 public void perform(Status object) {
76 try {
77 if (object.getIsoLanguageCode().equals("en")) {
78 final String tweet = object.getText().replaceAll("[^\\x00-\\x7F]", "") + " ";
79 final byte[] message = tweet.getBytes("US-ASCII");
80 final byte[] cmd = ArrayUtils.concatenate(init, new byte[] { SPEED, 1, CLEAR }, message, end);
81
82 System.out.println("Sending command " + tweet);
83 writeMessage(dev, cmd);
84 System.out.println("Done");
85 Thread.sleep(100 * message.length);
86 }
87 } catch (final Exception e) {
88 System.err.println(e);
89 }
90 }
91 });
92
93
94 }
95
96 private static void writeMessage(final SerialDevice dev, final byte[] cmd) throws IOException {
97 for (int i = 0; i < cmd.length; i++) {
98 if (cmd[i] == ' ') {
99 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 }