2021-02-14 23:58:32 +00:00
|
|
|
from typing import List
|
|
|
|
from controllers.controller import Controller
|
|
|
|
from multiprocessing import Queue
|
|
|
|
import serial
|
|
|
|
import sys
|
2021-04-04 21:34:46 +00:00
|
|
|
from setproctitle import setproctitle
|
2021-02-14 23:58:32 +00:00
|
|
|
class MattchBox(Controller):
|
|
|
|
ser: serial.Serial
|
|
|
|
|
|
|
|
def __init__(self, player_to_q: List[Queue], player_from_q: List[Queue]):
|
2021-04-04 21:34:46 +00:00
|
|
|
|
|
|
|
process_title = "ControllerHandler"
|
|
|
|
setproctitle(process_title)
|
|
|
|
#current_process().name = process_title
|
|
|
|
|
2021-02-14 23:58:32 +00:00
|
|
|
# connect to serial port
|
2021-03-22 00:33:14 +00:00
|
|
|
self.ser = serial.serial_for_url("/dev/cu.usbserial-210", do_not_open=True)
|
2021-02-14 23:58:32 +00:00
|
|
|
self.ser.baudrate = 2400
|
|
|
|
|
|
|
|
# TOOD: These need to be split in the player handler.
|
|
|
|
self.player_from_q = player_from_q
|
|
|
|
self.player_to_q = player_to_q
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.ser.open()
|
|
|
|
except serial.SerialException as e:
|
|
|
|
sys.stderr.write('Could not open serial port {}: {}\n'.format(self.ser.name, e))
|
|
|
|
return
|
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
self.handler()
|
2021-02-14 23:58:32 +00:00
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
def handler(self):
|
|
|
|
while True:
|
2021-02-14 23:58:32 +00:00
|
|
|
try:
|
2021-03-22 00:33:14 +00:00
|
|
|
if self.ser.is_open:
|
|
|
|
line = int.from_bytes(self.ser.read(1), "big") # Endianness doesn't matter for 1 byte.
|
|
|
|
print("Controller got:", line)
|
|
|
|
if (line == 255):
|
|
|
|
print("Sending back KeepAlive")
|
|
|
|
self.ser.write(b'\xff') # Send 255 back.
|
|
|
|
elif (line in [1,3,5]):
|
|
|
|
self.sendToPlayer(int(line / 2), "PLAY")
|
|
|
|
elif (line in [2,4,6]):
|
|
|
|
self.sendToPlayer(int(line / 2)-1, "STOP")
|
2021-02-14 23:58:32 +00:00
|
|
|
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
2021-03-22 00:33:14 +00:00
|
|
|
def sendToPlayer(self, channel: int, msg:str):
|
|
|
|
self.player_to_q[channel].put("CONTROLLER:" + msg)
|
|
|
|
|
2021-02-14 23:58:32 +00:00
|
|
|
|