ported sensors2osc to python3.3

This commit is contained in:
Stefan Kögl 2014-03-23 13:01:16 +01:00
parent e4a677fcea
commit 7c0f56bf20
19 changed files with 1447 additions and 90 deletions

View File

@ -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

View File

@ -18,7 +18,7 @@
# #
# Copyright (C) 2014 Stefan Kögl # Copyright (C) 2014 Stefan Kögl
from __future__ import absolute_import
import atexit import atexit
import os.path import os.path
@ -43,12 +43,12 @@ class Platform(object):
def connect(self): def connect(self):
print "connect serial" print("connect serial")
self.serial_sock = serial.Serial() self.serial_sock = serial.Serial()
self.serial_sock.port = self.args.device self.serial_sock.port = self.args.device
self.serial_sock.baudrate = 115200 self.serial_sock.baudrate = 115200
self.serial_sock.timeout = 0 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: while 1:
try: try:
self.serial_sock.open() self.serial_sock.open()
@ -59,12 +59,12 @@ class Platform(object):
def close(self): def close(self):
if self.serial_sock is not None: if self.serial_sock is not None:
print "close serial" print("close serial")
self.serial_sock.close() self.serial_sock.close()
def reconnect(self): def reconnect(self):
print "reconnect serial" print("reconnect serial")
self.close() self.close()
self.connect() self.connect()

View File

@ -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 <http://www.gnu.org/licenses/>.
#
# 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

View File

@ -18,7 +18,7 @@
# #
# Copyright (C) 2014 Stefan Kögl # Copyright (C) 2014 Stefan Kögl
from __future__ import absolute_import
from sensors2osc.common import * from sensors2osc.common import *
import time import time
@ -27,66 +27,66 @@ import time
def main(): def main():
platform = init("ehealth2osc") platform = init("ehealth2osc")
actor = platform.args.actor actor = bytes(platform.args.actor, "ascii")
while 1: while 1:
try: try:
data = platform.serial_sock.readline()[:-2] data = platform.serial_sock.readline()[:-2]
#print repr(data) #print repr(data)
except socket.error, msg: except socket.error as msg:
# got disconnected? # got disconnected?
print "serial socket error!!!", msg print("serial socket error!!!", msg)
platform.reconnect() platform.reconnect()
#print "got data", repr(data) print("got data", repr(data))
try: try:
airFlow, emg, temp = data.split(";") airFlow, emg, temp = bytearray(data).split(b";")
except ValueError, e: except ValueError as e:
print e print(e)
continue continue
try: try:
airFlow = int(airFlow) airFlow = int(airFlow)
except ValueError, e: except ValueError as e:
print e print(e)
continue continue
try: try:
osc_message = OSCMessage("/%s/airFlow" % actor) osc_message = OSCMessage(b"/" + actor + b"/airFlow")
osc_message.appendTypedArg(airFlow, "i") osc_message.appendTypedArg(airFlow, b"i")
platform.osc_sock.sendall(osc_message.encode_osc()) platform.osc_sock.sendall(osc_message.encode_osc())
except socket.error, msg: except socket.error as msg:
print "cannot connect to chaosc", msg print("cannot connect to chaosc", msg)
continue continue
try: try:
emg = int(emg) emg = int(emg)
except ValueError, e: except ValueError as e:
print e print(e)
continue continue
try: try:
osc_message = OSCMessage("/%s/emg" % actor) osc_message = OSCMessage(b"/" + actor + b"/emg")
osc_message.appendTypedArg(emg, "i") osc_message.appendTypedArg(emg, b"i")
platform.osc_sock.sendall(osc_message.encode_osc()) platform.osc_sock.sendall(osc_message.encode_osc())
except socket.error, msg: except socket.error as msg:
print "cannot connect to chaosc", msg print("cannot connect to chaosc", msg)
continue continue
try: try:
temp = int(temp) temp = int(temp)
except ValueError, e: except ValueError as e:
print e print(e)
continue continue
try: try:
osc_message = OSCMessage("/%s/temperatur" % actor) osc_message = OSCMessage(b"/" + actor + b"/temperatur")
osc_message.appendTypedArg(temp, "i") osc_message.appendTypedArg(temp, b"i")
platform.osc_sock.sendall(osc_message.encode_osc()) platform.osc_sock.sendall(osc_message.encode_osc())
except socket.error, msg: except socket.error as msg:
print "cannot connect to chaosc", msg print("cannot connect to chaosc", msg)
continue continue

View File

@ -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 <http://www.gnu.org/licenses/>.
#
# 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()

View File

