Skip to content

Commit 9c948fe

Browse files
Add support for character input ('A', 'B', 'C') in isPressed() (#33)
* 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 ♻️ * Update API docs ♻️ * Delete .gitignore * Update examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino --------- Co-authored-by: Leonardo Cavagnis <[email protected]>
1 parent 3c36732 commit 9c948fe

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

docs/api.md

Lines changed: 6 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

examples/Modulino_Buttons/Buttons_Basic/Buttons_Basic.ino

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,24 @@ 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-
// Also toggle the corresponding LED, for each of the three buttons
32-
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')) {
3335
Serial.println("Button A pressed!");
3436
button_a = !button_a;
35-
} else if (buttons.isPressed(1)) {
37+
} else if (buttons.isPressed("B")) {
3638
Serial.println("Button B pressed!");
3739
button_b = !button_b;
38-
} else if (buttons.isPressed(2)) {
40+
} else if (buttons.isPressed('C')) {
3941
Serial.println("Button C pressed!");
4042
button_c = !button_c;
4143
}
42-
44+
4345
// Update the LEDs above buttons, depending on the variables value
4446
buttons.setLeds(button_a, button_b, button_c);
4547
}

src/Modulino.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ class ModulinoButtons : public Module {
127127
PinStatus isPressed(int index) {
128128
return last_status[index] ? HIGH : LOW;
129129
}
130+
PinStatus isPressed(char button) {
131+
int index = buttonToIndex(button);
132+
if (index < 0) return LOW;
133+
return isPressed(index);
134+
}
135+
PinStatus isPressed(const char *button) {
136+
if (button == nullptr || button[0] == '\0' || button[1] != '\0') {
137+
return LOW;
138+
}
139+
return isPressed(button[0]);
140+
}
130141
bool update() {
131142
uint8_t buf[3];
132143
auto res = read((uint8_t*)buf, 3);
@@ -154,6 +165,14 @@ class ModulinoButtons : public Module {
154165
}
155166
private:
156167
bool last_status[3];
168+
int buttonToIndex(char button) {
169+
switch (toupper(button)) {
170+
case 'A': return 0;
171+
case 'B': return 1;
172+
case 'C': return 2;
173+
default: return -1;
174+
}
175+
}
157176
protected:
158177
uint8_t match[1] = { 0x7C }; // same as fw main.c
159178
};

0 commit comments

Comments
 (0)