59 lines
1.0 KiB
C++
59 lines
1.0 KiB
C++
|
#include <Arduino.h>
|
||
|
#include <string.h>
|
||
|
#include "sensors.h"
|
||
|
|
||
|
void PushButton::setup(const int pin){
|
||
|
pinMode(pin, INPUT);
|
||
|
_pin = pin;
|
||
|
}
|
||
|
|
||
|
bool PushButton::tick(){
|
||
|
const bool pushed = (digitalRead(_pin) == 0);
|
||
|
const bool toggle = (pushed != _pushed);
|
||
|
_pushed = pushed;
|
||
|
|
||
|
if( toggle && pushed ){ // button has been pressed
|
||
|
_pushStart = millis();
|
||
|
}
|
||
|
|
||
|
return toggle;
|
||
|
}
|
||
|
|
||
|
bool PushButton::tap(){
|
||
|
const bool toggle = tick();
|
||
|
|
||
|
// released and last less than hold threshold
|
||
|
return toggle && !_pushed && (millis()-_pushStart) < HOLD_MS_THRESHOLD;
|
||
|
}
|
||
|
|
||
|
bool PushButton::hold(){
|
||
|
const bool toggle = tick();
|
||
|
|
||
|
// released and last at ;east hold threshold
|
||
|
return toggle && !_pushed && (millis()-_pushStart) >= HOLD_MS_THRESHOLD;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
void Potentiometer::setup(const int pin){
|
||
|
pinMode(pin, INPUT);
|
||
|
_pin = pin;
|
||
|
}
|
||
|
|
||
|
short Potentiometer::range(){
|
||
|
return 1023 - analogRead(_pin);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
// pressure sensors
|
||
|
void Pressure::setup(const int pin){
|
||
|
pinMode(pin, INPUT);
|
||
|
_pin = pin;
|
||
|
}
|
||
|
|
||
|
short Pressure::value(){
|
||
|
return analogRead(_pin);
|
||
|
}
|