From 8fc54d1b2dec8bc9c11aaf6293abdd95de7f9e91 Mon Sep 17 00:00:00 2001 From: schneider Date: Mon, 12 Dec 2011 18:02:24 +0100 Subject: [PATCH] added first drafts of simple gamelib --- tools/game/r0ketrem0te/__init__.py | 0 tools/game/r0ketrem0te/rem0te.py | 53 +++++++++++++ tools/game/r0ketrem0te/serialinterface.py | 92 +++++++++++++++++++++++ tools/game/setup.py | 5 ++ tools/game/simpletest.py | 11 +++ 5 files changed, 161 insertions(+) create mode 100644 tools/game/r0ketrem0te/__init__.py create mode 100644 tools/game/r0ketrem0te/rem0te.py create mode 100644 tools/game/r0ketrem0te/serialinterface.py create mode 100644 tools/game/setup.py create mode 100644 tools/game/simpletest.py diff --git a/tools/game/r0ketrem0te/__init__.py b/tools/game/r0ketrem0te/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tools/game/r0ketrem0te/rem0te.py b/tools/game/r0ketrem0te/rem0te.py new file mode 100644 index 0000000..080e82b --- /dev/null +++ b/tools/game/r0ketrem0te/rem0te.py @@ -0,0 +1,53 @@ +import serialinterface +import thread +import threading +import Queue +import crcmod + +class r0ket: + def __init__(self, path2device): + self.ser = serialinterface.SerialInterface(path2device, 115200, 0) + self.free = threading.Lock() + self.packets = Queue.Queue() + thread.start_new_thread(self.readerThread,()) + self.setPacketLength(0x20) + self.crc = crcmod.predefined.mkCrcFun('crc-ccitt-false') + + def writeCommand(self, command, data): + crc = self.crc(data) + data += chr(crc>>8); + data += chr(crc&0xFF); + self.free.acquire() + print 'sending command:', command, 'len:', len(data), 'data:', list(data) + self.ser.writeMessage(command,data); + + def readerThread(self): + while True: + try: + (command, data) = self.ser.readMessage() + if command == '1': + self.newPacket(data) + elif command == '2': + self.free.release() + elif command: + while True: + pass + except Exception as e: + print e + + def newPacket(self, data): + print "received:", list(data) + self.packets.put(data) + + def gotPacket(self): + return not self.packets.empty() + + def getPacket(self): + return self.packets.get() + + def sendPacket(self, packet): + self.writeCommand('1', packet) + + def setPacketLength(self, length): + self.free.acquire() + self.ser.writeMessage('6', '%c'%length) diff --git a/tools/game/r0ketrem0te/serialinterface.py b/tools/game/r0ketrem0te/serialinterface.py new file mode 100644 index 0000000..67821c1 --- /dev/null +++ b/tools/game/r0ketrem0te/serialinterface.py @@ -0,0 +1,92 @@ +import serial +import string +import sys +import time + +class SerialInterface: + def __init__ ( self, path2device, baudrate, timeout=0): + self.portopen = False + while not self.portopen: + try: + self.ser = serial.Serial(path2device, baudrate) + self.path2device = path2device + self.baudrate = baudrate + self.timeout = timeout+1 + self.ser.flushInput() + self.ser.flushOutput() + if timeout: + self.ser.setTimeout(timeout+1) + self.portopen = True + except serial.SerialException: + print "exception while opening" + pass + time.sleep(1) + #print "done" + def close(self): + try: + self.portopen = False + self.ser.close() + except serial.SerialException: + pass + def reinit(self): + self.close() + print "reopening" + while not self.portopen: + self.__init__(self.path2device, self.baudrate, self.timeout) + time.sleep(1) + print "done" + + def writeMessage(self,command,message): + enc = "\\"+ command + message.replace('\\','\\\\') + "\\0"; + #print 'writing %s' % list(enc) + try: + self.ser.write(enc) + except : + pass + #self.reinit() + + def readMessage(self): + data = "" + escaped = False + stop = False + start = False + inframe = False + command = '' + while True: + starttime = time.time() + c = self.ser.read(1) + endtime = time.time() + if len(c) == 0: #A timout occured + if endtime-starttime < self.timeout - 1: + print "port broken" + self.reinit() + raise Exception() + else: + #print 'TIMEOUT' + return (False, '') + if escaped: + if c == '\\': + d = '\\' + elif c == '0': + stop = True + inframe = False + else: + start = True + inframe = True + command = c + data = "" + escaped = False + elif c == '\\': + escaped = 1 + else: + d = c + + if start and inframe: + start = False + elif stop: + #print 'received message: len=%d data=%s'%(len(data),data) + #print 'received message. command=',command, "data=" ,list(data) + return (command, data) + elif escaped == False and inframe: + data += str(d) + diff --git a/tools/game/setup.py b/tools/game/setup.py new file mode 100644 index 0000000..9ef3851 --- /dev/null +++ b/tools/game/setup.py @@ -0,0 +1,5 @@ +from distutils.core import setup +setup(name='r0ketrem0te', + version='1.0', + packages=['r0ketrem0te'], + ) diff --git a/tools/game/simpletest.py b/tools/game/simpletest.py new file mode 100644 index 0000000..f757cae --- /dev/null +++ b/tools/game/simpletest.py @@ -0,0 +1,11 @@ +import r0ketrem0te.rem0te +import time +r = r0ketrem0te.rem0te.r0ket('/dev/ttyACM0') + +while True: + r.sendPacket("\x20GA\x00\x00\x00\x00\x01\x02\x03\x04\x01\x02\x03\x01\x02\x51\x01\x02\x03\x04\x00test\x00\x06\x07\x08") + time.sleep(1) + #packet = r.getPacket() + #print list(packet) + #r.sendPacket('12345678901234567890123456789012') +