Skip to content

Commit 96572f1

Browse files
committedOct 29, 2024·
Update API docs
1 parent a1aac92 commit 96572f1

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed
 

‎src/modulino/buttons.py

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ def __init__(self, i2c_bus=None, address=None):
2626
self._on_button_c_long_press = None
2727

2828
def set_led_status(self, a, b, c):
29+
"""
30+
Turn on or off the button LEDs according to the given status.
31+
32+
Parameters:
33+
a (bool): The status of the LED A.
34+
b (bool): The status of the LED B.
35+
c (bool): The status of the LED C.
36+
"""
2937
data = bytearray(3)
3038
data[0] = 1 if a else 0
3139
data[1] = 1 if b else 0
@@ -116,6 +124,9 @@ def update(self):
116124
"""
117125
Update the button status and call the corresponding callbacks.
118126
Returns True if any of the buttons has changed its state.
127+
128+
Returns:
129+
bool: True if any of the buttons has changed its state.
119130
"""
120131
new_status = self.read(3)
121132
button_states_changed = new_status != self._current_buttons_status
@@ -168,6 +179,12 @@ def update(self):
168179
return button_states_changed
169180

170181
def is_pressed(self, index):
182+
"""
183+
Returns True if the button at the given index is currently pressed.
184+
185+
Parameters:
186+
index (int): The index of the button. A = 0, B = 1, C = 2.
187+
"""
171188
return self._current_buttons_status[index]
172189

173190
@property

‎src/modulino/buzzer.py

+12
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ def __init__(self, i2c_bus = None, address = None):
103103
self.no_tone()
104104

105105
def tone(self, frequency, lenght_ms=0xFFFF, blocking=False):
106+
"""
107+
Plays a tone with the given frequency and duration.
108+
If blocking is set to True, the function will wait until the tone is finished.
109+
110+
Parameters:
111+
frequency: The frequency of the tone in Hz
112+
lenght_ms: The duration of the tone in milliseconds
113+
blocking: If set to True, the function will wait until the tone is finished
114+
"""
106115
self.data[0:4]=frequency.to_bytes(4,'little')
107116
self.data[4:8]=lenght_ms.to_bytes(4,'little')
108117
self.write(self.data)
@@ -113,5 +122,8 @@ def tone(self, frequency, lenght_ms=0xFFFF, blocking=False):
113122
sleep_ms(lenght_ms - 5)
114123

115124
def no_tone(self):
125+
"""
126+
Stops the current tone from playing.
127+
"""
116128
self.data = bytearray(8)
117129
self.write(self.data)

‎src/modulino/knob.py

+49
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,16 @@ def _read_data(self):
7777
self.value = constrained_value
7878

7979
def reset(self):
80+
"""
81+
Resets the encoder value to 0.
82+
"""
8083
self.value = 0
8184

8285
def update(self):
86+
"""
87+
Reads new data from the Modulino and calls the corresponding callbacks
88+
if the encoder value or pressed status has changed.
89+
"""
8390
previous_value = self._encoder_value
8491
previous_pressed_status = self._pressed
8592

@@ -115,6 +122,12 @@ def range(self):
115122

116123
@range.setter
117124
def range(self, value):
125+
"""
126+
Sets the range of the encoder value.
127+
128+
Parameters:
129+
value (tuple): A tuple with two integers representing the minimum and maximum values of the range.
130+
"""
118131
if(value[0] < -32768 or value[1] > 32767):
119132
raise ValueError("Range must be between -32768 and 32767")
120133

@@ -135,6 +148,12 @@ def on_rotate_clockwise(self):
135148

136149
@on_rotate_clockwise.setter
137150
def on_rotate_clockwise(self, value):
151+
"""
152+
Sets the callback for the rotate clockwise event.
153+
154+
Parameters:
155+
value (function): The function to be called when the encoder is rotated clockwise.
156+
"""
138157
self._on_rotate_clockwise = value
139158

140159
@property
@@ -143,6 +162,12 @@ def on_rotate_counter_clockwise(self):
143162

144163
@on_rotate_counter_clockwise.setter
145164
def on_rotate_counter_clockwise(self, value):
165+
"""
166+
Sets the callback for the rotate counter clockwise event.
167+
168+
Parameters:
169+
value (function): The function to be called when the encoder is rotated counter clockwise.
170+
"""
146171
self._on_rotate_counter_clockwise = value
147172

148173
@property
@@ -151,6 +176,12 @@ def on_press(self):
151176

152177
@on_press.setter
153178
def on_press(self, value):
179+
"""
180+
Sets the callback for the press event.
181+
182+
Parameters:
183+
value (function): The function to be called when the encoder is pressed.
184+
"""
154185
self._on_press = value
155186

156187
@property
@@ -159,14 +190,29 @@ def on_release(self):
159190

160191
@on_release.setter
161192
def on_release(self, value):
193+
"""
194+
Sets the callback for the release event.
195+
196+
Parameters:
197+
value (function): The function to be called when the encoder is released.
198+
"""
162199
self._on_release = value
163200

