Skip to content

Commit 33a618f

Browse files
authored
Merge branch 'main' into interactive-address-changer
2 parents 409e6b3 + 10bda6d commit 33a618f

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
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: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
// Create a ModulinoButtons object
1212
ModulinoButtons buttons;
1313

14-
bool button_a = false;
15-
bool button_b = false;
16-
bool button_c = false;
14+
bool button_a = true;
15+
bool button_b = true;
16+
bool button_c = true;
1717

1818
void setup() {
1919
Serial.begin(9600);
@@ -24,16 +24,25 @@ 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-
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')) {
3235
Serial.println("Button A pressed!");
33-
} else if (buttons.isPressed(1)) {
36+
button_a = !button_a;
37+
} else if (buttons.isPressed("B")) {
3438
Serial.println("Button B pressed!");
35-
} else if (buttons.isPressed(2)) {
39+
button_b = !button_b;
40+
} else if (buttons.isPressed('C')) {
3641
Serial.println("Button C pressed!");
42+
button_c = !button_c;
3743
}
44+
45+
// Update the LEDs above buttons, depending on the variables value
46+
buttons.setLeds(button_a, button_b, button_c);
3847
}
3948
}

examples/Modulino_Knob/Knob_Basic/Knob_Basic.ino

Lines changed: 10 additions & 1 deletion
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,11 @@ void loop(){
3234
Serial.println("Clicked!");
3335
}
3436

35-
}
37+
if (direction == 1) {
38+
Serial.println("Rotated clockwise");
39+
} else if (direction == -1) {
40+
Serial.println("Rotated counter-clockwise");
41+
}
42+
43+
delay(10); // optional small delay to reduce serial spam
44+
}

src/Modulino.h

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
#ifndef ARDUINO_LIBRARIES_MODULINO_H
55
#define ARDUINO_LIBRARIES_MODULINO_H
66

7+
#if defined(ESP32) && defined(BOARD_HAS_PIN_REMAP) && defined(tone)
8+
#error "The current configuration is unsupported, switch Pin Numbering to "By GPIO number" or #undef tone and #undef noTone in the beginning of your sketch."
9+
#error "Learn more at: https://support.arduino.cc/hc/en-us/articles/10483225565980-Select-pin-numbering-for-Nano-ESP32-in-Arduino-IDE"
10+
#endif
11+
712
#include "Wire.h"
813
#include <vl53l4cd_class.h> // from stm32duino
914
#include <vl53l4ed_class.h> // from stm32duino
@@ -127,6 +132,17 @@ class ModulinoButtons : public Module {
127132
PinStatus isPressed(int index) {
128133
return last_status[index] ? HIGH : LOW;
129134
}
135+
PinStatus isPressed(char button) {
136+
int index = buttonToIndex(button);
137+
if (index < 0) return LOW;
138+
return isPressed(index);
139+
}
140+
PinStatus isPressed(const char *button) {
141+
if (button == nullptr || button[0] == '\0' || button[1] != '\0') {
142+
return LOW;
143+
}
144+
return isPressed(button[0]);
145+
}
130146
bool update() {
131147
uint8_t buf[3];
132148
auto res = read((uint8_t*)buf, 3);
@@ -154,6 +170,14 @@ class ModulinoButtons : public Module {
154170
}
155171
private:
156172
bool last_status[3];
173+
int buttonToIndex(char button) {
174+
switch (toupper(button)) {
175+
case 'A': return 0;
176+
case 'B': return 1;
177+
case 'C': return 2;
178+
default: return -1;
179+
}
180+
}
157181
protected:
158182
uint8_t match[1] = { 0x7C }; // same as fw main.c
159183
};
@@ -243,8 +267,9 @@ class ModulinoKnob : public Module {
243267
bool begin() {
244268
auto ret = Module::begin();
245269
if (ret) {
246-
// check for set() bug
247270
auto _val = get();
271+
_lastPosition = _val;
272+
_lastDebounceTime = millis();
248273
set(100);
249274
if (get() != 100) {
250275
_bug_on_set = true;
@@ -277,6 +302,24 @@ class ModulinoKnob : public Module {
277302
get();
278303
return _pressed;
279304
}
305+
int8_t getDirection() {
306+
unsigned long now = millis();
307+
if (now - _lastDebounceTime < DEBOUNCE_DELAY) {
308+
return 0;
309+
}
310+
int16_t current = get();
311+
int8_t direction = 0;
312+
if (current > _lastPosition) {
313+
direction = 1;
314+
} else if (current < _lastPosition) {
315+
direction = -1;
316+
}
317+
if (direction != 0) {
318+
_lastDebounceTime = now;
319+
_lastPosition = current;
320+
}
321+
return direction;
322+
}
280323
virtual uint8_t discover() {
281324
for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
282325
if (scan(match[i])) {
@@ -288,6 +331,9 @@ class ModulinoKnob : public Module {
288331
private:
289332
bool _pressed = false;
290333
bool _bug_on_set = false;
334+
int16_t _lastPosition = 0;
335+
unsigned long _lastDebounceTime = 0;
336+
static constexpr unsigned long DEBOUNCE_DELAY = 30;
291337
protected:
292338
uint8_t match[2] = { 0x74, 0x76 };
293339
};

0 commit comments

Comments
 (0)