checkpoint
[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                 System.out.println("*** reader thread begun");
90                 while(true) {
91                     try {
92                         byte b = sp==null ? 0 : in.readByte();
93                         ByteCallback bc = (ByteCallback)callbacks.remove(0);
94                         bc.call(b);
95                     } catch (Exception e) {
96                         e.printStackTrace();
97                     }
98                 }
99             }
100         };
101
102     public synchronized void readBus(ByteCallback bc) throws DeviceException {
103         try {
104             callbacks.add(bc);
105             if (sp!=null) {
106                 out.writeByte(2);
107                 out.flush();
108             }
109             if (reader != null) {
110                 reader.start();
111                 reader = null;
112             }
113         } catch (IOException e) { throw new DeviceException(e); }
114     }
115
116     public synchronized void readInterrupts(ByteCallback bc) throws DeviceException {
117         try {
118             callbacks.add(bc);
119             if (sp!=null) {
120                 out.writeByte(6);
121                 out.flush();
122             }
123             if (reader != null) {
124                 reader.start();
125                 reader = null;
126             }
127         } catch (IOException e) { throw new DeviceException(e); }
128     }
129
130     public synchronized void reset() throws DeviceException {
131         if (sp==null) return;
132         try {
133             Log.info(this, "resetting device");
134             sp.setDTR(true);
135             sp.setRTS(true);
136             Thread.sleep(500);
137             Log.info(this, "deasserting reset signal");
138             sp.setDTR(false);
139             sp.setRTS(false);
140             Thread.sleep(100);
141         } catch (InterruptedException e) { throw new DeviceException(e); }
142     }
143
144     private byte[][][] cache = new byte[24][][];
145     public synchronized byte mode4(int z, int y, int x) throws DeviceException {
146         if (cache[x]==null) return 0;
147         if (cache[x][y]==null) return 0;
148         return cache[x][y][z];
149     }
150
151     int lastz = 0;
152     int lastx = 0;
153     int lasty = 0;
154     public static int save = 0;
155     public static int saveof = 0;
156     public synchronized void mode4(int z, int y, int x, int d) throws DeviceException {
157         try {
158             /*
159             Log.info(this, "writing configuration frame [zyxd]: " +
160                       pad(1, Integer.toString(z&0xff, 16)) + " " +
161                       pad(1, Integer.toString(y&0xff, 16)) + " " +
162                       pad(1, Integer.toString(x&0xff, 16)) + " " +
163                       pad(1, Integer.toString(d&0xff, 16))
164                       );
165             */
166             boolean zchange = z!=lastz;
167             boolean ychange = y!=lasty;
168             boolean xchange = x!=lastx;
169             boolean zinc    = z==lastz+1;
170             boolean yinc    = y==lasty+1;
171             boolean xinc    = x==lastx+1;
172             boolean zdec    = z==lastz-1;
173             boolean ydec    = y==lasty-1;
174             boolean xdec    = x==lastx-1;
175             
176             //System.out.println(zchange + " " + ychange + " " + xchange);
177             if (sp!=null) {
178                 out.writeByte(0x80
179                               | (zinc?0x40:zdec?0x04:zchange?0x44:0x00)
180                               | (yinc?0x20:ydec?0x02:ychange?0x22:0x00)
181                               | (xinc?0x10:xdec?0x01:xchange?0x11:0x00));
182                 if (!zinc && !zdec && zchange) out.writeByte(z); else save++;
183                 if (!yinc && !ydec && ychange) out.writeByte(y); else save++;
184                 if (!xinc && !xdec && xchange) out.writeByte(x); else save++;
185                 saveof++;
186                 lastz = z;
187                 lastx = x;
188                 lasty = y;
189                 out.writeByte(d);
190             }
191             if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
192             if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
193             cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
194         } catch (IOException e) { throw new DeviceException(e); }
195     }
196
197     public synchronized void flush() throws DeviceException {
198         if (sp==null) return;
199         try {
200             out.flush();
201         } catch (IOException e) { throw new DeviceException(e); }
202     }
203
204     private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
205
206 }