add button controller
This commit is contained in:
parent
a8bebfc0fc
commit
6b4835d1da
40
code/ButtonController.cpp
Normal file
40
code/ButtonController.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
class ButtonController {
|
||||||
|
|
||||||
|
private:
|
||||||
|
int pin;
|
||||||
|
int new_value;
|
||||||
|
int old_value = HIGH;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ButtonController(int pin){
|
||||||
|
this->set_pin(pin);
|
||||||
|
};
|
||||||
|
|
||||||
|
void configure_pins(){
|
||||||
|
pinMode(this->pin, INPUT_PULLUP);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_pin(int pin){
|
||||||
|
this->pin = pin;
|
||||||
|
}
|
||||||
|
|
||||||
|
void read(){
|
||||||
|
this->new_value = digitalRead(this->pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool value_changed(){
|
||||||
|
return (this->new_value != this->old_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_pressed(){
|
||||||
|
if(this->value_changed()){
|
||||||
|
bool is_pressed = (this->new_value == LOW);
|
||||||
|
this->old_value = this->new_value;
|
||||||
|
return is_pressed;
|
||||||
|
}
|
||||||
|
return this->new_value == LOW;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user