Skip to content

Commit 889dd08

Browse files
committed
Add additional documentation
1 parent 14f0ebe commit 889dd08

File tree

9 files changed

+212
-61
lines changed

9 files changed

+212
-61
lines changed

src/modulino/buttons.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
from micropython import const
44

55
class ModulinoButtons(Modulino):
6+
"""
7+
Class to interact with the buttons of the Modulino Buttons.
8+
"""
69

710
default_addresses = [0x7C]
8-
default_long_press_duration = const(1000) # 1 second
11+
_default_long_press_duration = const(1000) # 1 second
912

1013
def __init__(self, i2c_bus=None, address=None):
1114
super().__init__(i2c_bus, address, "BUTTONS")
12-
self.long_press_duration = self.default_long_press_duration
15+
self.long_press_duration = self._default_long_press_duration
1316

1417
self._current_buttons_status = [None, None, None]
1518
self._last_press_timestamps = [None, None, None]
@@ -25,7 +28,7 @@ def __init__(self, i2c_bus=None, address=None):
2528
self._on_button_b_long_press = None
2629
self._on_button_c_long_press = None
2730

28-
def set_led_status(self, a, b, c):
31+
def set_led_status(self, a: bool, b: bool, c: bool):
2932
"""
3033
Turn on or off the button LEDs according to the given status.
3134
@@ -41,23 +44,23 @@ def set_led_status(self, a, b, c):
4144
self.write(data)
4245

4346
@property
44-
def long_press_duration(self):
45-
"""
47+
def long_press_duration(self) -> int:
48+
"""
4649
Returns the duration in milliseconds that the button must
4750
be pressed to trigger the long press event
4851
"""
4952
return self._long_press_duration
5053

5154
@long_press_duration.setter
52-
def long_press_duration(self, value):
55+
def long_press_duration(self, value : int):
5356
"""
5457
Sets the duration in milliseconds that the button must
5558
be pressed to trigger the long press event
5659
"""
5760
self._long_press_duration = value
5861

5962
@property
60-
def on_button_a_press(self):
63+
def on_button_a_press(self) -> function:
6164
"""
6265
Returns the callback for the press event of button A.
6366
"""
@@ -71,7 +74,7 @@ def on_button_a_press(self, value):
7174
self._on_button_a_press = value
7275

7376
@property
74-
def on_button_a_release(self):
77+
def on_button_a_release(self) -> function:
7578
"""
7679
Returns the callback for the release event of button A.
7780
"""
@@ -85,7 +88,7 @@ def on_button_a_release(self, value):
8588
self._on_button_a_release = value
8689

8790
@property
88-
def on_button_a_long_press(self):
91+
def on_button_a_long_press(self) -> function:
8992
"""
9093
Returns the callback for the long press event of button A.
9194
"""
@@ -99,7 +102,7 @@ def on_button_a_long_press(self, value):
99102
self._on_button_a_long_press = value
100103

101104
@property
102-
def on_button_b_press(self):
105+
def on_button_b_press(self) -> function:
103106
"""
104107
Returns the callback for the press event of button B.
105108
"""
@@ -113,7 +116,7 @@ def on_button_b_press(self, value):
113116
self._on_button_b_press = value
114117

115118
@property
116-
def on_button_b_release(self):
119+
def on_button_b_release(self) -> function:
117120
"""
118121
Returns the callback for the release event of button B.
119122
"""
@@ -127,7 +130,7 @@ def on_button_b_release(self, value):
127130
self._on_button_b_release = value
128131

129132
@property
130-
def on_button_b_long_press(self):
133+
def on_button_b_long_press(self) -> function:
131134
"""
132135
Returns the callback for the long press event of button B.
133136
"""
@@ -141,7 +144,7 @@ def on_button_b_long_press(self, value):
141144
self._on_button_b_long_press = value
142145

143146
@property
144-
def on_button_c_press(self):
147+
def on_button_c_press(self) -> function:
145148
"""
146149
Returns the callback for the press event of button C.
147150
"""
@@ -155,7 +158,7 @@ def on_button_c_press(self, value):
155158
self._on_button_c_press = value
156159

157160
@property
158-
def on_button_c_release(self):
161+
def on_button_c_release(self) -> function:
159162
"""
160163
Returns the callback for the release event of button C.
161164
"""
@@ -169,7 +172,7 @@ def on_button_c_release(self, value):
169172
self._on_button_c_release = value
170173

171174
@property
172-
def on_button_c_long_press(self):
175+
def on_button_c_long_press(self) -> function:
173176
"""
174177
Returns the callback for the long press event of button C.
175178
"""
@@ -182,7 +185,7 @@ def on_button_c_long_press(self, value):
182185
"""
183186
self._on_button_c_long_press = value
184187

185-
def update(self):
188+
def update(self) -> bool:
186189
"""
187190
Update the button status and call the corresponding callbacks.
188191
Returns True if any of the buttons has changed its state.
@@ -240,7 +243,7 @@ def update(self):
240243

