Skip to content

Commit

Permalink
Merge pull request #123 from Legion2/timeout
Browse files Browse the repository at this point in the history
Added timeout to SoftwarePlayback
  • Loading branch information
Legion2 authored Apr 15, 2020
2 parents 5e483d0 + 01b114b commit 5d9594c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/FastLEDController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ int FastLEDController::animation_step_count(int duration, int steps) {
bool FastLEDController::updateLEDs() {
lastUpdate = currentUpdate;
currentUpdate = millis();
if (currentUpdate - lastCommand > LED_CONTROLLER_TIMEOUT) {
timeoutAction();
}

bool anyUpdate = false;

Expand Down Expand Up @@ -436,3 +439,10 @@ void FastLEDController::setLEDColorValues(uint8_t channel, uint8_t color, uint8_
void FastLEDController::clearLEDColorValues(uint8_t channel) {
memset(channelData[channel].valuesBuffer[0], 0, channelData[channel].ledCount);
}

void FastLEDController::timeoutAction() {
for (int channelId = 0; channelId < CHANNEL_NUM; channelId++) {
triggerSave |= setLEDMode(channelId, ChannelMode::HardwarePlayback);
}
saveIfNeeded();
}
8 changes: 8 additions & 0 deletions src/FastLEDController.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#define EEPROM_ADDRESS 4
#endif

#ifndef LED_CONTROLLER_TIMEOUT
#define LED_CONTROLLER_TIMEOUT 30000
#endif

/**
* The default LEDController. This controller uses the FastLED library to implement the Hardware Lighting effects. Also
* all RGB values of the LEDs are stored into CRGB arrays which can be used by the FastLED library to show them on the
Expand Down Expand Up @@ -154,4 +158,8 @@ class FastLEDController : public LEDController {
virtual void setLEDColorValues(uint8_t channel, uint8_t color, uint8_t offset, const uint8_t* values,
size_t len) override;
virtual void clearLEDColorValues(uint8_t channel) override;
/**
* This function is called when a timeout occurs.
*/
virtual void timeoutAction();
};
15 changes: 10 additions & 5 deletions src/LEDController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
#include "TemperatureController.h"

void LEDController::handleLEDControl(const Command& command, const CorsairLightingProtocolResponse* response) {
lastCommand = millis();
auto& data = command.data;
if (command.command == WRITE_LED_TRIGGER) {
triggerLEDUpdate();
if (triggerSave) {
triggerSave = false;
save();
}
saveIfNeeded();
} else {
if (data[0] >= CHANNEL_NUM) {
response->sendError();
Expand Down Expand Up @@ -232,4 +230,11 @@ bool LEDController::setLEDPortType(uint8_t channel, PortType ledPortType) {
return true;
}
return false;
}
}

bool LEDController::saveIfNeeded() {
if (triggerSave) {
triggerSave = false;
save();
}
}
8 changes: 8 additions & 0 deletions src/LEDController.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ class LEDController : public ILEDController {
* Indicates that the configuration of the channels has been changed and should be saved.
*/
bool triggerSave = false;
/**
* Stores the time at which the last command was received by the LEDController.
*/
long lastCommand = 0;

/**
* Trigger update of the LEDs
Expand Down Expand Up @@ -246,4 +250,8 @@ class LEDController : public ILEDController {
virtual bool clearLEDGroups(uint8_t channel);
virtual bool save() = 0;
virtual bool load() = 0;
/**
* Save if triggerSave is true and then reset triggerSave.
*/
bool saveIfNeeded();
};

0 comments on commit 5d9594c

Please sign in to comment.