From 6b4835d1daf6ecfd0579a18fe01d9b77f3125483 Mon Sep 17 00:00:00 2001 From: rafa Date: Sat, 24 May 2025 13:35:06 +0100 Subject: [PATCH] add button controller --- code/ButtonController.cpp | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 code/ButtonController.cpp diff --git a/code/ButtonController.cpp b/code/ButtonController.cpp new file mode 100644 index 0000000..87ee7da --- /dev/null +++ b/code/ButtonController.cpp @@ -0,0 +1,40 @@ +#include + +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; + } + +}; \ No newline at end of file