-
Notifications
You must be signed in to change notification settings - Fork 3
Annotations fix #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Annotations fix #13
Changes from 5 commits
153d0cf
3a27672
0e1a961
6394511
0b4a257
d5fefb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
""" | ||
|
||
import time | ||
import microcontroller | ||
from pulseio import PulseIn | ||
from micropython import const | ||
|
||
|
@@ -43,7 +44,12 @@ class ScaleReading: | |
class DYMOScale: | ||
"""Interface to a DYMO postal scale.""" | ||
|
||
def __init__(self, data_pin, units_pin, timeout=1.0): | ||
def __init__( | ||
self, | ||
data_pin: microcontroller.pin, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little surprised this didn't raise a CI error! |
||
units_pin: microcontroller.pin, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's looks like |
||
timeout: float = 1.0, | ||
) -> None: | ||
"""Sets up a DYMO postal scale. | ||
:param ~pulseio.PulseIn data_pin: The data pin from the Dymo scale. | ||
:param ~digitalio.DigitalInOut units_pin: The grams/oz button from the Dymo scale. | ||
|
@@ -56,15 +62,15 @@ def __init__(self, data_pin, units_pin, timeout=1.0): | |
self.dymo = PulseIn(data_pin, maxlen=96, idle_state=True) | ||
|
||
@property | ||
def weight(self): | ||
def weight(self) -> ScaleReading: | ||
"""Weight in grams""" | ||
reading = self.get_scale_data() | ||
if reading.units == OUNCES: | ||
reading.weight *= 28.35 | ||
reading.units = GRAMS | ||
return reading | ||
|
||
def toggle_unit_button(self, switch_units=False): | ||
def toggle_unit_button(self, switch_units: bool = False) -> None: | ||
"""Toggles the unit button on the dymo. | ||
:param bool switch_units: Simulates pressing the units button. | ||
""" | ||
|
@@ -78,7 +84,7 @@ def toggle_unit_button(self, switch_units=False): | |
time.sleep(2) | ||
toggle_times += 1 | ||
|
||
def _read_pulse(self): | ||
def _read_pulse(self) -> None: | ||
"""Reads a pulse of SPI data on a pin that corresponds to DYMO scale | ||
output protocol (12 bytes of data at about 14KHz). | ||
""" | ||
|
@@ -93,7 +99,7 @@ def _read_pulse(self): | |
) | ||
self.dymo.pause() | ||
|
||
def get_scale_data(self): | ||
def get_scale_data(self) -> ScaleReading: | ||
"""Reads a pulse of SPI data and analyzes the resulting data.""" | ||
self._read_pulse() | ||
bits = [0] * 96 # there are 12 bytes = 96 bits of data | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CircuitPython interpreter doesn't actually store type annotations like other interpreters do, in an effort to save RAM on the microcontroller. Therefore, it's also best if we can save RAM by not importing libraries used purely for type annotations. Because it's okay to have things used only type annotations be undefined at runtime, we can use a
try
/except
block guarded by something that's only available on a computer, like thetyping
library. This way, the microcontroller won't end up importing anything in that block, whereas the computer will be able to successfully import everything! If thetyping
library isn't used either we can disable a pylint error that would occur for adding it.In this case, all the imports should look like this:
Let me know if you have any questions!