View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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; // instant replace
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  		// dev.close();
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 }