/* * Button.cpp * * Created on: Jun 8, 2024 * Author: Carst */ #include "STM32G071KBT6.hpp" #include "Button.hpp" namespace ElektronischeLast { void Button::init(GPIO_TypeDef* port, uint16_t pin) { LL_GPIO_InitTypeDef GPIO_InitStruct = { .Pin = pin, .Mode = LL_GPIO_MODE_INPUT, .Pull = LL_GPIO_PULL_UP, }; LL_GPIO_Init(port, &GPIO_InitStruct); this->port = port; this->pin = pin; this->state = LL_GPIO_IsInputPinSet(port, pin); this->stateLast = this->state; this->debounceTimer = systick; } bool Button::cyclic(void) { bool changed = false; if((systick - this->debounceTimer) > this->debounce) { bool state = LL_GPIO_IsInputPinSet(this->port, this->pin); this->stateLast = this->state; if(state != this->state) { changed = true; this->debounceTimer = systick; this->state = state; } } return changed; } bool Button::isPressed(void) { return !this->state; } bool Button::isReleased(void) { bool released = (!this->stateLast && this->state); this->stateLast = this->state; return released; } }