Better tactile processing

This commit is contained in:
Pawel Spychalski
2018-05-14 16:17:38 +02:00
parent 2fc84c78c5
commit b5cc91c814
4 changed files with 86 additions and 46 deletions

42
crossbow/tactile.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include "Arduino.h"
#include "tactile.h"
Tactile::Tactile(uint8_t pin) {
_pin = pin;
}
void Tactile::loop(void) {
uint8_t pinState = digitalRead(_pin);
//Press moment
if (pinState == LOW && _previousPinState == HIGH) {
_pressMillis = millis();
}
//Release moment
if (pinState == HIGH && _previousPinState == LOW) {
const uint32_t buttonTime = abs(millis() - _pressMillis);
if (buttonTime > TACTILE_LONG_PRESS_TIME) {
_state = TACTILE_STATE_LONG_PRESS;
} else if (buttonTime > TACTILE_MIN_PRESS_TIME) {
_state = TACTILE_STATE_SHORT_PRESS;
} else {
_state = TACTILE_STATE_NONE;
}
} else {
_state = TACTILE_STATE_NONE;
}
_previousPinState = pinState;
}
void Tactile::start(void) {
pinMode(_pin, INPUT_PULLUP);
}
uint8_t Tactile::getState(void) {
return _state;
}