X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=ao-tools%2Faltosui%2FAltosSerial.java;h=d02e25a965e7a9344b9ac0f6ab45b084a7953b40;hp=82663eab51d2a5d6711b352b354636538a5ea193;hb=c3f57ffdb6c74de90d982eacd604e658ce9b00a5;hpb=7f233369e32c3254165ee251df0a3dbc21ea5a29 diff --git a/ao-tools/altosui/AltosSerial.java b/ao-tools/altosui/AltosSerial.java index 82663eab..d02e25a9 100644 --- a/ao-tools/altosui/AltosSerial.java +++ b/ao-tools/altosui/AltosSerial.java @@ -21,43 +21,52 @@ package altosui; -import java.lang.String; -import java.lang.System; -import java.lang.Character; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.FileNotFoundException; +import java.lang.*; +import java.io.*; import java.util.concurrent.LinkedBlockingQueue; -import java.lang.InterruptedException; import java.util.LinkedList; -import altosui.AltosSerialMonitor; import java.util.Iterator; +import altosui.AltosSerialMonitor; +import libaltosJNI.libaltos; +import libaltosJNI.altos_device; +import libaltosJNI.SWIGTYPE_p_altos_file; +import libaltosJNI.SWIGTYPE_p_altos_list; /* * This class reads from the serial port and places each received * line in a queue. Dealing with that queue is left up to other * threads. */ -class AltosSerialReader implements Runnable { - FileInputStream serial_in; - LinkedBlockingQueue monitor_queue; + +public class AltosSerial implements Runnable { + + SWIGTYPE_p_altos_file altos; + LinkedList> monitors; LinkedBlockingQueue reply_queue; + Thread input_thread; String line; public void run () { int c; try { - while ((c = serial_in.read()) != -1) { + for (;;) { + c = libaltos.altos_getchar(altos, 0); + if (Thread.interrupted()) + break; + if (c == -1) + continue; if (c == '\r') continue; synchronized(this) { if (c == '\n') { if (line != "") { - if (line.startsWith("VERSION")) - monitor_queue.put(line); - else + if (line.startsWith("VERSION")) { + for (int e = 0; e < monitors.size(); e++) { + LinkedBlockingQueue q = monitors.get(e); + q.put(line); + } + } else reply_queue.put(line); line = ""; } @@ -66,25 +75,26 @@ class AltosSerialReader implements Runnable { } } } - } catch (IOException e) { } catch (InterruptedException e) { } } - public String get_telem() { - try { - return monitor_queue.take(); - } catch (InterruptedException e) { - return ""; - } + public void flush_reply() { + reply_queue.clear(); } - public String get_reply() { - try { - return reply_queue.take(); - } catch (InterruptedException e) { - return ""; - } + public String get_reply() throws InterruptedException { + libaltos.altos_flush(altos); + String line = reply_queue.take(); + return line; + } + + public void add_monitor(LinkedBlockingQueue q) { + monitors.add(q); + } + + public void remove_monitor(LinkedBlockingQueue q) { + monitors.remove(q); } public void flush () { @@ -94,56 +104,72 @@ class AltosSerialReader implements Runnable { reply_queue.clear(); } } - public AltosSerialReader (FileInputStream in) { - serial_in = in; - monitor_queue = new LinkedBlockingQueue (); - reply_queue = new LinkedBlockingQueue (); - line = ""; - } -} + public boolean opened() { + return altos != null; + } -public class AltosSerial implements Runnable { - FileInputStream serial_in = null; - FileOutputStream serial_out = null; - AltosSerialReader reader; - LinkedList callbacks; - - public void run() { - for (;;) { - String s = reader.get_reply(); - synchronized(callbacks) { - Iterator i = callbacks.iterator(); - while (i.hasNext()) { - i.next().data(s); - } + public void close() { + if (altos != null) + libaltos.altos_close(altos); + if (input_thread != null) { + try { + input_thread.interrupt(); + input_thread.join(); + } catch (InterruptedException e) { } + input_thread = null; + } + if (altos != null) { + libaltos.altos_free(altos); + altos = null; } } - public void start () { - try { - serial_out.write('?'); - serial_out.write('\r'); - } catch (IOException e) { - } - (new Thread(reader)).start(); - (new Thread(this)).start(); + public void putc(char c) { + if (altos != null) + libaltos.altos_putchar(altos, c); } - public void monitor(AltosSerialMonitor monitor) { - synchronized(callbacks) { - callbacks.add(monitor); - } + public void print(String data) { + for (int i = 0; i < data.length(); i++) + putc(data.charAt(i)); + } + + public void printf(String format, Object ... arguments) { + print(String.format(format, arguments)); } - public AltosSerial(String serial_name) { + public void open(altos_device device) throws FileNotFoundException { + close(); + altos = libaltos.altos_open(device); + if (altos == null) + throw new FileNotFoundException(device.getPath()); + input_thread = new Thread(this); + input_thread.start(); + print("\nE 0\n"); try { - serial_in = new FileInputStream(serial_name); - serial_out = new FileOutputStream(serial_name); - reader = new AltosSerialReader(serial_in); - callbacks = new LinkedList(); - } catch (FileNotFoundException e) { + Thread.sleep(200); + } catch (InterruptedException e) { } + flush(); + } + + public void set_channel(int channel) { + if (altos != null) + printf("m 0\nc r %d\nm 1\n", channel); + } + + public void set_callsign(String callsign) { + if (altos != null) + printf ("c c %s\n", callsign); + } + + public AltosSerial() { + altos = null; + input_thread = null; + line = ""; + monitors = new LinkedList> (); + reply_queue = new LinkedBlockingQueue (); } }