241244
return button_states_changed
242245

243-
def is_pressed(self, index):
246+
def is_pressed(self, index) -> bool:
244247
"""
245248
Returns True if the button at the given index is currently pressed.
246249
@@ -257,14 +260,14 @@ def button_a_pressed(self):
257260
return self.is_pressed(0)
258261

259262
@property
260-
def button_b_pressed(self):
263+
def button_b_pressed(self) -> bool:
261264
"""
262265
Returns True if button B is currently pressed.
263266
"""
264267
return self.is_pressed(1)
265268

266269
@property
267-
def button_c_pressed(self):
270+
def button_c_pressed(self) -> bool:
268271
"""
269272
Returns True if button C is currently pressed.
270273
"""

src/modulino/buzzer.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,10 @@
33

44
class ModulinoBuzzer(Modulino):
55
"""
6-
Class to play tones on the buzzer of the Modulino.
7-
The supported notes are defined as follows:
8-
- B0
9-
- C1, CS1, D1, DS1, E1, F1, FS1, G1, GS1, A1, AS1, B1
10-
- C2, CS2, D2, DS2, E2, F2, FS2, G2, GS2, A2, AS2, B2
11-
- C3, CS3, D3, DS3, E3, F3, FS3, G3, GS3, A3, AS3, B3
12-
- C4, CS4, D4, DS4, E4, F4, FS4, G4, GS4, A4, AS4, B4
13-
- C5, CS5, D5, DS5, E5, F5, FS5, G5, GS5, A5, AS5, B5
14-
- C6, CS6, D6, DS6, E6, F6, FS6, G6, GS6, A6, AS6, B6
15-
- C7, CS7, D7, DS7, E7, F7, FS7, G7, GS7, A7, AS7, B7
16-
- C8, CS8, D8, DS8
17-
- REST (Silence)
18-
19-
Those notes are accessible through ModulinoBuzzer.NOTES e.g. ModulinoBuzzer.NOTES["C4"]
6+
Class to play tones on the piezo element of the Modulino Buzzer.
7+
Predefined notes are available in the NOTES dictionary e.g. ModulinoBuzzer.NOTES["C4"]
208
"""
9+
2110
NOTES = {
2211
"B0": 31,
2312
"C1": 33,
@@ -110,6 +99,20 @@ class ModulinoBuzzer(Modulino):
11099
"DS8": 4978,
111100
"REST": 0
112101
}
102+
"""
103+
Dictionary with the notes and their corresponding frequencies.
104+
The supported notes are defined as follows:
105+
- B0
106+
- C1, CS1, D1, DS1, E1, F1, FS1, G1, GS1, A1, AS1, B1
107+
- C2, CS2, D2, DS2, E2, F2, FS2, G2, GS2, A2, AS2, B2
108+
- C3, CS3, D3, DS3, E3, F3, FS3, G3, GS3, A3, AS3, B3
109+
- C4, CS4, D4, DS4, E4, F4, FS4, G4, GS4, A4, AS4, B4
110+
- C5, CS5, D5, DS5, E5, F5, FS5, G5, GS5, A5, AS5, B5
111+
- C6, CS6, D6, DS6, E6, F6, FS6, G6, GS6, A6, AS6, B6
112+
- C7, CS7, D7, DS7, E7, F7, FS7, G7, GS7, A7, AS7, B7
113+
- C8, CS8, D8, DS8
114+
- REST (Silence)
115+
"""
113116

114117
default_addresses = [0x3C]
115118

src/modulino/distance.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
from .lib.vl53l4cd import VL53L4CD
33

44
class ModulinoDistance(Modulino):
5+
"""
6+
Class to interact with the distance sensor of the Modulino Distance.
7+
"""
8+
59
default_addresses = [0x29]
610
convert_default_addresses = False
711

@@ -14,13 +18,23 @@ def __init__(self, i2c_bus = None, address: int | None = None) -> None:
1418

1519
@property
1620
def _distance_raw(self):
21+
"""
22+
Reads the raw distance value from the sensor and clears the interrupt.
23+
24+
Returns:
25+
int: The distance in centimeters.
26+
"""
1727
while not self.sensor.data_ready:
1828
pass
1929
self.sensor.clear_interrupt()
2030
return self.sensor.distance
2131

2232
@property
2333
def distance(self):
34+
"""
35+
Returns:
36+
int: The distance in centimeters.
37+
"""
2438
while True:
2539
raw_distance = self._distance_raw
2640
# Filter out invalid readings

src/modulino/knob.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from .modulino import Modulino
22

33
class ModulinoKnob(Modulino):
4+
"""
5+
Class to interact with the rotary encoder of the Modulinio Knob.
6+
"""
47

58
# This module can have one of two default addresses
69
# This is for a use case where two encoders are bundled together in a package
@@ -36,19 +39,42 @@ def __init__(self, i2c_bus=None, address=None):
3639
self._encoder_value = None
3740
self._pressed_status = None
3841

39-
def _has_rotated_clockwise(self, previous_value, current_value):
42+
def _has_rotated_clockwise(self, previous_value, current_value) -> bool:
43+
"""
44+
Determines if the encoder has rotated clockwise.
45+
46+
Parameters:
47+
previous_value (int): The previous value of the encoder.
48+
current_value (int): The current value of the encoder.
49+
50+
Returns:
51+
bool: True if the encoder has rotated clockwise.
52+
"""
4053
# Calculate difference considering wraparound
4154
diff = (current_value- previous_value + 65536) % 65536
4255
# Clockwise rotation is indicated by a positive difference less than half the range
4356
return 0 < diff < 32768
4457

45-
def _has_rotated_counter_clockwise(self, previous_value, current_value):
58+
def _has_rotated_counter_clockwise(self, previous_value, current_value) -> bool:
59+
"""
60+
Determines if the encoder has rotated counter clockwise.
61+
62+
Parameters:
63+
previous_value (int): The previous value of the encoder.
64+
current_value (int): The current value of the encoder.
65+
66+
Returns:
67+
bool: True if the encoder has rotated counter clockwise.
68+
"""
4669
# Calculate difference considering wraparound
4770
diff = (previous_value - current_value+ 65536) % 65536
4871
# Counter-clockwise rotation is indicated by a positive difference less than half the range
4972
return 0 < diff < 32768
5073

51-
def _get_steps(self, previous_value, current_value):
74+
def _get_steps(self, previous_value, current_value) -> int:
75+
"""
76+
Calculates the number of steps the encoder has moved since the last update.
77+
"""
5278
# Calculate difference considering wraparound
5379
diff = (current_value - previous_value + 65536) % 65536
5480
# Clockwise rotation is indicated by a positive difference less than half the range
@@ -61,6 +87,11 @@ def _get_steps(self, previous_value, current_value):
6187
return 0
6288

6389
def _read_data(self):
90+
"""
91+
Reads the encoder value and pressed status from the Modulino.
92+
Adjusts the value to the range if it is set.
93+
Converts the encoder value to a signed 16-bit integer.
94+
"""
6495
data = self.read(3)
6596
self._pressed = data[2] != 0
6697
self._encoder_value = int.from_bytes(data[0:2], 'little', True)
@@ -82,10 +113,13 @@ def reset(self):
82113
"""
83114
self.value = 0
84115

