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