Skip to content

Commit e53f803

Browse files
committed
Add getDirection() support to ModulinoKnob and example ⚡
1 parent 59aef64 commit e53f803

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

examples/Modulino_Knob/Knob_Basic/Knob_Basic.ino

+11-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@ 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);
3032

31-
if(click){
33+
if (click) {
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

+16-1
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,27 @@ class ModulinoKnob : public Module {
274274
}
275275
return ret;
276276
}
277-
int16_t get() {
277+
bool update() {
278278
uint8_t buf[3];
279279
auto res = read(buf, 3);
280280
if (res == false) {
281281
return 0;
282282
}
283+
get(buf);
284+
return 1;
285+
}
286+
int16_t get(uint8_t * buf = nullptr) {
287+
if (buf == nullptr) {
288+
buf = (uint8_t*)malloc(3);
289+
if (buf == nullptr) {
290+
return 0;
291+
}
292+
auto res = read(buf, 3);
293+
if (res == false) {
294+
_pressed = false;
295+
return 0;
296+
}
297+
}
283298
_pressed = (buf[2] != 0);
284299
int16_t ret = buf[0] | (buf[1] << 8);
285300
return ret;

0 commit comments

Comments
 (0)