85-
def update(self):
116+
def update(self) -> bool:
86117
"""
87118
Reads new data from the Modulino and calls the corresponding callbacks
88119
if the encoder value or pressed status has changed.
120+
121+
Returns:
122+
bool: True if the encoder value or pressed status has changed.
89123
"""
90124
previous_value = self._encoder_value
91125
previous_pressed_status = self._pressed
@@ -117,7 +151,7 @@ def update(self):
117151
return (self._encoder_value != previous_value) or (self._pressed != previous_pressed_status)
118152

119153
@property
120-
def range(self):
154+
def range(self) -> tuple[int, int]:
121155
"""
122156
Returns the range of the encoder value.
123157
"""
@@ -146,7 +180,7 @@ def range(self, value):
146180
self.value = self._value_range[1]
147181

148182
@property
149-
def on_rotate_clockwise(self):
183+
def on_rotate_clockwise(self) -> function:
150184
"""
151185
Returns the callback for the rotate clockwise event.
152186
"""
@@ -163,7 +197,7 @@ def on_rotate_clockwise(self, value):
163197
self._on_rotate_clockwise = value
164198

165199
@property
166-
def on_rotate_counter_clockwise(self):
200+
def on_rotate_counter_clockwise(self) -> function:
167201
"""
168202
Returns the callback for the rotate counter clockwise event.
169203
"""
@@ -180,7 +214,7 @@ def on_rotate_counter_clockwise(self, value):
180214
self._on_rotate_counter_clockwise = value
181215

182216
@property
183-
def on_press(self):
217+
def on_press(self) -> function:
184218
"""
185219
Returns the callback for the press event.
186220
"""
@@ -197,7 +231,7 @@ def on_press(self, value):
197231
self._on_press = value
198232

199233
@property
200-
def on_release(self):
234+
def on_release(self) -> function:
201235
"""
202236
Returns the callback for the release event.
203237
"""
@@ -214,7 +248,7 @@ def on_release(self, value):
214248
self._on_release = value
215249

216250
@property
217-
def value(self):
251+
def value(self) -> int:
218252
"""
219253
Returns the current value of the encoder.
220254
"""
@@ -244,7 +278,7 @@ def value(self, new_value):
244278
self._encoder_value = new_value
245279

246280
@property
247-
def pressed(self):
281+
def pressed(self) -> bool:
248282
"""
249283
Returns the pressed status of the encoder.
250284
"""

0 commit comments

Comments
 (0)