From a7a8c0fb98d8f991f7e7aa5f9faa7a751a664950 Mon Sep 17 00:00:00 2001 From: frozenwizard <172203+frozenwizard@users.noreply.github.com> Date: Fri, 9 Jun 2023 22:49:57 -0500 Subject: [PATCH] Adding in randomization of firing fin funnels for nu gundam --- .pylintrc | 2 +- src/config/nu_gundam.json | 5 +++++ src/gunpla/BaseGundam.py | 7 ++++--- src/gunpla/nu_gundam.py | 24 ++++++++++++++++++++++++ src/pi/DisabledLED.py | 8 +++++++- src/pi/LED.py | 6 ++++++ src/pi/board_led.py | 2 +- src/test.py | 10 ++++++++-- 8 files changed, 56 insertions(+), 8 deletions(-) diff --git a/.pylintrc b/.pylintrc index 17934f9..5459585 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,6 +1,6 @@ [MASTER] disable= - C0114, W0719, C0103, W0718 + C0114, W0719, C0103, W0718, R0801 [FORMAT] max-line-length=140 \ No newline at end of file diff --git a/src/config/nu_gundam.json b/src/config/nu_gundam.json index 995db0d..44b6b4d 100644 --- a/src/config/nu_gundam.json +++ b/src/config/nu_gundam.json @@ -59,6 +59,11 @@ "name": "Fire Fin Funnels", "path": "fire_funnels", "method": "fire_funnels" + }, + { + "name": "Random", + "path": "random", + "method": "random_funnels" } ] } \ No newline at end of file diff --git a/src/gunpla/BaseGundam.py b/src/gunpla/BaseGundam.py index c5c9620..59d4b5b 100644 --- a/src/gunpla/BaseGundam.py +++ b/src/gunpla/BaseGundam.py @@ -1,7 +1,5 @@ import json -from machine import Pin - from src.pi.LED import LED from src.pi.board_led import BoardLED from src.phew.server import logging @@ -11,6 +9,9 @@ class BaseGundam: + """ + Base Gunpla. + """ board_led = BoardLED() def __init__(self): @@ -22,7 +23,7 @@ def get_config_file(self) -> str: Returns the path to the corresesponding Gundam json file This is abstract """ - pass + raise Exception("Not implemented") def add_routes(self, webserver: server) -> None: """ diff --git a/src/gunpla/nu_gundam.py b/src/gunpla/nu_gundam.py index 12410dd..84ebdbc 100644 --- a/src/gunpla/nu_gundam.py +++ b/src/gunpla/nu_gundam.py @@ -1,4 +1,6 @@ import time +import random + from src.gunpla.BaseGundam import BaseGundam from src.phew.server import Response, Request from src.pi import LED @@ -48,3 +50,25 @@ def fire_funnels(self, request: Request) -> Response: LEDEffects.fire(fin6) return Response("finished", 200) + + def random_funnels(self, request: Request) -> Response: + """ + Randomly fires fin funnels that are enabled for an infinite amount of time + This currently does not end and needs thread management to properly be able to be halted. + """ + funnels = [self._get_led_from_name("fin_funnel_1"), self._get_led_from_name("fin_funnel_2"), + self._get_led_from_name("fin_funnel_3"), self._get_led_from_name("fin_funnel_4"), + self._get_led_from_name("fin_funnel_5"), self._get_led_from_name("fin_funnel_6")] + + # Filter out funnels that are disabled. + funnels = [funnel for funnel in funnels if funnel.enabled()] + + if not funnels: + return Response("No funnels can be fired", 400) + + while True: + funnel = random.choice(funnels) + LEDEffects.fire(funnel) + time.sleep(random.uniform(0, 3)) + + return Response("finished", 200) diff --git a/src/pi/DisabledLED.py b/src/pi/DisabledLED.py index e69c3a5..57e9bf7 100644 --- a/src/pi/DisabledLED.py +++ b/src/pi/DisabledLED.py @@ -6,9 +6,15 @@ class DisabledLED(LED): An LED that is disabled in the configuration. When attempted to manipulate by turning it on or off, it does nothing. """ - def __init__(self, led_name: str): + def __init__(self, led_name: str): # pylint: disable=super-init-not-called self.led_name = led_name + def enabled(self) -> bool: + """ + Returns false as the LED is not connected + """ + return False + def name(self) -> str: return self.led_name diff --git a/src/pi/LED.py b/src/pi/LED.py index ff25a17..f0e1272 100644 --- a/src/pi/LED.py +++ b/src/pi/LED.py @@ -10,6 +10,12 @@ def __init__(self, pin_number: int, name: str): self.pin: Pin = Pin(pin_number, Pin.OUT) self.led_name = name + def enabled(self) -> bool: + ''' + Returns false as the LED is not connected + ''' + return True + def on(self) -> None: """ Turns on the LED light diff --git a/src/pi/board_led.py b/src/pi/board_led.py index 61253e3..0b93661 100644 --- a/src/pi/board_led.py +++ b/src/pi/board_led.py @@ -7,6 +7,6 @@ class BoardLED(LED): Special Representation of the onboard Pico LED """ - def __init__(self): + def __init__(self): # pylint # pylint: disable=(super-init-not-called self.pin: Pin = Pin("LED", Pin.OUT) self.led_name = "Board LED" diff --git a/src/test.py b/src/test.py index 324d41a..eed0021 100644 --- a/src/test.py +++ b/src/test.py @@ -2,10 +2,15 @@ # This requires just the basic Raspberry Pi Pico W # Once deployed on the Pico W, it will flash the onboard LED. -from machine import Pin import time +from machine import Pin + + def main(): + """ + Blinks the onboard Raspberry Pi Pico W LED several times. + """ led = Pin("LED", Pin.OUT) led.on() time.sleep(0.5) @@ -20,5 +25,6 @@ def main(): led.off() time.sleep(0.5) + if __name__ == "__main__": - main() \ No newline at end of file + main()