From 53c079c8c2c804312b5dc4c10c23bea721ff369c Mon Sep 17 00:00:00 2001 From: Antonio Bernardini Date: Wed, 30 Apr 2025 03:41:37 +0200 Subject: [PATCH 1/5] feat(buttons): allow isPressed() to accept 'A', 'B', 'C' as input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .gitignore | 28 ++++++++++++++++++++++++++++ src/Modulino.h | 19 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5577b0d --- /dev/null +++ b/.gitignore @@ -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 (se lo usi) +.vscode/ \ No newline at end of file diff --git a/src/Modulino.h b/src/Modulino.h index 6f62aaf..f768d24 100644 --- a/src/Modulino.h +++ b/src/Modulino.h @@ -127,6 +127,17 @@ class ModulinoButtons : public Module { PinStatus isPressed(int index) { return last_status[index] ? HIGH : LOW; } + PinStatus isPressed(char button) { + int index = buttonToIndex(button); + if (index < 0) return LOW; + return isPressed(index); + } + PinStatus isPressed(const char *button) { + if (button == nullptr || button[0] == '\0' || button[1] != '\0') { + return LOW; + } + return isPressed(button[0]); + } bool update() { uint8_t buf[3]; auto res = read((uint8_t*)buf, 3); @@ -154,6 +165,14 @@ class ModulinoButtons : public Module { } private: bool last_status[3]; + int buttonToIndex(char button) { + switch (toupper(button)) { + case 'A': return 0; + case 'B': return 1; + case 'C': return 2; + default: return -1; + } + } protected: uint8_t match[1] = { 0x7C }; // same as fw main.c }; From 59aef641723f70071cb29d06fe6ffb2409907973 Mon Sep 17 00:00:00 2001 From: Antonio Bernardini Date: Wed, 30 Apr 2025 03:57:53 +0200 Subject: [PATCH 2/5] Update example to document support for both numeric and letter indices :recycle: --- .../Buttons_Basic/Buttons_Basic.ino | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino b/examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino index f29f96b..b097834 100644 --- a/examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino +++ b/examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino @@ -24,23 +24,25 @@ void setup() { // 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 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)) { + // You can use either index (0=A, 1=B, 2=C) or letter ('A', 'B', 'C') to check buttons + // Below we use the letter-based method for better readability + + if (buttons.isPressed('A')) { Serial.println("Button A pressed!"); button_a = !button_a; - } else if (buttons.isPressed(1)) { + } else if (buttons.isPressed("B")) { Serial.println("Button B pressed!"); button_b = !button_b; - } else if (buttons.isPressed(2)) { + } else if (buttons.isPressed('C')) { Serial.println("Button C pressed!"); button_c = !button_c; } - - // Update the LEDs above buttons, depending on the variables value + + // Update the LEDs above buttons, depending on the variables' value buttons.setLeds(button_a, button_b, button_c); } } \ No newline at end of file From b8a67c711077f0049a1b0165dd7d380412bcd68f Mon Sep 17 00:00:00 2001 From: Antonio Bernardini Date: Thu, 8 May 2025 16:42:55 +0200 Subject: [PATCH 3/5] Update API docs :recycle: --- docs/api.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/api.md b/docs/api.md index 7c463c0..f1fe935 100644 --- a/docs/api.md +++ b/docs/api.md @@ -33,6 +33,12 @@ Represents a Modulino Buttons module. - **`PinStatus isPressed(int index)`** Returns the press status (HIGH/LOW) of the button at the specified index (_0-A, 1-B, 2-C_). +- **`PinStatus isPressed(char button)`** + Returns the press status (HIGH/LOW) of the button specified by its character ('A', 'B', 'C'). + +- **`PinStatus isPressed(const char *button)`** + Returns the press status (HIGH/LOW) of the button specified by its string ("A", "B", "C"). + - **`bool update()`** Updates the button status. Returns `true` if the status has changed, `false` otherwise. From ace923eeab41df3009059cc70ac5b8e806578ad1 Mon Sep 17 00:00:00 2001 From: Leonardo Cavagnis <45899760+leonardocavagnis@users.noreply.github.com> Date: Fri, 9 May 2025 12:22:21 +0200 Subject: [PATCH 4/5] Delete .gitignore --- .gitignore | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 5577b0d..0000000 --- a/.gitignore +++ /dev/null @@ -1,28 +0,0 @@ -# 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 (se lo usi) -.vscode/ \ No newline at end of file From 087c581af880af06db3055f6b116b33894a60ea7 Mon Sep 17 00:00:00 2001 From: Leonardo Cavagnis <45899760+leonardocavagnis@users.noreply.github.com> Date: Fri, 9 May 2025 12:23:28 +0200 Subject: [PATCH 5/5] Update examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino --- examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino b/examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino index b097834..9d5e4da 100644 --- a/examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino +++ b/examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino @@ -42,7 +42,7 @@ void loop() { button_c = !button_c; } - // Update the LEDs above buttons, depending on the variables' value + // Update the LEDs above buttons, depending on the variables value buttons.setLeds(button_a, button_b, button_c); } } \ No newline at end of file