diff --git a/code.py b/code.py index e7cc7e7e2cb436ee0149fe946bea0151b8ed1bb7..2b15acba71192d222ce8939919e5ca236bfcb6e7 100644 --- a/code.py +++ b/code.py @@ -1,29 +1,18 @@ -try: - import board - import neopixel - from adafruit_apds9960.apds9960 import APDS9960 - import touchio - from src.interact import run +import board +import neopixel +from adafruit_apds9960.apds9960 import APDS9960 +import touchio - # Setup board - i2c = board.I2C() - apds = APDS9960(i2c) - pixels = neopixel.NeoPixel(board.NEOPIXEL, 2) - touch = touchio.TouchIn(board.TOUCH2) - apds.enable_proximity = True - print("Loaded Trinkey...") +from src.interact import run - # Try/except is unnecessary in this case, but when code is controlling physical systems - # it is good practice to catch all exceptions and handle shutdown gracefully - try: - # Run engine - run(pixels, adps, touch) - except Exception as e: - pixels.fill((0, 0, 0)) - print("Exception has occurred! Shutting down...") - raise (e) +# Setup board +i2c = board.I2C() +apds = APDS9960(i2c) +pixels = neopixel.NeoPixel(board.NEOPIXEL, 2) +touch = touchio.TouchIn(board.TOUCH2) +apds.enable_proximity = True +print("Loaded Trinkey...") -# Outer try/except clause so code can be tested outside the Trinkey. -except ModuleNotFoundError: - pass +# Call our run function +run(apds, pixels, touch) \ No newline at end of file diff --git a/src/interact.py b/src/interact.py index bcd12690f618522d28f67d3c0f9be1cfcec26559..ced534e14a0b82ea4ef63c9ec37b1a485463736d 100644 --- a/src/interact.py +++ b/src/interact.py @@ -4,6 +4,14 @@ from .morse_engine import run_morse_engine def run(apds, pixels, touch): + """ + Runs the selected demo on the Trinkey. + + Args: + apds: proximity sensor. + pixels: LED controller object. + touch: touch sensor. + """ run_simple_pulse(pixels) # run_prox_pulse(apds, pixels) # run_morse_engine(apds, pixels, touch) diff --git a/src/morse_code.py b/src/morse_code.py index a38e3dd533a40f852fb0013b865788b3cfd3108a..732430b11c216991618f502eb6b176fbce5b5152 100644 --- a/src/morse_code.py +++ b/src/morse_code.py @@ -3,18 +3,35 @@ from .symbols import MORSE_SYMBOL_TO_LETTER, InvalidSymbolError def translate_complete_morse_symbol(message, symbol): """ - Decodes a character according to a modified international morse code standard. + Decodes a Morse code symbol and appends the corresponding character to the + message. - Raises an InvalidSymbolError if the morse symbol is not found. + Args: + message (str): current message string being built. + symbol (str): morse code symbol to be translated + (e.g., ".-", ".----", "del"). + + Returns: + str: The updated message with the translated symbol appended, or with + the last character removed if the symbol is "del". + + Raises: + InvalidSymbolError: If the symbol is not a valid Morse code character. """ raise InvalidSymbolError() def translate_message(morse): """ - Decodes a message from modified morse code to English. + Translates a Morse code message into an English string. + + Args: + morse (str): A series of Morse code symbols separated by "/". Each + symbol can represent a letter, number, or space. - Spaces signify the end of a character. The "del" character deletes the - previous character in the message. + Returns: + str: The translated English message. If any invalid symbols are + encountered, they are ignored. If the last potential symbol is not + valid, it is appended as is (i.e., as dots and dashes). """ return None diff --git a/src/morse_engine.py b/src/morse_engine.py index 823fdb1a83a5d5b76d697cd697f3dc556a4e8d4d..3fca079b56df283f045bb0c2e4e67715d608a352 100644 --- a/src/morse_engine.py +++ b/src/morse_engine.py @@ -16,25 +16,39 @@ GREEN = (0, 255, 0) def set_color(elapsed_t, px): - """Lights the LEDs on the trinkey depending on length of input. - The LED should be lit red (255, 0, 0) if the input is shorter than UNIT_TIME. - The LED should be lit yellow (220, 160, 0) if the input is between 1 and 3 times UNIT_TIME. - The LED should be lit green (0, 255, 0) if the input is longer than 3 times UNIT_TIME. + """ + Lights the LEDs on the trinkey depending on the length of input. + + Args: + elapsed_t (float): duration of proximity detection. + px : LED controller object. """ pass def add_mark(elapsed_t): - """Determines if input is a dot (.) or dash (-) depending on length of input. - Returns an empty string if the input is less than UNIT_TIME. - Returns a dot if the input is between 1 and 3 times UNIT_TIME. - Returns a dash if the input is greater than 3 times UNIT_TIME. + """ + Determines if input is a dot (.), dash (-), or nothing ("") based + on the length of input. + + Args: + elapsed_t (float): duration of proximity detection. + + Returns: + str: dot (.), dash (-), or empty (""). """ pass def run_morse_engine(apds, pixels, touch): - """A Tests Demo on Trinkey""" + """ + Runs Morse code translation based on proximity input. + + Args: + apds: proximity sensor. + pixels: LED controller object. + touch: touch sensor for confirming input. + """ down = None # Time when proximity sensor was first triggered elapsed = 0 # Duration proximity sensor has been triggered message = "" # Translated part of message diff --git a/src/prox_pulse.py b/src/prox_pulse.py index 197f7c78e1e9792f6b7b7fc55ab127fea34cfb49..1fdaf6e740f86ef49b59e81a325bdd260d66d927 100644 --- a/src/prox_pulse.py +++ b/src/prox_pulse.py @@ -8,13 +8,25 @@ GREEN = (30, 100, 10) def prox_pulse(px, color, prox): - """Map the THRESHOLD-255 proximity value to 1000-50 ms pulse - duration and call pulse.""" + """ + Maps proximity value to pulse duration and calls pulse. + + Args: + px: LED controller object. + color (tuple): color to pulse. + prox (int): proximity value. + """ pass # Delete this line when you start coding! def run_prox_pulse(apds, pixels): - """C Tests Demo on Trinkey""" + """ + Runs proximity-based pulsing demo. + + Args: + apds: proximity sensor. + pixels: LED controller object. + """ # Main loop while True: print(apds.proximity) diff --git a/src/pulse.py b/src/pulse.py index 67c897ab7b5956e21fc1ac0447bf0a6240903d50..aadc95efab344ce8778aa449d96e4a6c1f0a8346 100644 --- a/src/pulse.py +++ b/src/pulse.py @@ -6,13 +6,24 @@ TIME = 1 def pulse(px, color, duration): - """Fills the pixels with the given color for a duration, then turns them off - for that duration.""" + """ + Fills pixels with a color for a duration, then turns them off. + + Args: + px (): LED controller object. + color (tuple): Color to display. + duration (float): time in seconds to keep the color on/off. + """ pass # Delete this line when you start coding! def run_simple_pulse(pixels): - """D Tests Demo on Trinkey.""" + """ + Runs a simple pulse demo. + + Args: + pixels: LED controller object. + """ # Main loop while True: pulse(pixels, BLUE, TIME)