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