Skip to content

Commit

Permalink
Removed Ticker-based pulses. This allows to change pulse time on the …
Browse files Browse the repository at this point in the history
…go (#816)
  • Loading branch information
xoseperez committed May 29, 2018
1 parent 53aa1d1 commit aaab65f
Show file tree
Hide file tree
Showing 4 changed files with 2,320 additions and 2,306 deletions.
Binary file modified code/espurna/data/index.html.gz
Binary file not shown.
26 changes: 20 additions & 6 deletions code/espurna/relay.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef struct {
unsigned long delay_off; // Delay to turn relay OFF
unsigned char pulse; // RELAY_PULSE_NONE, RELAY_PULSE_OFF or RELAY_PULSE_ON
unsigned long pulse_ms; // Pulse length in millis
unsigned long pulse_start; // Current pulse start (millis), 0 means no pulse

// Status variables

Expand All @@ -34,10 +35,6 @@ typedef struct {
bool report; // Whether to report to own topic
bool group_report; // Whether to report to group topic

// Helping objects

Ticker pulseTicker; // Holds the pulse back timer

} relay_t;
std::vector<relay_t> _relays;
bool _relayRecursive = false;
Expand Down Expand Up @@ -203,6 +200,21 @@ void _relayProcess(bool mode) {

}

/**
* Walks the relay vector check if any relay has to pulse back
*/
void _relayPulseCheck() {
unsigned long current_time = millis();
for (unsigned char id = 0; id < _relays.size(); id++) {
if (_relays[id].pulse_start > 0) {
if (current_time - _relays[id].pulse_start > _relays[id].pulse_ms) {
_relays[id].pulse_start = 0;
relayToggle(id);
}
}
}
}

// -----------------------------------------------------------------------------
// RELAY
// -----------------------------------------------------------------------------
Expand All @@ -218,10 +230,10 @@ void relayPulse(unsigned char id) {
bool pulseStatus = (mode == RELAY_PULSE_ON);

if (pulseStatus == status) {
_relays[id].pulseTicker.detach();
_relays[id].pulse_start = 0;
} else {
DEBUG_MSG_P(PSTR("[RELAY] Scheduling relay #%d back in %lums (pulse)\n"), id, ms);
_relays[id].pulseTicker.once_ms(ms, relayToggle, id);
_relays[id].pulse_start = millis();
}

}
Expand Down Expand Up @@ -464,6 +476,7 @@ void _relayBoot() {
}
_relays[i].current_status = !status;
_relays[i].target_status = status;
_relays[i].pulse_start = 0;
#if RELAY_PROVIDER == RELAY_PROVIDER_STM
_relays[i].change_time = millis() + 3000 + 1000 * i;
#else
Expand Down Expand Up @@ -821,6 +834,7 @@ void _relayInitCommands() {
//------------------------------------------------------------------------------

void _relayLoop() {
_relayPulseCheck();
_relayProcess(false);
_relayProcess(true);
}
Expand Down
Loading

0 comments on commit aaab65f

Please sign in to comment.