added visualizer
[slipway.git] / src / edu / berkeley / obits / device / atmel / AvrDrone.java
1 package edu.berkeley.obits.device.atmel;
2
3 import edu.berkeley.obits.*;
4 import org.ibex.util.Log;
5 import java.io.*;
6 import java.util.*;
7 import gnu.io.*;
8
9 /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */
10 public class AvrDrone extends AtmelDevice {
11
12     final DataInputStream in;
13
14     final DataOutputStream out;
15
16     final SerialPort sp;
17
18     public AvrDrone() { sp = null; in = null; out = null; } 
19     public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException, DeviceException {
20         this.sp = sp;
21         //sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
22         sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
23         sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
24         sp.setInputBufferSize(1024);
25         //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
26         this.out = new DataOutputStream(sp.getOutputStream());
27         this.in = new DataInputStream(sp.getInputStream());
28         Log.debug(this, "consuming any leftover data on the serial port");
29         while(in.available() > 0) in.read();
30         reset();
31         Log.debug(this, "waiting for device to identify itself");
32         if (in.readByte() != (byte)'O')  throw new RuntimeException("didn't get the proper signature");
33         if (in.readByte() != (byte)'B')  throw new RuntimeException("didn't get the proper signature");
34         if (in.readByte() != (byte)'I')  throw new RuntimeException("didn't get the proper signature");
35         if (in.readByte() != (byte)'T')  throw new RuntimeException("didn't get the proper signature");
36         if (in.readByte() != (byte)'S')  throw new RuntimeException("didn't get the proper signature");
37         if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
38         Log.info(this, "device correctly identified itself; ready for operation");
39     }
40
41     public synchronized void scanFPGA(boolean on) throws DeviceException {
42         if (sp==null) return;
43         try {
44             if (on) {
45                 out.writeByte(3);
46                 out.flush();
47             } else {
48                 // FIXME
49             }
50         } catch (IOException e) { throw new DeviceException(e); }
51     }
52     // fixme!
53     public static int retval = 0;
54     public synchronized int readCount() throws DeviceException {
55         if (sp==null) return 0;
56         try {
57             if (reader != null) {
58                 reader.start();
59                 reader = null;
60             }
61             ByteCallback bc = new ByteCallback() {
62                     public synchronized void call(byte b) throws Exception {
63                         retval =
64                             ((b & 0xff) << 24) |
65                             ((in.read() & 0xff) << 16) |
66                             ((in.read() & 0xff) << 8) |
67                             ((in.read() & 0xff) << 0);
68                         this.notify();
69                     }
70                 };
71             synchronized(bc) {
72                 callbacks.add(bc);
73                 out.writeByte(6);
74                 out.flush();
75                 bc.wait();
76             }
77             return retval;
78         } catch (Exception e) { throw new DeviceException(e); }
79     }
80
81     public static interface ByteCallback {
82         public void call(byte b) throws Exception;
83     }
84
85     private Vector callbacks = new Vector();
86
87     private Thread reader = new Thread() {
88             public void run() {
89                 while(true) {
90                     try {
91                         byte b = sp==null ? 0 : in.readByte();
92                         ByteCallback bc = (ByteCallback)callbacks.remove(0);
93                         bc.call(b);
94                     } catch (Exception e) {
95                         e.printStackTrace();
96                     }
97                 }
98             }
99         };
100
101     public synchronized void readBus(ByteCallback bc) throws DeviceException {
102         try {
103             callbacks.add(bc);
104             if (sp!=null) {
105                 out.writeByte(2);
106                 out.flush();
107             }
108             if (reader != null) {
109                 reader.start();
110                 reader = null;
111             }
112         } catch (IOException e) { throw new DeviceException(e); }
113     }
114
115     public synchronized void readInterrupts(ByteCallback bc) throws DeviceException {
116         try {
117             callbacks.add(bc);
118             if (sp!=null) {
119                 out.writeByte(6);
120                 out.flush();
121             }
122             if (reader != null) {
123                 reader.start();
124                 reader = null;
125             }
126         } catch (IOException e) { throw new DeviceException(e); }
127     }
128
129     public synchronized void reset() throws DeviceException {
130         if (sp==null) return;
131         try {
132             Log.info(this, "resetting device");
133             sp.setDTR(true);
134             sp.setRTS(true);
135             Thread.sleep(500);
136             Log.info(this, "deasserting reset signal");
137             sp.setDTR(false);
138             sp.setRTS(false);
139             Thread.sleep(100);
140         } catch (InterruptedException e) { throw new DeviceException(e); }
141     }
142
143     private byte[][][] cache = new byte[24][][];
144     public synchronized byte mode4(int z, int y, int x) throws DeviceException {
145         if (cache[x]==null) return 0;
146         if (cache[x][y]==null) return 0;
147         return cache[x][y][z];
148     }
149
150     int lastz = 0;
151     int lastx = 0;
152     int lasty = 0;
153     public static int save = 0;
154     public static int saveof = 0;
155     public synchronized void mode4(int z, int y, int x, int d) throws DeviceException {
156         try {
157             /*
158             Log.info(this, "writing configuration frame [zyxd]: " +
159                       pad(1, Integer.toString(z&0xff, 16)) + " " +
160                       pad(1, Integer.toString(y&0xff, 16)) + " " +
161                       pad(1, Integer.toString(x&0xff, 16)) + " " +
162                       pad(1, Integer.toString(d&0xff, 16))
163                       );
164             */
165             boolean zchange = z!=lastz;
166             boolean ychange = y!=lasty;
167             boolean xchange = x!=lastx;
168             boolean zinc    = z==lastz+1;
169             boolean yinc    = y==lasty+1;
170             boolean xinc    = x==lastx+1;
171             boolean zdec    = z==lastz-1;
172             boolean ydec    = y==lasty-1;
173             boolean xdec    = x==lastx-1;
174             
175             //System.out.println(zchange + " " + ychange + " " + xchange);
176             if (sp!=null) {
177                 out.writeByte(0x80
178                               | (zinc?0x40:zdec?0x04:zchange?0x44:0x00)
179                               | (yinc?0x20:ydec?0x02:ychange?0x22:0x00)
180                               | (xinc?0x10:xdec?0x01:xchange?0x11:0x00));
181                 if (!zinc && !zdec && zchange) out.writeByte(z); else save++;
182                 if (!yinc && !ydec && ychange) out.writeByte(y); else save++;
183                 if (!xinc && !xdec && xchange) out.writeByte(x); else save++;
184                 saveof++;
185                 lastz = z;
186                 lastx = x;
187                 lasty = y;
188                 out.writeByte(d);
189             }
190             if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
191             if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
192             cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
193         } catch (IOException e) { throw new DeviceException(e); }
194     }
195
196     public synchronized void flush() throws DeviceException {
197         if (sp==null) return;
198         try {
199             out.flush();
200         } catch (IOException e) { throw new DeviceException(e); }
201     }
202
203     private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
204
205 }