4 public class FtdiUart {
6 protected int bits = 0;
7 protected SWIGTYPE_p_ftdi_context context = example.new_ftdi_context();
9 public OutputStream getOutputStream() { return out; }
10 public InputStream getInputStream() { return in; }
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);
21 public synchronized int readPins() {
23 byte[] b = new byte[1];
24 example.ftdi_read_pins(context, b);
30 getOutputStream().flush();
31 } catch (Exception e) { throw new RuntimeException(e); }
34 protected static int mask =
41 public synchronized void purge() {
42 example.ftdi_usb_purge_buffers(context);
44 public synchronized void uart() { uart(mask, bits); }
45 public synchronized void uart(int mask, int bits) {
46 example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
47 //example.ftdi_setflowctrl(context, (1 << 8));
49 public synchronized void dbangmode(int dmask) {
50 example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
53 protected int dbits = 0;
55 protected synchronized void dbang(int bit, boolean val) {
56 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
58 out.write((byte)dbits);
59 } catch (IOException e) { throw new RuntimeException(e); }
63 private final InputStream in = new InputStream() {
64 public int available() throws IOException {
68 public int read() throws IOException {
69 byte[] b = new byte[1];
71 while(result==0) result = read(b, 0, 1);
74 public int read(byte[] b, int off, int len) throws IOException {
75 // FIXME: blocking reads?
79 byte[] b0 = new byte[len];
80 synchronized(FtdiUart.this) {
81 result = example.ftdi_read_data(context, b0, len);
84 System.arraycopy(b0, 0, b, off, result);
87 try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); }
92 private final OutputStream out = new BufferedOutputStream(new OutputStream() {
93 public void write(int b) throws IOException {
94 byte[] d = new byte[1];
98 public void write(byte[] b, int off, int len) throws IOException {
99 byte[] b2 = new byte[64];
101 System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
103 synchronized(FtdiUart.this) {
104 result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));