checkpoint
[slipway.git] / src / edu / berkeley / obits / device / atmel / FtdiChip.java
1 package edu.berkeley.obits.device.atmel;
2 import com.ftdi.usb.*;
3 import java.io.*;
4
5 public class FtdiChip {
6
7     protected int bits = 0;
8     protected SWIGTYPE_p_ftdi_context context = example.new_ftdi_context();
9
10     public OutputStream getOutputStream() { return out; }
11     public InputStream  getInputStream() { return in; }
12
13     public FtdiChip() {
14         example.ftdi_init(context);
15         example.ftdi_usb_open(context, 0x6666, 0x3133);
16         example.ftdi_set_baudrate(context, 750 * 1000);
17         example.ftdi_set_line_property(context, 8, 0, 0);
18     }
19
20     public synchronized int readPins() {
21         byte[] b = new byte[1];
22         example.ftdi_read_pins(context, b);
23         return b[0];
24     }
25
26     ByteArrayOutputStream baos = new ByteArrayOutputStream();
27     public void flush() {
28         try {
29             byte[] bytes = baos.toByteArray();
30             baos = new ByteArrayOutputStream();
31             out.write(bytes, 0, bytes.length);
32             out.flush();
33         } catch (IOException e) { throw new RuntimeException(e); }
34     }
35
36     public boolean buffered = false;
37     protected static int mask =
38         (1<<0) |
39         (1<<1)// |
40         //(1<<2) |
41         //(1<<3)
42         ;
43
44     public synchronized void purge() {
45         example.ftdi_usb_purge_buffers(context);
46     }
47     public synchronized void uart() {
48         example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
49         example.ftdi_setflowctrl(context, (1 << 8));
50     }
51     public synchronized void dbangmode(int dmask) {
52         example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
53     }
54
55     protected int dbits = 0;
56     protected synchronized void dbang(int bit, boolean val) {
57         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
58         if (buffered) {
59             baos.write((byte)dbits);
60         } else {
61             try {
62                 out.write((byte)dbits);
63                 out.flush();
64             } catch (IOException e) { throw new RuntimeException(e); }
65         }
66     }
67
68  
69     private final InputStream in = new InputStream() {
70             public int available() throws IOException {
71                 // FIXME
72                 return 0;
73             }
74             public int read() throws IOException {
75                 byte[] b = new byte[1];
76                 int result = 0;
77                 while(result==0) result = read(b, 0, 1);
78                 return b[0] & 0xff;
79             }
80             public int read(byte[] b, int off, int len) throws IOException {
81                 // FIXME: blocking reads?
82                 int result = 0;
83                 while(true) {
84                     if (len==0) return 0;
85                     byte[] b0 = new byte[len];
86                     synchronized(FtdiChip.this) {
87                         result = example.ftdi_read_data(context, b0, len);
88                     }
89                     if (result>0) {
90                         System.arraycopy(b0, 0, b, off, result);
91                         return result;
92                     }
93                     try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
94                 }
95             }
96         };
97
98     private final OutputStream out = new BufferedOutputStream(new OutputStream() {
99             public void write(int b) throws IOException {
100                 byte[] d = new byte[1];
101                 d[0] = (byte)b;
102                 write(d, 0, 1);
103             }
104             public void write(byte[] b, int off, int len) throws IOException {
105                 byte[] b2 = new byte[64];
106                 while(len > 0) {
107                     System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
108                     int result;
109                     synchronized(FtdiChip.this) {
110                         result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
111                     }
112                     off += result;
113                     len -= result;
114                 }
115             }
116         });
117 }