Skip to content

Commit 7faa3f9

Browse files
committed
Add type hints
1 parent 889dd08 commit 7faa3f9

File tree

9 files changed

+228
-227
lines changed

9 files changed

+228
-227
lines changed

src/modulino/buttons.py

+22-22
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, i2c_bus=None, address=None):
2828
self._on_button_b_long_press = None
2929
self._on_button_c_long_press = None
3030

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

5454
@long_press_duration.setter
55-
def long_press_duration(self, value : int):
55+
def long_press_duration(self, value: int) -> None:
5656
"""
5757
Sets the duration in milliseconds that the button must
5858
be pressed to trigger the long press event
5959
"""
6060
self._long_press_duration = value
6161

6262
@property
63-
def on_button_a_press(self) -> function:
63+
def on_button_a_press(self):
6464
"""
6565
Returns the callback for the press event of button A.
6666
"""
6767
return self._on_button_a_press
6868

6969
@on_button_a_press.setter
70-
def on_button_a_press(self, value):
70+
def on_button_a_press(self, value) -> None:
7171
"""
7272
Sets the callback for the press event of button A.
7373
"""
7474
self._on_button_a_press = value
7575

7676
@property
77-
def on_button_a_release(self) -> function:
77+
def on_button_a_release(self):
7878
"""
7979
Returns the callback for the release event of button A.
8080
"""
8181
return self._on_button_a_release
8282

8383
@on_button_a_release.setter
84-
def on_button_a_release(self, value):
84+
def on_button_a_release(self, value) -> None:
8585
"""
8686
Sets the callback for the release event of button A.
8787
"""
8888
self._on_button_a_release = value
8989

9090
@property
91-
def on_button_a_long_press(self) -> function:
91+
def on_button_a_long_press(self):
9292
"""
9393
Returns the callback for the long press event of button A.
9494
"""
9595
return self._on_button_a_long_press
9696

9797
@on_button_a_long_press.setter
98-
def on_button_a_long_press(self, value):
98+
def on_button_a_long_press(self, value) -> None:
9999
"""
100100
Sets the callback for the long press event of button A.
101101
"""
102102
self._on_button_a_long_press = value
103103

104104
@property
105-
def on_button_b_press(self) -> function:
105+
def on_button_b_press(self):
106106
"""
107107
Returns the callback for the press event of button B.
108108
"""
109109
return self._on_button_b_press
110110

111111
@on_button_b_press.setter
112-
def on_button_b_press(self, value):
112+
def on_button_b_press(self, value) -> None:
113113
"""
114114
Sets the callback for the press event of button B.
115115
"""
116116
self._on_button_b_press = value
117117

118118
@property
119-
def on_button_b_release(self) -> function:
119+
def on_button_b_release(self):
120120
"""
121121
Returns the callback for the release event of button B.
122122
"""
123123
return self._on_button_b_release
124124

125125
@on_button_b_release.setter
126-
def on_button_b_release(self, value):
126+
def on_button_b_release(self, value) -> None:
127127
"""
128128
Sets the callback for the release event of button B.
129129
"""
130130
self._on_button_b_release = value
131131

132132
@property
133-
def on_button_b_long_press(self) -> function:
133+
def on_button_b_long_press(self):
134134
"""
135135
Returns the callback for the long press event of button B.
136136
"""
137137
return self._on_button_b_long_press
138138

139139
@on_button_b_long_press.setter
140-
def on_button_b_long_press(self, value):
140+
def on_button_b_long_press(self, value) -> None:
141141
"""
142142
Sets the callback for the long press event of button B.
143143
"""
144144
self._on_button_b_long_press = value
145145

146146
@property
147-
def on_button_c_press(self) -> function:
147+
def on_button_c_press(self):
148148
"""
149149
Returns the callback for the press event of button C.
150150
"""
151151
return self._on_button_c_press
152152

153153
@on_button_c_press.setter
154-
def on_button_c_press(self, value):
154+
def on_button_c_press(self, value) -> None:
155155
"""
156156
Sets the callback for the press event of button C.
157157
"""
158158
self._on_button_c_press = value
159159

160160
@property
161-
def on_button_c_release(self) -> function:
161+
def on_button_c_release(self):
162162
"""
163163
Returns the callback for the release event of button C.
164164
"""
165165
return self._on_button_c_release
166166

167167
@on_button_c_release.setter
168-
def on_button_c_release(self, value):
168+
def on_button_c_release(self, value) -> None:
169169
"""
170170
Sets the callback for the release event of button C.
171171
"""
172172
self._on_button_c_release = value
173173

174174
@property
175-
def on_button_c_long_press(self) -> function:
175+
def on_button_c_long_press(self):
176176
"""
177177
Returns the callback for the long press event of button C.
178178
"""
179179
return self._on_button_c_long_press
180180

181181
@on_button_c_long_press.setter
182-
def on_button_c_long_press(self, value):
182+
def on_button_c_long_press(self, value) -> None:
183183
"""
184184
Sets the callback for the long press event of button C.
185185
"""
@@ -243,7 +243,7 @@ def update(self) -> bool:
243243

244244
return button_states_changed
245245

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

255255
@property
256-
def button_a_pressed(self):
256+
def button_a_pressed(self) -> bool:
257257
"""
258258
Returns True if button A is currently pressed.
259259
"""

src/modulino/buzzer.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ModulinoBuzzer(Modulino):
77
Predefined notes are available in the NOTES dictionary e.g. ModulinoBuzzer.NOTES["C4"]
88
"""
99

10-
NOTES = {
10+
NOTES: dict[str, int] = {
1111
"B0": 31,
1212
"C1": 33,
1313
"CS1": 35,
@@ -116,12 +116,12 @@ class ModulinoBuzzer(Modulino):
116116

117117
default_addresses = [0x3C]
118118

119-
def __init__(self, i2c_bus = None, address = None):
119+
def __init__(self, i2c_bus=None, address=None):
120120
super().__init__(i2c_bus, address, "BUZZER")
121121
self.data = bytearray(8)
122122
self.no_tone()
123123

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

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

143-
def no_tone(self):
143+
def no_tone(self) -> None:
144144
"""
145145
Stops the current tone from playing.
146146
"""

src/modulino/distance.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, i2c_bus = None, address: int | None = None) -> None:
1717
self.sensor.start_ranging()
1818

1919
@property
20-
def _distance_raw(self):
20+
def _distance_raw(self) -> int:
2121
"""
2222
Reads the raw distance value from the sensor and clears the interrupt.
2323
@@ -30,7 +30,7 @@ def _distance_raw(self):
3030
return self.sensor.distance
3131

3232
@property
33-
def distance(self):
33+
def distance(self) -> int:
3434
"""
3535
Returns:
3636
int: The distance in centimeters.

0 commit comments

Comments
 (0)