feat: 全量同步 254 个常用的 Arduino 扩展库文件
This commit is contained in:
55
arduino-libs/arduino-cli/libraries/OneButton/CHANGELOG.md
Normal file
55
arduino-libs/arduino-cli/libraries/OneButton/CHANGELOG.md
Normal file
@@ -0,0 +1,55 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file starting 2021.
|
||||
|
||||
## [2.0.1] - 2021-01-31
|
||||
|
||||
* Compiler warning removed
|
||||
* Documentation
|
||||
|
||||
## [2.0.0] - 2021-01-22
|
||||
|
||||
* CHANGELOG created.
|
||||
* Many thanks to the improvements included from #27 (**@aslobodyanuk**), #59 (**@ShaggyDog18**) and #73 (**@geeksville**).
|
||||
|
||||
This is a major update with breaking changes.
|
||||
|
||||
The **states** are re-factored to support counting the clicks.
|
||||
|
||||
By design only one of the events (click, doubleClick, MultiClick) are triggered within one interaction.
|
||||
As a consequence a single-click interaction is detected after waiting some milliseconds (see setClickTicks()) without another click happening;
|
||||
Only if you have not attached any double-click event function the waiting time can be skipped.
|
||||
|
||||
Detecting a long 'down' not only works with the first but always as the last click.
|
||||
|
||||
The number of actual clicks can be retrieved from the library any time.
|
||||
|
||||
The function **getPressedTicks()** was removed. See example SimpleOneButton on how to get that time by using attachLongPressStart to save starting time.
|
||||
|
||||
The function **attachPressStart()** is removed as **attachLongPressStart()** does the same but also supports parameters.
|
||||
|
||||
One additional feature has been added not to call the event functions from the interrupt routine and detect
|
||||
the need for event functions to be called only when the tick() function is called from the main loop() method.
|
||||
This is because some boards and processors do not support timing or Serial functions (among others) from interrupt routines.
|
||||
|
||||
The function **isIdle()** was added to allow detect a current interaction.
|
||||
|
||||
The library now supports to detect multiple (>2) clicks in a row using **attachMultiClick()** .
|
||||
|
||||
|
||||
* The internal _state is using enum instead of plain numbers to make the library more readable.
|
||||
* functions that had been marked deprecated are now removed. (attachPress->attachLongPressXXX)
|
||||
* added const to constant parameters to enable meaningful compiler warnings.
|
||||
* added code for de-bouncing double clicks from pull 27.
|
||||
* added isIdle() function to find out that the internal state is `init`.
|
||||
|
||||
|
||||
### Examples
|
||||
|
||||
* Examples run on NodeMCU boards. (the library worked already).
|
||||
|
||||
* The **SimpleOneButton.ino** example got some cleanup and definition to be used with ESP8266 boards as well.
|
||||
|
||||
* The **InterruptOneButton.ino** example now is using attachInterrupt instead of UNO specific register modifications.
|
||||
|
||||
* The **SpecialInput.ino** example was added to shpow how to use the OneButton algorythm and input pattern recognition with your own source of input.
|
||||
15
arduino-libs/arduino-cli/libraries/OneButton/LICENSE
Normal file
15
arduino-libs/arduino-cli/libraries/OneButton/LICENSE
Normal file
@@ -0,0 +1,15 @@
|
||||
See http://www.mathertel.de/License.aspx
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2005-2014 by Matthias Hertel, http://www.mathertel.de/
|
||||
|
||||
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 the copyright owners 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 OWNER 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.
|
||||
160
arduino-libs/arduino-cli/libraries/OneButton/README.md
Normal file
160
arduino-libs/arduino-cli/libraries/OneButton/README.md
Normal file
@@ -0,0 +1,160 @@
|
||||
# Arduino OneButton Library
|
||||
|
||||
This Arduino library is improving the usage of a singe button for input.
|
||||
It shows how to use an digital input pin with a single pushbutton attached
|
||||
for detecting some of the typical button press events like single clicks, double clicks and long-time pressing.
|
||||
This enables you to reuse the same button for multiple functions and lowers the hardware investments.
|
||||
|
||||
This is also a sample for implementing simple finite-state machines by using the simple pattern above.
|
||||
|
||||
You can find more details on this library at
|
||||
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
|
||||
|
||||
The change log of this library can be found in [CHANGELOG](CHANGELOG.md).
|
||||
|
||||
|
||||
## Getting Started
|
||||
|
||||
Clone this repository into `Arduino/Libraries` or use the built-in Arduino IDE Library manager to install
|
||||
a copy of this library. You can find more detail about installing libraries
|
||||
[here, on Arduino's website](https://www.arduino.cc/en/guide/libraries).
|
||||
|
||||
```CPP
|
||||
#include <Arduino.h>
|
||||
#include <OneButton.h>
|
||||
```
|
||||
|
||||
Each physical button requires its own `OneButton` instance. You can initialize them like this:
|
||||
|
||||
|
||||
### Initialize a Button to GND
|
||||
|
||||
```CPP
|
||||
#define BUTTON_PIN 4
|
||||
|
||||
/**
|
||||
* Initialize a new OneButton instance for a button
|
||||
* connected to digital pin 4 and GND, which is active low
|
||||
* and uses the internal pull-up resistor.
|
||||
*/
|
||||
|
||||
OneButton btn = OneButton(
|
||||
BUTTON_PIN, // Input pin for the button
|
||||
true, // Button is active LOW
|
||||
true // Enable internal pull-up resistor
|
||||
);
|
||||
```
|
||||
|
||||
|
||||
### Initialize a Button to VCC
|
||||
|
||||
```CPP
|
||||
#define BUTTON_PIN 4
|
||||
|
||||
/**
|
||||
* Initialize a new OneButton instance for a button
|
||||
* connected to digital pin 4, which is active high.
|
||||
* As this does not use any internal resistor
|
||||
* an external resistor (4.7k) may be required to create a LOW signal when the button is not pressed.
|
||||
*/
|
||||
|
||||
OneButton btn = OneButton(
|
||||
BUTTON_PIN, // Input pin for the button
|
||||
false, // Button is active high
|
||||
false // Disable internal pull-up resistor
|
||||
);
|
||||
```
|
||||
|
||||
|
||||
### Attach State Events
|
||||
|
||||
Once you have your button initialized, you can handle events by attaching them to the button
|
||||
instance. Events can either be static functions or lambdas (without captured variables).
|
||||
|
||||
```CPP
|
||||
// Handler function for a single click:
|
||||
static void handleClick() {
|
||||
Serial.println("Clicked!");
|
||||
}
|
||||
|
||||
// Single Click event attachment
|
||||
btn.attachClick(handleClick);
|
||||
|
||||
// Double Click event attachment with lambda
|
||||
btn.attachDoubleClick([]() {
|
||||
Serial.println("Double Pressed!");
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
### Don't forget to `tick()`!
|
||||
|
||||
In order for `OneButton` to work correctly, you must call `tick()` on __each button instance__
|
||||
within your main `loop()`. If you're not getting any button events, this is probably why.
|
||||
|
||||
```CPP
|
||||
void loop() {
|
||||
btn.tick();
|
||||
|
||||
// Do other things...
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## State Events
|
||||
|
||||
Here's a full list of events handled by this library:
|
||||
|
||||
| Attach Function | Description |
|
||||
| ----------------------- | ------------------------------------------------------ |
|
||||
| `attachClick` | Fires as soon as a single click is detected. |
|
||||
| `attachDoubleClick` | Fires as soon as a double click is detected. |
|
||||
| `attachMultiClick` | Fires as soon as multiple clicks have been detected. |
|
||||
| `attachLongPressStart` | Fires as soon as the button is held down for 1 second. |
|
||||
| `attachDuringLongPress` | Fires periodically as long as the button is held down. |
|
||||
| `attachLongPressStop` | Fires when the button is released after a long hold. |
|
||||
|
||||
|
||||
### Event Timing
|
||||
|
||||
Valid events occur when `tick()` is called after a specified number of milliseconds. You can use
|
||||
the following functions to change the timing.
|
||||
|
||||
**Note:** Attaching a double click will increase the delay for detecting a single click. If a double
|
||||
click event is not attached, the library will assume a valid single click after one click duration,
|
||||
otherwise it must wait for the double click timeout to pass.
|
||||
|
||||
| Function | Default | Description |
|
||||
| ----------------------- | ---------- | ------------------------------------------------------------- |
|
||||
| `setDebounceTicks(int)` | `50 msec` | Period of time in which to ignore additional level changes. |
|
||||
| `setClickTicks(int)` | `500 msec` | Timeout used to distinguish single clicks from double clicks. |
|
||||
| `setPressTicks(int)` | `800 msec` | Duration to hold a button to trigger a long press. |
|
||||
|
||||
You may change these default values but be aware that when you specify too short times
|
||||
it is hard to click twice or you will create a press instead of a click.
|
||||
|
||||
|
||||
### Additional Functions
|
||||
|
||||
`OneButton` also provides a couple additional functions to use for querying button status:
|
||||
|
||||
| Function | Description |
|
||||
| ----------------------- | ------------------------------------------------------------------------------ |
|
||||
| `bool isLongPressed()` | Detect whether or not the button is currently inside a long press. |
|
||||
| `int getPressedTicks()` | Get the current number of milliseconds that the button has been held down for. |
|
||||
|
||||
|
||||
### `tick()` and `reset()`
|
||||
|
||||
You can specify a logic level when calling `tick(bool)`, which will skip reading the pin and use
|
||||
that level instead. If you wish to reset the internal state of your buttons, call `reset()`.
|
||||
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
If your buttons aren't acting they way they should, check these items:
|
||||
|
||||
1. Check your wiring and pin numbers.
|
||||
2. Did you call `tick()` on each button instance in your loop?
|
||||
3. Did you alter your clock timers in any way without adjusting ticks?
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
BlinkMachine.ino
|
||||
|
||||
This is a sample sketch to show how to use the OneButtonLibrary to detect double-click events on a button.
|
||||
|
||||
Copyright (c) by Matthias Hertel, http://www.mathertel.de
|
||||
This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
|
||||
More information on: http://www.mathertel.de/Arduino
|
||||
|
||||
The library internals are explained at
|
||||
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
|
||||
|
||||
Setup a test circuit:
|
||||
* Connect a pushbutton to pin A1 (Uno) or D3 (ESP8266) and ground.
|
||||
* The pin 13 (UNO) or D4 (ESP8266) is used for output attach a led and resistor to ground
|
||||
or see the built-in led on the standard arduino board.
|
||||
|
||||
The Sketch shows how to setup the library and bind a "machine" that can blink the LED slow or fast.
|
||||
A click on the button turns the led on.
|
||||
A doubleclick on the button changes the blink rate from ON to SLOW to FAST and back.
|
||||
In the loop function the button.tick function has to be called as often as you like.
|
||||
|
||||
State-Diagram
|
||||
|
||||
start
|
||||
| +-------\
|
||||
V V |
|
||||
-------- ------ |
|
||||
| OFF |<--click-+->| ON | |
|
||||
-------- | ------ |
|
||||
| | |
|
||||
| d-click |
|
||||
| | |
|
||||
| V |
|
||||
| ------ |
|
||||
+- | SLOW | |
|
||||
| ------ |
|
||||
| | |
|
||||
| d-click |
|
||||
| | |
|
||||
| V d-click
|
||||
| ------ |
|
||||
+--| FAST |---/
|
||||
------
|
||||
*/
|
||||
|
||||
// 06.10.2012 created by Matthias Hertel
|
||||
// 26.03.2017 state diagram added, minor changes
|
||||
|
||||
#include "OneButton.h"
|
||||
|
||||
// The actions I ca do...
|
||||
typedef enum {
|
||||
ACTION_OFF, // set LED "OFF".
|
||||
ACTION_ON, // set LED "ON"
|
||||
ACTION_SLOW, // blink LED "SLOW"
|
||||
ACTION_FAST // blink LED "FAST"
|
||||
}
|
||||
MyActions;
|
||||
|
||||
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
|
||||
// Example for Arduino UNO with input button on pin 2 and builtin LED on pin 13
|
||||
#define PIN_INPUT A1
|
||||
#define PIN_LED 13
|
||||
|
||||
#elif defined(ESP8266)
|
||||
// Example for NodeMCU with input button using FLASH button on D3 and using the led on -12 module (D4).
|
||||
// This LED is lighting on output level LOW.
|
||||
#define PIN_INPUT D3
|
||||
#define PIN_LED D4
|
||||
|
||||
#endif
|
||||
|
||||
// Setup a new OneButton on pin A1.
|
||||
OneButton button(PIN_INPUT, true);
|
||||
|
||||
MyActions nextAction = ACTION_OFF; // no action when starting
|
||||
|
||||
|
||||
// setup code here, to run once.
|
||||
void setup() {
|
||||
// enable the standard led on pin 13.
|
||||
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
|
||||
|
||||
// link the myClickFunction function to be called on a click event.
|
||||
button.attachClick(myClickFunction);
|
||||
|
||||
// link the doubleclick function to be called on a doubleclick event.
|
||||
button.attachDoubleClick(myDoubleClickFunction);
|
||||
|
||||
// set 80 msec. debouncing time. Default is 50 msec.
|
||||
button.setDebounceTicks(80);
|
||||
} // setup
|
||||
|
||||
|
||||
// main code here, to run repeatedly:
|
||||
void loop() {
|
||||
unsigned long now = millis();
|
||||
|
||||
// keep watching the push button:
|
||||
button.tick();
|
||||
|
||||
// You can implement other code in here or just wait a while
|
||||
|
||||
if (nextAction == ACTION_OFF) {
|
||||
// do nothing.
|
||||
digitalWrite(PIN_LED, LOW);
|
||||
|
||||
} else if (nextAction == ACTION_ON) {
|
||||
// turn LED on
|
||||
digitalWrite(PIN_LED, HIGH);
|
||||
|
||||
} else if (nextAction == ACTION_SLOW) {
|
||||
// do a slow blinking
|
||||
if (now % 1000 < 500) {
|
||||
digitalWrite(PIN_LED, LOW);
|
||||
} else {
|
||||
digitalWrite(PIN_LED, HIGH);
|
||||
} // if
|
||||
|
||||
} else if (nextAction == ACTION_FAST) {
|
||||
// do a fast blinking
|
||||
if (now % 200 < 100) {
|
||||
digitalWrite(PIN_LED, LOW);
|
||||
} else {
|
||||
digitalWrite(PIN_LED, HIGH);
|
||||
} // if
|
||||
} // if
|
||||
} // loop
|
||||
|
||||
|
||||
// this function will be called when the button was pressed 1 time and them some time has passed.
|
||||
void myClickFunction() {
|
||||
if (nextAction == ACTION_OFF)
|
||||
nextAction = ACTION_ON;
|
||||
else
|
||||
nextAction = ACTION_OFF;
|
||||
} // myClickFunction
|
||||
|
||||
|
||||
// this function will be called when the button was pressed 2 times in a short timeframe.
|
||||
void myDoubleClickFunction() {
|
||||
if (nextAction == ACTION_ON) {
|
||||
nextAction = ACTION_SLOW;
|
||||
|
||||
} else if (nextAction == ACTION_SLOW) {
|
||||
nextAction = ACTION_FAST;
|
||||
|
||||
} else if (nextAction == ACTION_FAST) {
|
||||
nextAction = ACTION_ON;
|
||||
} // if
|
||||
} // myDoubleClickFunction
|
||||
|
||||
// End
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
InterruptOneButton.ino - Example for the OneButtonLibrary library.
|
||||
This is a sample sketch to show how to use the OneButtonLibrary
|
||||
to detect double-click events on a button by using interrupts.
|
||||
The library internals are explained at
|
||||
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
|
||||
|
||||
Good article on Arduino UNO interrupts:
|
||||
https://arduino.stackexchange.com/questions/30968/how-do-interrupts-work-on-the-arduino-uno-and-similar-boards
|
||||
... and the processor datasheet.
|
||||
|
||||
Setup a test circuit:
|
||||
* Connect a pushbutton to pin A1 (ButtonPin) and ground.
|
||||
* The pin 13 (StatusPin) is used for output attach a led and resistor to ground
|
||||
or see the built-in led on the standard arduino board.
|
||||
|
||||
The sketch shows how to setup the library and bind the functions (singleClick, doubleClick) to the events.
|
||||
In the loop function the button.tick function must be called as often as you like.
|
||||
By using interrupts the internal state advances even when for longer time the button.tick is not called.
|
||||
*/
|
||||
|
||||
// 03.03.2011 created by Matthias Hertel
|
||||
// 01.12.2011 extension changed to work with the Arduino 1.0 environment
|
||||
// 04.11.2017 Interrupt version created.
|
||||
// 04.11.2017 Interrupt version using attachInterrupt.
|
||||
|
||||
#include "OneButton.h"
|
||||
|
||||
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
|
||||
// Example for Arduino UNO with input button on pin 2 and builtin LED on pin 13
|
||||
// attachInterrupt only supports pin 2 and 3 on UNO.
|
||||
// See https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
|
||||
#define PIN_INPUT 2
|
||||
#define PIN_LED 13
|
||||
|
||||
#elif defined(ESP8266)
|
||||
// Example for NodeMCU with input button using FLASH button on D3 and using the led on -12 module (D4).
|
||||
// This LED is lighting on output level LOW.
|
||||
#define PIN_INPUT D3
|
||||
#define PIN_LED D4
|
||||
|
||||
#endif
|
||||
|
||||
// Setup a new OneButton on pin PIN_INPUT
|
||||
// The 2. parameter activeLOW is true, because external wiring sets the button to LOW when pressed.
|
||||
OneButton button(PIN_INPUT, true);
|
||||
|
||||
// current LED state, staring with LOW (0)
|
||||
int ledState = LOW;
|
||||
|
||||
// save the millis when a press has started.
|
||||
unsigned long pressStartTime;
|
||||
|
||||
// In case the momentary button puts the input to HIGH when pressed:
|
||||
// The 2. parameter activeLOW is false when the external wiring sets the button to HIGH when pressed.
|
||||
// The 3. parameter can be used to disable the PullUp .
|
||||
// OneButton button(PIN_INPUT, false, false);
|
||||
|
||||
|
||||
// This function is called from the interrupt when the signal on the PIN_INPUT has changed.
|
||||
// do not use Serial in here.
|
||||
#if defined(ARDUINO_AVR_UNO) || defined (ARDUINO_AVR_NANO_EVERY)
|
||||
void checkTicks()
|
||||
{
|
||||
// include all buttons here to be checked
|
||||
button.tick(); // just call tick() to check the state.
|
||||
}
|
||||
|
||||
#elif defined(ESP8266)
|
||||
ICACHE_RAM_ATTR void checkTicks()
|
||||
{
|
||||
// include all buttons here to be checked
|
||||
button.tick(); // just call tick() to check the state.
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// this function will be called when the button was pressed 1 time only.
|
||||
void singleClick()
|
||||
{
|
||||
Serial.println("singleClick() detected.");
|
||||
} // singleClick
|
||||
|
||||
|
||||
// this function will be called when the button was pressed 2 times in a short timeframe.
|
||||
void doubleClick()
|
||||
{
|
||||
Serial.println("doubleClick() detected.");
|
||||
|
||||
ledState = !ledState; // reverse the LED
|
||||
digitalWrite(PIN_LED, ledState);
|
||||
} // doubleClick
|
||||
|
||||
|
||||
// this function will be called when the button was pressed 2 times in a short timeframe.
|
||||
void multiClick()
|
||||
{
|
||||
Serial.print("multiClick(");
|
||||
Serial.print(button.getNumberClicks());
|
||||
Serial.println(") detected.");
|
||||
|
||||
ledState = !ledState; // reverse the LED
|
||||
digitalWrite(PIN_LED, ledState);
|
||||
} // multiClick
|
||||
|
||||
|
||||
// this function will be called when the button was pressed 2 times in a short timeframe.
|
||||
void pressStart()
|
||||
{
|
||||
Serial.println("pressStart()");
|
||||
pressStartTime = millis() - 1000; // as set in setPressTicks()
|
||||
} // pressStart()
|
||||
|
||||
|
||||
// this function will be called when the button was pressed 2 times in a short timeframe.
|
||||
void pressStop()
|
||||
{
|
||||
Serial.print("pressStop(");
|
||||
Serial.print(millis() - pressStartTime);
|
||||
Serial.println(") detected.");
|
||||
} // pressStop()
|
||||
|
||||
|
||||
// setup code here, to run once:
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("One Button Example with interrupts.");
|
||||
|
||||
// enable the led output.
|
||||
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
|
||||
digitalWrite(PIN_LED, ledState);
|
||||
|
||||
// setup interrupt routine
|
||||
// when not registering to the interrupt the sketch also works when the tick is called frequently.
|
||||
attachInterrupt(digitalPinToInterrupt(PIN_INPUT), checkTicks, CHANGE);
|
||||
|
||||
// link the xxxclick functions to be called on xxxclick event.
|
||||
button.attachClick(singleClick);
|
||||
button.attachDoubleClick(doubleClick);
|
||||
button.attachMultiClick(multiClick);
|
||||
|
||||
button.setPressTicks(1000); // that is the time when LongPressStart is called
|
||||
button.attachLongPressStart(pressStart);
|
||||
button.attachLongPressStop(pressStop);
|
||||
|
||||
// A1-Option for UNO:
|
||||
// it is possible to use e.g. A1 but then some direct register modifications and an ISR has to be used:
|
||||
// You may have to modify the next 2 lines if using another pin than A1
|
||||
// PCICR |= (1 << PCIE1); // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port C.
|
||||
// PCMSK1 |= (1 << PCINT9); // This enables the interrupt for pin 1 of Port C: This is A1.
|
||||
|
||||
} // setup
|
||||
|
||||
// A1-Option for UNO:
|
||||
// The Interrupt Service Routine for Pin Change Interrupt 1
|
||||
// This routine will only be called on any signal change on A1: exactly where we need to check.
|
||||
// ISR(PCINT1_vect)
|
||||
// {
|
||||
// // keep watching the push button:
|
||||
// button.tick(); // just call tick() to check the state.
|
||||
// }
|
||||
|
||||
|
||||
// main code here, to run repeatedly:
|
||||
void loop()
|
||||
{
|
||||
// keep watching the push button, even when no interrupt happens:
|
||||
button.tick();
|
||||
|
||||
// You can implement other code in here or just wait a while
|
||||
delay(10);
|
||||
} // loop
|
||||
|
||||
|
||||
// End
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
This is a sample sketch to show how to use the OneButtonLibrary
|
||||
to detect double-click events on a button.
|
||||
The library internals are explained at
|
||||
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
|
||||
|
||||
Setup a test circuit:
|
||||
* Connect a pushbutton to pin A1 (ButtonPin) and ground.
|
||||
* The pin 13 (StatusPin) is used for output attach a led and resistor to ground
|
||||
or see the built-in led on the standard arduino board.
|
||||
|
||||
The sketch shows how to setup the library and bind the functions (singleClick, doubleClick) to the events.
|
||||
In the loop function the button.tick function must be called as often as you like.
|
||||
*/
|
||||
|
||||
// 03.03.2011 created by Matthias Hertel
|
||||
// 01.12.2011 extension changed to work with the Arduino 1.0 environment
|
||||
|
||||
#include "OneButton.h"
|
||||
|
||||
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
|
||||
// Example for Arduino UNO with input button on pin 2 and builtin LED on pin 13
|
||||
#define PIN_INPUT 2
|
||||
#define PIN_LED 13
|
||||
|
||||
#else if defined(ESP8266)
|
||||
// Example for NodeMCU with input button using FLASH button on D3 and using the led on -12 module (D4).
|
||||
// This LED is lighting on output level LOW.
|
||||
#define PIN_INPUT D3
|
||||
#define PIN_LED D4
|
||||
|
||||
#endif
|
||||
|
||||
// Setup a new OneButton on pin PIN_INPUT
|
||||
// The 2. parameter activeLOW is true, because external wiring sets the button to LOW when pressed.
|
||||
OneButton button(PIN_INPUT, true);
|
||||
|
||||
// In case the momentary button puts the input to HIGH when pressed:
|
||||
// The 2. parameter activeLOW is false when the external wiring sets the button to HIGH when pressed.
|
||||
// The 3. parameter can be used to disable the PullUp .
|
||||
// OneButton button(PIN_INPUT, false, false);
|
||||
|
||||
// current LED state, staring with LOW (0)
|
||||
int ledState = LOW;
|
||||
|
||||
// setup code here, to run once:
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("One Button Example with polling.");
|
||||
|
||||
// enable the standard led on pin 13.
|
||||
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
|
||||
|
||||
// enable the standard led on pin 13.
|
||||
digitalWrite(PIN_LED, ledState);
|
||||
|
||||
// link the doubleclick function to be called on a doubleclick event.
|
||||
button.attachDoubleClick(doubleClick);
|
||||
} // setup
|
||||
|
||||
|
||||
// main code here, to run repeatedly:
|
||||
void loop()
|
||||
{
|
||||
// keep watching the push button:
|
||||
button.tick();
|
||||
|
||||
// You can implement other code in here or just wait a while
|
||||
delay(10);
|
||||
} // loop
|
||||
|
||||
|
||||
// this function will be called when the button was pressed 2 times in a short timeframe.
|
||||
void doubleClick()
|
||||
{
|
||||
Serial.println("x2");
|
||||
|
||||
ledState = !ledState; // reverse the LED
|
||||
digitalWrite(PIN_LED, ledState);
|
||||
} // doubleClick
|
||||
|
||||
// End
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* SpecialInput.ino - Example for the OneButtonLibrary library.
|
||||
* This is a sample sketch to show how to use the OneClick library on other input sources than standard digital pins.
|
||||
*
|
||||
* The library internals are explained at
|
||||
* http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
|
||||
*
|
||||
* Setup a test circuit:
|
||||
* * Connect a pushbutton to pin 2 (ButtonPin) and ground.
|
||||
*
|
||||
* The sketch shows how to setup the library and bind the functions (singleClick, doubleClick) to the events.
|
||||
* In the loop function the button.tick function must be called as often as you like.
|
||||
*
|
||||
* * 22.01.2021 created by Matthias Hertel
|
||||
*/
|
||||
|
||||
#include "OneButton.h"
|
||||
|
||||
// This is an example on how to use the OneClick library on other input sources than standard digital pins.
|
||||
// 1. do not use a pin in the initialization of the OneClick library.
|
||||
// 2. pass the input state to the tick function.
|
||||
|
||||
// You can also find how to create an instance in setup and not by declaration.
|
||||
// You can also find how to use inline callback functions.
|
||||
|
||||
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
|
||||
#define PIN_INPUT 2
|
||||
|
||||
#else if defined(ESP8266)
|
||||
#define PIN_INPUT D3
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// OneButton instance will be created in setup.
|
||||
OneButton *button;
|
||||
|
||||
void fClicked(void *s)
|
||||
{
|
||||
Serial.print("Click:");
|
||||
Serial.println((char *)s);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("One Button Example with custom input.");
|
||||
|
||||
// create the OneButton instance without a pin.
|
||||
button = new OneButton();
|
||||
|
||||
// Here is an example on how to use a parameter to the registered function:
|
||||
button->attachClick(fClicked, "me");
|
||||
|
||||
// Here is an example on how to use an inline function:
|
||||
button->attachDoubleClick([]() { Serial.println("DoubleClick"); });
|
||||
|
||||
// setup your own source of input:
|
||||
pinMode(PIN_INPUT, INPUT_PULLUP);
|
||||
|
||||
} // setup()
|
||||
|
||||
void loop()
|
||||
{
|
||||
// read your own source of input:
|
||||
bool isPressed = (digitalRead(PIN_INPUT) == LOW);
|
||||
|
||||
// call tick frequently with current push-state of the input
|
||||
button->tick(isPressed);
|
||||
} // loop()
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
This is a sample sketch to show how to use the OneButtonLibrary
|
||||
to detect click events on 2 buttons in parallel.
|
||||
The library internals are explained at
|
||||
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
|
||||
|
||||
Setup a test circuit:
|
||||
* Connect a pushbutton to pin A1 (ButtonPin) and ground.
|
||||
* Connect a pushbutton to pin A2 (ButtonPin) and ground.
|
||||
* The Serial interface is used for output the detected button events.
|
||||
|
||||
The Sketch shows how to setup the library and bind 2 buttons to their functions.
|
||||
In the loop function the button1.tick and button2.tick functions have to be called as often as you like.
|
||||
*/
|
||||
|
||||
// 01.03.2014 created by Matthias Hertel
|
||||
// ... and working.
|
||||
|
||||
/* Sample output:
|
||||
|
||||
Starting TwoButtons...
|
||||
Button 1 click.
|
||||
Button 2 click.
|
||||
Button 1 doubleclick.
|
||||
Button 2 doubleclick.
|
||||
Button 1 longPress start
|
||||
Button 1 longPress...
|
||||
Button 1 longPress...
|
||||
Button 1 longPress...
|
||||
Button 1 longPress stop
|
||||
Button 2 longPress start
|
||||
Button 2 longPress...
|
||||
Button 2 longPress...
|
||||
Button 2 longPress stop
|
||||
|
||||
*/
|
||||
|
||||
#include "OneButton.h"
|
||||
|
||||
// Setup a new OneButton on pin A1.
|
||||
OneButton button1(A1, true);
|
||||
// Setup a new OneButton on pin A2.
|
||||
OneButton button2(A2, true);
|
||||
|
||||
|
||||
// setup code here, to run once:
|
||||
void setup() {
|
||||
// Setup the Serial port. see http://arduino.cc/en/Serial/IfSerial
|
||||
Serial.begin(115200);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for Leonardo only
|
||||
}
|
||||
Serial.println("Starting TwoButtons...");
|
||||
|
||||
// link the button 1 functions.
|
||||
button1.attachClick(click1);
|
||||
button1.attachDoubleClick(doubleclick1);
|
||||
button1.attachLongPressStart(longPressStart1);
|
||||
button1.attachLongPressStop(longPressStop1);
|
||||
button1.attachDuringLongPress(longPress1);
|
||||
|
||||
// link the button 2 functions.
|
||||
button2.attachClick(click2);
|
||||
button2.attachDoubleClick(doubleclick2);
|
||||
button2.attachLongPressStart(longPressStart2);
|
||||
button2.attachLongPressStop(longPressStop2);
|
||||
button2.attachDuringLongPress(longPress2);
|
||||
|
||||
} // setup
|
||||
|
||||
|
||||
// main code here, to run repeatedly:
|
||||
void loop() {
|
||||
// keep watching the push buttons:
|
||||
button1.tick();
|
||||
button2.tick();
|
||||
|
||||
// You can implement other code in here or just wait a while
|
||||
delay(10);
|
||||
} // loop
|
||||
|
||||
|
||||
// ----- button 1 callback functions
|
||||
|
||||
// This function will be called when the button1 was pressed 1 time (and no 2. button press followed).
|
||||
void click1() {
|
||||
Serial.println("Button 1 click.");
|
||||
} // click1
|
||||
|
||||
|
||||
// This function will be called when the button1 was pressed 2 times in a short timeframe.
|
||||
void doubleclick1() {
|
||||
Serial.println("Button 1 doubleclick.");
|
||||
} // doubleclick1
|
||||
|
||||
|
||||
// This function will be called once, when the button1 is pressed for a long time.
|
||||
void longPressStart1() {
|
||||
Serial.println("Button 1 longPress start");
|
||||
} // longPressStart1
|
||||
|
||||
|
||||
// This function will be called often, while the button1 is pressed for a long time.
|
||||
void longPress1() {
|
||||
Serial.println("Button 1 longPress...");
|
||||
} // longPress1
|
||||
|
||||
|
||||
// This function will be called once, when the button1 is released after beeing pressed for a long time.
|
||||
void longPressStop1() {
|
||||
Serial.println("Button 1 longPress stop");
|
||||
} // longPressStop1
|
||||
|
||||
|
||||
// ... and the same for button 2:
|
||||
|
||||
void click2() {
|
||||
Serial.println("Button 2 click.");
|
||||
} // click2
|
||||
|
||||
|
||||
void doubleclick2() {
|
||||
Serial.println("Button 2 doubleclick.");
|
||||
} // doubleclick2
|
||||
|
||||
|
||||
void longPressStart2() {
|
||||
Serial.println("Button 2 longPress start");
|
||||
} // longPressStart2
|
||||
|
||||
|
||||
void longPress2() {
|
||||
Serial.println("Button 2 longPress...");
|
||||
} // longPress2
|
||||
|
||||
void longPressStop2() {
|
||||
Serial.println("Button 2 longPress stop");
|
||||
} // longPressStop2
|
||||
|
||||
|
||||
// End
|
||||
|
||||
37
arduino-libs/arduino-cli/libraries/OneButton/keywords.txt
Normal file
37
arduino-libs/arduino-cli/libraries/OneButton/keywords.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map for OneButton
|
||||
#######################################
|
||||
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
callbackFunction KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
setClickTicks KEYWORD2
|
||||
setPressTicks KEYWORD2
|
||||
setDebounceTicks KEYWORD2
|
||||
attachClick KEYWORD2
|
||||
attachDoubleClick KEYWORD2
|
||||
attachLongPressStart KEYWORD2
|
||||
attachLongPressStop KEYWORD2
|
||||
attachDuringLongPress KEYWORD2
|
||||
isLongPressed KEYWORD2
|
||||
tick KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
OneButton KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
||||
|
||||
11
arduino-libs/arduino-cli/libraries/OneButton/library.json
Normal file
11
arduino-libs/arduino-cli/libraries/OneButton/library.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "OneButton",
|
||||
"keywords": "arduino, button, pushbutton",
|
||||
"description": "This Arduino library is improving the usage of a singe button for input. It shows how to use an digital input pin with a single pushbutton attached for detecting some of the typical button press events like single clicks, double clicks and long-time pressing. This enables you to reuse the same button for multiple functions and lowers the hardware invests.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mathertel/OneButton"
|
||||
},
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
name=OneButton
|
||||
version=2.0.1
|
||||
author=Matthias Hertel, mathertel@hotmail.com
|
||||
maintainer=Matthias Hertel <http://www.mathertel.de>
|
||||
sentence=Arduino library for improving the usage of a singe input button.
|
||||
paragraph=It supports detecting events like single, double, multiple clicks and long-time pressing. This enables you to reuse the same button for multiple functions and lowers the hardware invests.
|
||||
category=Signal Input/Output
|
||||
url=https://github.com/mathertel/OneButton
|
||||
architectures=*
|
||||
includes=OneButton.h
|
||||
license=BSD-3-Clause
|
||||
332
arduino-libs/arduino-cli/libraries/OneButton/src/OneButton.cpp
Normal file
332
arduino-libs/arduino-cli/libraries/OneButton/src/OneButton.cpp
Normal file
@@ -0,0 +1,332 @@
|
||||
/**
|
||||
* @file OneButton.cpp
|
||||
*
|
||||
* @brief Library for detecting button clicks, doubleclicks and long press
|
||||
* pattern on a single button.
|
||||
*
|
||||
* @author Matthias Hertel, https://www.mathertel.de
|
||||
* @Copyright Copyright (c) by Matthias Hertel, https://www.mathertel.de.
|
||||
*
|
||||
* This work is licensed under a BSD style license. See
|
||||
* http://www.mathertel.de/License.aspx
|
||||
*
|
||||
* More information on: https://www.mathertel.de/Arduino/OneButtonLibrary.aspx
|
||||
*
|
||||
* Changelog: see OneButton.h
|
||||
*/
|
||||
|
||||
#include "OneButton.h"
|
||||
|
||||
// ----- Initialization and Default Values -----
|
||||
|
||||
/**
|
||||
* @brief Construct a new OneButton object but not (yet) initialize the IO pin.
|
||||
*/
|
||||
OneButton::OneButton()
|
||||
{
|
||||
_pin = -1;
|
||||
// further initialization has moved to OneButton.h
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the OneButton library.
|
||||
* @param pin The pin to be used for input from a momentary button.
|
||||
* @param activeLow Set to true when the input level is LOW when the button is pressed, Default is true.
|
||||
* @param pullupActive Activate the internal pullup when available. Default is true.
|
||||
*/
|
||||
OneButton::OneButton(const int pin, const boolean activeLow, const bool pullupActive)
|
||||
{
|
||||
// OneButton();
|
||||
_pin = pin;
|
||||
|
||||
if (activeLow) {
|
||||
// the button connects the input pin to GND when pressed.
|
||||
_buttonPressed = LOW;
|
||||
|
||||
} else {
|
||||
// the button connects the input pin to VCC when pressed.
|
||||
_buttonPressed = HIGH;
|
||||
} // if
|
||||
|
||||
if (pullupActive) {
|
||||
// use the given pin as input and activate internal PULLUP resistor.
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
} else {
|
||||
// use the given pin as input
|
||||
pinMode(pin, INPUT);
|
||||
} // if
|
||||
} // OneButton
|
||||
|
||||
|
||||
// explicitly set the number of millisec that have to pass by before a click is assumed stable.
|
||||
void OneButton::setDebounceTicks(const int ticks)
|
||||
{
|
||||
_debounceTicks = ticks;
|
||||
} // setDebounceTicks
|
||||
|
||||
|
||||
// explicitly set the number of millisec that have to pass by before a click is detected.
|
||||
void OneButton::setClickTicks(const int ticks)
|
||||
{
|
||||
_clickTicks = ticks;
|
||||
} // setClickTicks
|
||||
|
||||
|
||||
// explicitly set the number of millisec that have to pass by before a long button press is detected.
|
||||
void OneButton::setPressTicks(const int ticks)
|
||||
{
|
||||
_pressTicks = ticks;
|
||||
} // setPressTicks
|
||||
|
||||
|
||||
// save function for click event
|
||||
void OneButton::attachClick(callbackFunction newFunction)
|
||||
{
|
||||
_clickFunc = newFunction;
|
||||
} // attachClick
|
||||
|
||||
|
||||
// save function for parameterized click event
|
||||
void OneButton::attachClick(parameterizedCallbackFunction newFunction, void *parameter)
|
||||
{
|
||||
_paramClickFunc = newFunction;
|
||||
_clickFuncParam = parameter;
|
||||
} // attachClick
|
||||
|
||||
|
||||
// save function for doubleClick event
|
||||
void OneButton::attachDoubleClick(callbackFunction newFunction)
|
||||
{
|
||||
_doubleClickFunc = newFunction;
|
||||
_maxClicks = max(_maxClicks, 2);
|
||||
} // attachDoubleClick
|
||||
|
||||
|
||||
// save function for parameterized doubleClick event
|
||||
void OneButton::attachDoubleClick(parameterizedCallbackFunction newFunction, void *parameter)
|
||||
{
|
||||
_paramDoubleClickFunc = newFunction;
|
||||
_doubleClickFuncParam = parameter;
|
||||
_maxClicks = max(_maxClicks, 2);
|
||||
} // attachDoubleClick
|
||||
|
||||
|
||||
// save function for multiClick event
|
||||
void OneButton::attachMultiClick(callbackFunction newFunction)
|
||||
{
|
||||
_multiClickFunc = newFunction;
|
||||
_maxClicks = max(_maxClicks, 100);
|
||||
} // attachMultiClick
|
||||
|
||||
|
||||
// save function for parameterized MultiClick event
|
||||
void OneButton::attachMultiClick(parameterizedCallbackFunction newFunction, void *parameter)
|
||||
{
|
||||
_paramMultiClickFunc = newFunction;
|
||||
_multiClickFuncParam = parameter;
|
||||
_maxClicks = max(_maxClicks, 100);
|
||||
} // attachMultiClick
|
||||
|
||||
|
||||
// save function for longPressStart event
|
||||
void OneButton::attachLongPressStart(callbackFunction newFunction)
|
||||
{
|
||||
_longPressStartFunc = newFunction;
|
||||
} // attachLongPressStart
|
||||
|
||||
|
||||
// save function for parameterized longPressStart event
|
||||
void OneButton::attachLongPressStart(parameterizedCallbackFunction newFunction, void *parameter)
|
||||
{
|
||||
_paramLongPressStartFunc = newFunction;
|
||||
_longPressStartFuncParam = parameter;
|
||||
} // attachLongPressStart
|
||||
|
||||
|
||||
// save function for longPressStop event
|
||||
void OneButton::attachLongPressStop(callbackFunction newFunction)
|
||||
{
|
||||
_longPressStopFunc = newFunction;
|
||||
} // attachLongPressStop
|
||||
|
||||
|
||||
// save function for parameterized longPressStop event
|
||||
void OneButton::attachLongPressStop(parameterizedCallbackFunction newFunction, void *parameter)
|
||||
{
|
||||
_paramLongPressStopFunc = newFunction;
|
||||
_longPressStopFuncParam = parameter;
|
||||
} // attachLongPressStop
|
||||
|
||||
|
||||
// save function for during longPress event
|
||||
void OneButton::attachDuringLongPress(callbackFunction newFunction)
|
||||
{
|
||||
_duringLongPressFunc = newFunction;
|
||||
} // attachDuringLongPress
|
||||
|
||||
|
||||
// save function for parameterized during longPress event
|
||||
void OneButton::attachDuringLongPress(parameterizedCallbackFunction newFunction, void *parameter)
|
||||
{
|
||||
_paramDuringLongPressFunc = newFunction;
|
||||
_duringLongPressFuncParam = parameter;
|
||||
} // attachDuringLongPress
|
||||
|
||||
|
||||
void OneButton::reset(void)
|
||||
{
|
||||
_state = OneButton::OCS_INIT;
|
||||
_lastState = OneButton::OCS_INIT;
|
||||
_nClicks = 0;
|
||||
_startTime = 0;
|
||||
}
|
||||
|
||||
|
||||
// ShaggyDog ---- return number of clicks in any case: single or multiple clicks
|
||||
int OneButton::getNumberClicks(void)
|
||||
{
|
||||
return _nClicks;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Check input of the configured pin and then advance the finite state
|
||||
* machine (FSM).
|
||||
*/
|
||||
void OneButton::tick(void)
|
||||
{
|
||||
if (_pin >= 0) {
|
||||
tick(digitalRead(_pin) == _buttonPressed);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Advance to a new state and save the last one to come back in cas of bouncing detection.
|
||||
*/
|
||||
void OneButton::_newState(stateMachine_t nextState)
|
||||
{
|
||||
_lastState = _state;
|
||||
_state = nextState;
|
||||
} // _newState()
|
||||
|
||||
|
||||
/**
|
||||
* @brief Run the finite state machine (FSM) using the given level.
|
||||
*/
|
||||
void OneButton::tick(bool activeLevel)
|
||||
{
|
||||
unsigned long now = millis(); // current (relative) time in msecs.
|
||||
unsigned long waitTime = (now - _startTime);
|
||||
|
||||
// Implementation of the state machine
|
||||
switch (_state) {
|
||||
case OneButton::OCS_INIT:
|
||||
// waiting for level to become active.
|
||||
if (activeLevel) {
|
||||
_newState(OneButton::OCS_DOWN);
|
||||
_startTime = now; // remember starting time
|
||||
_nClicks = 0;
|
||||
} // if
|
||||
break;
|
||||
|
||||
case OneButton::OCS_DOWN:
|
||||
// waiting for level to become inactive.
|
||||
|
||||
if ((!activeLevel) && (waitTime < _debounceTicks)) {
|
||||
// button was released to quickly so I assume some bouncing.
|
||||
_newState(_lastState);
|
||||
|
||||
} else if (!activeLevel) {
|
||||
_newState(OneButton::OCS_UP);
|
||||
_startTime = now; // remember starting time
|
||||
|
||||
} else if ((activeLevel) && (waitTime > _pressTicks)) {
|
||||
if (_longPressStartFunc) _longPressStartFunc();
|
||||
if (_paramLongPressStartFunc) _paramLongPressStartFunc(_longPressStartFuncParam);
|
||||
_newState(OneButton::OCS_PRESS);
|
||||
} // if
|
||||
break;
|
||||
|
||||
case OneButton::OCS_UP:
|
||||
// level is inactive
|
||||
|
||||
if ((activeLevel) && (waitTime < _debounceTicks)) {
|
||||
// button was pressed to quickly so I assume some bouncing.
|
||||
_newState(_lastState); // go back
|
||||
|
||||
} else if (waitTime >= _debounceTicks) {
|
||||
// count as a short button down
|
||||
_nClicks++;
|
||||
_newState(OneButton::OCS_COUNT);
|
||||
} // if
|
||||
break;
|
||||
|
||||
case OneButton::OCS_COUNT:
|
||||
// dobounce time is over, count clicks
|
||||
|
||||
if (activeLevel) {
|
||||
// button is down again
|
||||
_newState(OneButton::OCS_DOWN);
|
||||
_startTime = now; // remember starting time
|
||||
|
||||
} else if ((waitTime > _clickTicks) || (_nClicks == _maxClicks)) {
|
||||
// now we know how many clicks have been made.
|
||||
|
||||
if (_nClicks == 1) {
|
||||
// this was 1 click only.
|
||||
if (_clickFunc) _clickFunc();
|
||||
if (_paramClickFunc) _paramClickFunc(_clickFuncParam);
|
||||
|
||||
} else if (_nClicks == 2) {
|
||||
// this was a 2 click sequence.
|
||||
if (_doubleClickFunc) _doubleClickFunc();
|
||||
if (_paramDoubleClickFunc) _paramDoubleClickFunc(_doubleClickFuncParam);
|
||||
|
||||
} else {
|
||||
// this was a multi click sequence.
|
||||
if (_multiClickFunc) _multiClickFunc();
|
||||
if (_paramMultiClickFunc) _paramMultiClickFunc(_doubleClickFuncParam);
|
||||
} // if
|
||||
|
||||
reset();
|
||||
} // if
|
||||
break;
|
||||
|
||||
case OneButton::OCS_PRESS:
|
||||
// waiting for menu pin being release after long press.
|
||||
|
||||
if (!activeLevel) {
|
||||
_newState(OneButton::OCS_PRESSEND);
|
||||
_startTime = now;
|
||||
|
||||
} else {
|
||||
// still the button is pressed
|
||||
if (_duringLongPressFunc) _duringLongPressFunc();
|
||||
if (_paramDuringLongPressFunc) _paramDuringLongPressFunc(_duringLongPressFuncParam);
|
||||
} // if
|
||||
break;
|
||||
|
||||
case OneButton::OCS_PRESSEND:
|
||||
// button was released.
|
||||
|
||||
if ((activeLevel) && (waitTime < _debounceTicks)) {
|
||||
// button was released to quickly so I assume some bouncing.
|
||||
_newState(_lastState); // go back
|
||||
|
||||
} else if (waitTime >= _debounceTicks) {
|
||||
if (_longPressStopFunc) _longPressStopFunc();
|
||||
if (_paramLongPressStopFunc) _paramLongPressStopFunc(_longPressStopFuncParam);
|
||||
reset();
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
// nothing to do.
|
||||
break;
|
||||
} // if
|
||||
|
||||
} // OneButton.tick()
|
||||
|
||||
|
||||
// end.
|
||||
212
arduino-libs/arduino-cli/libraries/OneButton/src/OneButton.h
Normal file
212
arduino-libs/arduino-cli/libraries/OneButton/src/OneButton.h
Normal file
@@ -0,0 +1,212 @@
|
||||
// -----
|
||||
// OneButton.h - Library for detecting button clicks, doubleclicks and long
|
||||
// press pattern on a single button. This class is implemented for use with the
|
||||
// Arduino environment. Copyright (c) by Matthias Hertel,
|
||||
// http://www.mathertel.de This work is licensed under a BSD style license. See
|
||||
// http://www.mathertel.de/License.aspx More information on:
|
||||
// http://www.mathertel.de/Arduino
|
||||
// -----
|
||||
// 02.10.2010 created by Matthias Hertel
|
||||
// 21.04.2011 transformed into a library
|
||||
// 01.12.2011 include file changed to work with the Arduino 1.0 environment
|
||||
// 23.03.2014 Enhanced long press functionalities by adding longPressStart and
|
||||
// longPressStop callbacks
|
||||
// 21.09.2015 A simple way for debounce detection added.
|
||||
// 14.05.2017 Debouncing improvements.
|
||||
// 25.06.2018 Optional third parameter for deactivating pullup.
|
||||
// 26.09.2018 Anatoli Arkhipenko: Included solution to use library with other
|
||||
// sources of input.
|
||||
// 26.09.2018 Initialization moved into class declaration.
|
||||
// 26.09.2018 Jay M Ericsson: compiler warnings removed.
|
||||
// 29.01.2020 improvements from ShaggyDog18
|
||||
// -----
|
||||
|
||||
#ifndef OneButton_h
|
||||
#define OneButton_h
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
// ----- Callback function types -----
|
||||
|
||||
extern "C" {
|
||||
typedef void (*callbackFunction)(void);
|
||||
typedef void (*parameterizedCallbackFunction)(void *);
|
||||
}
|
||||
|
||||
|
||||
class OneButton
|
||||
{
|
||||
public:
|
||||
// ----- Constructor -----
|
||||
OneButton();
|
||||
|
||||
/**
|
||||
* Initialize the OneButton library.
|
||||
* @param pin The pin to be used for input from a momentary button.
|
||||
* @param activeLow Set to true when the input level is LOW when the button is pressed, Default is true.
|
||||
* @param pullupActive Activate the internal pullup when available. Default is true.
|
||||
*/
|
||||
OneButton(const int pin, const boolean activeLow = true, const bool pullupActive = true);
|
||||
|
||||
// ----- Set runtime parameters -----
|
||||
|
||||
/**
|
||||
* set # millisec after safe click is assumed.
|
||||
*/
|
||||
void setDebounceTicks(const int ticks);
|
||||
|
||||
/**
|
||||
* set # millisec after single click is assumed.
|
||||
*/
|
||||
void setClickTicks(const int ticks);
|
||||
|
||||
/**
|
||||
* set # millisec after press is assumed.
|
||||
*/
|
||||
void setPressTicks(const int ticks);
|
||||
|
||||
/**
|
||||
* Attach an event to be called when a single click is detected.
|
||||
* @param newFunction This function will be called when the event has been detected.
|
||||
*/
|
||||
void attachClick(callbackFunction newFunction);
|
||||
void attachClick(parameterizedCallbackFunction newFunction, void *parameter);
|
||||
|
||||
/**
|
||||
* Attach an event to be called after a double click is detected.
|
||||
* @param newFunction This function will be called when the event has been detected.
|
||||
*/
|
||||
void attachDoubleClick(callbackFunction newFunction);
|
||||
void attachDoubleClick(parameterizedCallbackFunction newFunction, void *parameter);
|
||||
|
||||
/**
|
||||
* Attach an event to be called after a multi click is detected.
|
||||
* @param newFunction This function will be called when the event has been detected.
|
||||
*/
|
||||
void attachMultiClick(callbackFunction newFunction);
|
||||
void attachMultiClick(parameterizedCallbackFunction newFunction, void *parameter);
|
||||
|
||||
/**
|
||||
* Attach an event to fire when the button is pressed and held down.
|
||||
* @param newFunction
|
||||
*/
|
||||
void attachLongPressStart(callbackFunction newFunction);
|
||||
void attachLongPressStart(parameterizedCallbackFunction newFunction, void *parameter);
|
||||
|
||||
/**
|
||||
* Attach an event to fire as soon as the button is released after a long press.
|
||||
* @param newFunction
|
||||
*/
|
||||
void attachLongPressStop(callbackFunction newFunction);
|
||||
void attachLongPressStop(parameterizedCallbackFunction newFunction, void *parameter);
|
||||
|
||||
/**
|
||||
* Attach an event to fire periodically while the button is held down.
|
||||
* @param newFunction
|
||||
*/
|
||||
void attachDuringLongPress(callbackFunction newFunction);
|
||||
void attachDuringLongPress(parameterizedCallbackFunction newFunction, void *parameter);
|
||||
|
||||
// ----- State machine functions -----
|
||||
|
||||
/**
|
||||
* @brief Call this function every some milliseconds for checking the input
|
||||
* level at the initialized digital pin.
|
||||
*/
|
||||
void tick(void);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Call this function every time the input level has changed.
|
||||
* Using this function no digital input pin is checked because the current
|
||||
* level is given by the parameter.
|
||||
*/
|
||||
void tick(bool level);
|
||||
|
||||
|
||||
/**
|
||||
* Reset the button state machine.
|
||||
*/
|
||||
void reset(void);
|
||||
|
||||
|
||||
/*
|
||||
* return number of clicks in any case: single or multiple clicks
|
||||
*/
|
||||
int getNumberClicks(void);
|
||||
|
||||
|
||||
/**
|
||||
* @return true if we are currently handling button press flow
|
||||
* (This allows power sensitive applications to know when it is safe to power down the main CPU)
|
||||
*/
|
||||
bool isIdle() const { return _state == OCS_INIT; }
|
||||
|
||||
/**
|
||||
* @return true when a long press is detected
|
||||
*/
|
||||
bool isLongPressed() const { return _state == OCS_PRESS; };
|
||||
|
||||
|
||||
private:
|
||||
int _pin; // hardware pin number.
|
||||
unsigned int _debounceTicks = 50; // number of ticks for debounce times.
|
||||
unsigned int _clickTicks = 400; // number of msecs before a click is detected.
|
||||
unsigned int _pressTicks = 800; // number of msecs before a long button press is detected
|
||||
|
||||
int _buttonPressed;
|
||||
|
||||
// These variables will hold functions acting as event source.
|
||||
callbackFunction _clickFunc = NULL;
|
||||
parameterizedCallbackFunction _paramClickFunc = NULL;
|
||||
void *_clickFuncParam = NULL;
|
||||
|
||||
callbackFunction _doubleClickFunc = NULL;
|
||||
parameterizedCallbackFunction _paramDoubleClickFunc = NULL;
|
||||
void *_doubleClickFuncParam = NULL;
|
||||
|
||||
callbackFunction _multiClickFunc = NULL;
|
||||
parameterizedCallbackFunction _paramMultiClickFunc = NULL;
|
||||
void *_multiClickFuncParam = NULL;
|
||||
|
||||
callbackFunction _longPressStartFunc = NULL;
|
||||
parameterizedCallbackFunction _paramLongPressStartFunc = NULL;
|
||||
void *_longPressStartFuncParam = NULL;
|
||||
|
||||
callbackFunction _longPressStopFunc = NULL;
|
||||
parameterizedCallbackFunction _paramLongPressStopFunc = NULL;
|
||||
void *_longPressStopFuncParam;
|
||||
|
||||
callbackFunction _duringLongPressFunc = NULL;
|
||||
parameterizedCallbackFunction _paramDuringLongPressFunc = NULL;
|
||||
void *_duringLongPressFuncParam = NULL;
|
||||
|
||||
// These variables that hold information across the upcoming tick calls.
|
||||
// They are initialized once on program start and are updated every time the
|
||||
// tick function is called.
|
||||
|
||||
// define FiniteStateMachine
|
||||
enum stateMachine_t : int {
|
||||
OCS_INIT = 0,
|
||||
OCS_DOWN = 1,
|
||||
OCS_UP = 2,
|
||||
OCS_COUNT = 3,
|
||||
OCS_PRESS = 6,
|
||||
OCS_PRESSEND = 7,
|
||||
UNKNOWN = 99
|
||||
};
|
||||
|
||||
/**
|
||||
* Advance to a new state and save the last one to come back in cas of bouncing detection.
|
||||
*/
|
||||
void _newState(stateMachine_t nextState);
|
||||
|
||||
stateMachine_t _state = OCS_INIT;
|
||||
stateMachine_t _lastState = OCS_INIT; // used for debouncing
|
||||
|
||||
unsigned long _startTime; // start of current input change to checking debouncing
|
||||
int _nClicks; // count the number of clicks with this variable
|
||||
int _maxClicks = 1; // max number (1, 2, multi=3) of clicks of interest by registration of event functions.
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user