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
|
sws_isr_15
|
||||||
};
|
};
|
||||||
|
|
||||||
void timerCallback(void *data) {
|
void __timerCallback(void *data) {
|
||||||
HIB *hib = static_cast<HIB *>(data);
|
HIB *hib = static_cast<HIB *>(data);
|
||||||
hib->debouncing = false;
|
hib->debouncing = false;
|
||||||
hib->invertState();
|
hib->invertState();
|
||||||
if (hib->state != digitalRead(hib->pin)){
|
if (hib->state != digitalRead(hib->pin)){
|
||||||
Serial.println("Debounce failed");
|
|
||||||
hib->state = !hib->state;
|
hib->state = !hib->state;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(hib->state != hib->initialState)
|
if(hib->state != hib->initialState)
|
||||||
hib->onButtonPressed();
|
hib->onInternalButtonPressed();
|
||||||
else
|
else
|
||||||
hib->onButtonReleased();
|
hib->onInternalButtonReleased();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
HIB::HIB(uint8_t p, uint8_t initState, unsigned long longPress):
|
HIB::HIB(uint8_t p, uint8_t initState,
|
||||||
previousMillis(0), longPressMsec(longPress),
|
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),
|
pin(p), initialState(initState),
|
||||||
state(initState), debouncing(false) {
|
state(initState), debouncing(false) {
|
||||||
pinMode(pin, INPUT_PULLUP);
|
pinMode(pin, INPUT_PULLUP);
|
||||||
HibList[pin] = this;
|
HibList[pin] = this;
|
||||||
attachInterrupt(digitalPinToInterrupt(pin), ISRList[pin], CHANGE);
|
attachInterrupt(digitalPinToInterrupt(pin), ISRList[pin], CHANGE);
|
||||||
os_timer_setfn(&timer, timerCallback, this);
|
os_timer_setfn(&timer, __timerCallback, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HIB::IRQ_handler(){
|
void HIB::IRQ_handler(){
|
||||||
if(!debouncing){
|
if(!debouncing){
|
||||||
debouncing = true;
|
debouncing = true;
|
||||||
os_timer_arm(&timer, 50, 0);
|
os_timer_arm(&timer, shortPressMsec, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void HIB::onButtonPressed(){
|
void HIB::onInternalButtonPressed(){
|
||||||
Serial.printf("Button Pressed %d\n", pin);
|
|
||||||
previousMillis = millis();
|
previousMillis = millis();
|
||||||
|
if(onButtonPressed)
|
||||||
|
onButtonPressed(pin);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HIB::onLongButtonPressed(){
|
void HIB::onInternalLongButtonPressed(){
|
||||||
Serial.printf("Long Button Pressed %d \n", pin);
|
if(onLongButtonPressed)
|
||||||
|
onLongButtonPressed(pin);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HIB::onButtonReleased(){
|
void HIB::onInternalButtonReleased(){
|
||||||
Serial.printf("Button Released %d \n", pin);
|
if(onButtonReleased)
|
||||||
|
onButtonReleased(pin);
|
||||||
if(millis() - previousMillis >= longPressMsec){
|
if(millis() - previousMillis >= longPressMsec){
|
||||||
onLongButtonPressed();
|
onInternalLongButtonPressed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
21
HIB.h
21
HIB.h
@ -3,22 +3,31 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#include "osapi.h"
|
#include "osapi.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Human Interface Button
|
// Human Interface Button
|
||||||
class HIB {
|
class HIB {
|
||||||
private:
|
private:
|
||||||
ETSTimer timer;
|
ETSTimer timer;
|
||||||
unsigned long previousMillis;
|
unsigned long previousMillis;
|
||||||
unsigned long longPressMsec;
|
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:
|
public:
|
||||||
uint8_t pin;
|
uint8_t pin;
|
||||||
uint8_t initialState;
|
uint8_t initialState;
|
||||||
uint8_t state;
|
uint8_t state;
|
||||||
uint8_t debouncing;
|
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 IRQ_handler();
|
||||||
void onButtonPressed();
|
|
||||||
void onButtonReleased();
|
|
||||||
void onLongButtonPressed();
|
|
||||||
void invertState();
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user