Skip to content

Commit

Permalink
Add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
sebromero committed Oct 30, 2024
1 parent 889dd08 commit 7faa3f9
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 227 deletions.
44 changes: 22 additions & 22 deletions src/modulino/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, i2c_bus=None, address=None):
self._on_button_b_long_press = None
self._on_button_c_long_press = None

def set_led_status(self, a: bool, b: bool, c: bool):
def set_led_status(self, a: bool, b: bool, c: bool) -> None:
"""
Turn on or off the button LEDs according to the given status.
Expand All @@ -52,134 +52,134 @@ def long_press_duration(self) -> int:
return self._long_press_duration

@long_press_duration.setter
def long_press_duration(self, value : int):
def long_press_duration(self, value: int) -> None:
"""
Sets the duration in milliseconds that the button must
be pressed to trigger the long press event
"""
self._long_press_duration = value

@property
def on_button_a_press(self) -> function:
def on_button_a_press(self):
"""
Returns the callback for the press event of button A.
"""
return self._on_button_a_press

@on_button_a_press.setter
def on_button_a_press(self, value):
def on_button_a_press(self, value) -> None:
"""
Sets the callback for the press event of button A.
"""
self._on_button_a_press = value

@property
def on_button_a_release(self) -> function:
def on_button_a_release(self):
"""
Returns the callback for the release event of button A.
"""
return self._on_button_a_release

@on_button_a_release.setter
def on_button_a_release(self, value):
def on_button_a_release(self, value) -> None:
"""
Sets the callback for the release event of button A.
"""
self._on_button_a_release = value

@property
def on_button_a_long_press(self) -> function:
def on_button_a_long_press(self):
"""
Returns the callback for the long press event of button A.
"""
return self._on_button_a_long_press

@on_button_a_long_press.setter
def on_button_a_long_press(self, value):
def on_button_a_long_press(self, value) -> None:
"""
Sets the callback for the long press event of button A.
"""
self._on_button_a_long_press = value

@property
def on_button_b_press(self) -> function:
def on_button_b_press(self):
"""
Returns the callback for the press event of button B.
"""
return self._on_button_b_press

@on_button_b_press.setter
def on_button_b_press(self, value):
def on_button_b_press(self, value) -> None:
"""
Sets the callback for the press event of button B.
"""
self._on_button_b_press = value

@property
def on_button_b_release(self) -> function:
def on_button_b_release(self):
"""
Returns the callback for the release event of button B.
"""
return self._on_button_b_release

@on_button_b_release.setter
def on_button_b_release(self, value):
def on_button_b_release(self, value) -> None:
"""
Sets the callback for the release event of button B.
"""
self._on_button_b_release = value

@property
def on_button_b_long_press(self) -> function:
def on_button_b_long_press(self):
"""
Returns the callback for the long press event of button B.
"""
return self._on_button_b_long_press

@on_button_b_long_press.setter
def on_button_b_long_press(self, value):
def on_button_b_long_press(self, value) -> None:
"""
Sets the callback for the long press event of button B.
"""
self._on_button_b_long_press = value

@property
def on_button_c_press(self) -> function:
def on_button_c_press(self):
"""
Returns the callback for the press event of button C.
"""
return self._on_button_c_press

@on_button_c_press.setter
def on_button_c_press(self, value):
def on_button_c_press(self, value) -> None:
"""
Sets the callback for the press event of button C.
"""
self._on_button_c_press = value

@property
def on_button_c_release(self) -> function:
def on_button_c_release(self):
"""
Returns the callback for the release event of button C.
"""
return self._on_button_c_release

@on_button_c_release.setter
def on_button_c_release(self, value):
def on_button_c_release(self, value) -> None:
"""
Sets the callback for the release event of button C.
"""
self._on_button_c_release = value

@property
def on_button_c_long_press(self) -> function:
def on_button_c_long_press(self):
"""
Returns the callback for the long press event of button C.
"""
return self._on_button_c_long_press

@on_button_c_long_press.setter
def on_button_c_long_press(self, value):
def on_button_c_long_press(self, value) -> None:
"""
Sets the callback for the long press event of button C.
"""
Expand Down Expand Up @@ -243,7 +243,7 @@ def update(self) -> bool:

return button_states_changed

def is_pressed(self, index) -> bool:
def is_pressed(self, index: int) -> bool:
"""
Returns True if the button at the given index is currently pressed.
Expand All @@ -253,7 +253,7 @@ def is_pressed(self, index) -> bool:
return self._current_buttons_status[index]

@property
def button_a_pressed(self):
def button_a_pressed(self) -> bool:
"""
Returns True if button A is currently pressed.
"""
Expand Down
12 changes: 6 additions & 6 deletions src/modulino/buzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ModulinoBuzzer(Modulino):
Predefined notes are available in the NOTES dictionary e.g. ModulinoBuzzer.NOTES["C4"]
"""

NOTES = {
NOTES: dict[str, int] = {
"B0": 31,
"C1": 33,
"CS1": 35,
Expand Down Expand Up @@ -116,12 +116,12 @@ class ModulinoBuzzer(Modulino):

default_addresses = [0x3C]

def __init__(self, i2c_bus = None, address = None):
def __init__(self, i2c_bus=None, address=None):
super().__init__(i2c_bus, address, "BUZZER")
self.data = bytearray(8)
self.no_tone()

def tone(self, frequency, lenght_ms=0xFFFF, blocking=False):
def tone(self, frequency: int, lenght_ms: int = 0xFFFF, blocking: bool = False) -> None:
"""
Plays a tone with the given frequency and duration.
If blocking is set to True, the function will wait until the tone is finished.
Expand All @@ -131,16 +131,16 @@ def tone(self, frequency, lenght_ms=0xFFFF, blocking=False):
lenght_ms: The duration of the tone in milliseconds. If omitted, the tone will play indefinitely
blocking: If set to True, the function will wait until the tone is finished
"""
self.data[0:4]=frequency.to_bytes(4,'little')
self.data[4:8]=lenght_ms.to_bytes(4,'little')
self.data[0:4] = frequency.to_bytes(4, 'little')
self.data[4:8] = lenght_ms.to_bytes(4, 'little')
self.write(self.data)

if blocking:
# Subtract 5ms to avoid unwanted pauses between tones
# Those pauses are caused by the time it takes to send the data to the buzzer
sleep_ms(lenght_ms - 5)

def no_tone(self):
def no_tone(self) -> None:
"""
Stops the current tone from playing.
"""
Expand Down
4 changes: 2 additions & 2 deletions src/modulino/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, i2c_bus = None, address: int | None = None) -> None:
self.sensor.start_ranging()

@property
def _distance_raw(self):
def _distance_raw(self) -> int:
"""
Reads the raw distance value from the sensor and clears the interrupt.
Expand All @@ -30,7 +30,7 @@ def _distance_raw(self):
return self.sensor.distance

@property
def distance(self):
def distance(self) -> int:
"""
Returns:
int: The distance in centimeters.
Expand Down
Loading

0 comments on commit 7faa3f9

Please sign in to comment.