First version with a button and added the Blinker library to the source root.

This commit is contained in:
Johannes Findeisen 2022-11-29 03:51:52 +01:00
commit 15e33b531f
9 changed files with 246 additions and 31 deletions

View 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);
}

View 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

View 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.

View 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.

View 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();
}

View 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": "*"
}

View 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

View 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":"\/"}}

View file

@ -1,56 +1,82 @@
#include "Blinker.h"
#include "DigiKeyboard.h"
#include "ezButton.h"
int ACTION_PIN = 0;
int LED_PIN = 1;
// the button pin
#define BUTTON_PIN 0
// the LED pin
#define LED_PIN 1
// the pause after last button press before executing a command
#define PAUSE 2000
// local variables
unsigned long last;
unsigned int count;
// create blinker
Blinker blinker(LED_PIN);
ezButton button(ACTION_PIN);
char *passwords[] = {
"0qMpI7Af06Z4XnMDhzXROnEgtuxPsCPLiBJT90Rb1F2BZKCmIhpLOdxtBpurUa9Tg",
"1qMpI7Af06Z4XnMDhzXROnEgtuxPsCPLiBJT90Rb1F2BZKCmIhpLOdxtBpurUa9Tg",
"2qMpI7Af06Z4XnMDhzXROnEgtuxPsCPLiBJT90Rb1F2BZKCmIhpLOdxtBpurUa9Tg",
"3qMpI7Af06Z4XnMDhzXROnEgtuxPsCPLiBJT90Rb1F2BZKCmIhpLOdxtBpurUa9Tg"
// the list of available passwords
const char *passwords[] = {
"PASSWORD1",
"PASSWORD2",
"PASSWORD3",
"PASSWORD4"
};
void setup() {
// set button pin to high
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH);
// light up the LED for a second to show that passkey is ready
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
// set blink effect (ON, OFF) in milliseconds
blinker.setDelay(25, 10000);
blinker.start();
button.setCountMode(COUNT_RISING);
button.setDebounceTime(0);
// initialize HID
DigiKeyboard.delay(0);
DigiKeyboard.sendKeyStroke(0);
}
void loop() {
// start LED blink effect
blinker.blink();
button.loop();
/*digitalWrite(LED_PIN, HIGH);
delay(25);
digitalWrite(1, LOW);
delay(10000);*/
if(button.isPressed()) {
//DigiKeyboard.println("Hello World!");
/*DigiKeyboard.println(passwords[0]);
DigiKeyboard.println(passwords[1]);
DigiKeyboard.println(passwords[2]);
DigiKeyboard.println(passwords[3]);*/
// read button state
if (digitalRead(BUTTON_PIN) == LOW) {
// button is pressed
last = millis();
count++;
// wait until button is released
while(digitalRead(BUTTON_PIN) == LOW) {
delay(1);
}
}
unsigned long count = button.getCount();
// press button five times to get a password
if(count == 5) {
//DigiKeyboard.println(count);
DigiKeyboard.println(passwords[0]);
button.resetCount();
// wait PAUSE time before executing a command
if (count > 0 && (millis() - last) >= PAUSE) {
// the switch case is the number of button presses
switch(count) {
case 1:
DigiKeyboard.println(passwords[0]);
break;
case 2:
DigiKeyboard.println(passwords[1]);
break;
case 3:
DigiKeyboard.println(passwords[2]);
break;
case 5:
DigiKeyboard.println(passwords[3]);
break;
default:
break;
}
count = 0; // reset the counter
}
}