adventsfenster/include/taster.h

54 lines
1.1 KiB
C++

#ifndef __taster_H__
#define __taster_H__
#include <Arduino.h>
#include <FunctionalInterrupt.h>
class taster
{
private:
volatile const unsigned long _BOUNCING_TIME_MS = 200;
volatile unsigned long _last_pressed;
size_t _taster_pin;
volatile unsigned long _number_pressed;
unsigned long _number_checked;
void IRAM_ATTR _taster_int()
{
if(millis()-_last_pressed > _BOUNCING_TIME_MS)
{
_last_pressed = millis();
_number_pressed += 1;
}
}
public:
taster(size_t pin);
~taster();
bool pressed();
};
taster::taster(size_t pin)
{
_taster_pin = pin;
_number_pressed = 0;
_number_checked = 0;
_last_pressed = 0;
pinMode(_taster_pin, INPUT_PULLUP);
attachInterrupt(_taster_pin, std::bind(&taster::_taster_int,this), FALLING);
}
taster::~taster()
{
detachInterrupt(_taster_pin);
}
bool taster::pressed() {
if (_number_pressed != _number_checked)
{
_number_checked = _number_pressed;
return true;
}
return false;
}
#endif