@ -18,7 +18,7 @@
# #
# Copyright (C) 2014 Stefan Kögl # Copyright (C) 2014 Stefan Kögl
from __future__ import absolute_import
import time import time
@ -29,28 +29,28 @@ from sensors2osc.common import *
def main(): def main():
platform = init("ekg2osc") platform = init("ekg2osc")
actor = platform.args.actor actor = bytes(platform.args.actor, "ascii")
while 1: while 1:
try: try:
t = platform.serial_sock.read(1) t = platform.serial_sock.read(1)
except socket.error, msg: except socket.error as msg:
# got disconnected? # got disconnected?
print "serial socket error!!!", msg print("serial socket error!!!", msg)
platform.reconnect() platform.reconnect()
try: try:
t = ord(t) t = ord(t)
except TypeError, e: except TypeError as e:
continue continue
try: try:
print "got value", t print("got value", t)
osc_message = OSCMessage("/%s/ekg" % actor) osc_message = OSCMessage(b"/" + actor + b"/ekg")
osc_message.appendTypedArg(t, "i") osc_message.appendTypedArg(t, b"i")
platform.osc_sock.sendall(osc_message.encode_osc()) platform.osc_sock.sendall(osc_message.encode_osc())
except socket.error, msg: except socket.error as msg:
print "cannot connect to chaosc" print("cannot connect to chaosc")
continue continue

View File

@ -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 <http://www.gnu.org/licenses/>.
#
# 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()

View File

@ -48,7 +48,7 @@ class Forwarder(object):
def close(self): def close(self):
"""Close all resources and unpublish service""" """Close all resources and unpublish service"""
print "%s: closing..." % (self.device, ) print("%s: closing..." % (self.device, ))
self.serial.close() self.serial.close()
@ -58,7 +58,7 @@ class EHealth2OSC(Forwarder):
def handle_read(self, osc_sock): def handle_read(self, osc_sock):
data = self.serial.readline()[:-2] data = self.serial.readline()[:-2]
print repr(data) print(repr(data))
try: try:
airFlow, emg, temp = data.split(";") airFlow, emg, temp = data.split(";")
except ValueError: except ValueError:
@ -106,7 +106,7 @@ class RingBuffer(object):
self.head = (self.head + 1) % self.length self.head = (self.head + 1) % self.length
def getData(self): def getData(self):
print "getData", self.ring_buf, self.head print("getData", self.ring_buf, self.head)
data = list() data = list()
for i in range(7, 1, -1): for i in range(7, 1, -1):
value = self.ring_buf[(self.head - i) % self.length] value = self.ring_buf[(self.head - i) % self.length]
@ -117,7 +117,7 @@ class RingBuffer(object):
raise ValueError("not complete - ringbuffer resettet") raise ValueError("not complete - ringbuffer resettet")
data.append(value) data.append(value)
if data[0] != 0x0 or data[1] != 0xff: if data[0] != 0x0 or data[1] != 0xff:
print "issue", data print("issue", data)
self.reset() self.reset()
self.ring_buf[0] = 0 self.ring_buf[0] = 0
self.head = 1 self.head = 1
@ -146,7 +146,7 @@ class Pulse2OSC(Forwarder):
osc_message.appendTypedArg(heart_rate, "i") osc_message.appendTypedArg(heart_rate, "i")
osc_message.appendTypedArg(o2, "i") osc_message.appendTypedArg(o2, "i")
osc_sock.sendall(osc_message.encode_osc()) 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 self.heartbeat_on = True
elif pulse == 1 and self.heartbeat_on: elif pulse == 1 and self.heartbeat_on:
#print "off heartbeat", datetime.datetime.now(), heart_signal #print "off heartbeat", datetime.datetime.now(), heart_signal
@ -156,8 +156,8 @@ class Pulse2OSC(Forwarder):
osc_message.appendTypedArg(heart_rate, "i") osc_message.appendTypedArg(heart_rate, "i")
osc_message.appendTypedArg(o2, "i") osc_message.appendTypedArg(o2, "i")
osc_sock.sendall(osc_message.encode_osc()) osc_sock.sendall(osc_message.encode_osc())
except ValueError, e: except ValueError as e:
print e print(e)
def main(): def main():
@ -187,30 +187,30 @@ def main():
used_devices = dict() used_devices = dict()
while 1: while 1:
for device, description in naming.iteritems(): for device, description in naming.items():
if os.path.exists(device): if os.path.exists(device):
if device not in used_devices: if device not in used_devices:
actor, platform = naming[device] actor, platform = naming[device]
if description[1] == "ehealth": if description[1] == "ehealth":
print device, actor, platform print(device, actor, platform)
used_devices[device] = EHealth2OSC(actor, platform, device) used_devices[device] = EHealth2OSC(actor, platform, device)
elif description[1] == "ekg": elif description[1] == "ekg":
print device, actor, platform print(device, actor, platform)
used_devices[device] = EKG2OSC(actor, platform, device) used_devices[device] = EKG2OSC(actor, platform, device)
elif description[1] == "pulse": elif description[1] == "pulse":
print device, actor, platform print(device, actor, platform)
used_devices[device] = Pulse2OSC(actor, platform, device) used_devices[device] = Pulse2OSC(actor, platform, device)
else: else:
raise ValueError("unknown description %r for device %r" % (description, device)) raise ValueError("unknown description %r for device %r" % (description, device))
else: else:
print "device missing", device print("device missing", device)
message = OSCMessage("/DeviceMissing") message = OSCMessage("/DeviceMissing")
message.appendTypedArg(description[0], "s") message.appendTypedArg(description[0], "s")
message.appendTypedArg(description[1], "s") message.appendTypedArg(description[1], "s")
osc_sock.sendall(message.encode_osc()) osc_sock.sendall(message.encode_osc())
read_map = {} read_map = {}
for forwarder in used_devices.values(): for forwarder in list(used_devices.values()):
read_map[forwarder.serial] = forwarder.handle_read read_map[forwarder.serial] = forwarder.handle_read
readers, writers, errors = select.select(read_map, [], [], 0.1) readers, writers, errors = select.select(read_map, [], [], 0.1)

