slotUI/freeslot.py

Sat, 03 Dec 2011 11:55:01 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Sat, 03 Dec 2011 11:55:01 +0100
changeset 33
c261350bb394
parent 27
3e617fcf999a
child 35
00166228a419
permissions
-rw-r--r--

fixed shortcut detection

"""
FreeSlot project
Blackbox communication library
"""

import serial
import sys

class SerialCommunicator():
    def __init__(self, device, speed):
        self.device = device
        self.speed = speed
        self.com = None
        self.connected = False

    def connect(self):
        if self.connected:
            return True
        try:
            self.com = serial.Serial(self.device, baudrate=self.speed, xonxoff=0, timeout=1)
        except serial.SerialException, err:
            print err
            sys.exit(1)
        self.connected = True
        return True

    def disconnect(self):
        self.com = None
        return True

    def write(self, msg, getanswer=False):
        self.com.write(msg + "\n")
        if getanswer:
            return self.readline()
        return None

    def readline(self):
        answer = self.com.readline()
        return string.strip(answer, "\n")

class Blackbox():
    def __init__(self):
        self.com = None
        self.info = None

    def connect(self, device="/dev/ttyUSB0", speed=115200):
        if self.com == None:
            self.com = SerialCommunicator(device, speed)
        if self.com.connected:
            self.com.disconnect()
        self.com.connect()
        self.info = self.readinfo()

    def disconnect():
        self.com.disconnect()

    def readinfo():
        """
        Read complete Information from connected box
        This does not include race+car status!
        """
        return None

    def progcar(self, carid, command, value):
        """
        Send program packets to specified car id
        valid command: speed, brake
        valid value: 4 bit integer (0..15)
        """
        return True

    def setmode(self, mode):
        """
        Switch the Blackbox mode
        Valid modes are: idle, prepare, race
        note: box will permanently send status info in race mode, so no
              polling is required
        """
        return True

    def getmode(self):
        self.readinfo()
        return self.info["mode"]

mercurial