164201
@property
165202
def value(self):
203+
"""
204+
Returns the current value of the encoder.
205+
"""
166206
return self._encoder_value
167207

168208
@value.setter
169209
def value(self, new_value):
210+
"""
211+
Sets the value of the encoder. This overrides the previous value.
212+
213+
Parameters:
214+
new_value (int): The new value of the encoder.
215+
"""
170216
if(self._value_range != None):
171217
if(new_value < self._value_range[0]) or (new_value > self._value_range[1]):
172218
raise ValueError(f"Value {new_value} is out of range ({self._value_range[0]} to {self._value_range[1]})")
@@ -184,4 +230,7 @@ def value(self, new_value):
184230

185231
@property
186232
def pressed(self):
233+
"""
234+
Returns the pressed status of the encoder.
235+
"""
187236
return self._pressed

‎src/modulino/pixels.py

+70
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,63 @@ def _mapi(self, x, in_min, in_max, out_min, out_max) -> int:
3535
return int(self._map(x, in_min, in_max, out_min, out_max))
3636

3737
def set_range_rgb(self, index_from, index_to, r, g, b, brightness=100):
38+
"""
39+
Sets the color of the LEDs in the given range to the given RGB values.
40+
41+
Parameters:
42+
index_from (int): The starting index of the range.
43+
index_to (int): The ending index (inclusive) of the range.
44+
r (int): The red value of the color.
45+
g (int): The green value of the color.
46+
b (int): The blue value of the color.
47+
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
48+
"""
3849
self.set_range_color(index_from, index_to, ModulinoColor(r, g, b), brightness)
3950

4051
def set_range_color(self, index_from, index_to, color, brightness=100):
52+
"""
53+
Sets the color of the LEDs in the given range to the given color.
54+
55+
Parameters:
56+
index_from (int): The starting index of the range.
57+
index_to (int): The ending index (inclusive) of the range.
58+
color (ModulinoColor): The color of the LEDs.
59+
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
60+
"""
4161
for i in range(index_from, index_to + 1):
4262
self.set_color(i, color, brightness)
4363

4464
def set_all_rgb(self, r, g, b, brightness=100):
65+
"""
66+
Sets the color of all the LEDs to the given RGB values.
67+
68+
Parameters:
69+
r (int): The red value of the color.
70+
g (int): The green value of the color.
71+
b (int): The blue value of the color.
72+
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
73+
"""
4574
self.set_all_color(ModulinoColor(r, g, b), brightness)
4675

4776
def set_all_color(self, color, brightness=100):
77+
"""
78+
Sets the color of all the LEDs to the given color.
79+
80+
Parameters:
81+
color (ModulinoColor): The color of the LEDs.
82+
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
83+
"""
4884
self.set_range_color(0, NUM_LEDS - 1, color, brightness)
4985

5086
def set_color(self, idx, rgb : ModulinoColor , brightness=100):
87+
"""
88+
Sets the color of the given LED index to the given color.
89+
90+
Parameters:
91+
idx (int): The index of the LED.
92+
rgb (ModulinoColor): The color of the LED.
93+
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
94+
"""
5195
if idx < 0 or idx >= NUM_LEDS:
5296
raise ValueError(f"LED index out of range {idx} (Valid: 0..{NUM_LEDS - 1})")
5397

@@ -57,17 +101,43 @@ def set_color(self, idx, rgb : ModulinoColor , brightness=100):
57101
self.data[byte_index: byte_index+4] = color_data_bytes.to_bytes(4, 'little')
58102

59103
def set_rgb(self, idx, r, g, b, brightness=100):
104+
"""
105+
Set the color of the given LED index to the given RGB values.
106+
107+
Parameters:
108+
idx (int): The index of the LED.
109+
r (int): The red value of the color.
110+
g (int): The green value of the color.
111+
b (int): The blue value of the color.
112+
brightness (int): The brightness of the LED. It should be a value between 0 and 100.
113+
"""
60114
self.set_color(idx, ModulinoColor(r, g, b), brightness)
61115

62116
def clear(self, idx):
117+
"""
118+
Turns off the LED at the given index.
119+
120+
Parameters:
121+
idx (int): The index of the LED.
122+
"""
63123
self.set_color(idx, ModulinoColor(0, 0, 0), 0)
64124

65125
def clear_range(self, start, end):
66126
for i in range(start, end):
67127
self.clear(i)
68128

69129
def clear_all(self):
130+
"""
131+
Turns all the LEDs off.
132+
133+
Parameters:
134+
idx (int): The index of the LED
135+
"""
70136
self.data = bytearray([0xE0] * NUM_LEDS * 4)
71137

72138
def show(self):
139+
"""
140+
Applies the changes to the LEDs. This function needs to be called after any changes to the LEDs.
141+
Otherwise, the changes will not be visible.
142+
"""
73143
self.write(self.data)

0 commit comments

Comments
 (0)
Please sign in to comment.