Skip to content

Commit a6edb10

Browse files
authored
Merge branch 'main' into patch-1
2 parents c66624e + 9c948fe commit a6edb10

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

docs/api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ Represents a Modulino Buttons module.
3333
- **`PinStatus isPressed(int index)`**
3434
Returns the press status (HIGH/LOW) of the button at the specified index (_0-A, 1-B, 2-C_).
3535

36+
- **`PinStatus isPressed(char button)`**
37+
Returns the press status (HIGH/LOW) of the button specified by its character ('A', 'B', 'C').
38+
39+
- **`PinStatus isPressed(const char *button)`**
40+
Returns the press status (HIGH/LOW) of the button specified by its string ("A", "B", "C").
41+
3642
- **`bool update()`**
3743
Updates the button status. Returns `true` if the status has changed, `false` otherwise.
3844

@@ -90,6 +96,12 @@ Represents a Modulino Knob module.
9096
- **`bool isPressed()`**
9197
Returns `true` if the button on the knob is pressed, `false` otherwise.
9298

99+
- **`int8_t getDirection()`**
100+
Returns the direction of the knob rotation.
101+
- `1` for clockwise
102+
- `-1` for counter-clockwise
103+
- `0` if no movement is detected
104+
93105
- **`void set(int16_t value)`**
94106
Sets the knob value.
95107

examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,24 @@ void setup() {
2424
// Turn on the LEDs above buttons A, B, and C
2525
buttons.setLeds(true, true, true);
2626
}
27+
2728
void loop() {
2829
// Check for new button events, returns true when button state changes
2930
if (buttons.update()) {
30-
// Check which button was pressed (0=A, 1=B, 2=C)
31-
// Also toggle the corresponding LED, for each of the three buttons
32-
if (buttons.isPressed(0)) {
31+
// You can use either index (0=A, 1=B, 2=C) or letter ('A', 'B', 'C') to check buttons
32+
// Below we use the letter-based method for better readability
33+
34+
if (buttons.isPressed('A')) {
3335
Serial.println("Button A pressed!");
3436
button_a = !button_a;
35-
} else if (buttons.isPressed(1)) {
37+
} else if (buttons.isPressed("B")) {
3638
Serial.println("Button B pressed!");
3739
button_b = !button_b;
38-
} else if (buttons.isPressed(2)) {
40+
} else if (buttons.isPressed('C')) {
3941
Serial.println("Button C pressed!");
4042
button_c = !button_c;
4143
}
42-
44+
4345
// Update the LEDs above buttons, depending on the variables value
4446
buttons.setLeds(button_a, button_b, button_c);
4547
}

examples/Modulino_Knob/Knob_Basic/Knob_Basic.ino

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ void loop(){
2424
int position = knob.get();
2525
// Check if the knob has been pressed (clicked)
2626
bool click = knob.isPressed();
27+
// Get the rotation direction
28+
int8_t direction = knob.getDirection();
2729

2830
Serial.print("Current position is: ");
2931
Serial.println(position);
@@ -32,4 +34,15 @@ void loop(){
3234
Serial.println("Clicked!");
3335
}
3436

37+
<<<<<<< patch-1
3538
}
39+
=======
40+
if (direction == 1) {
41+
Serial.println("Rotated clockwise");
42+
} else if (direction == -1) {
43+
Serial.println("Rotated counter-clockwise");
44+
}
45+
46+
delay(10); // optional small delay to reduce serial spam
47+
}
48+
>>>>>>> main

src/Modulino.h

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ class ModulinoButtons : public Module {
127127
PinStatus isPressed(int index) {
128128
return last_status[index] ? HIGH : LOW;
129129
}
130+
PinStatus isPressed(char button) {
131+
int index = buttonToIndex(button);
132+
if (index < 0) return LOW;
133+
return isPressed(index);
134+
}
135+
PinStatus isPressed(const char *button) {
136+
if (button == nullptr || button[0] == '\0' || button[1] != '\0') {
137+
return LOW;
138+
}
139+
return isPressed(button[0]);
140+
}
130141
bool update() {
131142
uint8_t buf[3];
132143
auto res = read((uint8_t*)buf, 3);
@@ -154,6 +165,14 @@ class ModulinoButtons : public Module {
154165
}
155166
private:
156167
bool last_status[3];
168+
int buttonToIndex(char button) {
169+
switch (toupper(button)) {
170+
case 'A': return 0;
171+
case 'B': return 1;
172+
case 'C': return 2;
173+
default: return -1;
174+
}
175+
}
157176
protected:
158177
uint8_t match[1] = { 0x7C }; // same as fw main.c
159178
};
@@ -243,8 +262,9 @@ class ModulinoKnob : public Module {
243262
bool begin() {
244263
auto ret = Module::begin();
245264
if (ret) {
246-
// check for set() bug
247265
auto _val = get();
266+
_lastPosition = _val;
267+
_lastDebounceTime = millis();
248268
set(100);
249269
if (get() != 100) {
250270
_bug_on_set = true;
@@ -277,6 +297,24 @@ class ModulinoKnob : public Module {
277297
get();
278298
return _pressed;
279299
}
300+
int8_t getDirection() {
301+
unsigned long now = millis();
302+
if (now - _lastDebounceTime < DEBOUNCE_DELAY) {
303+
return 0;
304+
}
305+
int16_t current = get();
306+
int8_t direction = 0;
307+
if (current > _lastPosition) {
308+
direction = 1;
309+
} else if (current < _lastPosition) {
310+
direction = -1;
311+
}
312+
if (direction != 0) {
313+
_lastDebounceTime = now;
314+
_lastPosition = current;
315+
}
316+
return direction;
317+
}
280318
virtual uint8_t discover() {
281319
for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
282320
if (scan(match[i])) {
@@ -288,6 +326,9 @@ class ModulinoKnob : public Module {
288326
private:
289327
bool _pressed = false;
290328
bool _bug_on_set = false;
329+
int16_t _lastPosition = 0;
330+
unsigned long _lastDebounceTime = 0;
331+
static constexpr unsigned long DEBOUNCE_DELAY = 30;
291332
protected:
292333
uint8_t match[2] = { 0x74, 0x76 };
293334
};

0 commit comments

Comments
 (0)