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