From c74020bbe18aa4cc72e0c0ea846ec1fbea8191db Mon Sep 17 00:00:00 2001 From: i-kl <76922174+i-kl@users.noreply.github.com> Date: Sun, 17 Jan 2021 00:33:01 +0100 Subject: [PATCH] src directory and library.json added preparing for PlatformIO (extension) --- library.json | 18 ++ library.properties | 2 +- LedBlinker.cpp => src/LedBlinker.cpp | 312 +++++++++++++-------------- LedBlinker.h => src/LedBlinker.h | 278 ++++++++++++------------ 4 files changed, 314 insertions(+), 296 deletions(-) create mode 100644 library.json rename LedBlinker.cpp => src/LedBlinker.cpp (96%) rename LedBlinker.h => src/LedBlinker.h (97%) diff --git a/library.json b/library.json new file mode 100644 index 0000000..c7c06c6 --- /dev/null +++ b/library.json @@ -0,0 +1,18 @@ +{ + "name": "LedBlinker", + "version": "1.0.2", + "description": "The goal of this library is to provide a simple mechanism to control one or more status LEDs of your project: just configure the LED object (pin, level) and change the blinking pattern (as bitmask) and speed any time you want a new 'Tempo and Rhythm'.", + "keywords": "leds, arduino, arduino-library, led, blink, blinker, non-blocking, led-blink", + "repository": + { + "type": "git", + "url": "https://github.com/i-kl/LedBlinker" + }, + "authors": + { + "name": "Istvan Klezli", + "maintainer": true + }, + "license": "MIT", + "frameworks": "Arduino" +} diff --git a/library.properties b/library.properties index 193e690..036b063 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=LedBlinker -version=1.0.1 +version=1.0.2 author=i-kl maintainer=Istvan Klezli sentence=For blinking one or more LEDs. diff --git a/LedBlinker.cpp b/src/LedBlinker.cpp similarity index 96% rename from LedBlinker.cpp rename to src/LedBlinker.cpp index b2a9920..fd2276c 100644 --- a/LedBlinker.cpp +++ b/src/LedBlinker.cpp @@ -1,156 +1,156 @@ -/* -LedBlinker - an Arduino library just for flashing a (status) LED - -Created by Istvan Klezli, 2021-Jan-03 - -MIT License - -Copyright (c) 2021 Istvan Klezli - -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 "LedBlinker.h" - -#ifndef __AVR__ -std::vector LedBlinker::m_vecLeds; -#endif - -LedBlinker::LedBlinker(uint8_t iLedPin, ActiveLevel activeState) - : m_iLedPin{iLedPin}, - m_activeState{activeState}, - m_iPhaseTime_ms{DEFAULT_PHASE_TIME_MS}, - m_pattern{0}, - m_iPatternLength{DEFAULT_PATTERN_LENGTH}, - m_ledState{false}, - m_iPhase{0}, - m_iLastTime{0} -{ - // set to OFF - digitalWrite(m_iLedPin, m_activeState == ActiveLevel::ACTIVE_LOW ? HIGH : LOW); - pinMode(m_iLedPin, OUTPUT); - -#ifndef __AVR__ - m_vecLeds.push_back(this); -#endif -} - -LedBlinker::~LedBlinker() -{ -#ifndef __AVR__ - m_vecLeds.erase(std::remove(m_vecLeds.begin(), m_vecLeds.end(), this)); -#endif -} - -/// Sets the phase time in milliseconds -void LedBlinker::setPhaseTime(uint32_t iPhaseTime_ms /*= DEFAULT_PHASE_TIME_MS*/) -{ - m_iPhaseTime_ms = iPhaseTime_ms; - m_iLastTime = millis(); -} - -/// Sets the pattern (bit mask) and the number of useful bits in the pattern -void LedBlinker::setPattern (Pattern pattern, uint8_t iPatternLength /*= DEFAULT_PATTERN_LENGTH*/) -{ - setPattern(static_cast(pattern), iPatternLength); -} - -void LedBlinker::setPattern (uint32_t pattern, uint8_t iPatternLength /*= DEFAULT_PATTERN_LENGTH*/) -{ - if (iPatternLength > 32) - return; - - if (m_pattern != pattern || m_iPatternLength != iPatternLength) - { - m_pattern = pattern; - m_iPatternLength = iPatternLength; - m_iPhase = 0; - } -} - -/// Turns ON -void LedBlinker::setOn() -{ - setPattern(Pattern::ON); -} - -/// Turns OFF -void LedBlinker::setOff() -{ - setPattern(Pattern::OFF); -} - -// must be called periodically -// - e.g. in the Arduino's loop() function - in this case the phase time set is used -// - or e.g. by an interrupt routine - in this case the phase time should be set to zero -void LedBlinker::update() -{ - if (m_iPhaseTime_ms != 0) - { - uint32_t now = millis(); - if (now - m_iLastTime < m_iPhaseTime_ms) - return; // not yet - - m_iLastTime = now; - } - - uint32_t iMask = 1; - iMask <<= m_iPhase; - bool bNewLedState = (m_pattern & iMask) > 0; - - // is there a change? - if (m_ledState != bNewLedState) - { - // store the new state - m_ledState = bNewLedState; - - // change the polarity if needed - if (m_activeState == ActiveLevel::ACTIVE_LOW) - bNewLedState = !bNewLedState; - - // feed the output led - digitalWrite(m_iLedPin, bNewLedState ? HIGH : LOW); - } - - // increase the phase... - if (++m_iPhase >= m_iPatternLength) - m_iPhase = 0; -} - -#ifndef __AVR__ -/// Turns all ON -void LedBlinker::setAllOn() -{ - for (LedBlinker* led : m_vecLeds) - led->setOn(); -} - -/// Turns all OFF -void LedBlinker::setAllOff() -{ - for (LedBlinker* led : m_vecLeds) - led->setOff(); -} - -void LedBlinker::updateAll() -{ - for (LedBlinker* led : m_vecLeds) - led->update(); -} -#endif +/* +LedBlinker - an Arduino library just for flashing a (status) LED + +Created by Istvan Klezli, 2021-Jan-03 + +MIT License + +Copyright (c) 2021 Istvan Klezli + +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 "LedBlinker.h" + +#ifndef __AVR__ +std::vector LedBlinker::m_vecLeds; +#endif + +LedBlinker::LedBlinker(uint8_t iLedPin, ActiveLevel activeState) + : m_iLedPin{iLedPin}, + m_activeState{activeState}, + m_iPhaseTime_ms{DEFAULT_PHASE_TIME_MS}, + m_pattern{0}, + m_iPatternLength{DEFAULT_PATTERN_LENGTH}, + m_ledState{false}, + m_iPhase{0}, + m_iLastTime{0} +{ + // set to OFF + digitalWrite(m_iLedPin, m_activeState == ActiveLevel::ACTIVE_LOW ? HIGH : LOW); + pinMode(m_iLedPin, OUTPUT); + +#ifndef __AVR__ + m_vecLeds.push_back(this); +#endif +} + +LedBlinker::~LedBlinker() +{ +#ifndef __AVR__ + m_vecLeds.erase(std::remove(m_vecLeds.begin(), m_vecLeds.end(), this)); +#endif +} + +/// Sets the phase time in milliseconds +void LedBlinker::setPhaseTime(uint32_t iPhaseTime_ms /*= DEFAULT_PHASE_TIME_MS*/) +{ + m_iPhaseTime_ms = iPhaseTime_ms; + m_iLastTime = millis(); +} + +/// Sets the pattern (bit mask) and the number of useful bits in the pattern +void LedBlinker::setPattern (Pattern pattern, uint8_t iPatternLength /*= DEFAULT_PATTERN_LENGTH*/) +{ + setPattern(static_cast(pattern), iPatternLength); +} + +void LedBlinker::setPattern (uint32_t pattern, uint8_t iPatternLength /*= DEFAULT_PATTERN_LENGTH*/) +{ + if (iPatternLength > 32) + return; + + if (m_pattern != pattern || m_iPatternLength != iPatternLength) + { + m_pattern = pattern; + m_iPatternLength = iPatternLength; + m_iPhase = 0; + } +} + +/// Turns ON +void LedBlinker::setOn() +{ + setPattern(Pattern::ON); +} + +/// Turns OFF +void LedBlinker::setOff() +{ + setPattern(Pattern::OFF); +} + +// must be called periodically +// - e.g. in the Arduino's loop() function - in this case the phase time set is used +// - or e.g. by an interrupt routine - in this case the phase time should be set to zero +void LedBlinker::update() +{ + if (m_iPhaseTime_ms != 0) + { + uint32_t now = millis(); + if (now - m_iLastTime < m_iPhaseTime_ms) + return; // not yet + + m_iLastTime = now; + } + + uint32_t iMask = 1; + iMask <<= m_iPhase; + bool bNewLedState = (m_pattern & iMask) > 0; + + // is there a change? + if (m_ledState != bNewLedState) + { + // store the new state + m_ledState = bNewLedState; + + // change the polarity if needed + if (m_activeState == ActiveLevel::ACTIVE_LOW) + bNewLedState = !bNewLedState; + + // feed the output led + digitalWrite(m_iLedPin, bNewLedState ? HIGH : LOW); + } + + // increase the phase... + if (++m_iPhase >= m_iPatternLength) + m_iPhase = 0; +} + +#ifndef __AVR__ +/// Turns all ON +void LedBlinker::setAllOn() +{ + for (LedBlinker* led : m_vecLeds) + led->setOn(); +} + +/// Turns all OFF +void LedBlinker::setAllOff() +{ + for (LedBlinker* led : m_vecLeds) + led->setOff(); +} + +void LedBlinker::updateAll() +{ + for (LedBlinker* led : m_vecLeds) + led->update(); +} +#endif diff --git a/LedBlinker.h b/src/LedBlinker.h similarity index 97% rename from LedBlinker.h rename to src/LedBlinker.h index d1ac8a8..39f3b1b 100644 --- a/LedBlinker.h +++ b/src/LedBlinker.h @@ -1,139 +1,139 @@ -/* -LedBlinker - an Arduino library just for flashing a (status) LED - -Created by Istvan Klezli, 2021-Jan-03 - -MIT License - -Copyright (c) 2021 Istvan Klezli - -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. -*/ - -#pragma once - -#include "Arduino.h" -#ifndef __AVR__ -#include -#endif - - -/// One instance of this class can handle one LED attached to a given GPIO pin. -/// The pin number and the active high or low state must be defined in the constructor. -/// The blinking pattern and the phase time can be defined after the creation of the -/// LedBlinker instance. The update() method must be called periodically. -/// In case of more LedBlinker instances (e.g. LEDs on different GPIO pins) -/// the class methods updateAll(), setAllOn(), setAllOff() can be used in order to -/// refresh/command each LED instances (not for AVR based boards) -class LedBlinker -{ - -public: - -static const uint8_t DEFAULT_PATTERN_LENGTH = 20; /// the pattern has 20 used bits by default -static const uint32_t DEFAULT_PHASE_TIME_MS = 100; /// phase time 100ms ==> a pattern takes 20 x 100ms - /// = 2 seconds by default. See also setPhaseTime() - /// and setPattern() below. - -/// Some general predefined patterns (each 20-bit) -enum class Pattern : uint32_t -{ - ON = UINT32_MAX, - OFF = 0, - SPEED_MAX = 0b01010101010101010101, - SPEED_MEDIUM = 0b00110011001100110011, - SPEED_SLOW = 0b00000111110000011111, - SPEED_VERY_SLOW = 0b00000000001111111111, - ONE_SHORT_FLASH = 0b00000000000000000001, - ONE_LONG_FLASH = 0b00000000000000000011, - TWO_SHORT_FLASHES = 0b00000000010000000001, -}; - - - -/// Defines what output voltage level is needed at the GPIO pin which the LED is attached to. -enum class ActiveLevel : uint8_t -{ - ACTIVE_HIGH, /// active high LED (cathode grounded, anode switched, like Arduino's built-in LED usually) - ACTIVE_LOW, /// active low LED (anode on Vcc, cathode switched, like ESP moduls' built-in LED usually) -}; - - /// Constructor needs the GPIO pin number the LED attached to and also the LED's active state - LedBlinker(uint8_t iLedPin, ActiveLevel activeState); - - /// Destructor, removes the instance from the static class member vector holding all LED instances (not for AVR) - ~LedBlinker(); - - /// Sets the phase time in milliseconds. Phase time x pattern length = period, which is 2 seconds by default. - /// The timing will be done internally by checking the current uptime (returned by millis()) and the last - /// update time. If the phase time is set to zero, no internal timing is performed, but the phase next phase - /// (i.e. the next bit in the pattern) will be taken always. This mode is useful if the update() is already - /// called in a timed way, e.g. by an interrupt routine. - void setPhaseTime(uint32_t iPhaseTime_ms = DEFAULT_PHASE_TIME_MS); - - /// Sets the blinking pattern, which is etiher a predefined one (see above enum Pattern) - /// or an arbitrary, user defined one (consider for example a symbol from the Morse code table). - /// The second argument indicates the number of useful bits in the pattern (20 by default). - void setPattern (uint32_t pattern, uint8_t iPatternLength = DEFAULT_PATTERN_LENGTH); - void setPattern (Pattern pattern, uint8_t iPatternLength = DEFAULT_PATTERN_LENGTH); - - /// Sets the ON pattern - void setOn(); - - /// Sets the OFF pattern - void setOff(); - - /// This method sets the LED state based on the current blinking pattern - /// (e.g. fast blinking) and must be called periodically (100ms proposed, - /// see above the constant definitions). - void update(); - -#ifndef __AVR__ - /// Calls setAllOff() for each LedBlinker instance (not for AVR) - static void setAllOff(); - - /// Calls setOn() for each LedBlinker instance (not for AVR) - static void setAllOn(); - - /// Calls update() for each LedBlinker instance (not for AVR) - static void updateAll(); -#endif - -protected: - -#ifndef __AVR__ - // variables for handling ALL LEDs - static std::vector m_vecLeds; /// Keeps track of all LedBlinker instances -#endif - - // part of the configuration of a LED, can be defined in the constructor only - uint8_t m_iLedPin; /// configuration: the GPIO pin number - ActiveLevel m_activeState; /// configuration: shows whether the LED needs HIGH or LOW for activated state - - // these variables define the blinking behaviour - uint32_t m_iPhaseTime_ms; /// phase time in milliseconds (Arduino function millis used, if set to zero, no waiting) - uint32_t m_pattern; /// current blinking pattern - uint8_t m_iPatternLength; /// number of used bits in the blinking pattern - - // these are internal variables - bool m_ledState; /// LED's current state (on or off) - uint8_t m_iPhase; /// current blinking phase - uint32_t m_iLastTime; /// timestamp of the last blinking (Arduino function millis used) - -}; +/* +LedBlinker - an Arduino library just for flashing a (status) LED + +Created by Istvan Klezli, 2021-Jan-03 + +MIT License + +Copyright (c) 2021 Istvan Klezli + +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. +*/ + +#pragma once + +#include "Arduino.h" +#ifndef __AVR__ +#include +#endif + + +/// One instance of this class can handle one LED attached to a given GPIO pin. +/// The pin number and the active high or low state must be defined in the constructor. +/// The blinking pattern and the phase time can be defined after the creation of the +/// LedBlinker instance. The update() method must be called periodically. +/// In case of more LedBlinker instances (e.g. LEDs on different GPIO pins) +/// the class methods updateAll(), setAllOn(), setAllOff() can be used in order to +/// refresh/command each LED instances (not for AVR based boards) +class LedBlinker +{ + +public: + +static const uint8_t DEFAULT_PATTERN_LENGTH = 20; /// the pattern has 20 used bits by default +static const uint32_t DEFAULT_PHASE_TIME_MS = 100; /// phase time 100ms ==> a pattern takes 20 x 100ms + /// = 2 seconds by default. See also setPhaseTime() + /// and setPattern() below. + +/// Some general predefined patterns (each 20-bit) +enum class Pattern : uint32_t +{ + ON = UINT32_MAX, + OFF = 0, + SPEED_MAX = 0b01010101010101010101, + SPEED_MEDIUM = 0b00110011001100110011, + SPEED_SLOW = 0b00000111110000011111, + SPEED_VERY_SLOW = 0b00000000001111111111, + ONE_SHORT_FLASH = 0b00000000000000000001, + ONE_LONG_FLASH = 0b00000000000000000011, + TWO_SHORT_FLASHES = 0b00000000010000000001, +}; + + + +/// Defines what output voltage level is needed at the GPIO pin which the LED is attached to. +enum class ActiveLevel : uint8_t +{ + ACTIVE_HIGH, /// active high LED (cathode grounded, anode switched, like Arduino's built-in LED usually) + ACTIVE_LOW, /// active low LED (anode on Vcc, cathode switched, like ESP moduls' built-in LED usually) +}; + + /// Constructor needs the GPIO pin number the LED attached to and also the LED's active state + LedBlinker(uint8_t iLedPin, ActiveLevel activeState); + + /// Destructor, removes the instance from the static class member vector holding all LED instances (not for AVR) + ~LedBlinker(); + + /// Sets the phase time in milliseconds. Phase time x pattern length = period, which is 2 seconds by default. + /// The timing will be done internally by checking the current uptime (returned by millis()) and the last + /// update time. If the phase time is set to zero, no internal timing is performed, but the phase next phase + /// (i.e. the next bit in the pattern) will be taken always. This mode is useful if the update() is already + /// called in a timed way, e.g. by an interrupt routine. + void setPhaseTime(uint32_t iPhaseTime_ms = DEFAULT_PHASE_TIME_MS); + + /// Sets the blinking pattern, which is etiher a predefined one (see above enum Pattern) + /// or an arbitrary, user defined one (consider for example a symbol from the Morse code table). + /// The second argument indicates the number of useful bits in the pattern (20 by default). + void setPattern (uint32_t pattern, uint8_t iPatternLength = DEFAULT_PATTERN_LENGTH); + void setPattern (Pattern pattern, uint8_t iPatternLength = DEFAULT_PATTERN_LENGTH); + + /// Sets the ON pattern + void setOn(); + + /// Sets the OFF pattern + void setOff(); + + /// This method sets the LED state based on the current blinking pattern + /// (e.g. fast blinking) and must be called periodically (100ms proposed, + /// see above the constant definitions). + void update(); + +#ifndef __AVR__ + /// Calls setAllOff() for each LedBlinker instance (not for AVR) + static void setAllOff(); + + /// Calls setOn() for each LedBlinker instance (not for AVR) + static void setAllOn(); + + /// Calls update() for each LedBlinker instance (not for AVR) + static void updateAll(); +#endif + +protected: + +#ifndef __AVR__ + // variables for handling ALL LEDs + static std::vector m_vecLeds; /// Keeps track of all LedBlinker instances +#endif + + // part of the configuration of a LED, can be defined in the constructor only + uint8_t m_iLedPin; /// configuration: the GPIO pin number + ActiveLevel m_activeState; /// configuration: shows whether the LED needs HIGH or LOW for activated state + + // these variables define the blinking behaviour + uint32_t m_iPhaseTime_ms; /// phase time in milliseconds (Arduino function millis used, if set to zero, no waiting) + uint32_t m_pattern; /// current blinking pattern + uint8_t m_iPatternLength; /// number of used bits in the blinking pattern + + // these are internal variables + bool m_ledState; /// LED's current state (on or off) + uint8_t m_iPhase; /// current blinking phase + uint32_t m_iLastTime; /// timestamp of the last blinking (Arduino function millis used) + +};