You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.3 KiB
C++

#include "TimedPin.h"
/***************************************************************************//**
* @brief Constructor
*******************************************************************************/
TimedPin::TimedPin(uint8_t pin_id, bool inverted) {
PinId = pin_id;
PinInvert = inverted;
PinMode = TPM_OFF;
OnTime = 0;
OffTime = 0;
}
void TimedPin::begin() {
pinMode(PinId, OUTPUT);
PinOff();
}
void TimedPin::loop() {
uint32_t t = millis();
int32_t tdif = t - RefTime;
switch (PinMode) {
case TPM_OFF:
break;
case TPM_ON:
break;
case TPM_BLINK: {
if (PinState) { // pin == on
if (tdif >= OnTime) { // on time expired
RefTime = t;
PinOff(); // toggle pin
}
} else { // pin == off
if (tdif >= OffTime) { // off time expired
RefTime = t;
PinOn(); // toggle pin
}
}
} break;
case TPM_TIMED_ON: {
if (tdif >= OnTime) { // on time expired
PinOff();
PinMode = TPM_OFF;
}
} break;
case TPM_TIMED_OFF: {
if (tdif >= OffTime) { // off time expired
PinOn();
PinMode = TPM_ON;
}
} break;
default: {
PinMode = TPM_OFF;
} break;
}
}
void TimedPin::OffTimed(uint32_t t) {
PinOff();
PinMode = TPM_TIMED_OFF;
RefTime = millis();
OffTime = t;
}
void TimedPin::OnTimed(uint32_t t) {
PinOn();
PinMode = TPM_TIMED_ON;
RefTime = millis();
OffTime = t;
}
void TimedPin::Blink(uint32_t t_on, uint32_t t_off) {
if (PinMode != TPM_BLINK) {
PinMode = TPM_BLINK;
PinOn();
}
RefTime = millis();
OnTime = t_on;
OffTime = t_off;
}
void TimedPin::On() {
PinOn();
PinMode = TPM_ON;
}
void TimedPin::Off() {
PinOff();
PinMode = TPM_OFF;
}
// =============================================================================
// Internal functions
// =============================================================================
void TimedPin::PinOn() {
PinState = true;
digitalWrite(PinId, PinInvert ? LOW : HIGH);
}
void TimedPin::PinOff() {
PinState = false;
digitalWrite(PinId, PinInvert ? HIGH : LOW);
}