2016-11-06 11:03:06 +01:00
|
|
|
#include "HIB.h"
|
2016-11-11 10:16:26 +01:00
|
|
|
#define MAX_PIN 15
|
2016-11-06 11:03:06 +01:00
|
|
|
|
2016-11-11 10:16:26 +01:00
|
|
|
HIB *HibList[MAX_PIN+1];
|
|
|
|
void ICACHE_RAM_ATTR sws_isr_0() { HibList[0]->IRQ_handler(); }
|
|
|
|
void ICACHE_RAM_ATTR sws_isr_1() { HibList[1]->IRQ_handler(); }
|
|
|
|
void ICACHE_RAM_ATTR sws_isr_2() { HibList[2]->IRQ_handler(); }
|
|
|
|
void ICACHE_RAM_ATTR sws_isr_3() { HibList[3]->IRQ_handler(); }
|
|
|
|
void ICACHE_RAM_ATTR sws_isr_4() { HibList[4]->IRQ_handler(); }
|
|
|
|
void ICACHE_RAM_ATTR sws_isr_5() { HibList[5]->IRQ_handler(); }
|
|
|
|
// Pin 6 to 11 can not be used
|
|
|
|
void ICACHE_RAM_ATTR sws_isr_12() { HibList[12]->IRQ_handler(); }
|
|
|
|
void ICACHE_RAM_ATTR sws_isr_13() { HibList[13]->IRQ_handler(); }
|
|
|
|
void ICACHE_RAM_ATTR sws_isr_14() { HibList[14]->IRQ_handler(); }
|
|
|
|
void ICACHE_RAM_ATTR sws_isr_15() { HibList[15]->IRQ_handler(); }
|
2016-11-06 11:03:06 +01:00
|
|
|
|
2016-11-11 10:16:26 +01:00
|
|
|
static void (*ISRList[MAX_PIN+1])() = {
|
|
|
|
sws_isr_0,
|
|
|
|
sws_isr_1,
|
|
|
|
sws_isr_2,
|
|
|
|
sws_isr_3,
|
|
|
|
sws_isr_4,
|
|
|
|
sws_isr_5,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sws_isr_12,
|
|
|
|
sws_isr_13,
|
|
|
|
sws_isr_14,
|
|
|
|
sws_isr_15
|
|
|
|
};
|
2016-11-06 11:03:06 +01:00
|
|
|
|
2016-11-13 23:34:21 +01:00
|
|
|
void __timerCallback(void *data) {
|
2016-11-11 10:16:26 +01:00
|
|
|
HIB *hib = static_cast<HIB *>(data);
|
|
|
|
hib->debouncing = false;
|
|
|
|
hib->invertState();
|
|
|
|
if (hib->state != digitalRead(hib->pin)){
|
|
|
|
hib->state = !hib->state;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(hib->state != hib->initialState)
|
2016-11-13 23:34:21 +01:00
|
|
|
hib->onInternalButtonPressed();
|
2016-11-11 10:16:26 +01:00
|
|
|
else
|
2016-11-13 23:34:21 +01:00
|
|
|
hib->onInternalButtonReleased();
|
2016-11-06 11:03:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-13 23:34:21 +01:00
|
|
|
HIB::HIB(uint8_t p, uint8_t initState,
|
2016-12-07 21:36:29 +01:00
|
|
|
void (* userOnButtonPressed)(uint8_t pin, int state),
|
|
|
|
void (* userOnButtonReleased)(uint8_t pin, int state),
|
2016-11-13 23:34:21 +01:00
|
|
|
void (* userOnLongButtonPressed)(uint8_t pin),
|
|
|
|
unsigned long longPress, unsigned long shortPress):
|
2016-12-07 21:36:29 +01:00
|
|
|
previousMillis(0),
|
2016-11-13 23:34:21 +01:00
|
|
|
longPressMsec(longPress),
|
|
|
|
shortPressMsec(shortPress),
|
|
|
|
onButtonPressed(userOnButtonPressed),
|
|
|
|
onButtonReleased(userOnButtonReleased),
|
|
|
|
onLongButtonPressed(userOnLongButtonPressed),
|
2016-11-11 10:16:26 +01:00
|
|
|
pin(p), initialState(initState),
|
|
|
|
state(initState), debouncing(false) {
|
2016-11-13 23:34:21 +01:00
|
|
|
pinMode(pin, INPUT_PULLUP);
|
|
|
|
HibList[pin] = this;
|
|
|
|
attachInterrupt(digitalPinToInterrupt(pin), ISRList[pin], CHANGE);
|
|
|
|
os_timer_setfn(&timer, __timerCallback, this);
|
|
|
|
}
|
2016-11-06 11:03:06 +01:00
|
|
|
|
2016-11-11 10:16:26 +01:00
|
|
|
void HIB::IRQ_handler(){
|
2016-11-06 11:03:06 +01:00
|
|
|
if(!debouncing){
|
|
|
|
debouncing = true;
|
2016-11-13 23:34:21 +01:00
|
|
|
os_timer_arm(&timer, shortPressMsec, 0);
|
2016-11-06 11:03:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-13 23:34:21 +01:00
|
|
|
void HIB::onInternalButtonPressed(){
|
2016-11-06 11:03:06 +01:00
|
|
|
previousMillis = millis();
|
2016-11-13 23:34:21 +01:00
|
|
|
if(onButtonPressed)
|
2016-12-07 21:36:29 +01:00
|
|
|
onButtonPressed(pin, state);
|
2016-11-06 11:03:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 23:34:21 +01:00
|
|
|
void HIB::onInternalLongButtonPressed(){
|
|
|
|
if(onLongButtonPressed)
|
|
|
|
onLongButtonPressed(pin);
|
2016-11-06 11:03:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 23:34:21 +01:00
|
|
|
void HIB::onInternalButtonReleased(){
|
|
|
|
if(onButtonReleased)
|
2016-12-07 21:36:29 +01:00
|
|
|
onButtonReleased(pin, state);
|
2016-11-06 11:03:06 +01:00
|
|
|
if(millis() - previousMillis >= longPressMsec){
|
2016-11-13 23:34:21 +01:00
|
|
|
onInternalLongButtonPressed();
|
2016-11-06 11:03:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 10:16:26 +01:00
|
|
|
void HIB::invertState(){
|
|
|
|
state = !state;
|
|
|
|
}
|