Added asynchronous non-blocking LED blinking.
This makes use of the Blinker library: https://github.com/MajenkoLibraries/Blinker
This commit is contained in:
parent
fae5de0bed
commit
4e3b67d764
9 changed files with 195 additions and 4 deletions
|
|
@ -1,9 +1,11 @@
|
||||||
#include "DigiKeyboard.h"
|
#include "DigiKeyboard.h"
|
||||||
|
#include <Blinker.h>
|
||||||
#include <ezButton.h>
|
#include <ezButton.h>
|
||||||
|
|
||||||
int ACTION_PIN = 0;
|
int ACTION_PIN = 0;
|
||||||
int LED_PIN = 1;
|
int LED_PIN = 1;
|
||||||
|
|
||||||
|
Blinker blinker(LED_PIN);
|
||||||
ezButton button(ACTION_PIN);
|
ezButton button(ACTION_PIN);
|
||||||
|
|
||||||
char *passwords[] = {
|
char *passwords[] = {
|
||||||
|
|
@ -14,10 +16,9 @@ char *passwords[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(LED_PIN, OUTPUT);
|
//pinMode(LED_PIN, OUTPUT);
|
||||||
digitalWrite(LED_PIN, LOW);
|
blinker.setDelay(25, 10000);
|
||||||
delay(500);
|
blinker.start();
|
||||||
digitalWrite(LED_PIN, HIGH);
|
|
||||||
//pinMode(ACTION_PIN, INPUT);
|
//pinMode(ACTION_PIN, INPUT);
|
||||||
button.setCountMode(COUNT_RISING);
|
button.setCountMode(COUNT_RISING);
|
||||||
button.setDebounceTime(0);
|
button.setDebounceTime(0);
|
||||||
|
|
@ -26,6 +27,7 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
blinker.blink();
|
||||||
button.loop();
|
button.loop();
|
||||||
|
|
||||||
/*digitalWrite(LED_PIN, HIGH);
|
/*digitalWrite(LED_PIN, HIGH);
|
||||||
|
|
|
||||||
44
libraries/Blinker/Blinker.cpp
Normal file
44
libraries/Blinker/Blinker.cpp
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
#include <Blinker.h>
|
||||||
|
|
||||||
|
Blinker::Blinker(int pin) {
|
||||||
|
_blinkPin = pin;
|
||||||
|
_highDelay = 500;
|
||||||
|
_lowDelay = 500;
|
||||||
|
_lastBlink = 0;
|
||||||
|
_blinkState = false;
|
||||||
|
_running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Blinker::setDelay(uint32_t d) {
|
||||||
|
_highDelay = d;
|
||||||
|
_lowDelay = d;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Blinker::setDelay(uint32_t h, uint32_t l) {
|
||||||
|
_highDelay = h;
|
||||||
|
_lowDelay = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Blinker::blink() {
|
||||||
|
uint32_t diff = millis() - _lastBlink;
|
||||||
|
uint32_t blinkDelay = _blinkState ? _highDelay : _lowDelay;
|
||||||
|
if (diff >= blinkDelay) {
|
||||||
|
_lastBlink = millis() - (diff - blinkDelay); // Account for any delay in calling blink
|
||||||
|
_blinkState = !_blinkState;
|
||||||
|
if (_running) {
|
||||||
|
digitalWrite(_blinkPin, _blinkState ? HIGH : LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Blinker::start() {
|
||||||
|
pinMode(_blinkPin, OUTPUT);
|
||||||
|
_running = true;
|
||||||
|
_lastBlink = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Blinker::stop(bool idleState) {
|
||||||
|
_running = false;
|
||||||
|
_blinkState = idleState;
|
||||||
|
digitalWrite(_blinkPin, _blinkState);
|
||||||
|
}
|
||||||
28
libraries/Blinker/Blinker.h
Normal file
28
libraries/Blinker/Blinker.h
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
#ifndef _BLINKER_H
|
||||||
|
#define _BLINKER_H
|
||||||
|
|
||||||
|
#if (ARDUINO >= 100)
|
||||||
|
# include <Arduino.h>
|
||||||
|
#else
|
||||||
|
# include <WProgram.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class Blinker {
|
||||||
|
private:
|
||||||
|
int _blinkPin;
|
||||||
|
uint32_t _highDelay;
|
||||||
|
uint32_t _lowDelay;
|
||||||
|
uint32_t _lastBlink;
|
||||||
|
boolean _blinkState;
|
||||||
|
boolean _running;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Blinker(int pin);
|
||||||
|
void setDelay(uint32_t d);
|
||||||
|
void setDelay(uint32_t h, uint32_t l);
|
||||||
|
void blink();
|
||||||
|
void start();
|
||||||
|
void stop(bool idleState = LOW);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
30
libraries/Blinker/LICENSE.md
Normal file
30
libraries/Blinker/LICENSE.md
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
|
||||||
|
Copyright (c) 2015, Majenko Technologies
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer in the documentation and/or
|
||||||
|
other materials provided with the distribution.
|
||||||
|
|
||||||
|
* Neither the name of Majenko Technologies nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
|
||||||
9
libraries/Blinker/README.md
Normal file
9
libraries/Blinker/README.md
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
Blinker Library
|
||||||
|
===============
|
||||||
|
|
||||||
|
This very simple little library will cause an output
|
||||||
|
to "blink" - that is repeatedly go HIGH then LOW with a
|
||||||
|
requested delay.
|
||||||
|
|
||||||
|
The operation is completely non-blocking, so you can
|
||||||
|
blink an output while doing other things.
|
||||||
54
libraries/Blinker/examples/BlinkTest/BlinkTest.ino
Normal file
54
libraries/Blinker/examples/BlinkTest/BlinkTest.ino
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2015, Majenko Technologies
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
* are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer in the documentation and/or
|
||||||
|
* other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Majenko Technologies nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Blinker.h>
|
||||||
|
|
||||||
|
// Most boards have an LED on pin 13. Let's blink it.
|
||||||
|
Blinker blinker(13);
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// The blinker defaults to 500ms delay, which is a 1 second period blinking.
|
||||||
|
// Let's make it faster - say 250ms delay.
|
||||||
|
blinker.setDelay(250);
|
||||||
|
|
||||||
|
// There is also an asymetric blink delay version which takes two delay values,
|
||||||
|
// one for the ON time and one for the OFF time:
|
||||||
|
// blinker.setDelay(50, 950);
|
||||||
|
|
||||||
|
// Start the blinker blinking. There's also a blinker.stop() function.
|
||||||
|
blinker.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// As long as you keep calling this method the output will toggle
|
||||||
|
// with the requested delay. It's non-blocking so you can do othe
|
||||||
|
// things while blinking.
|
||||||
|
blinker.blink();
|
||||||
|
}
|
||||||
13
libraries/Blinker/library.json
Normal file
13
libraries/Blinker/library.json
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"name": "Blinker",
|
||||||
|
"keywords": "led, blink",
|
||||||
|
"description": "Non-blocking LED blinking class",
|
||||||
|
"repository":
|
||||||
|
{
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/MajenkoLibraries/Blinker"
|
||||||
|
},
|
||||||
|
"version": "1.0.3",
|
||||||
|
"frameworks": "arduino",
|
||||||
|
"platforms": "*"
|
||||||
|
}
|
||||||
10
libraries/Blinker/library.properties
Normal file
10
libraries/Blinker/library.properties
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
name=Blinker
|
||||||
|
version=1.0.4
|
||||||
|
author=Matt Jenkins
|
||||||
|
maintainer=Matt Jenkins <matt@majenko.co.uk>
|
||||||
|
sentence=Blink an output in different patterns
|
||||||
|
paragraph=Blinker Library\n===============\n\nThis very simple little library will cause an output\nto "blink" - that is repeatedly go HIGH then LOW with a \nrequested delay.\n\nThe operation is completely non-blocking, so you can\nblink an output while doing other things.\n
|
||||||
|
url=https://github.com/MajenkoLibraries/Blinker
|
||||||
|
category=Signal Input/Output
|
||||||
|
architectures=*
|
||||||
|
includes=Blinker.h
|
||||||
1
libraries/Blinker/uecide.json
Normal file
1
libraries/Blinker/uecide.json
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
{"name":"Blinker","provides":"Blinker.h","license":"LICENSE.md","readme":"README.md","description":"Blink an output in different patterns","category":"IO","subcategory":"Digital","maintainer":"Matt Jenkins <matt@majenko.co.uk>","version":"1.0.4","family":"all","core":"all","install":{"examples":"\/","library.properties":"\/","src":"\/"}}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue