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