adventsfenster/include/taster.h

72 lines
1.7 KiB
C
Raw Normal View History

2021-11-03 17:55:42 +01:00
#ifndef __taster_H__
#define __taster_H__
#include <Arduino.h>
#include <FunctionalInterrupt.h>
class taster
{
private:
volatile const unsigned long _BOUNCING_TIME_MS = 300;
2021-11-03 17:55:42 +01:00
volatile unsigned long _last_pressed;
unsigned long _number_pressed;
2021-11-03 17:55:42 +01:00
unsigned long _number_checked;
size_t _taster_pin;
2021-12-08 18:25:51 +01:00
volatile unsigned long * _ptr_number_pressed;
2021-11-03 17:55:42 +01:00
void IRAM_ATTR _taster_int()
{
unsigned long msecs = millis();
if(msecs -_last_pressed > _BOUNCING_TIME_MS)
2021-11-03 17:55:42 +01:00
{
_last_pressed = msecs;
2021-11-03 17:55:42 +01:00
_number_pressed += 1;
}
}
public:
2021-12-08 18:25:51 +01:00
//taster(const size_t pin, volatile unsigned long* ptr_number_pressed);
taster(const size_t pin);
2021-11-03 17:55:42 +01:00
~taster();
bool pressed();
2021-12-08 18:25:51 +01:00
void reset();
void begin();
2021-11-03 17:55:42 +01:00
};
2021-12-08 18:25:51 +01:00
//taster::taster(size_t pin, volatile unsigned long* ptr_number_pressed)
2021-11-03 17:55:42 +01:00
taster::taster(size_t pin)
{
_taster_pin = pin;
//_ptr_number_pressed = ptr_number_pressed;
2021-11-03 17:55:42 +01:00
_number_checked = 0;
_number_pressed = 0;
2021-11-03 17:55:42 +01:00
_last_pressed = 0;
2021-12-08 18:25:51 +01:00
}
void taster::begin()
{
pinMode(_taster_pin, INPUT_PULLUP);
2021-11-03 17:55:42 +01:00
attachInterrupt(_taster_pin, std::bind(&taster::_taster_int,this), FALLING);
//attachInterrupt(_taster_pin, isrfunction, FALLING);
2021-11-03 17:55:42 +01:00
}
taster::~taster()
{
2021-12-08 18:25:51 +01:00
//detachInterrupt(_taster_pin);
2021-11-03 17:55:42 +01:00
}
bool taster::pressed() {
2021-12-08 18:25:51 +01:00
//if (*_ptr_number_pressed > _number_checked)
if (_number_pressed > _number_checked)
2021-11-03 17:55:42 +01:00
{
2021-12-08 18:25:51 +01:00
//_number_checked = *_ptr_number_pressed;
2021-11-03 17:55:42 +01:00
_number_checked = _number_pressed;
return true;
}
return false;
}
2021-12-08 18:25:51 +01:00
void taster::reset() {
_number_pressed = _number_checked;
}
2021-11-03 17:55:42 +01:00
#endif