checkpoint
[slipway.git] / src / com / ftdi / usb / FtdiUart.java
1 package com.ftdi.usb;
2 import java.io.*;
3
4 public class FtdiUart {
5
6     protected int bits = 0;
7     protected SWIGTYPE_p_ftdi_context context = example.new_ftdi_context();
8
9     public OutputStream getOutputStream() { return out; }
10     public InputStream  getInputStream() { return in; }
11
12     public FtdiUart(int vendor, int product) {
13         example.ftdi_init(context);
14         example.ftdi_usb_open(context, vendor, product);
15         example.ftdi_usb_reset(context);
16         example.ftdi_set_baudrate(context, 1500 * 1000);
17         example.ftdi_set_line_property(context, 8, 0, 0);
18         purge();
19     }
20
21     public synchronized int readPins() {
22         flush();
23         byte[] b = new byte[1];
24         example.ftdi_read_pins(context, b);
25         return b[0];
26     }
27
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(int mask, int bits) {
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     protected synchronized void dbang(int bit, boolean val) {
55         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
56         try {
57             out.write((byte)dbits);
58         } catch (IOException e) { throw new RuntimeException(e); }
59     }
60
61  
62     private final InputStream in = new InputStream() {
63             public int available() throws IOException {
64                 // FIXME
65                 return 0;
66             }
67             public int read() throws IOException {
68                 byte[] b = new byte[1];
69                 int result = 0;
70                 while(result==0) result = read(b, 0, 1);
71                 return b[0] & 0xff;
72             }
73             public int read(byte[] b, int off, int len) throws IOException {
74                 // FIXME: blocking reads?
75                 int result = 0;
76                 while(true) {
77                     if (len==0) return 0;
78                     byte[] b0 = new byte[len];
79                     synchronized(FtdiUart.this) {
80                         result = example.ftdi_read_data(context, b0, len);
81                     }
82                     if (result>0) {
83                         System.arraycopy(b0, 0, b, off, result);
84                         return result;
85                     }
86                     try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
87                 }
88             }
89         };
90
91     private final OutputStream out = new BufferedOutputStream(new OutputStream() {
92             public void write(int b) throws IOException {
93                 byte[] d = new byte[1];
94                 d[0] = (byte)b;
95                 write(d, 0, 1);
96             }
97             public void write(byte[] b, int off, int len) throws IOException {
98                 byte[] b2 = new byte[64];
99                 while(len > 0) {
100                     System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
101                     int result;
102                     synchronized(FtdiUart.this) {
103                         result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
104                     }
105                     off += result;
106                     len -= result;
107                 }
108             }
109         });
110 }