Browse Source

Beopardy-Style buttonreader

Sebastian Lohff 6 years ago
parent
commit
8baf7f05aa
1 changed files with 50 additions and 0 deletions
  1. 50
    0
      buttonreader.py

+ 50
- 0
buttonreader.py View File

@@ -68,6 +68,56 @@ class SerialInput(BaseInput):
68 68
 
69 69
 _add_input("Serial", SerialInput)
70 70
 
71
+class BeopardySerialInput(BaseInput):
72
+	def __init__(self, app, device, baudrate=9600, bytesize=8, parity='N', stopbits=1):
73
+		super(BeopardySerialInput, self).__init__(app)
74
+		try:
75
+			self._serial = serial.Serial(device, baudrate, bytesize, parity, stopbits)
76
+		except serial.SerialException as e:
77
+			raise InputException("Error: %s" % e)
78
+
79
+		# playerDict
80
+		self._playerDict = {
81
+			'a': 1,
82
+			'b': 2,
83
+			'c': 3,
84
+			'd': 4,
85
+		}
86
+
87
+		print(" >> serial %s initialized with rate %s %s%s%s" % (device, baudrate, bytesize, parity, stopbits))
88
+
89
+	def run(self):
90
+		while True:
91
+			c = self._serial.read(1)
92
+			#print("serial: read ", c)
93
+			if len(c) == 1 and c in self._playerDict:
94
+				#print("sending button event ", c, " ", self._playerDict[c])
95
+				self._sendButtonEvent(self._playerDict[c])
96
+
97
+	@QtCore.Slot(bool)
98
+	def buzzersOpen(self, isOpen):
99
+		self._serial.write("ABCD")
100
+		#print("serial: ABCD")
101
+		if isOpen:
102
+			# buzzers are open now
103
+			self._serial.write("R")
104
+			#print("serial: R")
105
+		else:
106
+			# tell them that buttons are closed now
107
+			self._serial.write("r")
108
+			#print("serial: r")
109
+
110
+	@QtCore.Slot(int)
111
+	def playerGotQuestion(self, playerNo):
112
+		if not (playerNo >= 1 and playerNo <= 4):
113
+			return
114
+
115
+		self._serial.write(chr(ord('a') + (playerNo-1)))
116
+		#print("serial: ", chr(ord('a') + (playerNo-1)))
117
+
118
+
119
+_add_input("BeopardySerial", BeopardySerialInput)
120
+
71 121
 class FifoInput(BaseInput):
72 122
 	def __init__(self, app, fifoPath, debug=False):
73 123
 		super(FifoInput, self).__init__(app)

Loading…
Cancel
Save