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     private 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, int baud) {
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, baud);
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 synchronized void purge() {
28         example.ftdi_usb_purge_buffers(context);
29     }
30     public synchronized void uart(int cbus_mask, int cbus_bits) {
31         example.ftdi_set_bitmode(context, (short)((cbus_mask << 4) | cbus_bits), (short)0x20);
32     }
33     public synchronized void dbangmode(int dmask) {
34         example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
35     }
36
37     protected int dbits = 0;
38
39     protected synchronized void dbang(int bit, boolean val) {
40         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
41         try {
42             out.write((byte)dbits);
43         } catch (IOException e) { throw new RuntimeException(e); }
44     }
45
46  
47     private final InputStream in = new InputStream() {
48             public int available() throws IOException {
49                 // FIXME
50                 return 0;
51             }
52             public int read() throws IOException {
53                 byte[] b = new byte[1];
54                 int result = 0;
55                 while(result==0) result = read(b, 0, 1);
56                 return b[0] & 0xff;
57             }
58             public int read(byte[] b, int off, int len) throws IOException {
59                 // FIXME: blocking reads?
60                 int result = 0;
61                 while(true) {
62                     if (len==0) return 0;
63                     byte[] b0 = new byte[len];
64                     synchronized(FtdiUart.this) {
65                         result = example.ftdi_read_data(context, b0, len);
66                     }
67                     if (result>0) {
68                         System.arraycopy(b0, 0, b, off, result);
69                         return result;
70                     }
71                     try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
72                 }
73             }
74         };
75
76     private final OutputStream out = new BufferedOutputStream(new OutputStream() {
77             public void write(int b) throws IOException {
78                 byte[] d = new byte[1];
79                 d[0] = (byte)b;
80                 write(d, 0, 1);
81             }
82             public void write(byte[] b, int off, int len) throws IOException {
83                 byte[] b2 = new byte[64];
84                 while(len > 0) {
85                     System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
86                     int result;
87                     synchronized(FtdiUart.this) {
88                         result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
89                     }
90                     off += result;
91                     len -= result;
92                 }
93             }
94         });
95 }