/* FlashLed.cpp - Flash LED Class Implementation */ #include "FlashLed.h" /* setOnTime - Set Flash On Time void setOnTime (int onTime) Sets the amount of time the LED will stay on while flashing. The value is in milliseconds. */ void FlashLed::setOnTime (int onTime) { _onTime = onTime; } /* getOnTime - Obtain the Flash On Time int getOnTime () Retrieves the amount of time the LED will stay on while flashing. The value returned is in milliseconds. */ int FlashLed::getOnTime () { return _onTime; } /* setOffTime - Set Flash Off Time void setOffTime (int offTime) Sets the amount of time the LED will stay off while flashing. The value is in milliseconds. */ void FlashLed::setOffTime (int offTime) { _offTime = offTime; } /* getOffTime - Obtain the Flash Off Time int getOffTime () Retrieves the amount of time the LED will stay off while flashing. The value returned is in milliseconds. */ int FlashLed::getOffTime () { return _offTime; } /* on - Turn the LED On void on () The LED is turned on and stays on. Any flashing is cancelled by this function. */ void FlashLed::on () { _flashing = false; BASECLASS::on(); } /* off - Turn the LED Off void off () The LED is turned off and stays off. Any flashing is cancelled by this function. */ void FlashLed::off () { _flashing = false; BASECLASS::off(); } /* toggle - Change the State of the LED void toggle () If the LED is off, it is turned on. If the LED is on, it is turned off. Any flashing is cancelled by this function. */ void FlashLed::toggle () { _flashing = false; BASECLASS::toggle(); } /* flash - Flash the LED void flash () Begins flashing of the LED. Flashing continues indefinitely. */ void FlashLed::flash () { _flashing = true; _timer = millis(); BASECLASS::on(); _cycles = 0; } /* flash - Flash LED for Specified Number of Cycles void flash (int count) Flashes the LED for the specified number of cycles. A cycle is defined as the on time plus the off time. */ void FlashLed::flash (int count) { _count = count; flash(); } /* update - Time Based Update void update () Implements the flashing effect. First, the update routine of the base class is called so that other time based effects may occur (like fading). Then if the LED is flashing, the current time is checked against the on time or off time to determine if it is time to change to the other state. */ void FlashLed::update () { BASECLASS::update(); // BASECLASS update if (_flashing) { // LED is flashing unsigned long now = millis(); // get the current time if (_isOn) { // LED is on if (now - _timer >= _onTime) { // on time has elapsed _timer = now; // new start time BASECLASS::off(); // turn off the LED // toggle LED state } } else { // LED is off if (now - _timer >= _offTime) { // off time has elapsed ++_cycles; // another cycle completed if (_count == _cycles) { // counting cycles completed _count = 0; // reset request count _flashing = false; // stop flashing } else { // cycles not complete or not counting _timer = now; // new start time BASECLASS::on(); // turn on the LED } } } } }