First Draft with only one GPIO usable
This commit is contained in:
parent
6e509e9c11
commit
d5396d7580
80
HIB.cpp
Normal file
80
HIB.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include "HIB.h"
|
||||
|
||||
static uint8_t pin;
|
||||
static uint8_t initialState;
|
||||
static int state;
|
||||
static volatile bool debouncing;
|
||||
static ETSTimer timer;
|
||||
static unsigned long previousMillis;
|
||||
static unsigned int longPressMsec;
|
||||
void timerCallback(void *);
|
||||
void IRQ_handler();
|
||||
void onButtonPressed();
|
||||
void onButtonReleased();
|
||||
void onLongButtonPressed();
|
||||
void setConfig(uint8_t p, uint8_t initState, int longPress );
|
||||
|
||||
|
||||
HIB::HIB(uint8_t pin, uint8_t initialState, int longPressMsec)
|
||||
{
|
||||
setConfig(pin, initialState, longPressMsec);
|
||||
}
|
||||
|
||||
HIB::HIB(){
|
||||
}
|
||||
|
||||
void HIB::configure(uint8_t pin, uint8_t initState, int longPressMsec){
|
||||
setConfig(pin, initState, longPressMsec);
|
||||
}
|
||||
|
||||
void setConfig(uint8_t p, uint8_t initState, int longPress ){
|
||||
pin = p;
|
||||
initialState = initState;
|
||||
state = initialState;
|
||||
debouncing = false;
|
||||
previousMillis = 0;
|
||||
longPressMsec = longPress;
|
||||
|
||||
pinMode(pin, INPUT);
|
||||
attachInterrupt(digitalPinToInterrupt(pin), IRQ_handler, CHANGE);
|
||||
os_timer_setfn(&timer, timerCallback, NULL);
|
||||
}
|
||||
|
||||
void IRQ_handler(){
|
||||
if(!debouncing){
|
||||
debouncing = true;
|
||||
os_timer_arm(&timer, 50, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void timerCallback(void *){
|
||||
debouncing = false;
|
||||
state = !state;
|
||||
if (state != digitalRead(pin)){
|
||||
Serial.println("Debounce failed");
|
||||
state = !state;
|
||||
return;
|
||||
}
|
||||
if(state != initialState)
|
||||
onButtonPressed();
|
||||
else
|
||||
onButtonReleased();
|
||||
}
|
||||
|
||||
void onButtonPressed(){
|
||||
Serial.println("Button Pressed");
|
||||
previousMillis = millis();
|
||||
}
|
||||
|
||||
void onLongButtonPressed(){
|
||||
Serial.println("Long Button Pressed");
|
||||
}
|
||||
|
||||
void onButtonReleased(){
|
||||
Serial.println("Button Released");
|
||||
if(millis() - previousMillis >= longPressMsec){
|
||||
onLongButtonPressed();
|
||||
}
|
||||
}
|
||||
|
||||
HIB Hib;
|
17
HIB.h
Normal file
17
HIB.h
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include "Arduino.h"
|
||||
extern "C" {
|
||||
#include "osapi.h"
|
||||
}
|
||||
// Human Interface Button
|
||||
class HIB {
|
||||
private:
|
||||
|
||||
public:
|
||||
HIB(uint8_t pin, uint8_t initialState, int longPressMsec = 5000);
|
||||
HIB();
|
||||
void configure(uint8_t pin, uint8_t initialState, int longPressMsec = 5000);
|
||||
};
|
||||
|
||||
|
||||
extern HIB Hib;
|
Loading…
Reference in New Issue
Block a user