Skip to content
Browse files

Add a byte sequence so the watching process can synchronize

  • Loading branch information...
1 parent 83e383a commit 89a412e9c269cf5ecfcfe6ca13e6a851429ddbcf @drbrain committed Oct 10, 2011
Showing with 67 additions and 34 deletions.
  1. +1 −0 temperature_monitor.pde
  2. +66 −34 watch.rb
View
1 temperature_monitor.pde
@@ -27,6 +27,7 @@ void loop(void)
delay(interval * 1000);
error_code = temp_sensor.readData();
+ write_int(0xFF00); // sync
write_int(error_code);
switch(error_code)
View
100 watch.rb
@@ -1,42 +1,74 @@
require 'rubygems'
require 'serialport'
-OK = 0
-BUS_HUNG = 1
-NOT_PRESENT = 2
-ACK_TIMEOUT = 3
-SYNC_TIMEOUT = 4
-DATA_TIMEOUT = 5
-CHECKSUM = 6
-TOO_QUICK = 7
-
device = Dir['/dev/tty.usbserial*'].first
-puts "listening on #{device}"
-xbee = SerialPort.new device, 9600, 8, 1, SerialPort::NONE
-
-loop do
- case val = xbee.read(2).unpack('n').first
- when 0 then
- temp, humid = xbee.read(4).unpack 'nn'
- temp = temp / 10.0
- humid = humid / 10.0
-
- puts "%4gC %4g%%" % [temp, humid]
- when 1 then
- puts "Bus hung"
- when 2 then
- puts "Not present"
- when 3 then
- puts "ACK timeout"
- when 4 then
- puts "Sync timeout"
- when 5 then
- puts "Data timeout"
- when 6 then
- puts "Polled too quick"
- else
- puts "garbage: #{val.inspect}"
+class Watcher
+
+ OK = 0
+ BUS_HUNG = 1
+ NOT_PRESENT = 2
+ ACK_TIMEOUT = 3
+ SYNC_TIMEOUT = 4
+ DATA_TIMEOUT = 5
+ CHECKSUM = 6
+ TOO_QUICK = 7
+
+ def initialize device
+ puts "listening on #{device}"
+ @xbee = SerialPort.new device, 9600, 8, 1, SerialPort::NONE
+ end
+
+ def get_int
+ @xbee.read(2).unpack('n').first
+ end
+
+ def get_byte
+ @xbee.read(1).unpack('C').first
+ end
+
+ def sync
+ state = [@xbee.get_byte]
+ loop do
+ state << @xbee.get_byte
+
+ break if state.last(2) == [0xFF, 0x00]
+ end
+ end
+
+ def display_data
+ loop do
+ sync
+
+ status = get_int
+
+ case status
+ when OK then
+ temp = get_int / 10.0
+ humid = get_int / 10.0
+
+ puts "%s %4gC %4g%%" % [Time.now, temp, humid]
+ when BUS_HUNG then
+ puts "Bus hung"
+ when NOT_PRESENT then
+ puts "Not present"
+ when ACK_TIMEOUT then
+ puts "ACK timeout"
+ when SYNC_TIMEOUT then
+ puts "Sync timeout"
+ when DATA_TIMEOUT then
+ puts "Data timeout"
+ when CHECKSUM then
+ puts "Checksum error"
+ when TOO_QUICK then
+ puts "Polled too quick"
+ else
+ puts "Garbage status: #{status.inspect}"
+ end
+ end
end
end
+watcher = Watcher.new device
+watcher.display_data
+

0 comments on commit 89a412e

Please sign in to comment.
Something went wrong with that request. Please try again.