diff --git a/Application/Inc/GPIO.hpp b/Application/Inc/GPIO.hpp new file mode 100644 index 0000000..4943120 --- /dev/null +++ b/Application/Inc/GPIO.hpp @@ -0,0 +1,38 @@ +#ifndef APPLICATION_INC_GPIO +#define APPLICATION_INC_GPIO + +#include "main.h" + +/** + * @brief Construct a new Switch:: Switch object + * + * @param freq Provide scheduler running frequency. 100Hz is good + * @warning setPort(GPIOX, GPIO_PIN_X) is required to run. + */ +class CustomGPIO { + public: + CustomGPIO(); + CustomGPIO(uint16_t); + virtual ~CustomGPIO(); + void setPort(GPIO_TypeDef *, uint16_t); + + void on(); + void off(); + void toggle(); + + bool getStatus(); + + void schedulerLowActive(); + void schedulerHighActive(); + + private: + GPIO_TypeDef *m_port; + uint16_t m_pin; + + uint16_t m_roll_period{100}; + uint16_t m_roll_timer{0}; + bool m_stable{true}; + bool m_pressed{false}; +}; + +#endif /* APPLICATION_INC_GPIO */ diff --git a/Application/Src/GPIO.cpp b/Application/Src/GPIO.cpp new file mode 100644 index 0000000..0939b1d --- /dev/null +++ b/Application/Src/GPIO.cpp @@ -0,0 +1,54 @@ +#include "GPIO.hpp" + +CustomGPIO::CustomGPIO() {} +CustomGPIO::CustomGPIO(uint16_t value) { m_roll_period = value; } +CustomGPIO::~CustomGPIO() {} + +void CustomGPIO::setPort(GPIO_TypeDef *port, uint16_t pin) { + m_port = port; + m_pin = pin; +} + +void CustomGPIO::on() { HAL_GPIO_WritePin(m_port, m_pin, GPIO_PIN_SET); } + +void CustomGPIO::off() { HAL_GPIO_WritePin(m_port, m_pin, GPIO_PIN_RESET); } + +void CustomGPIO::toggle() { HAL_GPIO_TogglePin(m_port, m_pin); } + +void CustomGPIO::schedulerLowActive() { + if (m_stable) { + if (!HAL_GPIO_ReadPin(m_port, m_pin)) + m_stable = false; + else + m_roll_timer = 0; + } + + else if (HAL_GPIO_ReadPin(m_port, m_pin) || ++m_roll_timer > m_roll_period) { + m_stable = true; + m_pressed = true; + m_roll_timer = m_roll_period * 0.8; + } +} + +void CustomGPIO::schedulerHighActive() { + if (m_stable) { + if (HAL_GPIO_ReadPin(m_port, m_pin)) + m_stable = false; + else + m_roll_timer = 0; + } + + else if (!HAL_GPIO_ReadPin(m_port, m_pin) || ++m_roll_timer > m_roll_period) { + m_stable = true; + m_pressed = true; + m_roll_timer = m_roll_period * 0.8; + } +} + +bool CustomGPIO::getStatus() { + if (m_pressed) { + m_pressed = !m_pressed; + return true; + } else + return false; +} \ No newline at end of file diff --git a/cmake/SourceList.cmake b/cmake/SourceList.cmake index 7f0cca3..3e3c48c 100644 --- a/cmake/SourceList.cmake +++ b/cmake/SourceList.cmake @@ -4,6 +4,7 @@ ${PROJ_PATH}/Application/Src/ADC.cpp ${PROJ_PATH}/Application/Src/CLI.cpp ${PROJ_PATH}/Application/Src/DAC.cpp ${PROJ_PATH}/Application/Src/Flash_STM32G431KB.cpp +${PROJ_PATH}/Application/Src/GPIO.cpp ${PROJ_PATH}/Application/Src/LED.cpp ${PROJ_PATH}/Application/Src/main.cpp ${PROJ_PATH}/Application/Src/SerialCOM.cpp