From 7548bf3f25661e4dc20c6110d3e2e2bf4d36d7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=B6gl?= Date: Sun, 16 Mar 2014 11:20:36 +0100 Subject: [PATCH] fixed pulse2osc and added pty testing scripts in conjunction with socat --- sensors2osc/sensors2osc/common.py | 3 +- sensors2osc/sensors2osc/ekg2osc.py | 31 ++++--- sensors2osc/sensors2osc/pulse2osc.py | 80 ++++++++++++------- .../{socat_tester.py => socat_ekg_test.py} | 0 .../{test.py => socat_pulse_test.py} | 0 5 files changed, 70 insertions(+), 44 deletions(-) rename sensors2osc/sensors2osc/{socat_tester.py => socat_ekg_test.py} (100%) rename sensors2osc/sensors2osc/{test.py => socat_pulse_test.py} (100%) diff --git a/sensors2osc/sensors2osc/common.py b/sensors2osc/sensors2osc/common.py index 9ad81df..6ef20cc 100644 --- a/sensors2osc/sensors2osc/common.py +++ b/sensors2osc/sensors2osc/common.py @@ -33,6 +33,7 @@ except ImportError as e: print(e) from chaosc.osc_lib import OSCMessage + class Platform(object): def __init__(self, args): self.args = args @@ -79,6 +80,6 @@ def init(name): args = create_args(name) platform = Platform(args) platform.connect() + atexit.register(platform.close) return platform - diff --git a/sensors2osc/sensors2osc/ekg2osc.py b/sensors2osc/sensors2osc/ekg2osc.py index 4bf685b..609bca0 100644 --- a/sensors2osc/sensors2osc/ekg2osc.py +++ b/sensors2osc/sensors2osc/ekg2osc.py @@ -20,29 +20,36 @@ from __future__ import absolute_import -import atexit -import socket -from sensors2osc import common +from sensors2osc.common import * + -atexit.register(common.close) def main(): - args, osc_sock = common.init("ekg2osc") + platform = init("ekg2osc") - actor = args.actor + actor = platform.args.actor while 1: try: - t = ord(common.serial_sock.read(1)) + t = platform.serial_sock.read(1) + except socket.error, msg: + # got disconnected? + print "serial socket error!!!", msg + platform.reconnect() + + try: + t = ord(t) + except TypeError, e: + continue + + try: print "got value", t osc_message = OSCMessage("/%s/ekg" % actor) osc_message.appendTypedArg(t, "i") - osc_sock.sendall(osc_message.encode_osc()) + platform.osc_sock.sendall(osc_message.encode_osc()) except socket.error, msg: - # got disconnected? - print "lost connection!!!" - common.reconnect(args) - + print "cannot connect to chaosc" + continue if __name__ == '__main__': diff --git a/sensors2osc/sensors2osc/pulse2osc.py b/sensors2osc/sensors2osc/pulse2osc.py index 37be1c2..32cd7c2 100644 --- a/sensors2osc/sensors2osc/pulse2osc.py +++ b/sensors2osc/sensors2osc/pulse2osc.py @@ -20,9 +20,11 @@ from __future__ import absolute_import + +from datetime import datetime + from sensors2osc.common import * -atexit.register(close) class RingBuffer(object): def __init__(self, length): @@ -41,7 +43,7 @@ class RingBuffer(object): def getData(self): print "getData", self.ring_buf, self.head data = list() - for i in range(7, 1, -1): + for i in range(self.length + 1, 1, -1): value = self.ring_buf[(self.head - i) % self.length] if value == -1: self.reset() @@ -59,44 +61,60 @@ class RingBuffer(object): def main(): - args, osc_sock = init("pulse2osc") + platform = init("pulse2osc") + + actor = platform.args.actor buf = RingBuffer(6) heartbeat_on = False while 1: try: - t = ord(serial_sock.read(1)) - print "got value", t - buf.append(t) - - if t == 0: - try: - heart_signal, heart_rate, o2, pulse = buf.getData() - - if pulse == 245 and not heartbeat_on: - osc_message = OSCMessage("/%s/heartbeat" % actor) - osc_message.appendTypedArg(1, "i") - osc_message.appendTypedArg(heart_rate, "i") - osc_message.appendTypedArg(o2, "i") - osc_sock.sendall(osc_message.encode_osc()) - print "heartbeat", datetime.datetime.now(), heart_signal - heartbeat_on = True - elif pulse == 1 and heartbeat_on: - #print "off heartbeat", datetime.datetime.now(), heart_signal - heartbeat_on = False - osc_message = OSCMessage("/%s/heartbeat" % actor) - osc_message.appendTypedArg(0, "i") - osc_message.appendTypedArg(heart_rate, "i") - osc_message.appendTypedArg(o2, "i") - osc_sock.sendall(osc_message.encode_osc()) - except ValueError, e: - print e + t = platform.serial_sock.read(1) except socket.error, msg: # got disconnected? - print "lost connection!!!" - reconnect(args) + print "serial socket error!!!", msg + platform.reconnect() + try: + t = ord(t) + except TypeError, e: + continue + + print "got value", t + buf.append(t) + + if t == 0: + try: + heart_signal, heart_rate, o2, pulse = buf.getData() + except ValueError, e: + print e + continue + + if pulse == 245 and not heartbeat_on: + heartbeat_on = True + try: + osc_message = OSCMessage("/%s/heartbeat" % actor) + osc_message.appendTypedArg(1, "i") + osc_message.appendTypedArg(heart_rate, "i") + osc_message.appendTypedArg(o2, "i") + platform.osc_sock.sendall(osc_message.encode_osc()) + print "on heartbeat", datetime.now(), heart_signal, heart_rate, o2, pulse + except socket.error, msg: + print "cannot connect to chaosc" + continue + elif pulse == 1 and heartbeat_on: + print "off heartbeat", datetime.now(), heart_signal, heart_rate, o2, pulse + heartbeat_on = False + try: + osc_message = OSCMessage("/%s/heartbeat" % actor) + osc_message.appendTypedArg(0, "i") + osc_message.appendTypedArg(heart_rate, "i") + osc_message.appendTypedArg(o2, "i") + platform.osc_sock.sendall(osc_message.encode_osc()) + except socket.error, msg: + print "cannot connect to chaosc" + continue if __name__ == '__main__': diff --git a/sensors2osc/sensors2osc/socat_tester.py b/sensors2osc/sensors2osc/socat_ekg_test.py similarity index 100% rename from sensors2osc/sensors2osc/socat_tester.py rename to sensors2osc/sensors2osc/socat_ekg_test.py diff --git a/sensors2osc/sensors2osc/test.py b/sensors2osc/sensors2osc/socat_pulse_test.py similarity index 100% rename from sensors2osc/sensors2osc/test.py rename to sensors2osc/sensors2osc/socat_pulse_test.py