From e60434e8d8ffdd11869d345607fd076c8ac12b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=B6gl?= Date: Sat, 15 Mar 2014 20:18:31 +0100 Subject: [PATCH] splitted the sensors2osc into distinct tools --- sensors2osc/sensors2osc/common.py | 92 ++++++++++++++++++++++ sensors2osc/sensors2osc/ehealth2osc.py | 72 +++++++++++++++++ sensors2osc/sensors2osc/ekg2osc.py | 47 ++++++++++++ sensors2osc/sensors2osc/pulse2osc.py | 102 +++++++++++++++++++++++++ 4 files changed, 313 insertions(+) create mode 100644 sensors2osc/sensors2osc/common.py create mode 100644 sensors2osc/sensors2osc/ehealth2osc.py create mode 100644 sensors2osc/sensors2osc/ekg2osc.py create mode 100644 sensors2osc/sensors2osc/pulse2osc.py diff --git a/sensors2osc/sensors2osc/common.py b/sensors2osc/sensors2osc/common.py new file mode 100644 index 0000000..267ae32 --- /dev/null +++ b/sensors2osc/sensors2osc/common.py @@ -0,0 +1,92 @@ +#!/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 + +serial_sock = None + +def connect(args): + print "connect serial" + global serial_sock + serial_sock = serial.Serial() + serial_sock.port = args.device + serial_sock.baudrate = 115200 + serial_sock.timeout = 0 + + +def close(): + global serial + if serial_sock is not None: + print "close serial" + serial_sock.close() + + +def reconnect(args): + print "reconnect serial" + global serial_sock + close() + connect(args) + + +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) + + + while 1: + try: + t = ord(serial_sock.read(1)) + print "got value", t + osc_message = OSCMessage("/%s/ekg" % actor) + osc_message.appendTypedArg(t, "i") + osc_sock.sendall(osc_message.encode_osc()) + except socket.error, msg: + # got disconnected? + print "lost connection!!!" + reconnect(args) + +def init(name): + args = create_args(name) + osc_sock = socket.socket(2, 2, 17) + osc_sock.connect((args.chaosc_host, args.chaosc_port)) + + connect(args) + return args, osc_sock + diff --git a/sensors2osc/sensors2osc/ehealth2osc.py b/sensors2osc/sensors2osc/ehealth2osc.py new file mode 100644 index 0000000..5e05f4d --- /dev/null +++ b/sensors2osc/sensors2osc/ehealth2osc.py @@ -0,0 +1,72 @@ +#!/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 * + + +def main(): + args, osc_sock = init("ehealth2osc") + + actor = args.actor + + while 1: + try: + data = serial_sock.readline()[:-2] + print "got data", repr(data) + try: + airFlow, emg, temp = data.split(";") + except ValueError: + continue + + try: + airFlow = int(airFlow) + osc_message = OSCMessage("/%s/airFlow" % actor) + osc_message.appendTypedArg(airFlow, "i") + osc_sock.sendall(osc_message.encode_osc()) + except ValueError: + pass + + try: + emg = int(emg) + osc_message = OSCMessage("/%s/emg" % actor) + osc_message.appendTypedArg(emg, "i") + osc_sock.sendall(osc_message.encode_osc()) + except ValueError: + pass + + try: + temp = int(temp) + osc_message = OSCMessage("/%s/temperatur" % actor) + osc_message.appendTypedArg(temp, "i") + osc_sock.sendall(osc_message.encode_osc()) + except ValueError: + pass + except socket.error, msg: + # got disconnected? + print "lost connection!!!" + reconnect(args) + + + +if __name__ == '__main__': + atexit.register(close) + main() diff --git a/sensors2osc/sensors2osc/ekg2osc.py b/sensors2osc/sensors2osc/ekg2osc.py new file mode 100644 index 0000000..3970a06 --- /dev/null +++ b/sensors2osc/sensors2osc/ekg2osc.py @@ -0,0 +1,47 @@ +#!/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 * + + +def main(): + args, osc_sock = init("ekg2osc") + + actor = args.actor + + while 1: + try: + t = ord(serial_sock.read(1)) + print "got value", t + osc_message = OSCMessage("/%s/ekg" % actor) + osc_message.appendTypedArg(t, "i") + osc_sock.sendall(osc_message.encode_osc()) + except socket.error, msg: + # got disconnected? + print "lost connection!!!" + reconnect(args) + + + +if __name__ == '__main__': + atexit.register(close) + main() diff --git a/sensors2osc/sensors2osc/pulse2osc.py b/sensors2osc/sensors2osc/pulse2osc.py new file mode 100644 index 0000000..3683cce --- /dev/null +++ b/sensors2osc/sensors2osc/pulse2osc.py @@ -0,0 +1,102 @@ +#!/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 * + +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:] + + +def main(): + args, osc_sock = init("pulse2osc") + + 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 + except socket.error, msg: + # got disconnected? + print "lost connection!!!" + reconnect(args) + + + +if __name__ == '__main__': + atexit.register(close) + main()