ArduinoLibs
BlinkLED.cpp
00001 /*
00002  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00019  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00020  * DEALINGS IN THE SOFTWARE.
00021  */
00022 
00023 #include "BlinkLED.h"
00024 #if defined(ARDUINO) && ARDUINO >= 100
00025 #include <Arduino.h>
00026 #else
00027 #include <WProgram.h>
00028 #endif
00029 
00064 BlinkLED::BlinkLED(uint8_t pin, unsigned long onTime, unsigned long offTime, bool initialState)
00065     : _pin(pin)
00066     , _state(initialState)
00067     , _paused(false)
00068     , _onTime(onTime)
00069     , _offTime(offTime)
00070 {
00071     pinMode(pin, OUTPUT);
00072     digitalWrite(pin, initialState ? HIGH : LOW);
00073     _lastChange = millis();
00074 }
00075 
00079 void BlinkLED::loop()
00080 {
00081     if (_paused)
00082         return;
00083     unsigned long currentTime = millis();
00084     if (_state) {
00085         if ((currentTime - _lastChange) >= _onTime) {
00086             digitalWrite(_pin, LOW);
00087             _lastChange += _onTime;
00088             _state = false;
00089         }
00090     } else {
00091         if ((currentTime - _lastChange) >= _offTime) {
00092             digitalWrite(_pin, HIGH);
00093             _lastChange += _offTime;
00094             _state = true;
00095         }
00096     }
00097 }
00098 
00122 void BlinkLED::setBlinkRate(unsigned long onTime, unsigned long offTime)
00123 {
00124     _onTime = onTime;
00125     _offTime = offTime;
00126 }
00127 
00145 void BlinkLED::setState(bool state)
00146 {
00147     if (_state != state) {
00148         digitalWrite(_pin, state ? HIGH : LOW);
00149         _state = state;
00150         _lastChange = millis();
00151     }
00152 }
00153 
00170 void BlinkLED::resume()
00171 {
00172     if (_paused) {
00173         _paused = false;
00174         unsigned long currentTime = millis();
00175         if (_state) {
00176             if ((currentTime - _lastChange) >= _onTime) {
00177                 digitalWrite(_pin, LOW);
00178                 _lastChange = currentTime;
00179                 _state = false;
00180             }
00181         } else {
00182             if ((currentTime - _lastChange) >= _offTime) {
00183                 digitalWrite(_pin, HIGH);
00184                 _lastChange = currentTime;
00185                 _state = true;
00186             }
00187         }
00188     }
00189 }
00190 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator