Add WIP controller support.

This commit is contained in:
Matthew Stratford 2021-02-14 23:58:32 +00:00
parent 4741694d66
commit 0c176bea03
5 changed files with 73 additions and 0 deletions

View file

@ -7,3 +7,4 @@ setproctitle
pyttsx3 pyttsx3
websockets websockets
typing_extensions typing_extensions
pyserial

0
controllers/__init__.py Normal file
View file

24
controllers/controller.py Normal file
View file

@ -0,0 +1,24 @@
from multiprocessing import Queue
from typing import Any, Callable, List
# Main controller class. All implementations of controller support should inherit this.
class Controller():
callbacks: List[Callable] = []
player_to_q: List[Queue]
player_from_q: List[Queue]
def __init__(self, player_to_q: List[Queue], player_from_q: List[Queue]):
self.receive()
return
# Registers a function for the controller class to call to tell BAPSicle to do something.
def register_callback(self, callback: Callable):
self.callbacks.append(callback)
return
# Loop etc in here to process the data from your controller and call the callbacks.
def receive(self):
return

View file

@ -0,0 +1,43 @@
from typing import List
from controllers.controller import Controller
from multiprocessing import Queue
import serial
import sys
class MattchBox(Controller):
ser: serial.Serial
def __init__(self, player_to_q: List[Queue], player_from_q: List[Queue]):
# connect to serial port
self.ser = serial.serial_for_url("/dev/cu.usbserial-310", do_not_open=True)
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
self.receive()
def receive(self):
while self.ser.is_open:
try:
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.player_to_q[int(line / 2)].put("PLAY")
elif (line in [2,4,6]):
self.player_to_q[int(line / 2)-1].put("STOP")
except:
continue

View file

@ -13,6 +13,7 @@
October, November 2020 October, November 2020
""" """
import asyncio import asyncio
from controllers.mattchbox_usb import MattchBox
import copy import copy
import multiprocessing import multiprocessing
import queue import queue
@ -420,6 +421,10 @@ async def startServer():
websockets_server = multiprocessing.Process(target=WebsocketServer, args=(channel_to_q, channel_from_q, state)) websockets_server = multiprocessing.Process(target=WebsocketServer, args=(channel_to_q, channel_from_q, state))
websockets_server.start() websockets_server.start()
controller_handler = multiprocessing.Process(target=MattchBox, args=(channel_to_q, channel_from_q))
controller_handler.start()
if not isMacOS(): if not isMacOS():
# Temporary RIP. # Temporary RIP.