Commit 0159f516 authored by Mathieu Maret's avatar Mathieu Maret

Add external callback

And better members/methods protection
parent 187dbd40
......@@ -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);
}
pinMode(pin, INPUT_PULLUP);
HibList[pin] = this;
attachInterrupt(digitalPinToInterrupt(pin), ISRList[pin], CHANGE);
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();
}
}
......
......@@ -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();
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment