Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3c36732

Browse files
AntonioBernaleonardocavagnis
andauthored
Add getDirection() support to ModulinoKnob and example ⚡ (#34)
* feat(buttons): allow isPressed() to accept 'A', 'B', 'C' as input Added support for character and string-based input to `isPressed()` in `ModulinoButtons`, so it now accepts both index (0–2) and letter identifiers (`'A'`, `'B'`, `'C'`) matching the physical button labeling. Closes #3 * Update example to document support for both numeric and letter indices ♻️ * Add getDirection() support to ModulinoKnob and example ⚡ * Fix: 🔥 * Fix: restore files 🔥 * Minor fixes ♻️ * Update API docs ♻️ * Update API docs ♻️ * Update examples/Modulino_Knob/Knob_Basic/Knob_Basic.ino --------- Co-authored-by: Leonardo Cavagnis <45899760+leonardocavagnis@users.noreply.github.com>
1 parent fd69d03 commit 3c36732

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

‎docs/api.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ Represents a Modulino Knob module.
9090
- **`bool isPressed()`**
9191
Returns `true` if the button on the knob is pressed, `false` otherwise.
9292

93+
- **`int8_t getDirection()`**
94+
Returns the direction of the knob rotation.
95+
- `1` for clockwise
96+
- `-1` for counter-clockwise
97+
- `0` if no movement is detected
98+
9399
- **`void set(int16_t value)`**
94100
Sets the knob value.
95101

‎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: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ class ModulinoKnob : public Module {
243243
bool begin() {
244244
auto ret = Module::begin();
245245
if (ret) {
246-
// check for set() bug
247246
auto _val = get();
247+
_lastPosition = _val;
248+
_lastDebounceTime = millis();
248249
set(100);
249250
if (get() != 100) {
250251
_bug_on_set = true;
@@ -277,6 +278,24 @@ class ModulinoKnob : public Module {
277278
get();
278279
return _pressed;
279280
}
281+
int8_t getDirection() {
282+
unsigned long now = millis();
283+
if (now - _lastDebounceTime < DEBOUNCE_DELAY) {
284+
return 0;
285+
}
286+
int16_t current = get();
287+
int8_t direction = 0;
288+
if (current > _lastPosition) {
289+
direction = 1;
290+
} else if (current < _lastPosition) {
291+
direction = -1;
292+
}
293+
if (direction != 0) {
294+
_lastDebounceTime = now;
295+
_lastPosition = current;
296+
}
297+
return direction;
298+
}
280299
virtual uint8_t discover() {
281300
for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
282301
if (scan(match[i])) {
@@ -288,6 +307,9 @@ class ModulinoKnob : public Module {
288307
private:
289308
bool _pressed = false;
290309
bool _bug_on_set = false;
310+
int16_t _lastPosition = 0;
311+
unsigned long _lastDebounceTime = 0;
312+
static constexpr unsigned long DEBOUNCE_DELAY = 30;
291313
protected:
292314
uint8_t match[2] = { 0x74, 0x76 };
293315
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /