Add external callback
And better members/methods protection
This commit is contained in:
parent
187dbd40af
commit
0159f516c7
43
HIB.cpp
43
HIB.cpp
@ -33,53 +33,64 @@ static void (*ISRList[MAX_PIN+1])() = {
|
||||
sws_isr_15
|
||||
};
|
||||
|
||||
void timerCallback(void *data) {
|
||||
void __timerCallback(void *data) {
|
||||
HIB *hib = static_cast<HIB *>(data);
|
||||
hib->debouncing = false;
|
||||
hib->invertState();
|
||||
if (hib->state != digitalRead(hib->pin)){
|
||||
Serial.println("Debounce failed");
|
||||
hib->state = !hib->state;
|
||||
return;
|
||||
}
|
||||
if(hib->state != hib->initialState)
|
||||
hib->onButtonPressed();
|
||||
hib->onInternalButtonPressed();
|
||||
else
|
||||
hib->onButtonReleased();
|
||||
hib->onInternalButtonReleased();
|
||||
}
|
||||
|
||||
|
||||
HIB::HIB(uint8_t p, uint8_t initState, unsigned long longPress):
|
||||
previousMillis(0), longPressMsec(longPress),
|
||||
HIB::HIB(uint8_t p, uint8_t initState,
|
||||
void (* userOnButtonPressed)(uint8_t pin),
|
||||
void (* userOnButtonReleased)(uint8_t pin),
|
||||
void (* userOnLongButtonPressed)(uint8_t pin),
|
||||
unsigned long longPress, unsigned long shortPress):
|
||||
previousMillis(0),
|
||||
longPressMsec(longPress),
|
||||
shortPressMsec(shortPress),
|
||||
onButtonPressed(userOnButtonPressed),
|
||||
onButtonReleased(userOnButtonReleased),
|
||||
onLongButtonPressed(userOnLongButtonPressed),
|
||||
pin(p), initialState(initState),
|
||||
state(initState), debouncing(false) {
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
HibList[pin] = this;
|
||||
attachInterrupt(digitalPinToInterrupt(pin), ISRList[pin], CHANGE);
|
||||
os_timer_setfn(&timer, timerCallback, this);
|
||||
}
|
||||
os_timer_setfn(&timer, __timerCallback, this);
|
||||
}
|
||||
|
||||
void HIB::IRQ_handler(){
|
||||
if(!debouncing){
|
||||
debouncing = true;
|
||||
os_timer_arm(&timer, 50, 0);
|
||||
os_timer_arm(&timer, shortPressMsec, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HIB::onButtonPressed(){
|
||||
Serial.printf("Button Pressed %d\n", pin);
|
||||
void HIB::onInternalButtonPressed(){
|
||||
previousMillis = millis();
|
||||
if(onButtonPressed)
|
||||
onButtonPressed(pin);
|
||||
}
|
||||
|
||||
void HIB::onLongButtonPressed(){
|
||||
Serial.printf("Long Button Pressed %d \n", pin);
|
||||
void HIB::onInternalLongButtonPressed(){
|
||||
if(onLongButtonPressed)
|
||||
onLongButtonPressed(pin);
|
||||
}
|
||||
|
||||
void HIB::onButtonReleased(){
|
||||
Serial.printf("Button Released %d \n", pin);
|
||||
void HIB::onInternalButtonReleased(){
|
||||
if(onButtonReleased)
|
||||
onButtonReleased(pin);
|
||||
if(millis() - previousMillis >= longPressMsec){
|
||||
onLongButtonPressed();
|
||||
onInternalLongButtonPressed();
|
||||
}
|
||||
}
|
||||
|
||||
|
21
HIB.h
21
HIB.h
@ -3,22 +3,31 @@
|
||||
extern "C" {
|
||||
#include "osapi.h"
|
||||
}
|
||||
|
||||
// Human Interface Button
|
||||
class HIB {
|
||||
private:
|
||||
ETSTimer timer;
|
||||
unsigned long previousMillis;
|
||||
unsigned long longPressMsec;
|
||||
|
||||
unsigned long shortPressMsec;
|
||||
void onInternalButtonPressed();
|
||||
void onInternalButtonReleased();
|
||||
void onInternalLongButtonPressed();
|
||||
void invertState();
|
||||
void (* onButtonPressed)(uint8_t pin);
|
||||
void (* onButtonReleased)(uint8_t pin);
|
||||
void (* onLongButtonPressed)(uint8_t pin);
|
||||
friend void __timerCallback(void *data);
|
||||
public:
|
||||
uint8_t pin;
|
||||
uint8_t initialState;
|
||||
uint8_t state;
|
||||
uint8_t debouncing;
|
||||
HIB(uint8_t pin, uint8_t initialState, unsigned long longPressMsec = 5000);
|
||||
HIB(uint8_t pin, uint8_t initialState,
|
||||
void (* onButtonPressed)(uint8_t pin) = NULL,
|
||||
void (* onButtonReleased)(uint8_t pin) = NULL,
|
||||
void (* onLongButtonPressed)(uint8_t pin) = NULL,
|
||||
unsigned long longPressMsec = 5000, unsigned long shortPressMsec = 50);
|
||||
void IRQ_handler();
|
||||
void onButtonPressed();
|
||||
void onButtonReleased();
|
||||
void onLongButtonPressed();
|
||||
void invertState();
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user