From 7c0f56bf209eb7c474048e6b9f3ece30c99928f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=B6gl?= Date: Sun, 23 Mar 2014 13:01:16 +0100 Subject: [PATCH] ported sensors2osc to python3.3 --- sensors2osc/sensors2osc/.rej | 449 ++++++++++++++++++ sensors2osc/sensors2osc/common.py | 10 +- sensors2osc/sensors2osc/common.py.bak | 91 ++++ sensors2osc/sensors2osc/ehealth2osc.py | 52 +- sensors2osc/sensors2osc/ehealth2osc.py.bak | 94 ++++ sensors2osc/sensors2osc/ekg2osc.py | 20 +- sensors2osc/sensors2osc/ekg2osc.py.bak | 58 +++ sensors2osc/sensors2osc/main.py | 26 +- sensors2osc/sensors2osc/main.py.bak | 218 +++++++++ sensors2osc/sensors2osc/pulse2osc.py | 48 +- sensors2osc/sensors2osc/pulse2osc.py.bak | 122 +++++ sensors2osc/sensors2osc/sensorTest.py | 16 +- sensors2osc/sensors2osc/sensorTest.py.bak | 190 ++++++++ sensors2osc/sensors2osc/socat_ehealth_test.py | 4 +- sensors2osc/sensors2osc/socat_ekg_test.py | 4 +- sensors2osc/sensors2osc/socat_ekg_test.py.bak | 70 +++ sensors2osc/sensors2osc/socat_pulse_test.py | 2 +- .../sensors2osc/socat_pulse_test.py.bak | 62 +++ sensors2osc/setup.py | 1 + 19 files changed, 1447 insertions(+), 90 deletions(-) create mode 100644 sensors2osc/sensors2osc/.rej create mode 100644 sensors2osc/sensors2osc/common.py.bak create mode 100644 sensors2osc/sensors2osc/ehealth2osc.py.bak create mode 100644 sensors2osc/sensors2osc/ekg2osc.py.bak create mode 100644 sensors2osc/sensors2osc/main.py.bak create mode 100644 sensors2osc/sensors2osc/pulse2osc.py.bak create mode 100644 sensors2osc/sensors2osc/sensorTest.py.bak create mode 100644 sensors2osc/sensors2osc/socat_ekg_test.py.bak create mode 100644 sensors2osc/sensors2osc/socat_pulse_test.py.bak diff --git a/sensors2osc/sensors2osc/.rej b/sensors2osc/sensors2osc/.rej new file mode 100644 index 0000000..84e5777 --- /dev/null +++ b/sensors2osc/sensors2osc/.rej @@ -0,0 +1,449 @@ +--- sensors2osc/common.py (original) ++++ sensors2osc/common.py (refactored) +@@ -18,7 +18,7 @@ + # + # Copyright (C) 2014 Stefan Kögl + +-from __future__ import absolute_import ++ + + import atexit + import os.path +@@ -43,12 +43,12 @@ + + + def connect(self): +- print "connect serial" ++ print("connect serial") + self.serial_sock = serial.Serial() + self.serial_sock.port = self.args.device + self.serial_sock.baudrate = 115200 + self.serial_sock.timeout = 0 +- print "waiting for the device %r to come up" % self.args.device ++ print("waiting for the device %r to come up" % self.args.device) + while 1: + try: + self.serial_sock.open() +@@ -59,12 +59,12 @@ + + def close(self): + if self.serial_sock is not None: +- print "close serial" ++ print("close serial") + self.serial_sock.close() + + + def reconnect(self): +- print "reconnect serial" ++ print("reconnect serial") + self.close() + self.connect() + +--- sensors2osc/ehealth2osc.py (original) ++++ sensors2osc/ehealth2osc.py (refactored) +@@ -18,7 +18,7 @@ + # + # Copyright (C) 2014 Stefan Kögl + +-from __future__ import absolute_import ++ + + from sensors2osc.common import * + import time +@@ -33,60 +33,60 @@ + try: + data = platform.serial_sock.readline()[:-2] + #print repr(data) +- except socket.error, msg: ++ except socket.error as msg: + # got disconnected? +- print "serial socket error!!!", msg ++ print("serial socket error!!!", msg) + platform.reconnect() + + #print "got data", repr(data) + try: + airFlow, emg, temp = data.split(";") +- except ValueError, e: +- print e ++ except ValueError as e: ++ print(e) + continue + + try: + airFlow = int(airFlow) +- except ValueError, e: +- print e ++ except ValueError as e: ++ print(e) + continue + + try: + osc_message = OSCMessage("/%s/airFlow" % actor) + osc_message.appendTypedArg(airFlow, "i") + platform.osc_sock.sendall(osc_message.encode_osc()) +- except socket.error, msg: +- print "cannot connect to chaosc", msg ++ except socket.error as msg: ++ print("cannot connect to chaosc", msg) + continue + + + try: + emg = int(emg) +- except ValueError, e: +- print e ++ except ValueError as e: ++ print(e) + continue + + try: + osc_message = OSCMessage("/%s/emg" % actor) + osc_message.appendTypedArg(emg, "i") + platform.osc_sock.sendall(osc_message.encode_osc()) +- except socket.error, msg: +- print "cannot connect to chaosc", msg ++ except socket.error as msg: ++ print("cannot connect to chaosc", msg) + continue + + + try: + temp = int(temp) +- except ValueError, e: +- print e ++ except ValueError as e: ++ print(e) + continue + + try: + osc_message = OSCMessage("/%s/temperatur" % actor) + osc_message.appendTypedArg(temp, "i") + platform.osc_sock.sendall(osc_message.encode_osc()) +- except socket.error, msg: +- print "cannot connect to chaosc", msg ++ except socket.error as msg: ++ print("cannot connect to chaosc", msg) + continue + + +--- sensors2osc/ekg2osc.py (original) ++++ sensors2osc/ekg2osc.py (refactored) +@@ -18,7 +18,7 @@ + # + # Copyright (C) 2014 Stefan Kögl + +-from __future__ import absolute_import ++ + + import time + +@@ -34,23 +34,23 @@ + while 1: + try: + t = platform.serial_sock.read(1) +- except socket.error, msg: ++ except socket.error as msg: + # got disconnected? +- print "serial socket error!!!", msg ++ print("serial socket error!!!", msg) + platform.reconnect() + + try: + t = ord(t) +- except TypeError, e: ++ except TypeError as e: + continue + + try: +- print "got value", t ++ print("got value", t) + osc_message = OSCMessage("/%s/ekg" % actor) + osc_message.appendTypedArg(t, "i") + platform.osc_sock.sendall(osc_message.encode_osc()) +- except socket.error, msg: +- print "cannot connect to chaosc" ++ except socket.error as msg: ++ print("cannot connect to chaosc") + continue + + +--- sensors2osc/main.py (original) ++++ sensors2osc/main.py (refactored) +@@ -48,7 +48,7 @@ + + def close(self): + """Close all resources and unpublish service""" +- print "%s: closing..." % (self.device, ) ++ print("%s: closing..." % (self.device, )) + self.serial.close() + + +@@ -58,7 +58,7 @@ + + def handle_read(self, osc_sock): + data = self.serial.readline()[:-2] +- print repr(data) ++ print(repr(data)) + try: + airFlow, emg, temp = data.split(";") + except ValueError: +@@ -106,7 +106,7 @@ + self.head = (self.head + 1) % self.length + + def getData(self): +- print "getData", self.ring_buf, self.head ++ print("getData", self.ring_buf, self.head) + data = list() + for i in range(7, 1, -1): + value = self.ring_buf[(self.head - i) % self.length] +@@ -117,7 +117,7 @@ + raise ValueError("not complete - ringbuffer resettet") + data.append(value) + if data[0] != 0x0 or data[1] != 0xff: +- print "issue", data ++ print("issue", data) + self.reset() + self.ring_buf[0] = 0 + self.head = 1 +@@ -146,7 +146,7 @@ + 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 ++ print("heartbeat", datetime.datetime.now(), heart_signal) + self.heartbeat_on = True + elif pulse == 1 and self.heartbeat_on: + #print "off heartbeat", datetime.datetime.now(), heart_signal +@@ -156,8 +156,8 @@ + osc_message.appendTypedArg(heart_rate, "i") + osc_message.appendTypedArg(o2, "i") + osc_sock.sendall(osc_message.encode_osc()) +- except ValueError, e: +- print e ++ except ValueError as e: ++ print(e) + + + def main(): +@@ -187,30 +187,30 @@ + used_devices = dict() + + while 1: +- for device, description in naming.iteritems(): ++ for device, description in naming.items(): + if os.path.exists(device): + if device not in used_devices: + actor, platform = naming[device] + if description[1] == "ehealth": +- print device, actor, platform ++ print(device, actor, platform) + used_devices[device] = EHealth2OSC(actor, platform, device) + elif description[1] == "ekg": +- print device, actor, platform ++ print(device, actor, platform) + used_devices[device] = EKG2OSC(actor, platform, device) + elif description[1] == "pulse": +- print device, actor, platform ++ print(device, actor, platform) + used_devices[device] = Pulse2OSC(actor, platform, device) + else: + raise ValueError("unknown description %r for device %r" % (description, device)) + else: +- print "device missing", device ++ print("device missing", device) + message = OSCMessage("/DeviceMissing") + message.appendTypedArg(description[0], "s") + message.appendTypedArg(description[1], "s") + osc_sock.sendall(message.encode_osc()) + + read_map = {} +- for forwarder in used_devices.values(): ++ for forwarder in list(used_devices.values()): + read_map[forwarder.serial] = forwarder.handle_read + + readers, writers, errors = select.select(read_map, [], [], 0.1) +--- sensors2osc/pulse2osc.py (original) ++++ sensors2osc/pulse2osc.py (refactored) +@@ -18,7 +18,7 @@ + # + # Copyright (C) 2014 Stefan Kögl + +-from __future__ import absolute_import ++ + + import time + from datetime import datetime +@@ -41,7 +41,7 @@ + self.head = (self.head + 1) % self.length + + def getData(self): +- print "getData", self.ring_buf, self.head ++ print("getData", self.ring_buf, self.head) + data = list() + for i in range(self.length + 1, 1, -1): + value = self.ring_buf[(self.head - i) % self.length] +@@ -52,7 +52,7 @@ + raise ValueError("not complete - ringbuffer resettet") + data.append(value) + if data[0] != 0x0 or data[1] != 0xff: +- print "issue", data ++ print("issue", data) + self.reset() + self.ring_buf[0] = 0 + self.head = 1 +@@ -71,24 +71,24 @@ + while 1: + try: + t = platform.serial_sock.read(1) +- except socket.error, msg: ++ except socket.error as msg: + # got disconnected? +- print "serial socket error!!!", msg ++ print("serial socket error!!!", msg) + platform.reconnect() + + try: + t = ord(t) +- except TypeError, e: ++ except TypeError as e: + continue + +- print "got value", t ++ print("got value", t) + buf.append(t) + + if t == 0: + try: + heart_signal, heart_rate, o2, pulse = buf.getData() +- except ValueError, e: +- print e ++ except ValueError as e: ++ print(e) + continue + + if pulse == 245 and not heartbeat_on: +@@ -99,12 +99,12 @@ + 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" ++ print("on heartbeat", datetime.now(), heart_signal, heart_rate, o2, pulse) ++ except socket.error as msg: ++ print("cannot connect to chaosc") + continue + elif pulse == 1 and heartbeat_on: +- print "off heartbeat", datetime.now(), heart_signal, heart_rate, o2, pulse ++ print("off heartbeat", datetime.now(), heart_signal, heart_rate, o2, pulse) + heartbeat_on = False + try: + osc_message = OSCMessage("/%s/heartbeat" % actor) +@@ -112,8 +112,8 @@ + 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" ++ except socket.error as msg: ++ print("cannot connect to chaosc") + continue + time.sleep(0.1) + +--- sensors2osc/sensorTest.py (original) ++++ sensors2osc/sensorTest.py (refactored) +@@ -48,7 +48,7 @@ + + def close(self): + """Close all resources and unpublish service""" +- print "%s: closing..." % (self.device, ) ++ print("%s: closing..." % (self.device, )) + self.serial.close() + + +@@ -58,7 +58,7 @@ + + def handle_read(self, osc_sock): + data = self.serial.readline()[:-2] +- print repr(data) ++ print(repr(data)) + try: + airFlow, emg, temp = data.split(";") + except ValueError: +@@ -94,7 +94,7 @@ + class RingBuffer(object): + def __init__(self, length): + self.length = length +- self.ring_buf = [-1 for i in xrange(length)] ++ self.ring_buf = [-1 for i in range(length)] + self.head = 0 + + def append(self, value): +@@ -102,7 +102,7 @@ + self.head = (self.head + 1) % self.length + + def getData(self): +- print "getData", self.ring_buf, self.head ++ print("getData", self.ring_buf, self.head) + data = list() + for i in range(7, 1, -1): + value = self.ring_buf[(self.head - i) % self.length] +@@ -135,7 +135,7 @@ + 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 ++ print("heartbeat", datetime.datetime.now(), heart_signal) + self.heartbeat_on = True + elif pulse == 1 and self.heartbeat_on: + #print "off heartbeat", datetime.datetime.now(), heart_signal +@@ -145,8 +145,8 @@ + osc_message.appendTypedArg(heart_rate, "i") + osc_message.appendTypedArg(o2, "i") + osc_sock.sendall(osc_message.encode_osc()) +- except ValueError, e: +- print e ++ except ValueError as e: ++ print(e) + + + def main(): +@@ -182,7 +182,7 @@ + + while 1: + read_map = {} +- for forwarder in used_devices.values(): ++ for forwarder in list(used_devices.values()): + read_map[forwarder.serial] = forwarder.handle_read + + readers, writers, errors = select.select(read_map, [], [], 0.1) +--- sensors2osc/socat_ekg_test.py (original) ++++ sensors2osc/socat_ekg_test.py (refactored) +@@ -55,13 +55,13 @@ + count = 0 + + if data_points % (5 * steps) == 0: +- print "new steps", steps, delta ++ print("new steps", steps, delta) + steps += delta + + if steps <= min_steps: + delta = 1 + elif steps >= max_steps: +- print "change step sign", steps, delta ++ print("change step sign", steps, delta) + delta = -1 + + time.sleep(0.02) +--- sensors2osc/socat_pulse_test.py (original) ++++ sensors2osc/socat_pulse_test.py (refactored) +@@ -21,7 +21,7 @@ + # socat -d -d PTY,raw,echo=0,link=/tmp/pty1,b115200,user=stefan PTY,raw,echo=0,link=/tmp/pty2,b115200,user=stefan + + +-from __future__ import absolute_import ++ + + import serial, time, random, sys, random, struct + diff --git a/sensors2osc/sensors2osc/common.py b/sensors2osc/sensors2osc/common.py index 25d6217..6acf9f0 100644 --- a/sensors2osc/sensors2osc/common.py +++ b/sensors2osc/sensors2osc/common.py @@ -18,7 +18,7 @@ # # Copyright (C) 2014 Stefan Kögl -from __future__ import absolute_import + import atexit import os.path @@ -43,12 +43,12 @@ class Platform(object): def connect(self): - print "connect serial" + print("connect serial") self.serial_sock = serial.Serial() self.serial_sock.port = self.args.device self.serial_sock.baudrate = 115200 self.serial_sock.timeout = 0 - print "waiting for the device %r to come up" % self.args.device + print("waiting for the device %r to come up" % self.args.device) while 1: try: self.serial_sock.open() @@ -59,12 +59,12 @@ class Platform(object): def close(self): if self.serial_sock is not None: - print "close serial" + print("close serial") self.serial_sock.close() def reconnect(self): - print "reconnect serial" + print("reconnect serial") self.close() self.connect() diff --git a/sensors2osc/sensors2osc/common.py.bak b/sensors2osc/sensors2osc/common.py.bak new file mode 100644 index 0000000..25d6217 --- /dev/null +++ b/sensors2osc/sensors2osc/common.py.bak @@ -0,0 +1,91 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# This file is part of sensors2osc package +# +# sensors2osc is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# sensors2osc is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with sensors2osc. If not, see . +# +# Copyright (C) 2014 Stefan Kögl + +from __future__ import absolute_import + +import atexit +import os.path +import serial +import socket + +from chaosc.argparser_groups import * + +try: + from chaosc.c_osc_lib import OSCMessage +except ImportError as e: + print(e) + from chaosc.osc_lib import OSCMessage + + +class Platform(object): + def __init__(self, args): + self.args = args + self.serial_sock = None + self.osc_sock = socket.socket(10, 2, 17) + self.osc_sock.connect((self.args.chaosc_host, self.args.chaosc_port)) + + + def connect(self): + print "connect serial" + self.serial_sock = serial.Serial() + self.serial_sock.port = self.args.device + self.serial_sock.baudrate = 115200 + self.serial_sock.timeout = 0 + print "waiting for the device %r to come up" % self.args.device + while 1: + try: + self.serial_sock.open() + break + except serial.serialtuil.SerialException: + pass + + + def close(self): + if self.serial_sock is not None: + print "close serial" + self.serial_sock.close() + + + def reconnect(self): + print "reconnect serial" + self.close() + self.connect() + + +def create_args(name): + arg_parser = create_arg_parser(name) + main_group = arg_parser.add_argument_group("main") + main_group.add_argument("-d", '--device', required=True, + type=str, help='device node under /dev') + main_group.add_argument("-a", '--actor', required=True, + type=str, help='actor name') + add_chaosc_group(arg_parser) + + args = finalize_arg_parser(arg_parser) + return args + + +def init(name): + args = create_args(name) + platform = Platform(args) + platform.connect() + atexit.register(platform.close) + + return platform diff --git a/sensors2osc/sensors2osc/ehealth2osc.py b/sensors2osc/sensors2osc/ehealth2osc.py index 3d5fb72..20707a1 100644 --- a/sensors2osc/sensors2osc/ehealth2osc.py +++ b/sensors2osc/sensors2osc/ehealth2osc.py @@ -18,7 +18,7 @@ # # Copyright (C) 2014 Stefan Kögl -from __future__ import absolute_import + from sensors2osc.common import * import time @@ -27,66 +27,66 @@ import time def main(): platform = init("ehealth2osc") - actor = platform.args.actor + actor = bytes(platform.args.actor, "ascii") while 1: try: data = platform.serial_sock.readline()[:-2] #print repr(data) - except socket.error, msg: + except socket.error as msg: # got disconnected? - print "serial socket error!!!", msg + print("serial socket error!!!", msg) platform.reconnect() - #print "got data", repr(data) + print("got data", repr(data)) try: - airFlow, emg, temp = data.split(";") - except ValueError, e: - print e + airFlow, emg, temp = bytearray(data).split(b";") + except ValueError as e: + print(e) continue try: airFlow = int(airFlow) - except ValueError, e: - print e + except ValueError as e: + print(e) continue try: - osc_message = OSCMessage("/%s/airFlow" % actor) - osc_message.appendTypedArg(airFlow, "i") + osc_message = OSCMessage(b"/" + actor + b"/airFlow") + osc_message.appendTypedArg(airFlow, b"i") platform.osc_sock.sendall(osc_message.encode_osc()) - except socket.error, msg: - print "cannot connect to chaosc", msg + except socket.error as msg: + print("cannot connect to chaosc", msg) continue try: emg = int(emg) - except ValueError, e: - print e + except ValueError as e: + print(e) continue try: - osc_message = OSCMessage("/%s/emg" % actor) - osc_message.appendTypedArg(emg, "i") + osc_message = OSCMessage(b"/" + actor + b"/emg") + osc_message.appendTypedArg(emg, b"i") platform.osc_sock.sendall(osc_message.encode_osc()) - except socket.error, msg: - print "cannot connect to chaosc", msg + except socket.error as msg: + print("cannot connect to chaosc", msg) continue try: temp = int(temp) - except ValueError, e: - print e + except ValueError as e: + print(e) continue try: - osc_message = OSCMessage("/%s/temperatur" % actor) - osc_message.appendTypedArg(temp, "i") + osc_message = OSCMessage(b"/" + actor + b"/temperatur") + osc_message.appendTypedArg(temp, b"i") platform.osc_sock.sendall(osc_message.encode_osc()) - except socket.error, msg: - print "cannot connect to chaosc", msg + except socket.error as msg: + print("cannot connect to chaosc", msg) continue diff --git a/sensors2osc/sensors2osc/ehealth2osc.py.bak b/sensors2osc/sensors2osc/ehealth2osc.py.bak new file mode 100644 index 0000000..3d5fb72 --- /dev/null +++ b/sensors2osc/sensors2osc/ehealth2osc.py.bak @@ -0,0 +1,94 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# This file is part of sensors2osc package +# +# sensors2osc is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# sensors2osc is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with sensors2osc. If not, see . +# +# Copyright (C) 2014 Stefan Kögl + +from __future__ import absolute_import + +from sensors2osc.common import * +import time + + +def main(): + platform = init("ehealth2osc") + + actor = platform.args.actor + + while 1: + try: + data = platform.serial_sock.readline()[:-2] + #print repr(data) + except socket.error, msg: + # got disconnected? + print "serial socket error!!!", msg + platform.reconnect() + + #print "got data", repr(data) + try: + airFlow, emg, temp = data.split(";") + except ValueError, e: + print e + continue + + try: + airFlow = int(airFlow) + except ValueError, e: + print e + continue + + try: + osc_message = OSCMessage("/%s/airFlow" % actor) + osc_message.appendTypedArg(airFlow, "i") + platform.osc_sock.sendall(osc_message.encode_osc()) + except socket.error, msg: + print "cannot connect to chaosc", msg + continue + + + try: + emg = int(emg) + except ValueError, e: + print e + continue + + try: + osc_message = OSCMessage("/%s/emg" % actor) + osc_message.appendTypedArg(emg, "i") + platform.osc_sock.sendall(osc_message.encode_osc()) + except socket.error, msg: + print "cannot connect to chaosc", msg + continue + + + try: + temp = int(temp) + except ValueError, e: + print e + continue + + try: + osc_message = OSCMessage("/%s/temperatur" % actor) + osc_message.appendTypedArg(temp, "i") + platform.osc_sock.sendall(osc_message.encode_osc()) + except socket.error, msg: + print "cannot connect to chaosc", msg + continue + + +if __name__ == '__main__': + main() diff --git a/sensors2osc/sensors2osc/ekg2osc.py b/sensors2osc/sensors2osc/ekg2osc.py index 2b57563..04982ae 100644 --- a/sensors2osc/sensors2osc/ekg2osc.py +++ b/sensors2osc/sensors2osc/ekg2osc.py @@ -18,7 +18,7 @@ # # Copyright (C) 2014 Stefan Kögl -from __future__ import absolute_import + import time @@ -29,28 +29,28 @@ from sensors2osc.common import * def main(): platform = init("ekg2osc") - actor = platform.args.actor + actor = bytes(platform.args.actor, "ascii") while 1: try: t = platform.serial_sock.read(1) - except socket.error, msg: + except socket.error as msg: # got disconnected? - print "serial socket error!!!", msg + print("serial socket error!!!", msg) platform.reconnect() try: t = ord(t) - except TypeError, e: + except TypeError as e: continue try: - print "got value", t - osc_message = OSCMessage("/%s/ekg" % actor) - osc_message.appendTypedArg(t, "i") + print("got value", t) + osc_message = OSCMessage(b"/" + actor + b"/ekg") + osc_message.appendTypedArg(t, b"i") platform.osc_sock.sendall(osc_message.encode_osc()) - except socket.error, msg: - print "cannot connect to chaosc" + except socket.error as msg: + print("cannot connect to chaosc") continue diff --git a/sensors2osc/sensors2osc/ekg2osc.py.bak b/sensors2osc/sensors2osc/ekg2osc.py.bak new file mode 100644 index 0000000..2b57563 --- /dev/null +++ b/sensors2osc/sensors2osc/ekg2osc.py.bak @@ -0,0 +1,58 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# This file is part of sensors2osc package +# +# sensors2osc is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# sensors2osc is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with sensors2osc. If not, see . +# +# Copyright (C) 2014 Stefan Kögl + +from __future__ import absolute_import + +import time + +from sensors2osc.common import * + + + +def main(): + platform = init("ekg2osc") + + actor = platform.args.actor + + while 1: + try: + 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") + platform.osc_sock.sendall(osc_message.encode_osc()) + except socket.error, msg: + print "cannot connect to chaosc" + continue + + +if __name__ == '__main__': + main() diff --git a/sensors2osc/sensors2osc/main.py b/sensors2osc/sensors2osc/main.py index fa5545d..1dd9c75 100644 --- a/sensors2osc/sensors2osc/main.py +++ b/sensors2osc/sensors2osc/main.py @@ -48,7 +48,7 @@ class Forwarder(object): def close(self): """Close all resources and unpublish service""" - print "%s: closing..." % (self.device, ) + print("%s: closing..." % (self.device, )) self.serial.close() @@ -58,7 +58,7 @@ class EHealth2OSC(Forwarder): def handle_read(self, osc_sock): data = self.serial.readline()[:-2] - print repr(data) + print(repr(data)) try: airFlow, emg, temp = data.split(";") except ValueError: @@ -106,7 +106,7 @@ class RingBuffer(object): self.head = (self.head + 1) % self.length def getData(self): - print "getData", self.ring_buf, self.head + print("getData", self.ring_buf, self.head) data = list() for i in range(7, 1, -1): value = self.ring_buf[(self.head - i) % self.length] @@ -117,7 +117,7 @@ class RingBuffer(object): raise ValueError("not complete - ringbuffer resettet") data.append(value) if data[0] != 0x0 or data[1] != 0xff: - print "issue", data + print("issue", data) self.reset() self.ring_buf[0] = 0 self.head = 1 @@ -146,7 +146,7 @@ class Pulse2OSC(Forwarder): 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 + print("heartbeat", datetime.datetime.now(), heart_signal) self.heartbeat_on = True elif pulse == 1 and self.heartbeat_on: #print "off heartbeat", datetime.datetime.now(), heart_signal @@ -156,8 +156,8 @@ class Pulse2OSC(Forwarder): osc_message.appendTypedArg(heart_rate, "i") osc_message.appendTypedArg(o2, "i") osc_sock.sendall(osc_message.encode_osc()) - except ValueError, e: - print e + except ValueError as e: + print(e) def main(): @@ -187,30 +187,30 @@ def main(): used_devices = dict() while 1: - for device, description in naming.iteritems(): + for device, description in naming.items(): if os.path.exists(device): if device not in used_devices: actor, platform = naming[device] if description[1] == "ehealth": - print device, actor, platform + print(device, actor, platform) used_devices[device] = EHealth2OSC(actor, platform, device) elif description[1] == "ekg": - print device, actor, platform + print(device, actor, platform) used_devices[device] = EKG2OSC(actor, platform, device) elif description[1] == "pulse": - print device, actor, platform + print(device, actor, platform) used_devices[device] = Pulse2OSC(actor, platform, device) else: raise ValueError("unknown description %r for device %r" % (description, device)) else: - print "device missing", device + print("device missing", device) message = OSCMessage("/DeviceMissing") message.appendTypedArg(description[0], "s") message.appendTypedArg(description[1], "s") osc_sock.sendall(message.encode_osc()) read_map = {} - for forwarder in used_devices.values(): + for forwarder in list(used_devices.values()): read_map[forwarder.serial] = forwarder.handle_read readers, writers, errors = select.select(read_map, [], [], 0.1) diff --git a/sensors2osc/sensors2osc/main.py.bak b/sensors2osc/sensors2osc/main.py.bak new file mode 100644 index 0000000..fa5545d --- /dev/null +++ b/sensors2osc/sensors2osc/main.py.bak @@ -0,0 +1,218 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# This file is part of chaosc +# +# chaosc is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# chaosc is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with chaosc. If not, see . +# +# Copyright (C) 2014 Stefan Kögl + +import argparse +import os.path +import select +import serial +import socket +import sys +import datetime + +try: + from chaosc.c_osc_lib import OSCMessage +except ImportError as e: + print(e) + from chaosc.osc_lib import OSCMessage + + +class Forwarder(object): + def __init__(self, actor, platform, device): + self.actor = actor + self.platform = platform + self.device = device + self.serial = serial.Serial() + self.serial.port = device + self.serial.baudrate = 115200 + self.serial.timeout = 0 + self.buf_ser2osc = "" + + self.serial.open() + + def close(self): + """Close all resources and unpublish service""" + print "%s: closing..." % (self.device, ) + self.serial.close() + + +class EHealth2OSC(Forwarder): + def __init__(self, actor, platform, device): + super(EHealth2OSC, self).__init__(actor, platform, device) + + def handle_read(self, osc_sock): + data = self.serial.readline()[:-2] + print repr(data) + try: + airFlow, emg, temp = data.split(";") + except ValueError: + return + try: + airFlow = int(airFlow) + emg = int(emg) + temp = int(temp); + except ValueError: + return + osc_message = OSCMessage("/%s/airFlow" % self.actor) + osc_message.appendTypedArg(airFlow, "i") + osc_sock.sendall(osc_message.encode_osc()) + osc_message = OSCMessage("/%s/emg" % self.actor) + osc_message.appendTypedArg(emg, "i") + osc_sock.sendall(osc_message.encode_osc()) + osc_message = OSCMessage("/%s/temperatur" % self.actor) + osc_message.appendTypedArg(temp, "i") + osc_sock.sendall(osc_message.encode_osc()) + + +class EKG2OSC(Forwarder): + def __init__(self, actor, platform, device): + super(EKG2OSC, self).__init__(actor, platform, device) + + def handle_read(self, osc_sock): + t = ord(self.serial.read(1)) + osc_message = OSCMessage("/%s/ekg" % self.actor) + osc_message.appendTypedArg(t, "i") + osc_sock.sendall(osc_message.encode_osc()) + + +class RingBuffer(object): + def __init__(self, length): + self.length = length + self.ring_buf = list() + self.reset() + + def reset(self): + self.ring_buf = [-1] * self.length + self.head = 0 + + def append(self, value): + self.ring_buf[self.head] = value + self.head = (self.head + 1) % self.length + + def getData(self): + print "getData", self.ring_buf, self.head + data = list() + for i in range(7, 1, -1): + value = self.ring_buf[(self.head - i) % self.length] + if value == -1: + self.reset() + self.ring_buf[0] = 0 + self.head = 1 + raise ValueError("not complete - ringbuffer resettet") + data.append(value) + if data[0] != 0x0 or data[1] != 0xff: + print "issue", data + self.reset() + self.ring_buf[0] = 0 + self.head = 1 + raise ValueError("not synced - ringbuffer resettet") + return data[2:] + + + +class Pulse2OSC(Forwarder): + def __init__(self, actor, platform, device): + super(Pulse2OSC, self).__init__(actor, platform, device) + self.buf = RingBuffer(6) + self.heartbeat_on = False + + def handle_read(self, osc_sock): + t = ord(self.serial.read(1)) + self.buf.append(t) + + if t == 0: + try: + heart_signal, heart_rate, o2, pulse = self.buf.getData() + + if pulse == 245 and not self.heartbeat_on: + osc_message = OSCMessage("/%s/heartbeat" % self.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 + self.heartbeat_on = True + elif pulse == 1 and self.heartbeat_on: + #print "off heartbeat", datetime.datetime.now(), heart_signal + self.heartbeat_on = False + osc_message = OSCMessage("/%s/heartbeat" % self.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 + + +def main(): + parser = argparse.ArgumentParser(prog='psychose_actor') + parser.add_argument("-H", '--chaosc_host', required=True, + type=str, help='host of chaosc instance to control') + parser.add_argument("-p", '--chaosc_port', required=True, + type=int, help='port of chaosc instance to control') + + args = parser.parse_args(sys.argv[1:]) + + osc_sock = socket.socket(2, 2, 17) + osc_sock.connect((args.chaosc_host, args.chaosc_port)) + + naming = { + "/dev/ttyUSB0" : ["bjoern", "ehealth"], + "/dev/ttyACM0" : ["bjoern", "ekg"], + "/dev/ttyACM1" : ["bjoern", "pulse"], + "/dev/ttyUSB1" : ["merle", "ehealth"], + "/dev/ttyACM2" : ["merle", "ekg"], + "/dev/ttyACM3" : ["merle", "pulse"], + "/dev/ttyUSB2" : ["uwe", "ehealth"], + "/dev/ttyACM4" : ["uwe", "ekg"], + "/dev/ttyACM5" : ["uwe", "pulse"] + } + + used_devices = dict() + + while 1: + for device, description in naming.iteritems(): + if os.path.exists(device): + if device not in used_devices: + actor, platform = naming[device] + if description[1] == "ehealth": + print device, actor, platform + used_devices[device] = EHealth2OSC(actor, platform, device) + elif description[1] == "ekg": + print device, actor, platform + used_devices[device] = EKG2OSC(actor, platform, device) + elif description[1] == "pulse": + print device, actor, platform + used_devices[device] = Pulse2OSC(actor, platform, device) + else: + raise ValueError("unknown description %r for device %r" % (description, device)) + else: + print "device missing", device + message = OSCMessage("/DeviceMissing") + message.appendTypedArg(description[0], "s") + message.appendTypedArg(description[1], "s") + osc_sock.sendall(message.encode_osc()) + + read_map = {} + for forwarder in used_devices.values(): + read_map[forwarder.serial] = forwarder.handle_read + + readers, writers, errors = select.select(read_map, [], [], 0.1) + for reader in readers: + read_map[reader](osc_sock) diff --git a/sensors2osc/sensors2osc/pulse2osc.py b/sensors2osc/sensors2osc/pulse2osc.py index a99a31e..a3cce45 100644 --- a/sensors2osc/sensors2osc/pulse2osc.py +++ b/sensors2osc/sensors2osc/pulse2osc.py @@ -18,7 +18,7 @@ # # Copyright (C) 2014 Stefan Kögl -from __future__ import absolute_import + import time from datetime import datetime @@ -41,7 +41,7 @@ class RingBuffer(object): self.head = (self.head + 1) % self.length def getData(self): - print "getData", self.ring_buf, self.head + print("getData", self.ring_buf, self.head) data = list() for i in range(self.length + 1, 1, -1): value = self.ring_buf[(self.head - i) % self.length] @@ -52,7 +52,7 @@ class RingBuffer(object): raise ValueError("not complete - ringbuffer resettet") data.append(value) if data[0] != 0x0 or data[1] != 0xff: - print "issue", data + print("issue", data) self.reset() self.ring_buf[0] = 0 self.head = 1 @@ -63,7 +63,7 @@ class RingBuffer(object): def main(): platform = init("pulse2osc") - actor = platform.args.actor + actor = bytes(platform.args.actor, "ascii") buf = RingBuffer(6) heartbeat_on = False @@ -71,49 +71,49 @@ def main(): while 1: try: t = platform.serial_sock.read(1) - except socket.error, msg: + except socket.error as msg: # got disconnected? - print "serial socket error!!!", msg + print("serial socket error!!!", msg) platform.reconnect() try: t = ord(t) - except TypeError, e: + except TypeError as e: continue - print "got value", t + print("got value", t) buf.append(t) if t == 0: try: heart_signal, heart_rate, o2, pulse = buf.getData() - except ValueError, e: - print e + except ValueError as 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") + osc_message = OSCMessage(b"/" + actor + b"/heartbeat") + osc_message.appendTypedArg(1, b"i") + osc_message.appendTypedArg(heart_rate, b"i") + osc_message.appendTypedArg(o2, b"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" + print("on heartbeat", datetime.now(), heart_signal, heart_rate, o2, pulse) + except socket.error as msg: + print("cannot connect to chaosc") continue elif pulse == 1 and heartbeat_on: - print "off heartbeat", datetime.now(), heart_signal, heart_rate, o2, pulse + 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") + osc_message = OSCMessage(b"/" + actor + b"/heartbeat") + osc_message.appendTypedArg(0, b"i") + osc_message.appendTypedArg(heart_rate, b"i") + osc_message.appendTypedArg(o2, b"i") platform.osc_sock.sendall(osc_message.encode_osc()) - except socket.error, msg: - print "cannot connect to chaosc" + except socket.error as msg: + print("cannot connect to chaosc") continue time.sleep(0.1) diff --git a/sensors2osc/sensors2osc/pulse2osc.py.bak b/sensors2osc/sensors2osc/pulse2osc.py.bak new file mode 100644 index 0000000..a99a31e --- /dev/null +++ b/sensors2osc/sensors2osc/pulse2osc.py.bak @@ -0,0 +1,122 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# This file is part of sensors2osc package +# +# sensors2osc is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# sensors2osc is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with sensors2osc. If not, see . +# +# Copyright (C) 2014 Stefan Kögl + +from __future__ import absolute_import + +import time +from datetime import datetime + +from sensors2osc.common import * + + +class RingBuffer(object): + def __init__(self, length): + self.length = length + self.ring_buf = list() + self.reset() + + def reset(self): + self.ring_buf = [-1] * self.length + self.head = 0 + + def append(self, value): + self.ring_buf[self.head] = value + self.head = (self.head + 1) % self.length + + def getData(self): + print "getData", self.ring_buf, self.head + data = list() + for i in range(self.length + 1, 1, -1): + value = self.ring_buf[(self.head - i) % self.length] + if value == -1: + self.reset() + self.ring_buf[0] = 0 + self.head = 1 + raise ValueError("not complete - ringbuffer resettet") + data.append(value) + if data[0] != 0x0 or data[1] != 0xff: + print "issue", data + self.reset() + self.ring_buf[0] = 0 + self.head = 1 + raise ValueError("not synced - ringbuffer resettet") + return data[2:] + + +def main(): + platform = init("pulse2osc") + + actor = platform.args.actor + + buf = RingBuffer(6) + heartbeat_on = False + + while 1: + try: + 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 + + 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 + time.sleep(0.1) + + +if __name__ == '__main__': + main() diff --git a/sensors2osc/sensors2osc/sensorTest.py b/sensors2osc/sensors2osc/sensorTest.py index 79ab7b7..43d0c1d 100644 --- a/sensors2osc/sensors2osc/sensorTest.py +++ b/sensors2osc/sensors2osc/sensorTest.py @@ -48,7 +48,7 @@ class Forwarder(object): def close(self): """Close all resources and unpublish service""" - print "%s: closing..." % (self.device, ) + print("%s: closing..." % (self.device, )) self.serial.close() @@ -58,7 +58,7 @@ class EHealth2OSC(Forwarder): def handle_read(self, osc_sock): data = self.serial.readline()[:-2] - print repr(data) + print(repr(data)) try: airFlow, emg, temp = data.split(";") except ValueError: @@ -94,7 +94,7 @@ class EKG2OSC(Forwarder): class RingBuffer(object): def __init__(self, length): self.length = length - self.ring_buf = [-1 for i in xrange(length)] + self.ring_buf = [-1 for i in range(length)] self.head = 0 def append(self, value): @@ -102,7 +102,7 @@ class RingBuffer(object): self.head = (self.head + 1) % self.length def getData(self): - print "getData", self.ring_buf, self.head + print("getData", self.ring_buf, self.head) data = list() for i in range(7, 1, -1): value = self.ring_buf[(self.head - i) % self.length] @@ -135,7 +135,7 @@ class Pulse2OSC(Forwarder): 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 + print("heartbeat", datetime.datetime.now(), heart_signal) self.heartbeat_on = True elif pulse == 1 and self.heartbeat_on: #print "off heartbeat", datetime.datetime.now(), heart_signal @@ -145,8 +145,8 @@ class Pulse2OSC(Forwarder): osc_message.appendTypedArg(heart_rate, "i") osc_message.appendTypedArg(o2, "i") osc_sock.sendall(osc_message.encode_osc()) - except ValueError, e: - print e + except ValueError as e: + print(e) def main(): @@ -182,7 +182,7 @@ def main(): while 1: read_map = {} - for forwarder in used_devices.values(): + for forwarder in list(used_devices.values()): read_map[forwarder.serial] = forwarder.handle_read readers, writers, errors = select.select(read_map, [], [], 0.1) diff --git a/sensors2osc/sensors2osc/sensorTest.py.bak b/sensors2osc/sensors2osc/sensorTest.py.bak new file mode 100644 index 0000000..79ab7b7 --- /dev/null +++ b/sensors2osc/sensors2osc/sensorTest.py.bak @@ -0,0 +1,190 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# This file is part of sensors2osc package +# +# sensors2osc is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# sensors2osc is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with sensors2osc. If not, see . +# +# Copyright (C) 2014 Stefan Kögl + +import argparse +import os.path +import select +import serial +import socket +import sys +import datetime + +try: + from chaosc.c_osc_lib import OSCMessage +except ImportError as e: + print(e) + from chaosc.osc_lib import OSCMessage + + +class Forwarder(object): + def __init__(self, actor, platform, device): + self.actor = actor + self.platform = platform + self.device = device + self.serial = serial.Serial() + self.serial.port = device + self.serial.baudrate = 115200 + self.serial.timeout = 0 + self.buf_ser2osc = "" + + self.serial.open() + + def close(self): + """Close all resources and unpublish service""" + print "%s: closing..." % (self.device, ) + self.serial.close() + + +class EHealth2OSC(Forwarder): + def __init__(self, actor, platform, device): + super(EHealth2OSC, self).__init__(actor, platform, device) + + def handle_read(self, osc_sock): + data = self.serial.readline()[:-2] + print repr(data) + try: + airFlow, emg, temp = data.split(";") + except ValueError: + return + try: + airFlow = int(airFlow) + emg = int(emg) + temp = int(temp); + except ValueError: + return + osc_message = OSCMessage("/%s/airFlow" % self.actor) + osc_message.appendTypedArg(airFlow, "i") + osc_sock.sendall(osc_message.encode_osc()) + osc_message = OSCMessage("/%s/emg" % self.actor) + osc_message.appendTypedArg(emg, "i") + osc_sock.sendall(osc_message.encode_osc()) + osc_message = OSCMessage("/%s/temperatur" % self.actor) + osc_message.appendTypedArg(temp, "i") + osc_sock.sendall(osc_message.encode_osc()) + + +class EKG2OSC(Forwarder): + def __init__(self, actor, platform, device): + super(EKG2OSC, self).__init__(actor, platform, device) + + def handle_read(self, osc_sock): + t = ord(self.serial.read(1)) + osc_message = OSCMessage("/%s/ekg" % self.actor) + osc_message.appendTypedArg(t, "i") + osc_sock.sendall(osc_message.encode_osc()) + + +class RingBuffer(object): + def __init__(self, length): + self.length = length + self.ring_buf = [-1 for i in xrange(length)] + self.head = 0 + + def append(self, value): + self.ring_buf[self.head] = value + self.head = (self.head + 1) % self.length + + def getData(self): + print "getData", self.ring_buf, self.head + data = list() + for i in range(7, 1, -1): + value = self.ring_buf[(self.head - i) % self.length] + if value == -1: + raise ValueError("not complete") + data.append(value) + if data[0] != 0x0 or data[1] != 0xff: + raise ValueError("not synced") + return data[2:] + + + +class Pulse2OSC(Forwarder): + def __init__(self, actor, platform, device): + super(Pulse2OSC, self).__init__(actor, platform, device) + self.buf = RingBuffer(6) + self.heartbeat_on = False + + def handle_read(self, osc_sock): + t = ord(self.serial.read(1)) + self.buf.append(t) + + if t == 0: + try: + heart_signal, heart_rate, o2, pulse = self.buf.getData() + + if pulse == 245 and not self.heartbeat_on: + osc_message = OSCMessage("/%s/heartbeat" % self.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 + self.heartbeat_on = True + elif pulse == 1 and self.heartbeat_on: + #print "off heartbeat", datetime.datetime.now(), heart_signal + self.heartbeat_on = False + osc_message = OSCMessage("/%s/heartbeat" % self.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 + + +def main(): + parser = argparse.ArgumentParser(prog='psychose_actor') + parser.add_argument("-H", '--chaosc_host', required=True, + type=str, help='host of chaosc instance to control') + parser.add_argument("-p", '--chaosc_port', required=True, + type=int, help='port of chaosc instance to control') + parser.add_argument("-t", '--type', required=True, + type=str, help='ekg, pulse, ehealth') + parser.add_argument("-d", '--device', required=True, + type=str, help='device node under /dev') + parser.add_argument("-a", '--actor', required=True, + type=str, help='actor name') + + + args = parser.parse_args(sys.argv[1:]) + + osc_sock = socket.socket(2, 2, 17) + osc_sock.connect((args.chaosc_host, args.chaosc_port)) + + used_devices = dict() + + actor = args.actor + if args.type == "ehealth": + used_devices[device] = EHealth2OSC(actor, "ehealth", args.device) + elif args.type == "ekg": + used_devices[device] = EKG2OSC(actor, "ekg", args.device) + elif args.type == "pulse": + used_devices[device] = Pulse2OSC(actor, "pulse", args.device) + else: + raise ValueError("unknown description %r for device %r" % (description, device)) + + while 1: + read_map = {} + for forwarder in used_devices.values(): + read_map[forwarder.serial] = forwarder.handle_read + + readers, writers, errors = select.select(read_map, [], [], 0.1) + for reader in readers: + read_map[reader](osc_sock) diff --git a/sensors2osc/sensors2osc/socat_ehealth_test.py b/sensors2osc/sensors2osc/socat_ehealth_test.py index 2d75cef..58a366c 100644 --- a/sensors2osc/sensors2osc/socat_ehealth_test.py +++ b/sensors2osc/sensors2osc/socat_ehealth_test.py @@ -31,5 +31,7 @@ serial_sock.open() while 1: - serial_sock.write("%d;%d;%d\r\n" % (random.randint(0,1023), random.randint(0,1023), random.randint(0,1023))) + data = b";".join((bytes(str(random.randint(0,1023)), "ascii"), bytes(str(random.randint(0,1023)), "ascii"), bytes(str(random.randint(0,1023)), "ascii"))) + b"\r\n" + print("data", data) + serial_sock.write(data) time.sleep(0.1) diff --git a/sensors2osc/sensors2osc/socat_ekg_test.py b/sensors2osc/sensors2osc/socat_ekg_test.py index 8d80ea3..a8f5d1a 100644 --- a/sensors2osc/sensors2osc/socat_ekg_test.py +++ b/sensors2osc/sensors2osc/socat_ekg_test.py @@ -55,13 +55,13 @@ while 1: count = 0 if data_points % (5 * steps) == 0: - print "new steps", steps, delta + print("new steps", steps, delta) steps += delta if steps <= min_steps: delta = 1 elif steps >= max_steps: - print "change step sign", steps, delta + print("change step sign", steps, delta) delta = -1 time.sleep(0.02) diff --git a/sensors2osc/sensors2osc/socat_ekg_test.py.bak b/sensors2osc/sensors2osc/socat_ekg_test.py.bak new file mode 100644 index 0000000..8d80ea3 --- /dev/null +++ b/sensors2osc/sensors2osc/socat_ekg_test.py.bak @@ -0,0 +1,70 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# This file is part of sensors2osc package +# +# sensors2osc is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# sensors2osc is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with sensors2osc. If not, see . +# +# Copyright (C) 2014 Stefan Kögl + +# used this line before opening that script +# socat -d -d PTY,raw,echo=0,link=/tmp/pty1,b115200,user=stefan PTY,raw,echo=0,link=/tmp/pty2,b115200,user=stefan + +import serial, sys, time, random, struct + +serial_sock = serial.Serial() +serial_sock.port = sys.argv[1] +serial_sock.baudrate = 115200 +serial_sock.timeout = 0 +serial_sock.open() + +data_points = 0 + +min_steps = 17 +max_steps = 43 +steps = random.randint(17,43) +count = 0 +delta = 1 + +result = list() + +while 1: + value = random.randint(0, steps) + if count < int(steps / 100. * 20): + value = random.randint(0,20) + elif count < int(steps / 2.): + value = random.randint(20,50) + elif count == int(steps / 2.): + value = 255 + elif count < int(steps / 100. * 70): + value = random.randint(20,50) + elif count <= steps: + value = random.randint(0,20) + elif count >= steps: + count = 0 + + if data_points % (5 * steps) == 0: + print "new steps", steps, delta + steps += delta + + if steps <= min_steps: + delta = 1 + elif steps >= max_steps: + print "change step sign", steps, delta + delta = -1 + + time.sleep(0.02) + count += 1 + data_points += 1 + serial_sock.write(struct.pack("B", value)) diff --git a/sensors2osc/sensors2osc/socat_pulse_test.py b/sensors2osc/sensors2osc/socat_pulse_test.py index d424bff..853f6b3 100644 --- a/sensors2osc/sensors2osc/socat_pulse_test.py +++ b/sensors2osc/sensors2osc/socat_pulse_test.py @@ -21,7 +21,7 @@ # socat -d -d PTY,raw,echo=0,link=/tmp/pty1,b115200,user=stefan PTY,raw,echo=0,link=/tmp/pty2,b115200,user=stefan -from __future__ import absolute_import + import serial, time, random, sys, random, struct diff --git a/sensors2osc/sensors2osc/socat_pulse_test.py.bak b/sensors2osc/sensors2osc/socat_pulse_test.py.bak new file mode 100644 index 0000000..d424bff --- /dev/null +++ b/sensors2osc/sensors2osc/socat_pulse_test.py.bak @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- + +# This file is part of chaosc +# +# chaosc is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# chaosc is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with chaosc. If not, see . +# +# Copyright (C) 2014 Stefan Kögl + +# used this line before opening that script +# socat -d -d PTY,raw,echo=0,link=/tmp/pty1,b115200,user=stefan PTY,raw,echo=0,link=/tmp/pty2,b115200,user=stefan + + +from __future__ import absolute_import + +import serial, time, random, sys, random, struct + + +serial_sock = serial.Serial() +serial_sock.port = sys.argv[1] +serial_sock.baudrate = 115200 +serial_sock.timeout = 0 +serial_sock.open() + + +class DataGenenerator(object): + def __init__(self): + self.get_i = 0 + + def read(self): + value = None + if self.get_i == 0: + value = random.randint(1, 254) + elif self.get_i == 1: + value = random.sample((1, 245), 1)[0] + elif self.get_i == 2: + value = 0 + elif self.get_i == 3: + value = 255 + elif self.get_i == 4: + value = random.randint(1, 255) + elif self.get_i == 5: + value = random.randint(1, 255) + + self.get_i = (self.get_i + 1) % 6 + return value + +r = DataGenenerator() + +while 1: + serial_sock.write(struct.pack("B", r.read())) + #time.sleep(0.1) diff --git a/sensors2osc/setup.py b/sensors2osc/setup.py index f4aaaa9..773f4bb 100644 --- a/sensors2osc/setup.py +++ b/sensors2osc/setup.py @@ -7,6 +7,7 @@ use_setuptools() import sys from setuptools import find_packages, setup +extras = dict() if sys.version_info >= (3,): extras['use_2to3'] = True