implemented better ring buffer and cleaned up code

This commit is contained in:
Stefan Kögl 2014-03-08 22:02:50 +01:00
parent 1bda5388d6
commit 72f9a44b4e
1 changed files with 57 additions and 65 deletions

View File

@ -2,24 +2,19 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import argparse import argparse
import ConfigParser
import os.path import os.path
import random
import re
import select import select
import serial import serial
import socket import socket
import sys import sys
import time
import struct
import datetime import datetime
try: try:
from chaosc.c_osc_lib import * from chaosc.c_osc_lib import OSCMessage
except ImportError as e: except ImportError as e:
print(e) print(e)
from chaosc.osc_lib import * from chaosc.osc_lib import OSCMessage
class Forwarder(object): class Forwarder(object):
@ -33,18 +28,11 @@ class Forwarder(object):
self.serial.timeout = 0 self.serial.timeout = 0
self.buf_ser2osc = "" self.buf_ser2osc = ""
#try:
self.serial.open() self.serial.open()
#self.serial.setRTS(False)
self.alive = True
#except Exception, e:
#print "opening", e
#self.serial.close()
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.alive = False
self.serial.close() self.serial.close()
@ -52,7 +40,7 @@ class EHealth2OSC(Forwarder):
def __init__(self, actor, platform, device): def __init__(self, actor, platform, device):
super(EHealth2OSC, self).__init__(actor, platform, device) super(EHealth2OSC, self).__init__(actor, platform, device)
def handleRead(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:
@ -84,7 +72,7 @@ class EKG2OSC(Forwarder):
def __init__(self, actor, platform, device): def __init__(self, actor, platform, device):
super(EKG2OSC, self).__init__(actor, platform, device) super(EKG2OSC, self).__init__(actor, platform, device)
def handleRead(self, osc_sock): def handle_read(self, osc_sock):
t = ord(self.serial.read(1)) t = ord(self.serial.read(1))
osc_message = OSCMessage("/%s/ekg" % self.actor) osc_message = OSCMessage("/%s/ekg" % self.actor)
osc_message.appendTypedArg(t, "i") osc_message.appendTypedArg(t, "i")
@ -92,31 +80,44 @@ class EKG2OSC(Forwarder):
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 and data[1] != 0xff:
raise ValueError("not synced")
return data[2:]
class Pulse2OSC(Forwarder): class Pulse2OSC(Forwarder):
def __init__(self, actor, platform, device): def __init__(self, actor, platform, device):
super(Pulse2OSC, self).__init__(actor, platform, device) super(Pulse2OSC, self).__init__(actor, platform, device)
self.buf = [0 for i in xrange(24)] self.buf = RingBuffer(6)
self.position = 0
self.start = -1
self.heartbeat_send = False self.heartbeat_send = False
def handleRead(self, osc_sock): def handle_read(self, osc_sock):
t = ord(self.serial.read(1)) t = ord(self.serial.read(1))
pos = (self.position + 1) % 24 self.buf.append(t)
self.buf[pos] = t
self.position = pos
if t == 0: if t == 0:
self.start = pos try:
heart_signal, heart_rate, o2, pulse = self.buf.getData()
#print "start", self.start
if self.start > -1:
data = range(6)
for i in range(6):
data[i] = self.buf[(self.start + i) % 24]
sync1, sync2, heart_signal, heart_rate, o2, pulse = data
#print sync1, sync2, heart_signal, heart_rate, o2, pulse
if pulse == 245 and not self.heartbeat_send: if pulse == 245 and not self.heartbeat_send:
osc_message = OSCMessage("/%s/heartbeat" % self.actor) osc_message = OSCMessage("/%s/heartbeat" % self.actor)
osc_message.appendTypedArg(1, "i") osc_message.appendTypedArg(1, "i")
@ -133,16 +134,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:
#osc_message = OSCMessage("/%s/o2" % self.actor) print e
#osc_message.appendTypedArg(o2, "i")
#osc_sock.sendall(osc_message.encode_osc())
#osc_message = OSCMessage("/%s/heartrate" % self.actor)
#osc_message.appendTypedArg(heart_rate, "i")
#osc_sock.sendall(osc_message.encode_osc())
self.start = -1
@ -156,7 +149,6 @@ def main():
args = parser.parse_args(sys.argv[1:]) args = parser.parse_args(sys.argv[1:])
connections = list()
osc_sock = socket.socket(2, 2, 17) osc_sock = socket.socket(2, 2, 17)
osc_sock.connect((args.chaosc_host, args.chaosc_port)) osc_sock.connect((args.chaosc_host, args.chaosc_port))
@ -199,14 +191,14 @@ def main():
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
m = OSCMessage("/DeviceMissing") message = OSCMessage("/DeviceMissing")
m.appendTypedArg(description[0], "s") message.appendTypedArg(description[0], "s")
m.appendTypedArg(description[1], "s") message.appendTypedArg(description[1], "s")
osc_sock.sendall(m.encode_osc()) osc_sock.sendall(message.encode_osc())
read_map = {} read_map = {}
for forwarder in used_devices.values(): for forwarder in used_devices.values():
read_map[forwarder.serial] = forwarder.handleRead 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)
#print "readers", readers #print "readers", readers