From 03563c765d8b0ab3689c91b2b533c68e11650577 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 17:35:11 +1200 Subject: altosdroid: Add new "TelemetryReader" class to handle Telemetry * Add MSG_TELEMETRY messages to both AltosDroid and TelemetryService to handle passing of AltosState object all the way back to the UI. * Remove linkedblockinglist from TelemetryService * (MSG_TELEMETRY is a rename of MSG_INCOMING_TELEM in AltosDroid) * commented code in case statement inside AltosDroind - won't work with the objects it is currently passed. * Add new "MSG_DEVCONFIG" message to AltosDroid - allows TelemetryService to pass information about the connected device back to the UI. Signed-off-by: Mike Beattie --- .../altusmetrum/AltosDroid/TelemetryReader.java | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java new file mode 100644 index 00000000..bfa5db5c --- /dev/null +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java @@ -0,0 +1,75 @@ +package org.altusmetrum.AltosDroid; + +import java.text.*; +import java.io.*; +import java.util.concurrent.*; +import android.util.Log; +import android.os.Handler; + +import org.altusmetrum.AltosLib.*; + + +public class TelemetryReader extends Thread { + + private static final String TAG = "TelemetryReader"; + + int crc_errors; + + Handler handler; + + AltosLink link; + AltosRecord previous; + + LinkedBlockingQueue telem; + + public AltosRecord read() throws ParseException, AltosCRCException, InterruptedException, IOException { + AltosLine l = telem.take(); + if (l.line == null) + throw new IOException("IO error"); + AltosRecord next = AltosTelemetry.parse(l.line, previous); + previous = next; + return next; + } + + public void close() { + previous = null; + link.remove_monitor(telem); + link = null; + telem.clear(); + telem = null; + } + + public void run() { + AltosState state = null; + + try { + for (;;) { + try { + AltosRecord record = read(); + if (record == null) + break; + state = new AltosState(record, state); + + handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget(); + } catch (ParseException pp) { + Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage())); + } catch (AltosCRCException ce) { + ++crc_errors; + } + } + } catch (InterruptedException ee) { + } catch (IOException ie) { + } finally { + close(); + } + } + + public TelemetryReader (AltosLink in_link, Handler in_handler) { + link = in_link; + handler = in_handler; + + previous = null; + telem = new LinkedBlockingQueue(); + link.add_monitor(telem); + } +} -- cgit v1.2.3