57 lines
1.0 KiB
C++
57 lines
1.0 KiB
C++
/*
|
|
* 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;
|
|
}
|
|
|
|
void Button::cyclic(void)
|
|
{
|
|
if((systick - this->debounceTimer) > this->debounce)
|
|
{
|
|
bool state = LL_GPIO_IsInputPinSet(this->port, this->pin);
|
|
this->stateLast = this->state;
|
|
if(state != this->state)
|
|
{
|
|
this->debounceTimer = systick;
|
|
this->state = state;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Button::isPressed(void)
|
|
{
|
|
return !this->state;
|
|
}
|
|
|
|
bool Button::isReleased(void)
|
|
{
|
|
bool released = (!this->stateLast && this->state);
|
|
this->stateLast = this->state;
|
|
return released;
|
|
}
|
|
}
|
|
|