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_baudrate(context, 1500 * 1000);
19         //example.ftdi_set_baudrate(context, 750 * 1000 * 4);
20         example.ftdi_set_line_property(context, 8, 0, 0);
21         purge();
22     }
23
24     public synchronized int readPins() {
25         flush();
26         byte[] b = new byte[1];
27         example.ftdi_read_pins(context, b);
28         return b[0];
29     }
30
31     public void flush() {
32         try {
33             getOutputStream().flush();
34         } catch (Exception e) { throw new RuntimeException(e); }
35     }
36
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
57     protected synchronized void dbang(int bit, boolean val) {
58         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
59         try {
60             out.write((byte)dbits);
61         } catch (IOException e) { throw new RuntimeException(e); }
62     }
63
64  
65     private final InputStream in = new InputStream() {
66             public int available() throws IOException {
67                 // FIXME
68                 return 0;
69             }
70             public int read() throws IOException {
71                 byte[] b = new byte[1];
72                 int result = 0;
73                 while(result==0) result = read(b, 0, 1);
74                 return b[0] & 0xff;
75             }
76             public int read(byte[] b, int off, int len) throws IOException {
77                 // FIXME: blocking reads?
78                 int result = 0;
79                 while(true) {
80                     if (len==0) return 0;
81                     byte[] b0 = new byte[len];
82                     synchronized(FtdiChip.this) {
83                         result = example.ftdi_read_data(context, b0, len);
84                     }
85                     if (result>0) {
86                         System.arraycopy(b0, 0, b, off, result);
87                         return result;
88                     }
89                     try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
90                 }
91             }
92         };
93
94     private final OutputStream out = new BufferedOutputStream(new OutputStream() {
95             public void write(int b) throws IOException {
96                 byte[] d = new byte[1];
97                 d[0] = (byte)b;
98                 write(d, 0, 1);
99             }
100             public void write(byte[] b, int off, int len) throws IOException {
101                 byte[] b2 = new byte[64];
102                 while(len > 0) {
103                     System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
104                     int result;
105                     synchronized(FtdiChip.this) {
106                         result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
107                     }
108                     off += result;
109                     len -= result;
110                 }
111             }
112         });
113 }