CLEANUP: Tidy up code
This commit is contained in:
parent
6fb5c3ff79
commit
3cc48e2d23
@ -7,64 +7,36 @@
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
namespace floatpump {
|
||||
namespace io {
|
||||
namespace floatpump::io {
|
||||
|
||||
class RelayChannel {
|
||||
public:
|
||||
class RelayChannel {
|
||||
public:
|
||||
|
||||
enum state {
|
||||
ON, OFF
|
||||
};
|
||||
|
||||
RelayChannel(GPIO_TypeDef *gpio, uint16_t pin, bool inverted = false, state initial = state::OFF): m_gpio(gpio), m_gpio_pin(pin), m_inverted(inverted) {
|
||||
if(initial == state::ON)
|
||||
switchRelay(state::ON);
|
||||
};
|
||||
|
||||
void switchRelay(state st) {
|
||||
uint32_t elapesd = HAL_GetTick() - m_lastTick;
|
||||
|
||||
if(st == state::ON && m_state == state::OFF && getRemainingCooldown() == 0) {
|
||||
//Check if cooldown expired and enable Relay if true
|
||||
HAL_GPIO_WritePin(m_gpio, m_gpio_pin, (m_inverted) ? GPIO_PIN_RESET : GPIO_PIN_SET);
|
||||
m_state = state::ON;
|
||||
} else if(st == state::OFF && m_state == state::ON) {
|
||||
//Store last tick value when turning off
|
||||
//Always offer turning off!
|
||||
m_lastTick = HAL_GetTick();
|
||||
HAL_GPIO_WritePin(m_gpio, m_gpio_pin, (m_inverted) ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
m_state = state::OFF;
|
||||
}
|
||||
}
|
||||
|
||||
void setCooldown(uint16_t ms) {
|
||||
m_cooldown = ms;
|
||||
}
|
||||
|
||||
uint16_t getRemainingCooldown() {
|
||||
uint32_t elapsed = (HAL_GetTick() - m_lastTick);
|
||||
|
||||
if(elapsed <= m_cooldown)
|
||||
return m_cooldown - (HAL_GetTick() - m_lastTick);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
GPIO_TypeDef *m_gpio;
|
||||
uint16_t m_gpio_pin;
|
||||
|
||||
uint16_t m_cooldown = 0;
|
||||
|
||||
uint32_t m_lastTick = 0;
|
||||
|
||||
bool m_inverted = false;
|
||||
state m_state = state::OFF;
|
||||
enum state {
|
||||
ON, OFF
|
||||
};
|
||||
|
||||
} // floatpump
|
||||
RelayChannel(GPIO_TypeDef *gpio, uint16_t pin, bool inverted = false, state initial = state::OFF);;
|
||||
|
||||
void switchRelay(state st);
|
||||
|
||||
void setCooldown(uint16_t ms);
|
||||
|
||||
[[nodiscard]] uint16_t getRemainingCooldown() const;
|
||||
|
||||
|
||||
private:
|
||||
GPIO_TypeDef *m_gpio;
|
||||
uint16_t m_gpio_pin;
|
||||
|
||||
uint16_t m_cooldown = 0;
|
||||
|
||||
uint32_t m_lastTick = 0;
|
||||
|
||||
bool m_inverted = false;
|
||||
state m_state = state::OFF;
|
||||
};
|
||||
|
||||
} // io
|
||||
|
||||
#endif //FLOATPUMP_RELAYCHANNEL_H
|
||||
|
@ -6,5 +6,38 @@
|
||||
|
||||
namespace floatpump {
|
||||
namespace io {
|
||||
RelayChannel::RelayChannel(GPIO_TypeDef *gpio, uint16_t pin, bool inverted, RelayChannel::state initial)
|
||||
: m_gpio(gpio), m_gpio_pin(pin), m_inverted(inverted) {
|
||||
if (initial == state::ON)
|
||||
switchRelay(state::ON);
|
||||
}
|
||||
|
||||
void RelayChannel::switchRelay(RelayChannel::state st) {
|
||||
|
||||
if (st == state::ON && m_state == state::OFF && getRemainingCooldown() == 0) {
|
||||
//Check if cooldown expired and enable Relay if true
|
||||
HAL_GPIO_WritePin(m_gpio, m_gpio_pin, (m_inverted) ? GPIO_PIN_RESET : GPIO_PIN_SET);
|
||||
m_state = state::ON;
|
||||
} else if (st == state::OFF && m_state == state::ON) {
|
||||
//Store last tick value when turning off
|
||||
//Always offer turning off!
|
||||
m_lastTick = HAL_GetTick();
|
||||
HAL_GPIO_WritePin(m_gpio, m_gpio_pin, (m_inverted) ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
m_state = state::OFF;
|
||||
}
|
||||
}
|
||||
|
||||
void RelayChannel::setCooldown(uint16_t ms) {
|
||||
m_cooldown = ms;
|
||||
}
|
||||
|
||||
uint16_t RelayChannel::getRemainingCooldown() const {
|
||||
uint32_t elapsed = (HAL_GetTick() - m_lastTick);
|
||||
|
||||
if (elapsed <= m_cooldown)
|
||||
return m_cooldown - (HAL_GetTick() - m_lastTick);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
} // floatpump
|
||||
} // io
|
Loading…
Reference in New Issue
Block a user