View File

@ -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 <http://www.gnu.org/licenses/>.
#
# 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)

View File

@ -18,7 +18,7 @@
# #
# Copyright (C) 2014 Stefan Kögl # Copyright (C) 2014 Stefan Kögl
from __future__ import absolute_import
import time import time
from datetime import datetime from datetime import datetime
@ -41,7 +41,7 @@ class RingBuffer(object):
self.head = (self.head + 1) % self.length self.head = (self.head + 1) % self.length
def getData(self): def getData(self):
print "getData", self.ring_buf, self.head print("getData", self.ring_buf, self.head)
data = list() data = list()
for i in range(self.length + 1, 1, -1): for i in range(self.length + 1, 1, -1):
value = self.ring_buf[(self.head - i) % self.length] value = self.ring_buf[(self.head - i) % self.length]
@ -52,7 +52,7 @@ class RingBuffer(object):
raise ValueError("not complete - ringbuffer resettet") raise ValueError("not complete - ringbuffer resettet")
data.append(value) data.append(value)
if data[0] != 0x0 or data[1] != 0xff: if data[0] != 0x0 or data[1] != 0xff:
print "issue", data print("issue", data)
self.reset() self.reset()
self.ring_buf[0] = 0 self.ring_buf[0] = 0
self.head = 1 self.head = 1
@ -63,7 +63,7 @@ class RingBuffer(object):
def main(): def main():
platform = init("pulse2osc") platform = init("pulse2osc")
actor = platform.args.actor actor = bytes(platform.args.actor, "ascii")
buf = RingBuffer(6) buf = RingBuffer(6)
heartbeat_on = False heartbeat_on = False
@ -71,49 +71,49 @@ def main():
while 1: while 1:
try: try:
t = platform.serial_sock.read(1) t = platform.serial_sock.read(1)
except socket.error, msg: except socket.error as msg:
# got disconnected? # got disconnected?
print "serial socket error!!!", msg print("serial socket error!!!", msg)
platform.reconnect() platform.reconnect()
try: try:
t = ord(t) t = ord(t)
except TypeError, e: except TypeError as e:
continue continue
print "got value", t print("got value", t)
buf.append(t) buf.append(t)
if t == 0: if t == 0:
try: try:
heart_signal, heart_rate, o2, pulse = buf.getData() heart_signal, heart_rate, o2, pulse = buf.getData()
except ValueError, e: except ValueError as e:
print e print(e)
continue continue
if pulse == 245 and not heartbeat_on: if pulse == 245 and not heartbeat_on:
heartbeat_on = True heartbeat_on = True
try: try:
osc_message = OSCMessage("/%s/heartbeat" % actor) osc_message = OSCMessage(b"/" + actor + b"/heartbeat")
osc_message.appendTypedArg(1, "i") osc_message.appendTypedArg(1, b"i")
osc_message.appendTypedArg(heart_rate, "i") osc_message.appendTypedArg(heart_rate, b"i")
osc_message.appendTypedArg(o2, "i") osc_message.appendTypedArg(o2, b"i")
platform.osc_sock.sendall(osc_message.encode_osc()) platform.osc_sock.sendall(osc_message.encode_osc())
print "on heartbeat", datetime.now(), heart_signal, heart_rate, o2, pulse print("on heartbeat", datetime.now(), heart_signal, heart_rate, o2, pulse)
except socket.error, msg: except socket.error as msg:
print "cannot connect to chaosc" print("cannot connect to chaosc")
continue continue
elif pulse == 1 and heartbeat_on: 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 heartbeat_on = False
try: try:
osc_message = OSCMessage("/%s/heartbeat" % actor) osc_message = OSCMessage(b"/" + actor + b"/heartbeat")
osc_message.appendTypedArg(0, "i") osc_message.appendTypedArg(0, b"i")
osc_message.appendTypedArg(heart_rate, "i") osc_message.appendTypedArg(heart_rate, b"i")
osc_message.appendTypedArg(o2, "i") osc_message.appendTypedArg(o2, b"i")
platform.osc_sock.sendall(osc_message.encode_osc()) platform.osc_sock.sendall(osc_message.encode_osc())
except socket.error, msg: except socket.error as msg:
print "cannot connect to chaosc" print("cannot connect to chaosc")
continue continue
time.sleep(0.1) time.sleep(0.1)

