Add buzzer and led support
This commit is contained in:
0
rpi/hardware/buzzer/__init__.py
Normal file
0
rpi/hardware/buzzer/__init__.py
Normal file
20
rpi/hardware/buzzer/buzzer.py
Normal file
20
rpi/hardware/buzzer/buzzer.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import _thread
|
||||||
|
import time
|
||||||
|
|
||||||
|
import grovepi
|
||||||
|
|
||||||
|
class Buzzer:
|
||||||
|
|
||||||
|
_pin : int
|
||||||
|
|
||||||
|
def __init__(self, pin : int):
|
||||||
|
self._pin = pin
|
||||||
|
grovepi.pinMode(self._pin, "OUTPUT")
|
||||||
|
|
||||||
|
def beep(self, duration = 1):
|
||||||
|
_thread.start_new_thread(self._beep, (duration,))
|
||||||
|
|
||||||
|
def _beep(self, duration = 1):
|
||||||
|
grovepi.digitalWrite(self._pin, 1)
|
||||||
|
time.sleep(duration)
|
||||||
|
grovepi.digitalWrite(self._pin, 0)
|
||||||
0
rpi/hardware/led/__init__.py
Normal file
0
rpi/hardware/led/__init__.py
Normal file
15
rpi/hardware/led/led.py
Normal file
15
rpi/hardware/led/led.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import grovepi
|
||||||
|
|
||||||
|
class Led:
|
||||||
|
|
||||||
|
_pin : int
|
||||||
|
|
||||||
|
def __init__(self, pin : int):
|
||||||
|
self._pin = pin
|
||||||
|
grovepi.pinMode(self._pin, "OUTPUT")
|
||||||
|
|
||||||
|
def on(self):
|
||||||
|
grovepi.digitalWrite(self._pin, 1)
|
||||||
|
|
||||||
|
def off(self):
|
||||||
|
grovepi.digitalWrite(self._pin, 0)
|
||||||
@@ -14,7 +14,7 @@ class ClockService:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.screen = Screen()
|
self.screen = Screen()
|
||||||
self.screen.enableBackground()
|
self.screen.enableBackground()
|
||||||
self.screen.displayMessage("Waiting for start...")
|
self.screen.displayMessage("Waiting...")
|
||||||
|
|
||||||
def start(self, time_control : int, increment : int ):
|
def start(self, time_control : int, increment : int ):
|
||||||
self.white_clock = self.__init_clock(time_control, increment)
|
self.white_clock = self.__init_clock(time_control, increment)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from hardware.buzzer.buzzer import Buzzer
|
||||||
|
from hardware.led.led import Led
|
||||||
from models.exceptions.ServiceException import ServiceException
|
from models.exceptions.ServiceException import ServiceException
|
||||||
from services.clock_service import ClockService
|
from services.clock_service import ClockService
|
||||||
from services.detection_service import DetectionService
|
from services.detection_service import DetectionService
|
||||||
@@ -8,11 +10,15 @@ class GameService:
|
|||||||
_detection_service : DetectionService
|
_detection_service : DetectionService
|
||||||
_clock_service : ClockService
|
_clock_service : ClockService
|
||||||
_has_started : bool
|
_has_started : bool
|
||||||
|
_led : Led
|
||||||
|
_buzzer : Buzzer
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._detection_service = DetectionService()
|
self._detection_service = DetectionService()
|
||||||
self._clock_service = ClockService()
|
self._clock_service = ClockService()
|
||||||
self._has_started = False
|
self._has_started = False
|
||||||
|
self._led = Led(7)
|
||||||
|
self._buzzer = Buzzer(6)
|
||||||
|
|
||||||
def start(self, white_name, back_name, time_control : int, increment : int ) -> None:
|
def start(self, white_name, back_name, time_control : int, increment : int ) -> None:
|
||||||
if self._has_started :
|
if self._has_started :
|
||||||
@@ -20,10 +26,12 @@ class GameService:
|
|||||||
self._clock_service.start(time_control, increment)
|
self._clock_service.start(time_control, increment)
|
||||||
self._clock_service.set_on_terminated(self.stop)
|
self._clock_service.set_on_terminated(self.stop)
|
||||||
self._has_started = True
|
self._has_started = True
|
||||||
|
self._led.on()
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self._clock_service.stop()
|
self._clock_service.stop()
|
||||||
self._detection_service.stop()
|
self._detection_service.stop()
|
||||||
|
self._buzzer.beep()
|
||||||
self._has_started = False
|
self._has_started = False
|
||||||
|
|
||||||
def make_move(self) -> tuple[bytes, str] | None:
|
def make_move(self) -> tuple[bytes, str] | None:
|
||||||
|
|||||||
Reference in New Issue
Block a user