2025-02-09 18:58:01 +01:00

42 lines
833 B
C++

#include <Arduino.h>
#include "Btn.h"
Btn::Btn(int pin, unsigned long debounce_delay) {
pinMode(pin, INPUT_PULLUP);
_pin = pin;
_debounce_delay = debounce_delay;
_last_reading = HIGH;
_last_state = HIGH;
_last_debounce_time = 0;
}
void Btn::checkState(){
int reading = digitalRead(_pin);
if (reading != _last_reading) {
_last_debounce_time = millis();
}
if ((millis() - _last_debounce_time) > _debounce_delay) {
if (reading != _state) {
SerialUSB.print(_pin);
SerialUSB.print(": ");
SerialUSB.println(_state);
_state = reading;
}
}
_last_reading = reading;
}
int Btn::getState(){
this->checkState();
return _state;
}
int Btn::getStateOnce(){
this->checkState();
if(_state != _last_state){
_last_state = _state;
return _state;
}else{
return -1;
}
}