Skip to content

Commit 55be58a

Browse files
committed
Add steps parameter to knob callback
1 parent 59babea commit 55be58a

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

examples/knob.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
knob.on_press = lambda: print("🔘 Pressed!")
2525
knob.on_release = lambda: knob.reset()
26-
knob.on_rotate_clockwise = lambda value: print(f"🎛️ Rotated clockwise! Value: {value}")
27-
knob.on_rotate_counter_clockwise = lambda value: print(f"🎛️ Rotated counter clockwise! Value: {value}")
26+
knob.on_rotate_clockwise = lambda steps, value: print(f"🎛️ Rotated {steps} steps clockwise! Value: {value}")
27+
knob.on_rotate_counter_clockwise = lambda steps, value: print(f"🎛️ Rotated {steps} steps counter clockwise! Value: {value}")
2828

2929
while True:
3030
if(knob.update()):

examples/knob_buzzer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
knob.range = (0, len(notes) - 1)
2121
knob.on_press = lambda : buzzer.no_tone()
2222

23-
def on_knob_rotate_clockwise(value):
23+
def on_knob_rotate_clockwise(_, value):
2424
frequency = notes[value]
2525
print(f"🎵 Frequency: {frequency} Hz")
2626
buzzer.tone(frequency)
2727

28-
def on_knob_rotate_counter_clockwise(value):
28+
def on_knob_rotate_counter_clockwise(_, value):
2929
frequency = notes[value]
3030
print(f"🎵 Frequency: {frequency} Hz")
3131
buzzer.tone(frequency)

src/modulino/knob.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ def _has_rotated_counter_clockwise(self, previous_value, current_value):
4646
# Counter-clockwise rotation is indicated by a positive difference less than half the range
4747
return 0 < diff < 32768
4848

49+
def _get_steps(self, previous_value, current_value):
50+
# Calculate difference considering wraparound
51+
diff = (current_value - previous_value + 65536) % 65536
52+
# Clockwise rotation is indicated by a positive difference less than half the range
53+
if 0 < diff < 32768:
54+
return diff
55+
# Counter-clockwise rotation is indicated by a negative difference less than half the range
56+
elif 32768 < diff < 65536:
57+
return diff - 65536
58+
else:
59+
return 0
60+
4961
def _read_data(self):
5062
data = self.read(3)
5163
self._pressed = data[2] != 0
@@ -78,11 +90,14 @@ def update(self):
7890
has_rotated_clockwise = self._has_rotated_clockwise(previous_value, self._encoder_value)
7991
has_rotated_counter_clockwise = self._has_rotated_counter_clockwise(previous_value, self._encoder_value)
8092

93+
# Figure out how many steps the encoder has moved since the last update
94+
steps = self._get_steps(previous_value, self._encoder_value)
95+
8196
if(self._on_rotate_clockwise and has_rotated_clockwise):
82-
self._on_rotate_clockwise(self._encoder_value)
97+
self._on_rotate_clockwise(steps, self._encoder_value)
8398

8499
if(self._on_rotate_counter_clockwise and has_rotated_counter_clockwise):
85-
self._on_rotate_counter_clockwise(self._encoder_value)
100+
self._on_rotate_counter_clockwise(steps, self._encoder_value)
86101

87102
if(self._on_press and self._pressed and not previous_pressed_status):
88103
self._on_press()

0 commit comments

Comments
 (0)