-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from jasonyang-ee:dev
Add: GPIO class
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters