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