Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: GPIO class #12

Merged
merged 1 commit into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Application/Inc/GPIO.hpp
Original file line number Diff line number Diff line change
@@ -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 */
54 changes: 54 additions & 0 deletions Application/Src/GPIO.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions cmake/SourceList.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down