Skip to content

Commit

Permalink
Merge pull request #12 from jasonyang-ee:dev
Browse files Browse the repository at this point in the history
Add: GPIO class
  • Loading branch information
jasonyang-ee committed Jun 4, 2023
2 parents 80b4266 + 090f0e2 commit f86b188
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
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

0 comments on commit f86b188

Please sign in to comment.