View File

@ -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 <http://www.gnu.org/licenses/>.
#
# 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()

View File

@ -48,7 +48,7 @@ class Forwarder(object):
def close(self): def close(self):
"""Close all resources and unpublish service""" """Close all resources and unpublish service"""
print "%s: closing..." % (self.device, ) print("%s: closing..." % (self.device, ))
self.serial.close() self.serial.close()
@ -58,7 +58,7 @@ class EHealth2OSC(Forwarder):
def handle_read(self, osc_sock): def handle_read(self, osc_sock):
data = self.serial.readline()[:-2] data = self.serial.readline()[:-2]
print repr(data) print(repr(data))
try: try:
airFlow, emg, temp = data.split(";") airFlow, emg, temp = data.split(";")
except ValueError: except ValueError:
@ -94,7 +94,7 @@ class EKG2OSC(Forwarder):
class RingBuffer(object): class RingBuffer(object):
def __init__(self, length): def __init__(self, length):
self.length = 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 self.head = 0
def append(self, value): def append(self, value):
@ -102,7 +102,7 @@ class RingBuffer(object):
self.head = (self.head + 1) % self.length self.head = (self.head + 1) % self.length
def getData(self): def getData(self):
print "getData", self.ring_buf, self.head print("getData", self.ring_buf, self.head)
data = list() data = list()
for i in range(7, 1, -1): for i in range(7, 1, -1):
value = self.ring_buf[(self.head - i) % self.length] 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(heart_rate, "i")
osc_message.appendTypedArg(o2, "i") osc_message.appendTypedArg(o2, "i")
osc_sock.sendall(osc_message.encode_osc()) 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 self.heartbeat_on = True
elif pulse == 1 and self.heartbeat_on: elif pulse == 1 and self.heartbeat_on:
#print "off heartbeat", datetime.datetime.now(), heart_signal #print "off heartbeat", datetime.datetime.now(), heart_signal
@ -145,8 +145,8 @@ class Pulse2OSC(Forwarder):
osc_message.appendTypedArg(heart_rate, "i") osc_message.appendTypedArg(heart_rate, "i")
osc_message.appendTypedArg(o2, "i") osc_message.appendTypedArg(o2, "i")
osc_sock.sendall(osc_message.encode_osc()) osc_sock.sendall(osc_message.encode_osc())
except ValueError, e: except ValueError as e:
print e print(e)
def main(): def main():
@ -182,7 +182,7 @@ def main():
while 1: while 1:
read_map = {} read_map = {}
for forwarder in used_devices.values(): for forwarder in list(used_devices.values()):
read_map[forwarder.serial] = forwarder.handle_read read_map[forwarder.serial] = forwarder.handle_read
readers, writers, errors = select.select(read_map, [], [], 0.1) readers, writers, errors = select.select(read_map, [], [], 0.1)

View File

@ -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 <http://www.gnu.org/licenses/>.
#
# 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)

View File

@ -31,5 +31,7 @@ serial_sock.open()
while 1: 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) time.sleep(0.1)

View File

@ -55,13 +55,13 @@ while 1:
count = 0 count = 0
if data_points % (5 * steps) == 0: if data_points % (5 * steps) == 0:
print "new steps", steps, delta print("new steps", steps, delta)
steps += delta steps += delta
if steps <= min_steps: if steps <= min_steps:
delta = 1 delta = 1
elif steps >= max_steps: elif steps >= max_steps:
print "change step sign", steps, delta print("change step sign", steps, delta)
delta = -1 delta = -1
time.sleep(0.02) time.sleep(0.02)

View File

@ -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 <http://www.gnu.org/licenses/>.
#
# 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))

View File

@ -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 # 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 import serial, time, random, sys, random, struct

View File

@ -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 <http://www.gnu.org/licenses/>.
#
# 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)

View File

@ -7,6 +7,7 @@ use_setuptools()
import sys import sys
from setuptools import find_packages, setup from setuptools import find_packages, setup
extras = dict()
if sys.version_info >= (3,): if sys.version_info >= (3,):
extras['use_2to3'] = True extras['use_2to3'] = True