1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

Support class for blinking LED's

This commit is contained in:
Rhys Weatherley 2012-04-10 13:00:47 +10:00
parent c27a375f5e
commit fa144c9a87
5 changed files with 270 additions and 19 deletions

View File

@ -27,6 +27,8 @@ Nacelle lights with a 3 LED chase will have 4 LED's on each output - two in
each nacelle. each nacelle.
*/ */
#include <BlinkLED.h>
#define NAV_LIGHTS A2 // Red/green navigational lights #define NAV_LIGHTS A2 // Red/green navigational lights
#define STROBE_LIGHT A3 // Strobe light #define STROBE_LIGHT A3 // Strobe light
#define NACELLE_1 3 // Nacelle twirl chase LED 1 #define NACELLE_1 3 // Nacelle twirl chase LED 1
@ -37,11 +39,10 @@ each nacelle.
#define NACELLE_6 11 // Nacelle twirl chase LED 6 #define NACELLE_6 11 // Nacelle twirl chase LED 6
#define NACELLE_RATE A0 // Analog input that defines the rate of the nacelle chase #define NACELLE_RATE A0 // Analog input that defines the rate of the nacelle chase
#define NAV_LIGHTS_PERIOD 2000 #define NAV_LIGHTS_ON 1000
#define NAV_LIGHTS_OFF 1000 #define NAV_LIGHTS_OFF 1000
#define STROBE_LIGHT_PERIOD 900 #define STROBE_LIGHT_ON 70
#define STROBE_LIGHT_OFF 830 #define STROBE_LIGHT_OFF 830
#define NACELLE_PERIOD 500
#define NACELLE_MIN_PERIOD 25 #define NACELLE_MIN_PERIOD 25
#define NACELLE_MAX_PERIOD 250 #define NACELLE_MAX_PERIOD 250
#define NACELLE_DIM_VALUE 32 // Value for dimming previous LED in chase, 0..255 #define NACELLE_DIM_VALUE 32 // Value for dimming previous LED in chase, 0..255
@ -61,12 +62,11 @@ unsigned long lastNacelleTime = 0;
unsigned long nacellePeriod = 0; unsigned long nacellePeriod = 0;
byte index = 0; byte index = 0;
BlinkLED navLights(NAV_LIGHTS, NAV_LIGHTS_ON, NAV_LIGHTS_OFF);
BlinkLED strobeLight(STROBE_LIGHT, STROBE_LIGHT_ON, STROBE_LIGHT_OFF);
void setup() { void setup() {
// Configure the outputs and set them to be initially LOW. // Configure the outputs and set them to be initially LOW.
pinMode(NAV_LIGHTS, OUTPUT);
digitalWrite(NAV_LIGHTS, LOW);
pinMode(STROBE_LIGHT, OUTPUT);
digitalWrite(STROBE_LIGHT, LOW);
for (int index = 0; index < nacelleChaseLen; ++index) { for (int index = 0; index < nacelleChaseLen; ++index) {
byte pin = nacelleChase[index]; byte pin = nacelleChase[index];
pinMode(pin, OUTPUT); pinMode(pin, OUTPUT);
@ -93,17 +93,9 @@ void loop() {
// How long since the application started? // How long since the application started?
unsigned long sinceStart = millis() - startTime; unsigned long sinceStart = millis() - startTime;
// Update the navigation lights. // Update the navigation and strobe lights.
if ((sinceStart % NAV_LIGHTS_PERIOD) >= NAV_LIGHTS_OFF) navLights.loop();
digitalWrite(NAV_LIGHTS, HIGH); strobeLight.loop();
else
digitalWrite(NAV_LIGHTS, LOW);
// Update the strobe light.
if ((sinceStart % STROBE_LIGHT_PERIOD) >= STROBE_LIGHT_OFF)
digitalWrite(STROBE_LIGHT, HIGH);
else
digitalWrite(STROBE_LIGHT, LOW);
// Update the nacelle lights - uniform LED chase of length 1 to 6. // Update the nacelle lights - uniform LED chase of length 1 to 6.
if ((sinceStart - lastNacelleTime) >= nacellePeriod) { if ((sinceStart - lastNacelleTime) >= nacellePeriod) {

View File

@ -610,7 +610,7 @@ WARN_LOGFILE =
# directories like "/usr/src/myproject". Separate the files or directories # directories like "/usr/src/myproject". Separate the files or directories
# with spaces. # with spaces.
INPUT = ../libraries/FreetronicsLCD . INPUT = ../libraries/FreetronicsLCD ../libraries/BlinkLED .
# This tag can be used to specify the character encoding of the source files # This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is

View File

@ -0,0 +1,192 @@
/*
* Copyright (C) 2012 Southern Storm Software, Pty Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "BlinkLED.h"
#include <WProgram.h>
/**
* \class BlinkLED BlinkLED.h <BlinkLED.h>
* \brief Blink a LED on a digital output pin.
*
* BlinkLED simplies the process of blinking a LED by encapsulating the
* control logic into a single class. The following example strobes the
* status LED on D13 with a period of 70 milliseconds on, 930 milliseconds off
* (the LED is initially off):
*
* \code
* #include <BlinkLED.h>
*
* BlinkLED statusBlink(13, 70, 930);
*
* void setup() {}
*
* void loop() {
* statusBlink.loop();
* }
* \endcode
*
* The current state() of the LED can be changed immediately by calling
* setState(). The blink rate can be modified with setBlinkRate().
* And the blink cycle can be suspended and restarted with pause()
* and resume().
*/
/**
* \brief Initialize a blinking LED on the specified \a pin.
*
* The LED will blink with a rate defined by \a onTime and \a offTime
* (in milliseconds). Initially the LED's state is given by \a initialState,
* where true means initially on and false means initially off.
*/
BlinkLED::BlinkLED(uint8_t pin, unsigned long onTime, unsigned long offTime, bool initialState)
: _pin(pin)
, _state(initialState)
, _paused(false)
, _onTime(onTime)
, _offTime(offTime)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, initialState ? HIGH : LOW);
_lastChange = millis();
}
/**
* Perform a single iteration of the blink loop for this LED.
*/
void BlinkLED::loop()
{
if (_paused)
return;
unsigned long currentTime = millis();
if (_state) {
if ((currentTime - _lastChange) >= _onTime) {
digitalWrite(_pin, LOW);
_lastChange += _onTime;
_state = false;
}
} else {
if ((currentTime - _lastChange) >= _offTime) {
digitalWrite(_pin, HIGH);
_lastChange += _offTime;
_state = true;
}
}
}
/**
* \fn unsigned long BlinkLED::onTime() const
* \brief Returns the number of milliseconds the LED will be on.
*
* \sa offTime(), setBlinkRate()
*/
/**
* \fn unsigned long BlinkLED::offTime() const
* \brief Returns the number of milliseconds the LED will be off.
*
* \sa onTime(), setBlinkRate()
*/
/**
* \brief Sets the \a onTime and \a offTime (in milliseconds).
*
* The change takes effect immediately. If the current onTime() or
* offTime() has now expired, then the LED will immediately switch to
* the opposite state().
*
* \sa onTime(), offTime()
*/
void BlinkLED::setBlinkRate(unsigned long onTime, unsigned long offTime)
{
_onTime = onTime;
_offTime = offTime;
}
/**
* \fn bool BlinkLED::state() const
* \brief Returns the current state of the LED; true is on, false is off.
*
* \sa setState()
*/
/**
* \brief Sets the current \a state of the LED, where true is on, false is off.
*
* If the LED is already set to \a state, then it will complete its current
* cycle of onTime() or offTime(). Otherwise the LED is immediately set to
* \a state and a new cycle begins.
*
* \sa state()
*/
void BlinkLED::setState(bool state)
{
if (_state != state) {
digitalWrite(_pin, state ? HIGH : LOW);
_state = state;
_lastChange = millis();
}
}
/**
* \fn void BlinkLED::pause()
* \brief Pauses the LED blink cycle in its current state().
*
* \sa resume(), isPaused()
*/
/**
* \brief Resumes the LED blink cycle after a pause().
*
* The LED will complete its current onTime() or offTime() and then
* will switch to the opposite state(). If onTime() or offTime() has
* already expired, then the LED will immediately switch state.
*
* \sa pause(), isPaused()
*/
void BlinkLED::resume()
{
if (_paused) {
_paused = false;
unsigned long currentTime = millis();
if (_state) {
if ((currentTime - _lastChange) >= _onTime) {
digitalWrite(_pin, LOW);
_lastChange = currentTime;
_state = false;
}
} else {
if ((currentTime - _lastChange) >= _offTime) {
digitalWrite(_pin, HIGH);
_lastChange = currentTime;
_state = true;
}
}
}
}
/**
* \fn bool BlinkLED::isPaused()
* \brief Returns true if the LED blink cycle is paused; false otherwise.
*
* \sa pause(), resume()
*/

View File

@ -0,0 +1,55 @@
/*
* Copyright (C) 2012 Southern Storm Software, Pty Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef BlinkLED_h
#define BlinkLED_h
#include <inttypes.h>
class BlinkLED
{
public:
BlinkLED(uint8_t pin, unsigned long onTime, unsigned long offTime, bool initialState = false);
void loop();
unsigned long onTime() const { return _onTime; }
unsigned long offTime() const { return _offTime; }
void setBlinkRate(unsigned long onTime, unsigned long offTime);
bool state() const { return _state; }
void setState(bool state);
void pause() { _paused = true; }
void resume();
bool isPaused() const { return _paused; }
private:
uint8_t _pin;
bool _state;
bool _paused;
unsigned long _onTime;
unsigned long _offTime;
unsigned long _lastChange;
};
#endif

View File

@ -0,0 +1,12 @@
BlinkLED KEYWORD1
onTime KEYWORD2
offTime KEYWORD2
setBlinkRate KEYWORD2
state KEYWORD2
setState KEYWORD2
pause KEYWORD2
resume KEYWORD2
isPaused KEYWORD2