Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs1-24fa
project03
Commits
a9cbb517
Commit
a9cbb517
authored
6 months ago
by
Antonio Caceres
Browse files
Options
Download
Email Patches
Plain Diff
Add abstract classes and Final to type hinting.
parent
9a638e7a
master
No related merge requests found
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
src/abstract.py
+27
-0
src/abstract.py
src/interact.py
+2
-1
src/interact.py
src/morse_code.py
+2
-2
src/morse_code.py
src/morse_engine.py
+11
-8
src/morse_engine.py
src/prox_pulse.py
+3
-2
src/prox_pulse.py
src/pulse.py
+8
-5
src/pulse.py
src/symbols.py
+3
-1
src/symbols.py
with
56 additions
and
19 deletions
+56
-19
src/abstract.py
0 → 100644
View file @
a9cbb517
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
:
...
This diff is collapsed.
Click to expand it.
src/interact.py
View file @
a9cbb517
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.
...
...
This diff is collapsed.
Click to expand it.
src/morse_code.py
View file @
a9cbb517
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.
...
...
This diff is collapsed.
Click to expand it.
src/morse_engine.py
View file @
a9cbb517
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.
...
...
This diff is collapsed.
Click to expand it.
src/prox_pulse.py
View file @
a9cbb517
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.
...
...
This diff is collapsed.
Click to expand it.
src/pulse.py
View file @
a9cbb517
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.
...
...
This diff is collapsed.
Click to expand it.
src/symbols.py
View file @
a9cbb517
MORSE_SYMBOL_TO_LETTER
=
{
from
typing
import
Final
MORSE_SYMBOL_TO_LETTER
:
Final
[
dict
[
str
,
str
]]
=
{
".-"
:
"A"
,
"-..."
:
"B"
,
"-.-."
:
"C"
,
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help