From ca668d23e48f8443754dad5a6cc6c538a334fde1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=B6gl?= Date: Sun, 16 Mar 2014 10:43:56 +0100 Subject: [PATCH] fixed ekg2osc --- sensors2osc/sensors2osc/common.py | 50 +++++++++--------- sensors2osc/sensors2osc/main.py | 17 +++++-- sensors2osc/sensors2osc/socat_tester.py | 37 ++++++++++++++ sensors2osc/sensors2osc/test.py | 67 +++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 26 deletions(-) create mode 100644 sensors2osc/sensors2osc/socat_tester.py create mode 100644 sensors2osc/sensors2osc/test.py diff --git a/sensors2osc/sensors2osc/common.py b/sensors2osc/sensors2osc/common.py index e488baa..9ad81df 100644 --- a/sensors2osc/sensors2osc/common.py +++ b/sensors2osc/sensors2osc/common.py @@ -33,29 +33,33 @@ 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 +class Platform(object): + def __init__(self, args): + self.args = args + self.serial_sock = None + self.osc_sock = socket.socket(2, 2, 17) + self.osc_sock.connect((self.args.chaosc_host, self.args.chaosc_port)) -def close(): - global serial - if serial_sock is not None: - print "close serial" - serial_sock.close() + 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 + self.serial_sock.open() -def reconnect(args): - print "reconnect serial" - global serial_sock - close() - connect(args) + 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): @@ -70,11 +74,11 @@ def create_args(name): args = finalize_arg_parser(arg_parser) return args + def init(name): args = create_args(name) - osc_sock = socket.socket(2, 2, 17) - osc_sock.connect((args.chaosc_host, args.chaosc_port)) + platform = Platform(args) + platform.connect() - connect(args) - return args, osc_sock + return platform diff --git a/sensors2osc/sensors2osc/main.py b/sensors2osc/sensors2osc/main.py index e09e3be..fa5545d 100644 --- a/sensors2osc/sensors2osc/main.py +++ b/sensors2osc/sensors2osc/main.py @@ -94,7 +94,11 @@ 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 = list() + self.reset() + + def reset(self): + self.ring_buf = [-1] * self.length self.head = 0 def append(self, value): @@ -107,10 +111,17 @@ class RingBuffer(object): for i in range(7, 1, -1): value = self.ring_buf[(self.head - i) % self.length] if value == -1: - raise ValueError("not complete") + 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: - raise ValueError("not synced") + print "issue", data + self.reset() + self.ring_buf[0] = 0 + self.head = 1 + raise ValueError("not synced - ringbuffer resettet") return data[2:] diff --git a/sensors2osc/sensors2osc/socat_tester.py b/sensors2osc/sensors2osc/socat_tester.py new file mode 100644 index 0000000..4349b09 --- /dev/null +++ b/sensors2osc/sensors2osc/socat_tester.py @@ -0,0 +1,37 @@ +#!/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 + +serial_sock = serial.Serial() + +serial_sock = serial.Serial() +serial_sock.port = "/tmp/pty2" +serial_sock.baudrate = 115200 +serial_sock.timeout = 0 +serial_sock.open() +import time, random, struct + +while 1: + serial_sock.write(struct.pack("B", random.randint(0,255))) + #time.sleep(.5) diff --git a/sensors2osc/sensors2osc/test.py b/sensors2osc/sensors2osc/test.py new file mode 100644 index 0000000..85e1bea --- /dev/null +++ b/sensors2osc/sensors2osc/test.py @@ -0,0 +1,67 @@ +# -*- 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 + +from __future__ import absolute_import + +import time, random + +from sensors2osc.main import RingBuffer + + +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 + +def parse(ring_buffer, reader): + t = r.read() + print t + ring_buffer.append(t) + + if t == 0: + try: + my_data = ring_buffer.getData() + print my_data + except ValueError, e: + print e + + +ring_buffer = RingBuffer(6) +r = DataGenenerator() + +while 1: + parse(ring_buffer, r) + time.sleep(0.5)