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