From dda6c7a2ad7819129e99ca8c1a7ccc301c41a8c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andis=20Z=C4=ABle?= Date: Wed, 10 Apr 2024 15:45:50 +0300 Subject: [PATCH] Created button class --- firmware/src/hw/button.cpp | 85 ++++++++++++++++++++++++++++++++++++++ firmware/src/hw/button.h | 41 ++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 firmware/src/hw/button.cpp create mode 100644 firmware/src/hw/button.h diff --git a/firmware/src/hw/button.cpp b/firmware/src/hw/button.cpp new file mode 100644 index 0000000..12fc029 --- /dev/null +++ b/firmware/src/hw/button.cpp @@ -0,0 +1,85 @@ +/**** Includes ****/ +#include "../utils/utils.h" +#include "button.h" + +using namespace hw; + +/**** Private definitions ****/ +/**** Private constants ****/ +/**** Private variables ****/ +/**** Private function declarations ****/ + +/**** Public function definitions ****/ +hw::Button::Button(board::DigitalIn* din_ch, uint8_t act_lvl, uint8_t dbnc_lim, uint8_t init_state) +{ + this->din_ch = din_ch; + + if(act_lvl) this->act_lvl = board::DIN_HIGH; + else this->act_lvl = board::DIN_LOW; + + this->dbnc_cnter = 0; + this->dbnc_lim = dbnc_lim; + + if(init_state) this->state = BUTTON_ON; + else this->state = BUTTON_OFF; + + this->time = 0; + this->is_new = 0; +} + +hw::Button::~Button(void) +{ + return; +} + +uint8_t hw::Button::read(void) +{ + // Read din level + uint8_t lvl = this->din_ch->read(); + + // Increase state counter + this->time = util::sat_add(this->time, 1); + + // Determine next state + uint8_t next_state = BUTTON_OFF; + if(lvl==this->act_lvl) next_state = BUTTON_ON; + + // Advance debounce sample counter + if(next_state != this->state) this->dbnc_cnter++; + else this->dbnc_cnter = 0; + + // Check for debounce end + if(this->dbnc_cnter < this->dbnc_lim) return this->state; + + // Debounce end. Apply new state. + this->state = next_state; + this->time = 0; + this->is_new = 1; + this->dbnc_cnter = 0; + + return this->state; +} + +uint8_t hw::Button::force_read(void) +{ + // Read din level + uint8_t lvl = this->din_ch->read(); + + // Cancels active debounce + this->dbnc_cnter = 0; + + // Determine next state + uint8_t next_state = BUTTON_OFF; + if(lvl==this->act_lvl) next_state = BUTTON_ON; + + if(next_state != this->state) + { + this->state = next_state; + this->time = 0; + this->is_new = 1; + }; + + return this->state; +} + +/**** Private function definitions ****/ diff --git a/firmware/src/hw/button.h b/firmware/src/hw/button.h new file mode 100644 index 0000000..8c7b87e --- /dev/null +++ b/firmware/src/hw/button.h @@ -0,0 +1,41 @@ +#ifndef BUTTON_H_ +#define BUTTON_H_ + +/**** Includes ****/ +#include +#include "../board/din.h" + +namespace hw { + +/**** Public definitions ****/ +const uint8_t BUTTON_OFF = 0; +const uint8_t BUTTON_ON = 1; + +class Button +{ + protected: + board::DigitalIn* din_ch; + uint8_t act_lvl; + uint8_t dbnc_cnter; + + public: + Button(board::DigitalIn* din_ch, uint8_t act_lvl, uint8_t dbnc_lim, uint8_t init_state); + ~Button(void); + + uint8_t state; + uint16_t time; + uint8_t dbnc_lim; + uint8_t is_new; + + uint8_t read(void); + uint8_t force_read(void); +}; + +/**** Public function declarations ****/ + +#ifdef TESTING +#endif + +} //namespace + +#endif /* BUTTON_H_ */ \ No newline at end of file