Commit a9cbb517 authored by Antonio Caceres's avatar Antonio Caceres
Browse files

Add abstract classes and Final to type hinting.

parent 9a638e7a
Showing with 56 additions and 19 deletions
+56 -19
from abc import ABC, abstractmethod
class ProximitySensor(ABC):
"""Abstract proximity sensor."""
@property
@abstractmethod
def proximity(self) -> int:
...
class LEDController(ABC):
"""Abstract LED controller."""
@abstractmethod
def fill(self, color: tuple[int, int, int]) -> None:
...
class TouchSensor(ABC):
"""Abstract touch sensor."""
@property
@abstractmethod
def value(self) -> bool:
...
from .abstract import LEDController, ProximitySensor, TouchSensor
from .pulse import run_simple_pulse
from .prox_pulse import run_prox_pulse
from .morse_engine import run_morse_engine
def run(apds, pixels, touch):
def run(apds: ProximitySensor, pixels: LEDController, touch: TouchSensor):
"""
Runs the selected demo on the Trinkey.
......
from .symbols import MORSE_SYMBOL_TO_LETTER, InvalidSymbolError
def append_morse_symbol(message, symbol):
def append_morse_symbol(message: str, symbol: str) -> str:
"""
Decodes a Morse code symbol and appends the corresponding character to the
message.
......@@ -21,7 +21,7 @@ def append_morse_symbol(message, symbol):
raise InvalidSymbolError()
def translate_message(morse):
def translate_message(morse: str) -> str:
"""
Translates a Morse code message into an English string.
......
import time
from typing import Final
from src import morse_code
from .abstract import LEDController, TouchSensor, ProximitySensor
# Constants (Keep same or tests will break)
THRESHOLD = 75
UNIT_TIME = 0.24
THRESHOLD: Final[int] = 75
UNIT_TIME: Final[float] = 0.24
RED = (255, 0, 0)
YELLOW = (220, 160, 0)
GREEN = (0, 255, 0)
RED : Final[tuple[int, int, int]] = (255, 0, 0)
YELLOW: Final[tuple[int, int, int]] = (220, 160, 0)
GREEN : Final[tuple[int, int, int]] = (0, 255, 0)
# Legacy ham radio license requirement
# - 0.24 sec unit time (5 words per minute)
......@@ -15,7 +18,7 @@ GREEN = (0, 255, 0)
# - 0.034 sec unit time (35 words per minute)
def set_color(elapsed_t, px):
def set_color(elapsed_t: float, px: LEDController):
"""
Lights the LEDs on the trinkey depending on the length of input.
......@@ -26,7 +29,7 @@ def set_color(elapsed_t, px):
pass
def add_mark(elapsed_t):
def add_mark(elapsed_t: float) -> str:
"""
Determines if input is a dot (.), dash (-), or nothing ("") based
on the length of input.
......@@ -40,7 +43,7 @@ def add_mark(elapsed_t):
pass
def run_morse_engine(apds, pixels, touch):
def run_morse_engine(apds: ProximitySensor, pixels: LEDController, touch: TouchSensor):
"""
Runs Morse code translation based on proximity input.
......
import time
from .pulse import pulse
from .abstract import LEDController, ProximitySensor
THRESHOLD = 75
......@@ -7,7 +8,7 @@ RED = (150, 10, 10)
GREEN = (30, 100, 10)
def prox_pulse(px, color, prox):
def prox_pulse(px: LEDController, color: tuple[int, int, int], prox: int) -> None:
"""
Maps proximity value to pulse duration and calls pulse.
......@@ -19,7 +20,7 @@ def prox_pulse(px, color, prox):
pass # Delete this line when you start coding!
def run_prox_pulse(apds, pixels):
def run_prox_pulse(apds: ProximitySensor, pixels: LEDController) -> None:
"""
Runs proximity-based pulsing demo.
......
import time
from typing import Final
THRESHOLD = 75
BLUE = (0, 0, 255)
TIME = 1
from .abstract import LEDController
THRESHOLD: Final[int] = 75
BLUE: Final[tuple[int, int, int]] = (0, 0, 255)
TIME: Final[int] = 1
def pulse(px, color, duration):
def pulse(px: LEDController, color: tuple[int, int, int], duration: float):
"""
Fills pixels with a color for a duration, then turns them off.
......@@ -17,7 +20,7 @@ def pulse(px, color, duration):
pass # Delete this line when you start coding!
def run_simple_pulse(pixels):
def run_simple_pulse(pixels: LEDController):
"""
Runs a simple pulse demo.
......
MORSE_SYMBOL_TO_LETTER = {
from typing import Final
MORSE_SYMBOL_TO_LETTER: Final[dict[str, str]] = {
".-": "A",
"-...": "B",
"-.-.": "C",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment