Skip to content
Merged
28 changes: 28 additions & 0 deletions .gitignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file.
It is already included in the PR #33

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Compiled object files
*.o
*.a
*.so
*.out

# Arduino build folder
build/
*.elf
*.bin
*.hex
*.eep

# Arduino CLI and IDE cache
*.d
*.dep
*.map
*.lst

# MacOS specific
.DS_Store

# Backup files
*~
*.swp

# VS Code
.vscode/
34 changes: 17 additions & 17 deletions examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this file :)
this modifies doesn't add any feature to the PR

Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ bool button_b = true;
bool button_c = true;

void setup() {
Serial.begin(9600);
// Initialize Modulino I2C communication
Modulino.begin();
// Detect and connect to buttons module
buttons.begin();
// Turn on the LEDs above buttons A, B, and C
buttons.setLeds(true, true, true);
Serial.begin(9600);
// Initialize Modulino I2C communication
Modulino.begin();
// Detect and connect to buttons module
buttons.begin();
// Turn on the LEDs above buttons A, B, and C
buttons.setLeds(true, true, true);
}
void loop() {
// Check for new button events, returns true when button state changes
if (buttons.update()) {
// Check for new button events, returns true when button state changes
if (buttons.update()) {
// Check which button was pressed (0=A, 1=B, 2=C)
// Also toggle the corresponding LED, for each of the three buttons
if (buttons.isPressed(0)) {
Serial.println("Button A pressed!");
button_a = !button_a;
Serial.println("Button A pressed!");
button_a = !button_a;
} else if (buttons.isPressed(1)) {
Serial.println("Button B pressed!");
button_b = !button_b;
Serial.println("Button B pressed!");
button_b = !button_b;
} else if (buttons.isPressed(2)) {
Serial.println("Button C pressed!");
button_c = !button_c;
Serial.println("Button C pressed!");
button_c = !button_c;
}

// Update the LEDs above buttons, depending on the variables value
buttons.setLeds(button_a, button_b, button_c);
}
}
}
13 changes: 11 additions & 2 deletions examples/Modulino_Knob/Knob_Basic/Knob_Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ void loop(){
int position = knob.get();
// Check if the knob has been pressed (clicked)
bool click = knob.isPressed();
// Get the rotation direction
int8_t direction = knob.getDirection();

Serial.print("Current position is: ");
Serial.println(position);

if(click){
if (click) {
Serial.println("Clicked!");
}

}
if (direction == 1) {
Serial.println("Rotated clockwise");
} else if (direction == -1) {
Serial.println("Rotated counter-clockwise");
}

delay(10); // optional small delay to reduce serial spam
}
41 changes: 39 additions & 2 deletions src/Modulino.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ class ModulinoKnob : public Module {
bool begin() {
auto ret = Module::begin();
if (ret) {
// check for set() bug
auto _val = get();
_lastPosition = _val;
_lastDebounceTime = millis();
set(100);
if (get() != 100) {
_bug_on_set = true;
Expand All @@ -255,12 +256,27 @@ class ModulinoKnob : public Module {
}
return ret;
}
int16_t get() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove it.
it is already included in the PR #32

bool update() {
uint8_t buf[3];
auto res = read(buf, 3);
if (res == false) {
return 0;
}
get(buf);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here
it is already included in the PR #32

return 1;
}
int16_t get(uint8_t * buf = nullptr) {
if (buf == nullptr) {
buf = (uint8_t*)malloc(3);
if (buf == nullptr) {
return 0;
}
auto res = read(buf, 3);
if (res == false) {
_pressed = false;
return 0;
}
}
_pressed = (buf[2] != 0);
int16_t ret = buf[0] | (buf[1] << 8);
return ret;
Expand All @@ -277,6 +293,24 @@ class ModulinoKnob : public Module {
get();
return _pressed;
}
int8_t getDirection() {
unsigned long now = millis();
if (now - _lastDebounceTime < DEBOUNCE_DELAY) {
return 0;
}
int16_t current = get();
int8_t direction = 0;
if (current > _lastPosition) {
direction = 1;
} else if (current < _lastPosition) {
direction = -1;
}
if (direction != 0) {
_lastDebounceTime = now;
_lastPosition = current;
}
return direction;
}
virtual uint8_t discover() {
for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
if (scan(match[i])) {
Expand All @@ -288,6 +322,9 @@ class ModulinoKnob : public Module {
private:
bool _pressed = false;
bool _bug_on_set = false;
int16_t _lastPosition = 0;
unsigned long _lastDebounceTime = 0;
static constexpr unsigned long DEBOUNCE_DELAY = 30;
protected:
uint8_t match[2] = { 0x74, 0x76 };
};
Expand Down