-
Notifications
You must be signed in to change notification settings - Fork 7.6k
General examples updates #7727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
General examples updates #7727
Changes from 11 commits
e1f49f8
714e919
3d8348c
1cad7c1
9eae97b
fc3fe6a
420f290
372cedc
75e4d81
4f2b58f
a2dd31a
ae99c71
387e562
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,39 @@ | ||
#include <Arduino.h> | ||
#include <FunctionalInterrupt.h> | ||
|
||
#define BUTTON1 16 | ||
#define BUTTON2 17 | ||
|
||
class Button | ||
{ | ||
public: | ||
Button(uint8_t reqPin) : PIN(reqPin){ | ||
pinMode(PIN, INPUT_PULLUP); | ||
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While your changes provide simpler code, the original example shows how to attach an interrupt to a class method (and that is often asked), so I suggest to keep the old way and use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pedrominatel could you please explain why we went from c++ to c? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was just a matter of getting consistency across many examples in C, and not in C++. I would prefer to keep the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we agree that the example with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. Is that ok for you @PilnyTomas? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PilnyTomas this is still left to be done |
||
}; | ||
~Button() { | ||
detachInterrupt(PIN); | ||
} | ||
|
||
void ARDUINO_ISR_ATTR isr() { | ||
numberKeyPresses += 1; | ||
pressed = true; | ||
} | ||
|
||
void checkPressed() { | ||
if (pressed) { | ||
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses); | ||
pressed = false; | ||
} | ||
} | ||
|
||
private: | ||
const uint8_t PIN; | ||
volatile uint32_t numberKeyPresses; | ||
volatile bool pressed; | ||
struct Button { | ||
uint8_t PIN; | ||
volatile uint32_t numberKeyPresses; | ||
volatile int pressed; | ||
}; | ||
|
||
Button button1(BUTTON1); | ||
Button button2(BUTTON2); | ||
void isr(void* param) { | ||
struct Button *button = (struct Button*) param; | ||
button->numberKeyPresses += 1; | ||
button->pressed = 1; | ||
} | ||
|
||
void checkPressed(struct Button* button) { | ||
if(button->pressed) { | ||
Serial.printf("Button on pin %u has been pressed %u times\n", button->PIN, button->numberKeyPresses); | ||
button->pressed = 0; | ||
} | ||
} | ||
|
||
struct Button button1 = {BUTTON1, 0, 0}; | ||
struct Button button2 = {BUTTON2, 0, 0}; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.begin(115200); | ||
pinMode(button1.PIN, INPUT_PULLUP); | ||
pinMode(button2.PIN, INPUT_PULLUP); | ||
attachInterruptArg(button1.PIN, isr, (void*)&button1, FALLING); | ||
attachInterruptArg(button2.PIN, isr, (void*)&button2, FALLING); | ||
} | ||
|
||
void loop() { | ||
button1.checkPressed(); | ||
button2.checkPressed(); | ||
checkPressed(&button1); | ||
checkPressed(&button2); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you either have
USE_NAME
defined, or not, I suggest removing this and using#ifdef
+#else
in the code below. Else it looks like you could have both defined at the same time.