added first drafts of simple gamelib

This commit is contained in:
schneider 2011-12-12 18:02:24 +01:00
parent 50fd8a6613
commit 8fc54d1b2d
5 changed files with 161 additions and 0 deletions

View File

View File

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

View File

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

5
tools/game/setup.py Normal file
View File

@ -0,0 +1,5 @@
from distutils.core import setup
setup(name='r0ketrem0te',
version='1.0',
packages=['r0ketrem0te'],
)

11
tools/game/simpletest.py Normal file
View File

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