splitted the sensors2osc into distinct tools

This commit is contained in:
Stefan Kögl 2014-03-15 20:18:31 +01:00
parent 838869c0b1
commit e60434e8d8
4 changed files with 313 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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