4 public class FtdiUart {
6 protected SWIGTYPE_p_ftdi_context context = example.new_ftdi_context();
8 public OutputStream getOutputStream() { return out; }
9 public InputStream getInputStream() { return in; }
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);
20 public synchronized int readPins() {
22 byte[] b = new byte[1];
23 example.ftdi_read_pins(context, b);
29 getOutputStream().flush();
30 } catch (Exception e) { throw new RuntimeException(e); }
33 public synchronized void purge() {
34 example.ftdi_usb_purge_buffers(context);
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);
39 public synchronized void dbangmode(int dmask) {
40 example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
43 protected int dbits = 0;
45 protected synchronized void dbang(int bit, boolean val) {
46 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
48 out.write((byte)dbits);
49 } catch (IOException e) { throw new RuntimeException(e); }
53 private final InputStream in = new InputStream() {
54 public int available() throws IOException {
58 public int read() throws IOException {
59 byte[] b = new byte[1];
61 while(result==0) result = read(b, 0, 1);
64 public int read(byte[] b, int off, int len) throws IOException {
65 // FIXME: blocking reads?
69 byte[] b0 = new byte[len];
70 synchronized(FtdiUart.this) {
71 result = example.ftdi_read_data(context, b0, len);
74 System.arraycopy(b0, 0, b, off, result);
77 try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); }
82 private final OutputStream out = new BufferedOutputStream(new OutputStream() {
83 public void write(int b) throws IOException {
84 byte[] d = new byte[1];
88 public void write(byte[] b, int off, int len) throws IOException {
89 byte[] b2 = new byte[64];
91 System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
93 synchronized(FtdiUart.this) {
94 result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));