First Draft with only one GPIO usable

This commit is contained in:
Mathieu Maret 2016-11-06 11:03:06 +01:00
parent 6e509e9c11
commit d5396d7580
2 changed files with 97 additions and 0 deletions

80
HIB.cpp Normal file
View 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
View 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;