Commit 1e7de354 authored by Adam Blank's avatar Adam Blank
Browse files

Initial commit

parents
No related merge requests found
Showing with 406 additions and 0 deletions
+406 -0
try:
import board
import neopixel
from adafruit_apds9960.apds9960 import APDS9960
import touchio
from src.interact import run
# 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...")
# 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)
# Outer try/except clause so code can be tested outside the Trinkey.
except ModuleNotFoundError:
pass
{
"upload": ["src/interact.py", "src/morse_code.py", "src/morse_engine.py", "src/prox_pulse.py", "src/pulse.py"]
}
[tool.pytest.ini_options]
pythonpath = "src"
[pytest]
addopts = -vv --color=yes --no-header --disable-warnings -p pytest_clarity -rN -x
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):
run_simple_pulse(pixels)
# run_prox_pulse(apds, pixels)
# run_morse_engine(apds, pixels, touch)
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.
Raises an InvalidSymbolError if the morse symbol is not found.
"""
raise InvalidSymbolError()
def translate_message(morse):
"""
Decodes a message from modified morse code to English.
Spaces signify the end of a character. The "del" character deletes the
previous character in the message.
"""
return None
import time
from src import morse_code
# Constants (Keep same or tests will break)
THRESHOLD = 75
UNIT_TIME = 0.24
RED = (255, 0, 0)
YELLOW = (220, 160, 0)
GREEN = (0, 255, 0)
# Legacy ham radio license requirement
# - 0.24 sec unit time (5 words per minute)
# Fastest recorded transcription on a straight key
# - 0.034 sec unit time (35 words per minute)
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.
"""
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.
"""
pass
def run_morse_engine(apds, pixels, touch):
"""A Tests Demo on Trinkey"""
down = None # Time when proximity sensor was first triggered
elapsed = 0 # Duration proximity sensor has been triggered
message = "" # Translated part of message
letter = "" # Dots and dashes of current (unfinished) letter
# Main loop
while True:
if touch.value:
print("Touched!")
print(message, letter, "<==")
import time
from .pulse import pulse
THRESHOLD = 75
RED = (150, 10, 10)
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."""
pass # Delete this line when you start coding!
def run_prox_pulse(apds, pixels):
"""C Tests Demo on Trinkey"""
# Main loop
while True:
print(apds.proximity)
import time
THRESHOLD = 75
BLUE = (0, 0, 255)
TIME = 1
def pulse(px, color, duration):
"""Fills the pixels with the given color for a duration, then turns them off
for that duration."""
pass # Delete this line when you start coding!
def run_simple_pulse(pixels):
"""D Tests Demo on Trinkey."""
# Main loop
while True:
pulse(pixels, BLUE, TIME)
MORSE_SYMBOL_TO_LETTER = {
".-": "A",
"-...": "B",
"-.-.": "C",
"-..": "D",
".": "E",
"..-.": "F",
"--.": "G",
"....": "H",
"..": "I",
".---": "J",
"-.-": "K",
".-..": "L",
"--": "M",
"-.": "N",
"---": "O",
".--.": "P",
"--.-": "Q",
".-.": "R",
"...": "S",
"-": "T",
"..-": "U",
"...-": "V",
".--": "W",
"-..-": "X",
"-.--": "Y",
"--..": "Z",
".----": "1",
"..---": "2",
"...--": "3",
"....-": "4",
".....": "5",
"-....": "6",
"--...": "7",
"---..": "8",
"----.": "9",
"-----": "0",
"----": " ",
"......": "del",
}
class InvalidSymbolError(Exception):
pass
import pytest
import src.pulse
from tests.helpers.naming import apply_names
from tests.helpers.pixel import Pixel, fake_sleep
src.pulse.time.sleep = fake_sleep
@pytest.mark.parametrize(
"set_color, time",
apply_names('pulse', [True, True], [
((255, 0, 0), 0.05),
((0, 255, 0), 0.04),
((0, 0, 255), 0.03),
((132, 144, 72), 0.1),
])
)
def test_pulse(set_color, time):
pixel = Pixel(time)
src.pulse.pulse(pixel, set_color, time)
color, hist = pixel.look()
assert hist == [
((0, 0, 0), 0),
(set_color, 1),
((0, 0, 0), 2),
], "the pulse function should change color, wait, change color again, and wait again"
import pytest
from src.morse_code import translate_complete_morse_symbol
from src.symbols import InvalidSymbolError
from tests.helpers.translate_symbol_data import SINGLE_SYMBOL_TESTS
from tests.helpers.naming import apply_names
@pytest.mark.parametrize(
"seq, msg, expected",
apply_names(
"translate_complete_morse_symbol", [True, True, False], SINGLE_SYMBOL_TESTS
),
)
def test_add_random(seq, msg, expected):
if expected is None:
with pytest.raises(InvalidSymbolError):
result = translate_complete_morse_symbol(msg, seq)
else:
result = translate_complete_morse_symbol(msg, seq)
assert (
result == expected
), "a valid morse code symbol wasn't translated correctly"
@pytest.mark.parametrize(
"msg, expected",
apply_names(
"translate_complete_morse_symbol",
[True, False],
[
("JUMPOUTTHEHOUSE", "JUMPOUTTHEHOUS"),
("VAMP ANTHEM ", "VAMP ANTHEM"),
("SKY", "SK"),
("", ""),
("H", ""),
(" ", ""),
("M3TAMORPHOSIS", "M3TAMORPHOSI"),
],
),
)
def test_delete(msg, expected):
result = translate_complete_morse_symbol(msg, "......")
assert result == expected
import pytest
import os
from src.morse_code import translate_message
from tests.helpers.translate_message_data import WHOLE_MESSAGE_TESTS
from tests.helpers.naming import apply_names
@pytest.mark.parametrize(
"msg, expected",
apply_names("translate_message", [True, False], WHOLE_MESSAGE_TESTS),
)
def test_decode_message(msg, expected):
result = translate_message(msg)
assert result == expected
import pytest
import math
import src.prox_pulse
from tests.helpers.naming import apply_names
from tests.helpers.pixel import Pixel
from tests.helpers.prox_pulse_data import PROX_PULSE_DATA
from tests.helpers.pixel import Pixel, fake_sleep
src.prox_pulse.time.sleep = fake_sleep
@pytest.mark.parametrize("prox_val, duration, set_color", apply_names(
"prox_pulse", [False, True, True], PROX_PULSE_DATA))
def test_prox_pulse(prox_val, set_color, duration):
pixel = Pixel(duration)
src.prox_pulse.prox_pulse(pixel, set_color, prox_val)
color, hist = pixel.look()
assert hist == [
((0, 0, 0), 0),
(set_color, 1),
((0, 0, 0), 2),
], "the prox_pulse function should have pulsed the provided color"
import pytest
from src.morse_engine import add_mark
from tests.helpers.add_mark_data import ADD_MARK_DATA
from tests.helpers.naming import apply_names
@pytest.mark.parametrize("interval, expected", apply_names('add_mark', [True, False], ADD_MARK_DATA))
def test_add_mark(interval, expected):
mark = add_mark(interval)
assert mark == expected, "add_mark should have added the mark '" + \
str(expected) + "', but you added '" + mark + "'"
import pytest
from src.morse_engine import set_color
from tests.helpers.naming import apply_names
from tests.helpers.pixel import Pixel
from tests.helpers.set_color_data import SET_COLOR_DATA
@pytest.mark.parametrize("interval, expected", apply_names('set_color', [True, False], SET_COLOR_DATA))
def test_set_color(interval, expected):
pixel = Pixel(-1)
set_color(interval, pixel)
color, hist = pixel.look()
assert color == expected, "set_color should have changed the color to " + \
str(expected) + ", but you changed it to " + str(color)
import sys
sys.path.append(".")
ADD_MARK_DATA = [
(0.000000000000000000e+00, 0.000000000000000000e+00),
(5.999999999999999778e-02, 0.000000000000000000e+00),
(1.199999999999999956e-01, 0.000000000000000000e+00),
(1.799999999999999933e-01, 0.000000000000000000e+00),
(2.399999999999999911e-01, 1.000000000000000000e+00),
(2.999999999999999889e-01, 1.000000000000000000e+00),
(3.599999999999999867e-01, 1.000000000000000000e+00),
(4.199999999999999845e-01, 1.000000000000000000e+00),
(4.799999999999999822e-01, 1.000000000000000000e+00),
(5.400000000000000355e-01, 1.000000000000000000e+00),
(5.999999999999999778e-01, 1.000000000000000000e+00),
(6.599999999999999201e-01, 1.000000000000000000e+00),
(7.199999999999999734e-01, 2.000000000000000000e+00),
(7.800000000000000266e-01, 2.000000000000000000e+00),
(8.399999999999999689e-01, 2.000000000000000000e+00),
(8.999999999999999112e-01, 2.000000000000000000e+00),
(9.599999999999999645e-01, 2.000000000000000000e+00),
(1.020000000000000018e+00, 2.000000000000000000e+00),
(1.080000000000000071e+00, 2.000000000000000000e+00),
(1.139999999999999902e+00, 2.000000000000000000e+00),
(1.199999999999999956e+00, 2.000000000000000000e+00),
(1.199999999999999956e+00, 2.000000000000000000e+00),
(3.480000000000000426e+00, 2.000000000000000000e+00),
(5.760000000000000675e+00, 2.000000000000000000e+00),
(8.040000000000000924e+00, 2.000000000000000000e+00),
(1.032000000000000028e+01, 2.000000000000000000e+00),
(1.260000000000000142e+01, 2.000000000000000000e+00),
(1.488000000000000078e+01, 2.000000000000000000e+00),
(1.716000000000000014e+01, 2.000000000000000000e+00),
(1.944000000000000128e+01, 2.000000000000000000e+00),
(2.172000000000000242e+01, 2.000000000000000000e+00),
(2.400000000000000000e+01, 2.000000000000000000e+00),
]
mark_cases = []
for i in range(len(ADD_MARK_DATA)):
m = ADD_MARK_DATA[i]
t = m[0]
mark_num = m[1]
if mark_num == 0.0:
mark = ""
elif mark_num == 1.0:
mark = "."
else:
mark = "-"
ADD_MARK_DATA[i] = (t, mark)
import pytest
def trans(x):
return str(x)
def apply_names(func, args, tests, noparens=False):
cases = []
for case in tests:
extra = ", ".join(
args[i] + trans(x)
for i, x in enumerate(case)
if (args[i] is not True and args[i] is not False)
)
cases.append(
pytest.param(
*case,
id=func
+ ("(" if not noparens else "")
+ ", ".join(trans(x) for i, x in enumerate(case) if args[i] is True)
+ (")" if not noparens else noparens)
+ ((", " + extra) if extra else "")
)
)
return cases
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