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