From 8baf7f05aa956514104be54c9db775475d68e6b4 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Thu, 19 Dec 2013 18:12:41 +0100 Subject: [PATCH] Beopardy-Style buttonreader --- buttonreader.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/buttonreader.py b/buttonreader.py index 436a697..f93a963 100644 --- a/buttonreader.py +++ b/buttonreader.py @@ -68,6 +68,56 @@ class SerialInput(BaseInput): _add_input("Serial", SerialInput) +class BeopardySerialInput(BaseInput): + def __init__(self, app, device, baudrate=9600, bytesize=8, parity='N', stopbits=1): + super(BeopardySerialInput, self).__init__(app) + try: + self._serial = serial.Serial(device, baudrate, bytesize, parity, stopbits) + except serial.SerialException as e: + raise InputException("Error: %s" % e) + + # playerDict + self._playerDict = { + 'a': 1, + 'b': 2, + 'c': 3, + 'd': 4, + } + + print(" >> serial %s initialized with rate %s %s%s%s" % (device, baudrate, bytesize, parity, stopbits)) + + def run(self): + while True: + c = self._serial.read(1) + #print("serial: read ", c) + if len(c) == 1 and c in self._playerDict: + #print("sending button event ", c, " ", self._playerDict[c]) + self._sendButtonEvent(self._playerDict[c]) + + @QtCore.Slot(bool) + def buzzersOpen(self, isOpen): + self._serial.write("ABCD") + #print("serial: ABCD") + if isOpen: + # buzzers are open now + self._serial.write("R") + #print("serial: R") + else: + # tell them that buttons are closed now + self._serial.write("r") + #print("serial: r") + + @QtCore.Slot(int) + def playerGotQuestion(self, playerNo): + if not (playerNo >= 1 and playerNo <= 4): + return + + self._serial.write(chr(ord('a') + (playerNo-1))) + #print("serial: ", chr(ord('a') + (playerNo-1))) + + +_add_input("BeopardySerial", BeopardySerialInput) + class FifoInput(BaseInput): def __init__(self, app, fifoPath, debug=False): super(FifoInput, self).__init__(app)