first commit
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* A program that prints out the time (min/avg/max) taken by the
|
||||
* AceButton::check() method.
|
||||
*/
|
||||
|
||||
#include <AceButton.h>
|
||||
#include "ProfilingButtonConfig.h"
|
||||
using namespace ace_button;
|
||||
|
||||
// The pin number attached to the button.
|
||||
const int BUTTON_PIN = 2;
|
||||
|
||||
ProfilingButtonConfig buttonConfig;
|
||||
|
||||
// One button wired using the ProfilingButtonConfig.
|
||||
AceButton button(&buttonConfig);
|
||||
|
||||
const unsigned long STATS_PRINT_INTERVAL = 2000;
|
||||
unsigned long lastStatsPrintedTime;
|
||||
TimingStats stats;
|
||||
|
||||
const uint8_t LOOP_MODE_START = 0;
|
||||
const uint8_t LOOP_MODE_IDLE = 1;
|
||||
const uint8_t LOOP_MODE_PRESS_RELEASE = 2;
|
||||
const uint8_t LOOP_MODE_CLICK = 3;
|
||||
const uint8_t LOOP_MODE_DOUBLE_CLICK = 4;
|
||||
const uint8_t LOOP_MODE_LONG_PRESS = 5;
|
||||
const uint8_t LOOP_MODE_END = 6;
|
||||
uint8_t loopMode;
|
||||
uint8_t loopEventType;
|
||||
|
||||
void handleEvent(AceButton*, uint8_t, uint8_t);
|
||||
|
||||
void setup() {
|
||||
delay(1000); // some microcontrollers reboot twice
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // for Leonardo/Micro
|
||||
Serial.println(F("setup(): begin"));
|
||||
|
||||
// Button uses the built-in pull up register.
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
button.init(BUTTON_PIN);
|
||||
|
||||
// Configure the ButtonConfig with the event handler, and enable all higher
|
||||
// level events.
|
||||
buttonConfig.setEventHandler(handleEvent);
|
||||
buttonConfig.setFeature(ButtonConfig::kFeatureClick);
|
||||
buttonConfig.setFeature(ButtonConfig::kFeatureDoubleClick);
|
||||
buttonConfig.setFeature(ButtonConfig::kFeatureLongPress);
|
||||
buttonConfig.setFeature(ButtonConfig::kFeatureRepeatPress);
|
||||
buttonConfig.setFeature(ButtonConfig::kFeatureSuppressAll);
|
||||
buttonConfig.setTimingStats(&stats);
|
||||
|
||||
lastStatsPrintedTime = millis();
|
||||
loopMode = LOOP_MODE_START;
|
||||
loopEventType = AceButton::kEventPressed;
|
||||
|
||||
Serial.println(F("setup(): end"));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(1); // Decrease sampling frequency to about 1000 Hz
|
||||
button.check();
|
||||
|
||||
switch (loopMode) {
|
||||
case LOOP_MODE_START:
|
||||
loopStart();
|
||||
break;
|
||||
case LOOP_MODE_IDLE:
|
||||
loopIdle();
|
||||
break;
|
||||
case LOOP_MODE_PRESS_RELEASE:
|
||||
loopPressRelease();
|
||||
break;
|
||||
case LOOP_MODE_CLICK:
|
||||
loopClick();
|
||||
break;
|
||||
case LOOP_MODE_DOUBLE_CLICK:
|
||||
loopDoubleClick();
|
||||
break;
|
||||
case LOOP_MODE_LONG_PRESS:
|
||||
loopLongPress();
|
||||
break;
|
||||
case LOOP_MODE_END:
|
||||
loopEnd();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void loopStart() {
|
||||
static unsigned long start = millis();
|
||||
|
||||
// Wait one iteration for things to cool down.
|
||||
if (millis() - start > STATS_PRINT_INTERVAL) {
|
||||
Serial.println(F("------------------------+-------------+---------+"));
|
||||
Serial.println(F("button event | min/avg/max | samples |"));
|
||||
Serial.println(F("------------------------+-------------+---------+"));
|
||||
nextMode();
|
||||
}
|
||||
}
|
||||
|
||||
void loopEnd() {
|
||||
Serial.println(F("------------------------+-------------+---------+"));
|
||||
nextMode();
|
||||
}
|
||||
|
||||
void loopIdle() {
|
||||
static unsigned long start = millis();
|
||||
|
||||
if (millis() - start > STATS_PRINT_INTERVAL) {
|
||||
Serial.print(F("idle | "));
|
||||
printStats();
|
||||
Serial.println(F(" |"));
|
||||
nextMode();
|
||||
}
|
||||
}
|
||||
|
||||
void loopPressRelease() {
|
||||
static unsigned long start = millis();
|
||||
|
||||
unsigned long now = millis();
|
||||
unsigned long elapsed = now - start;
|
||||
if (100 <= elapsed && elapsed < 1000) buttonConfig.setButtonState(LOW);
|
||||
if (1000 <= elapsed) buttonConfig.setButtonState(HIGH);
|
||||
|
||||
if (millis() - start > STATS_PRINT_INTERVAL) {
|
||||
if (loopEventType == AceButton::kEventReleased) {
|
||||
Serial.print(F("press/release | "));
|
||||
printStats();
|
||||
Serial.println(F(" |"));
|
||||
}
|
||||
nextMode();
|
||||
}
|
||||
}
|
||||
|
||||
void loopClick() {
|
||||
static unsigned long start = millis();
|
||||
|
||||
unsigned long now = millis();
|
||||
unsigned long elapsed = now - start;
|
||||
if (100 <= elapsed && elapsed < 200) buttonConfig.setButtonState(LOW);
|
||||
if (200 <= elapsed) buttonConfig.setButtonState(HIGH);
|
||||
|
||||
if (millis() - start > STATS_PRINT_INTERVAL) {
|
||||
if (loopEventType == AceButton::kEventClicked) {
|
||||
Serial.print(F("click | "));
|
||||
printStats();
|
||||
Serial.println(F(" |"));
|
||||
}
|
||||
nextMode();
|
||||
}
|
||||
}
|
||||
|
||||
void loopDoubleClick() {
|
||||
static unsigned long start = millis();
|
||||
|
||||
unsigned long now = millis();
|
||||
unsigned long elapsed = now - start;
|
||||
if (100 <= elapsed && elapsed < 200) buttonConfig.setButtonState(LOW);
|
||||
if (200 <= elapsed && elapsed < 300) buttonConfig.setButtonState(HIGH);
|
||||
if (300 <= elapsed && elapsed < 400) buttonConfig.setButtonState(LOW);
|
||||
if (400 <= elapsed) buttonConfig.setButtonState(HIGH);
|
||||
|
||||
if (millis() - start > STATS_PRINT_INTERVAL) {
|
||||
if (loopEventType == AceButton::kEventDoubleClicked) {
|
||||
Serial.print(F("double click | "));
|
||||
printStats();
|
||||
Serial.println(F(" |"));
|
||||
}
|
||||
nextMode();
|
||||
}
|
||||
}
|
||||
|
||||
void loopLongPress() {
|
||||
static unsigned long start = millis();
|
||||
|
||||
unsigned long now = millis();
|
||||
unsigned long elapsed = now - start;
|
||||
if (100 <= elapsed) buttonConfig.setButtonState(LOW);
|
||||
|
||||
if (millis() - start > STATS_PRINT_INTERVAL) {
|
||||
if (loopEventType == AceButton::kEventRepeatPressed) {
|
||||
Serial.print(F("long press/repeat press | "));
|
||||
printStats();
|
||||
Serial.println(F(" |"));
|
||||
}
|
||||
nextMode();
|
||||
}
|
||||
}
|
||||
|
||||
void nextMode() {
|
||||
stats.reset();
|
||||
buttonConfig.setButtonState(HIGH);
|
||||
loopMode++;
|
||||
}
|
||||
|
||||
void printStats() {
|
||||
printInt(stats.getMin());
|
||||
Serial.print('/');
|
||||
printInt(stats.getAvg());
|
||||
Serial.print('/');
|
||||
printInt(stats.getMax());
|
||||
Serial.print(F(" | "));
|
||||
printInt(stats.getCount());
|
||||
}
|
||||
|
||||
// print integer within 3 characters, padded on left with spaces
|
||||
void printInt(uint16_t i) {
|
||||
if (i < 100) Serial.print(' ');
|
||||
if (i < 10) Serial.print(' ');
|
||||
Serial.print(i);
|
||||
}
|
||||
|
||||
// An empty event handler.
|
||||
void handleEvent(AceButton* /* button */, uint8_t eventType,
|
||||
uint8_t /* buttonState */) {
|
||||
loopEventType = eventType;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef PROFILING_BUTTON_CONFIG_H
|
||||
#define PROFILING_BUTTON_CONFIG_H
|
||||
|
||||
#include <Arduino.h> // LOW, HIGH
|
||||
#include <ButtonConfig.h>
|
||||
|
||||
namespace ace_button {
|
||||
|
||||
/**
|
||||
* A subclass of ButtonConfig which overrides readButton() so that the
|
||||
* AutoBenchmark sketch can inject button clicks into the AceButton::check()
|
||||
* loop, which we can use to collect timing stats.
|
||||
*/
|
||||
class ProfilingButtonConfig: public ButtonConfig {
|
||||
public:
|
||||
ProfilingButtonConfig():
|
||||
mButtonState(HIGH) {}
|
||||
|
||||
void init() override {
|
||||
ButtonConfig::init();
|
||||
mButtonState = HIGH;
|
||||
}
|
||||
|
||||
int readButton(uint8_t /* pin */) override { return mButtonState; }
|
||||
|
||||
/** Set the state of the fake physical button. */
|
||||
void setButtonState(int buttonState) { mButtonState = buttonState; }
|
||||
|
||||
private:
|
||||
// Disable copy-constructor and assignment operator
|
||||
ProfilingButtonConfig(const ProfilingButtonConfig&) = delete;
|
||||
ProfilingButtonConfig& operator=(const ProfilingButtonConfig&) = delete;
|
||||
|
||||
int mButtonState;
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,111 @@
|
||||
# AutoBenchmark
|
||||
|
||||
This sketch measures the amount of time consumed by the `AceButton::check()`
|
||||
method when processing various button events. It uses a special
|
||||
`ProfilingButtonConfig` object that allows the program to inject button events
|
||||
into the library. The profiling numbers come from activating the `TimingStats`
|
||||
object that has been instrumented into the `AceButton::check()` method.
|
||||
|
||||
Note that `ProfilingButtonConfig` class generates synthetic button events,
|
||||
bypassing the actual `digitalRead()` function. The `digitalRead()` function on
|
||||
an Arduino AVR platform (UNO, Nano, etc) is
|
||||
[known to be slow](https://forum.arduino.cc/index.php?topic=337578)
|
||||
which will add to the timing values in actual usage.
|
||||
The [digitalWriteFast library](https://github.com/NicksonYap/digitalWriteFast)
|
||||
might be an alternative if speed is critical.
|
||||
|
||||
## Benchmark Results
|
||||
|
||||
In all of the tests, the **min** time for the "idle" case is larger than any of
|
||||
the other button events. This is because when a button event occurs, the
|
||||
`AceButton::checkDebounced()` method returns immediately until the debouncing
|
||||
time is over which brings down the minimum time. No debouncing is done in the
|
||||
"idle" case so the minimum code path takes more CPU cycles.
|
||||
|
||||
All times are in microseconds. The "samples" column is the number of
|
||||
`TimingStats::update()` calls that were made.
|
||||
|
||||
### Arduino Nano
|
||||
|
||||
* 16MHz ATmega328P
|
||||
|
||||
```
|
||||
------------------------+-------------+---------+
|
||||
button event | min/avg/max | samples |
|
||||
------------------------+-------------+---------+
|
||||
idle | 12/ 13/ 20 | 1934 |
|
||||
press/release | 8/ 14/ 20 | 1925 |
|
||||
click | 8/ 14/ 24 | 1925 |
|
||||
double click | 8/ 13/ 24 | 1925 |
|
||||
long press/repeat press | 8/ 15/ 24 | 1927 |
|
||||
------------------------+-------------+---------+
|
||||
```
|
||||
|
||||
### Arduino Pro Micro
|
||||
|
||||
* 16MHz ATmega32U4
|
||||
|
||||
```
|
||||
------------------------+-------------+---------+
|
||||
button event | min/avg/max | samples |
|
||||
------------------------+-------------+---------+
|
||||
idle | 12/ 13/ 24 | 1935 |
|
||||
press/release | 8/ 14/ 24 | 1928 |
|
||||
click | 8/ 13/ 24 | 1928 |
|
||||
double click | 8/ 13/ 24 | 1926 |
|
||||
long press/repeat press | 8/ 15/ 28 | 1928 |
|
||||
------------------------+-------------+---------+
|
||||
```
|
||||
|
||||
### Teensy 3.2
|
||||
|
||||
* 96 MHz ARM Cortex-M4
|
||||
|
||||
```
|
||||
------------------------+-------------+---------+
|
||||
button event | min/avg/max | samples |
|
||||
------------------------+-------------+---------+
|
||||
idle | 3/ 3/ 5 | 1985 |
|
||||
press/release | 1/ 3/ 6 | 1983 |
|
||||
click | 1/ 3/ 6 | 1984 |
|
||||
double click | 1/ 3/ 6 | 1984 |
|
||||
long press/repeat press | 1/ 3/ 6 | 1983 |
|
||||
------------------------+-------------+---------+
|
||||
```
|
||||
|
||||
### NodeMCU 1.0 clone
|
||||
|
||||
* 80MHz ESP8266
|
||||
|
||||
```
|
||||
------------------------+-------------+---------+
|
||||
button event | min/avg/max | samples |
|
||||
------------------------+-------------+---------+
|
||||
idle | 7/ 8/ 24 | 1922 |
|
||||
press/release | 6/ 8/ 53 | 1919 |
|
||||
click | 6/ 8/ 50 | 1920 |
|
||||
double click | 6/ 8/ 67 | 1910 |
|
||||
long press/repeat press | 6/ 9/ 60 | 1894 |
|
||||
------------------------+-------------+---------+
|
||||
```
|
||||
|
||||
The large **max** times for "double click" and "long press" seem to be
|
||||
reproducible. I have not researched this but my speculation is that the system
|
||||
WiFi code interrupts the `AceButton::check()` method right when the "double
|
||||
click" and "long press" samples are taken, causing the extra latency.
|
||||
|
||||
### ESP32-01 Dev Board
|
||||
|
||||
* 240 MHz Tensilica LX6
|
||||
|
||||
```
|
||||
------------------------+-------------+---------+
|
||||
button event | min/avg/max | samples |
|
||||
------------------------+-------------+---------+
|
||||
idle | 3/ 3/ 3 | 2002 |
|
||||
press/release | 2/ 2/ 8 | 2002 |
|
||||
click | 2/ 2/ 7 | 2002 |
|
||||
double click | 2/ 2/ 4 | 2002 |
|
||||
long press/repeat press | 2/ 2/ 4 | 2002 |
|
||||
------------------------+-------------+---------+
|
||||
```
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* A demo sketch that uses the AceButton Library
|
||||
* (https://github.com/bxparks/AceButton) and the CapitiveSense Library
|
||||
* (https://github.com/PaulStoffregen/CapacitiveSensor) to detect touches and
|
||||
* clicks of a capacitive switch.
|
||||
*
|
||||
* Prompted by questions from Gaston Loos.
|
||||
*
|
||||
* Brian T. Park 2018
|
||||
*/
|
||||
|
||||
#if defined(ESP32) || defined(ESP8266)
|
||||
#error ESP32 or ESP8266 not supported
|
||||
#endif
|
||||
|
||||
#include <CapacitiveSensor.h>
|
||||
#include <AceButton.h>
|
||||
using namespace ace_button;
|
||||
|
||||
/**
|
||||
* A subclass of ButtonConfig that emulates a mechanical switch connected to a
|
||||
* pull-up resistor on the input pin. A "touch" sends a LOW signal, just like a
|
||||
* mechnical switch.
|
||||
*/
|
||||
class CapacitiveConfig: public ButtonConfig {
|
||||
public:
|
||||
CapacitiveConfig(CapacitiveSensor& sensor):
|
||||
mSensor(sensor) {}
|
||||
|
||||
protected:
|
||||
// Number of iterations to sample the capacitive switch. Higher number
|
||||
// provides better smoothing but increases the time taken for a single read.
|
||||
static const uint8_t kSamples = 30;
|
||||
|
||||
// The threshold value which is considered to be a "touch" on the switch.
|
||||
static const long kTouchThreshold = 100;
|
||||
|
||||
int readButton(uint8_t /*pin*/) override {
|
||||
long total = mSensor.capacitiveSensor(kSamples);
|
||||
return (total > kTouchThreshold) ? LOW : HIGH;
|
||||
}
|
||||
|
||||
private:
|
||||
CapacitiveSensor& mSensor;
|
||||
};
|
||||
|
||||
// Timeout for a single read of the capacitive switch.
|
||||
static const unsigned long TIMEOUT_MILLIS = 10;
|
||||
|
||||
// I used a 1M resistor between pins 4 (send) & metal plate, and a 1K resistor
|
||||
// between the plate and pin 6 (receive). Try adjusting the
|
||||
// CapacitiveConfig::kTouchThreshold value for other resistor values.
|
||||
CapacitiveSensor capSensor(4, 6);
|
||||
|
||||
CapacitiveConfig buttonConfig(capSensor);
|
||||
AceButton button(&buttonConfig);
|
||||
|
||||
void handleEvent(AceButton* /* button */, uint8_t eventType,
|
||||
uint8_t /* buttonState */) {
|
||||
switch (eventType) {
|
||||
case AceButton::kEventPressed:
|
||||
Serial.println(F("Pressed"));
|
||||
break;
|
||||
case AceButton::kEventClicked:
|
||||
Serial.println(F("Clicked"));
|
||||
break;
|
||||
case AceButton::kEventDoubleClicked:
|
||||
Serial.println(F("DoubleClicked"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // Leonardo/Micro
|
||||
|
||||
// Set the timeout to 10 millisecond so that AceButton::check()
|
||||
// can have about 4-5 iterations during the 50 millisecond debouncing time.
|
||||
capSensor.set_CS_Timeout_Millis(TIMEOUT_MILLIS);
|
||||
|
||||
// Configure the button using CapacitiveConfig.
|
||||
buttonConfig.setFeature(ButtonConfig::kFeatureClick);
|
||||
buttonConfig.setFeature(ButtonConfig::kFeatureDoubleClick);
|
||||
buttonConfig.setEventHandler(handleEvent);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
unsigned long start = millis();
|
||||
button.check();
|
||||
|
||||
// check on performance in milliseconds
|
||||
unsigned long duration = millis() - start;
|
||||
if (duration > TIMEOUT_MILLIS) {
|
||||
Serial.print(F("duration: "));
|
||||
Serial.println(duration);
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* A demo that combines the techniques of ClickVersusDoubleClickUsingReleased
|
||||
* and ClickVersusDoubleClickUsingSuppression by using both event types to
|
||||
* trigger a "Clicked". I think this is the best of both worlds. If someone does
|
||||
* a simple Press/Release, the Release gets triggered. If someone does a quick
|
||||
* click, then a Click gets triggers (after a delay to wait for the potential
|
||||
* DoubleClick).
|
||||
*
|
||||
* See Also:
|
||||
* examples/ClickVersusDoubleClickUsingReleased/
|
||||
* examples/ClickVersusDoubleClickUsingSuppression/
|
||||
*/
|
||||
|
||||
#include <AceButton.h>
|
||||
using namespace ace_button;
|
||||
|
||||
// The pin number attached to the button.
|
||||
const int BUTTON_PIN = 2;
|
||||
|
||||
#ifdef ESP32
|
||||
// Different ESP32 boards use different pins
|
||||
const int LED_PIN = 2;
|
||||
#else
|
||||
const int LED_PIN = LED_BUILTIN;
|
||||
#endif
|
||||
|
||||
// LED states. Some microcontrollers wire their built-in LED the reverse.
|
||||
const int LED_ON = HIGH;
|
||||
const int LED_OFF = LOW;
|
||||
|
||||
// One button wired to the pin at BUTTON_PIN. Automatically uses the default
|
||||
// ButtonConfig. The alternative is to call the AceButton::init() method in
|
||||
// setup() below.
|
||||
AceButton button(BUTTON_PIN);
|
||||
|
||||
void handleEvent(AceButton*, uint8_t, uint8_t);
|
||||
|
||||
void setup() {
|
||||
// initialize built-in LED as an output
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
|
||||
// Button uses the built-in pull up register.
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
|
||||
ButtonConfig* buttonConfig = button.getButtonConfig();
|
||||
buttonConfig->setEventHandler(handleEvent);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
|
||||
buttonConfig->setFeature(
|
||||
ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterClick);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterDoubleClick);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Should be called every 20ms or faster for the default debouncing time
|
||||
// of ~50ms.
|
||||
button.check();
|
||||
}
|
||||
|
||||
// The event handler for the button.
|
||||
void handleEvent(AceButton* /* button */, uint8_t eventType,
|
||||
uint8_t /* buttonState */) {
|
||||
switch (eventType) {
|
||||
case AceButton::kEventClicked:
|
||||
case AceButton::kEventReleased:
|
||||
digitalWrite(LED_PIN, LED_ON);
|
||||
break;
|
||||
case AceButton::kEventDoubleClicked:
|
||||
digitalWrite(LED_PIN, LED_OFF);
|
||||
break;
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* A demo that distinguishes a "Clicked" from a DoubleClicked by using a
|
||||
* Released event instead of a Clicked. Released turns on the LED. A DoubleClick
|
||||
* turns off the LED.
|
||||
*
|
||||
* Normally, AceButton cannot separate a Clicked from a DoubleClicked because
|
||||
* the Clicked event will always trigger if a DoubleClicked occurs. We cannot
|
||||
* suppress the first Clicked because it has already occurred by the time the
|
||||
* DoubleClicked occurs, and the first Clicked cannot predict the future.
|
||||
*
|
||||
* This version uses a Released event instead of a Clicked to turn the LED on,
|
||||
* while suppressing the Released after a DoubleClicked, and we ignore the
|
||||
* Clicked event that we can't suppress. The disadvantage of this version is
|
||||
* that if a user accidentally makes a normal Clicked event (a rapid
|
||||
* Pressed/Released), nothing happens to the LED. Depending on the application,
|
||||
* this may or may not be the desirable result.
|
||||
*
|
||||
* See Also:
|
||||
* examples/ClickVersusDoubleClickUsingSuppression/
|
||||
* - uses the kFeatureSuppressClickBeforeDoubleClick
|
||||
*/
|
||||
|
||||
#include <AceButton.h>
|
||||
using namespace ace_button;
|
||||
|
||||
// The pin number attached to the button.
|
||||
const int BUTTON_PIN = 2;
|
||||
|
||||
#ifdef ESP32
|
||||
// Different ESP32 boards use different pins
|
||||
const int LED_PIN = 2;
|
||||
#else
|
||||
const int LED_PIN = LED_BUILTIN;
|
||||
#endif
|
||||
|
||||
// LED states. Some microcontrollers wire their built-in LED the reverse.
|
||||
const int LED_ON = HIGH;
|
||||
const int LED_OFF = LOW;
|
||||
|
||||
// One button wired to the pin at BUTTON_PIN. Automatically uses the default
|
||||
// ButtonConfig. The alternative is to call the AceButton::init() method in
|
||||
// setup() below.
|
||||
AceButton button(BUTTON_PIN);
|
||||
|
||||
void handleEvent(AceButton*, uint8_t, uint8_t);
|
||||
|
||||
void setup() {
|
||||
// initialize built-in LED as an output
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
|
||||
// Button uses the built-in pull up register.
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
|
||||
ButtonConfig* buttonConfig = button.getButtonConfig();
|
||||
buttonConfig->setEventHandler(handleEvent);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterClick);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterDoubleClick);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Should be called every 20ms or faster for the default debouncing time
|
||||
// of ~50ms.
|
||||
button.check();
|
||||
}
|
||||
|
||||
// The event handler for the button.
|
||||
void handleEvent(AceButton* /* button */, uint8_t eventType,
|
||||
uint8_t /* buttonState */) {
|
||||
switch (eventType) {
|
||||
case AceButton::kEventReleased:
|
||||
digitalWrite(LED_PIN, LED_ON);
|
||||
break;
|
||||
case AceButton::kEventDoubleClicked:
|
||||
digitalWrite(LED_PIN, LED_OFF);
|
||||
break;
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* A demo that uses kFeatureSuppressClickBeforeDoubleClick to distinguish a
|
||||
* Clicked event from a DoubleClicked event. Click turns on the LED. A
|
||||
* DoubleClick turns off the LED.
|
||||
*
|
||||
* The only way to suppress the Clicked "after" a DoubleClicked is to postpone
|
||||
* the sending of the Clicked event until getDoubleClickDelay() time after the
|
||||
* Clicked. At that time, we can tell if a DoubleClicked has occurred or not.
|
||||
* But this means that every Clicked event is delayed by (kClickDelay +
|
||||
* kDoubleClickDelay + 2 * kDebounceDelay) which is 700 ms using the default
|
||||
* values, and you'll notice this delay in the LED turning on.
|
||||
*
|
||||
* The other side-effect is that if a user doesn't input a clean Click (which
|
||||
* results in a normal Press/Release sequence), then nothing happens to the LED.
|
||||
* Depending on the application, this may or may not be the desirable result.
|
||||
|
||||
* See Also:
|
||||
* examples/ClickVersusDoubleClickUsingReleased/
|
||||
* - uses the Released event instead of the Clicked event
|
||||
*/
|
||||
|
||||
#include <AceButton.h>
|
||||
using namespace ace_button;
|
||||
|
||||
// The pin number attached to the button.
|
||||
const int BUTTON_PIN = 2;
|
||||
|
||||
#ifdef ESP32
|
||||
// Different ESP32 boards use different pins
|
||||
const int LED_PIN = 2;
|
||||
#else
|
||||
const int LED_PIN = LED_BUILTIN;
|
||||
#endif
|
||||
|
||||
// LED states. Some microcontrollers wire their built-in LED the reverse.
|
||||
const int LED_ON = HIGH;
|
||||
const int LED_OFF = LOW;
|
||||
|
||||
// One button wired to the pin at BUTTON_PIN. Automatically uses the default
|
||||
// ButtonConfig. The alternative is to call the AceButton::init() method in
|
||||
// setup() below.
|
||||
AceButton button(BUTTON_PIN);
|
||||
|
||||
void handleEvent(AceButton*, uint8_t, uint8_t);
|
||||
|
||||
void setup() {
|
||||
// initialize built-in LED as an output
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
|
||||
// Button uses the built-in pull up register.
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
|
||||
ButtonConfig* buttonConfig = button.getButtonConfig();
|
||||
buttonConfig->setEventHandler(handleEvent);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
|
||||
buttonConfig->setFeature(
|
||||
ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Should be called every 20ms or faster for the default debouncing time
|
||||
// of ~50ms.
|
||||
button.check();
|
||||
}
|
||||
|
||||
// The event handler for the button.
|
||||
void handleEvent(AceButton* /* button */, uint8_t eventType,
|
||||
uint8_t /* buttonState */) {
|
||||
switch (eventType) {
|
||||
case AceButton::kEventClicked:
|
||||
digitalWrite(LED_PIN, LED_ON);
|
||||
break;
|
||||
case AceButton::kEventDoubleClicked:
|
||||
digitalWrite(LED_PIN, LED_OFF);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* A demo of a simplest AceButton that has a visible effect. One button is
|
||||
* connected to the digital pin BUTTON_PIN. It uses the internal pull-up
|
||||
* resistor (INPUT_PULLUP). Pressing the button turns on the built-in LED.
|
||||
* Releasing the button turns off the LED.
|
||||
*/
|
||||
|
||||
#include <AceButton.h>
|
||||
|
||||
using namespace ace_button;
|
||||
|
||||
// Some ESP32 boards have multiple builtin LEDs so don't define LED_BUILTIN.
|
||||
#if defined(ESP32)
|
||||
const int LED_PIN = 2;
|
||||
#else
|
||||
const int LED_PIN = LED_BUILTIN;
|
||||
#endif
|
||||
|
||||
const int BUTTON_PIN = 2;
|
||||
const int LED_ON = HIGH;
|
||||
const int LED_OFF = LOW;
|
||||
|
||||
AceButton button(BUTTON_PIN);
|
||||
|
||||
void handleEvent(AceButton*, uint8_t, uint8_t);
|
||||
|
||||
void setup() {
|
||||
delay(2000);
|
||||
|
||||
#if defined(ARDUINO_AVR_LEONARDO)
|
||||
RXLED0; // LED off
|
||||
TXLED0; // LED off
|
||||
#endif
|
||||
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
button.setEventHandler(handleEvent);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
button.check();
|
||||
}
|
||||
|
||||
void handleEvent(AceButton* /* button */, uint8_t eventType,
|
||||
uint8_t /* buttonState */) {
|
||||
switch (eventType) {
|
||||
case AceButton::kEventPressed:
|
||||
digitalWrite(LED_PIN, LED_ON);
|
||||
break;
|
||||
case AceButton::kEventReleased:
|
||||
digitalWrite(LED_PIN, LED_OFF);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* A copy of HelloWorld which allows us to measure the size of the AceButton
|
||||
* library. Set USE_ACE_BUTTON to 1 to include AceButton, 0 to exclude
|
||||
* AceButton.
|
||||
*/
|
||||
|
||||
#include <AceButton.h>
|
||||
|
||||
using namespace ace_button;
|
||||
|
||||
// Set this to 0 to disable the AceButton code, so that we can
|
||||
// figure out how many bytes is consumed by the AceButton library.
|
||||
#define USE_ACE_BUTTON 1
|
||||
|
||||
// Some ESP32 boards have multiple builtin LEDs so don't define LED_BUILTIN.
|
||||
#if defined(ESP32)
|
||||
const int LED_PIN = 2;
|
||||
#else
|
||||
const int LED_PIN = LED_BUILTIN;
|
||||
#endif
|
||||
|
||||
const int BUTTON_PIN = 2;
|
||||
const int LED_ON = HIGH;
|
||||
const int LED_OFF = LOW;
|
||||
|
||||
#if USE_ACE_BUTTON == 1
|
||||
AceButton button(BUTTON_PIN);
|
||||
|
||||
void handleEvent(AceButton*, uint8_t, uint8_t);
|
||||
#endif
|
||||
|
||||
void setup() {
|
||||
delay(2000);
|
||||
|
||||
#if defined(ARDUINO_AVR_LEONARDO)
|
||||
RXLED0; // LED off
|
||||
TXLED0; // LED off
|
||||
#endif
|
||||
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
|
||||
#if USE_ACE_BUTTON == 1
|
||||
button.setEventHandler(handleEvent);
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop() {
|
||||
#if USE_ACE_BUTTON == 1
|
||||
button.check();
|
||||
#endif
|
||||
}
|
||||
|
||||
#if USE_ACE_BUTTON == 1
|
||||
void handleEvent(AceButton* /* button */, uint8_t eventType,
|
||||
uint8_t /* buttonState */) {
|
||||
switch (eventType) {
|
||||
case AceButton::kEventPressed:
|
||||
digitalWrite(LED_PIN, LED_ON);
|
||||
break;
|
||||
case AceButton::kEventReleased:
|
||||
digitalWrite(LED_PIN, LED_OFF);
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
# LibrarySizeBenchmark
|
||||
|
||||
A small sketch to determine the size of the AceButton library.
|
||||
First we compile it with `#define USE_ACE_BUTTON 1` to include the library.
|
||||
Then we compile it with `#define USE_ACE_BUTTON 0` to exclude the library.
|
||||
The difference should give us a rough idea of the size of the library.
|
||||
(The compiler will produce slightly difference results for different programs.)
|
||||
|
||||
```
|
||||
-------------+--------------+---------------+------------+
|
||||
board | AceButton | w/o AceButton | Difference |
|
||||
-------------+--------------+---------------+------------+
|
||||
ATmega328P | 2282/ 55 | 1126 / 41 | 1156/14 |
|
||||
ESP8266 | 249364/28076 | 248032/28048 | 1332/28 |
|
||||
ESP32 | 195588/14044 | 194424/14028 | 1164/16 |
|
||||
Teensy 3.2 | 9852/ 3476 | 8760/ 3460 | 1092/16 |
|
||||
-------------+--------------+---------------+------------+
|
||||
```
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* A demo of a simple AceButton used to handle the events from one button.
|
||||
* Similar to HelloButton with some additions;
|
||||
* - more comments
|
||||
* - prints out the button events to the Serial monitor
|
||||
* - enables the all button events, including LongPress and RepeatPress
|
||||
* - suppresses lower-level events when higher-level events are detected
|
||||
* (e.g. Clicked suppressed Released, DoubleClicked suppresses the
|
||||
* second Clicked, LongPressed suppressed the Released, etc.)
|
||||
* - setup() determines if a button was pressed during reboot
|
||||
*/
|
||||
|
||||
#include <AceButton.h>
|
||||
using namespace ace_button;
|
||||
|
||||
// The pin number attached to the button.
|
||||
const int BUTTON_PIN = 2;
|
||||
|
||||
#ifdef ESP32
|
||||
// Different ESP32 boards use different pins
|
||||
const int LED_PIN = 2;
|
||||
#else
|
||||
const int LED_PIN = LED_BUILTIN;
|
||||
#endif
|
||||
|
||||
// LED states. Some microcontrollers wire their built-in LED the reverse.
|
||||
const int LED_ON = HIGH;
|
||||
const int LED_OFF = LOW;
|
||||
|
||||
// One button wired to the pin at BUTTON_PIN. Automatically uses the default
|
||||
// ButtonConfig. The alternative is to call the AceButton::init() method in
|
||||
// setup() below.
|
||||
AceButton button(BUTTON_PIN);
|
||||
|
||||
void handleEvent(AceButton*, uint8_t, uint8_t);
|
||||
|
||||
void setup() {
|
||||
delay(1000); // some microcontrollers reboot twice
|
||||
Serial.begin(115200);
|
||||
while (! Serial); // Wait until Serial is ready - Leonardo/Micro
|
||||
Serial.println(F("setup(): begin"));
|
||||
|
||||
// initialize built-in LED as an output
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
|
||||
// Button uses the built-in pull up register.
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
|
||||
// Configure the ButtonConfig with the event handler, and enable all higher
|
||||
// level events.
|
||||
ButtonConfig* buttonConfig = button.getButtonConfig();
|
||||
buttonConfig->setEventHandler(handleEvent);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureClick);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
|
||||
|
||||
// Check if the button was pressed while booting
|
||||
if (button.isPressedRaw()) {
|
||||
Serial.println(F("setup(): button was pressed while booting"));
|
||||
}
|
||||
|
||||
Serial.println(F("setup(): ready"));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Should be called every 4-5ms or faster, for the default debouncing time
|
||||
// of ~20ms.
|
||||
button.check();
|
||||
}
|
||||
|
||||
// The event handler for the button.
|
||||
void handleEvent(AceButton* /* button */, uint8_t eventType,
|
||||
uint8_t buttonState) {
|
||||
|
||||
// Print out a message for all events.
|
||||
Serial.print(F("handleEvent(): eventType: "));
|
||||
Serial.print(eventType);
|
||||
Serial.print(F("; buttonState: "));
|
||||
Serial.println(buttonState);
|
||||
|
||||
// Control the LED only for the Pressed and Released events.
|
||||
// Notice that if the MCU is rebooted while the button is pressed down, no
|
||||
// event is triggered and the LED remains off.
|
||||
switch (eventType) {
|
||||
case AceButton::kEventPressed:
|
||||
digitalWrite(LED_PIN, LED_ON);
|
||||
break;
|
||||
case AceButton::kEventReleased:
|
||||
digitalWrite(LED_PIN, LED_OFF);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* A demo of a simple AceButton used to handle the events from one button.
|
||||
* Pretty much the same as SingleButton.ino example but the button is wired with
|
||||
* an external pull-down resistor, instead of using the built-in pull-up
|
||||
* resistor.
|
||||
*/
|
||||
|
||||
#include <AceButton.h>
|
||||
using namespace ace_button;
|
||||
|
||||
// The pin number attached to the button.
|
||||
const int BUTTON_PIN = 7;
|
||||
|
||||
#ifdef ESP32
|
||||
// Different ESP32 boards use different pins
|
||||
const int LED_PIN = 2;
|
||||
#else
|
||||
const int LED_PIN = LED_BUILTIN;
|
||||
#endif
|
||||
|
||||
// LED states. Some microcontrollers wire their built-in LED the reverse.
|
||||
const int LED_ON = HIGH;
|
||||
const int LED_OFF = LOW;
|
||||
|
||||
// Automatically uses the default ButtonConfig. We will configure this later
|
||||
// using AceButton::init() method in setup() below.
|
||||
AceButton button;
|
||||
|
||||
void handleEvent(AceButton*, uint8_t, uint8_t);
|
||||
|
||||
void setup() {
|
||||
delay(1000); // some microcontrollers reboot twice
|
||||
Serial.begin(115200);
|
||||
while (! Serial); // Wait until Serial is ready - Leonardo/Micro
|
||||
Serial.println(F("setup(): begin"));
|
||||
|
||||
// initialize built-in LED as an output
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
|
||||
// Button uses an external 10k resistor.
|
||||
pinMode(BUTTON_PIN, INPUT);
|
||||
|
||||
// We use the AceButton::init() method here instead of using the constructor
|
||||
// to show an alternative. Using init() allows the configuration of the
|
||||
// hardware pin and the button to be placed closer to each other.
|
||||
button.init(BUTTON_PIN, LOW);
|
||||
|
||||
// Configure the ButtonConfig with the event handler, and enable the LongPress
|
||||
// and RepeatPress events which are turned off by default.
|
||||
ButtonConfig* buttonConfig = button.getButtonConfig();
|
||||
buttonConfig->setEventHandler(handleEvent);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureClick);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
|
||||
|
||||
Serial.println(F("setup(): ready"));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Should be called every 20ms or faster for the default debouncing time
|
||||
// of ~50ms.
|
||||
button.check();
|
||||
}
|
||||
|
||||
// The event handler for the button.
|
||||
void handleEvent(AceButton* /* button */, uint8_t eventType,
|
||||
uint8_t buttonState) {
|
||||
|
||||
// Print out a message for all events.
|
||||
Serial.print(F("handleEvent(): eventType: "));
|
||||
Serial.print(eventType);
|
||||
Serial.print(F("; buttonState: "));
|
||||
Serial.println(buttonState);
|
||||
|
||||
// Control the LED only for the Pressed and Released events.
|
||||
// Notice that if the MCU is rebooted while the button is pressed down, no
|
||||
// event is triggered and the LED remains off.
|
||||
switch (eventType) {
|
||||
case AceButton::kEventReleased:
|
||||
digitalWrite(LED_PIN, LED_ON);
|
||||
break;
|
||||
case AceButton::kEventPressed:
|
||||
digitalWrite(LED_PIN, LED_OFF);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* A demo app that calculates the ammount of time taken by the
|
||||
* AceButton::check() method. It acts like a stopwatch, supporting the following
|
||||
* operations:
|
||||
*
|
||||
* - press: start the stopwatch
|
||||
* - press: stop the stopwatch, printing out the result
|
||||
* - long press: reset the stopwatch, allowing press to start the process again
|
||||
*
|
||||
* Each 'long press' alternates between enableAllEvents() and disableAllEvents()
|
||||
* so that we can compare the timing of check() when all events are off (except
|
||||
* Pressed and Released) and when all events are on.
|
||||
*/
|
||||
|
||||
#include <AceButton.h>
|
||||
|
||||
using namespace ace_button;
|
||||
|
||||
// The pin number attached to the button.
|
||||
const uint8_t BUTTON_PIN = 2;
|
||||
|
||||
// Create one button.
|
||||
AceButton button;
|
||||
|
||||
// counters to determine the duration of a single call to AceButton::check()
|
||||
uint16_t innerLoopCounter = 0;
|
||||
uint16_t outerLoopCounter = 0;
|
||||
unsigned long startMillis = 0;
|
||||
unsigned long stopMillis = 0;
|
||||
|
||||
// states of the stopwatch
|
||||
const uint8_t STOPWATCH_INIT = 0;
|
||||
const uint8_t STOPWATCH_STARTED = 1;
|
||||
const uint8_t STOPWATCH_STOPPED = 2;
|
||||
|
||||
// implements a finite state machine (FSM)
|
||||
uint8_t stopwatchState = STOPWATCH_INIT;
|
||||
|
||||
bool allEventsEnabled = false;
|
||||
|
||||
void handleEvent(AceButton*, uint8_t, uint8_t);
|
||||
|
||||
void setup() {
|
||||
delay(1000); // some boards reboot twice
|
||||
Serial.begin(115200);
|
||||
while (! Serial); // Wait until Serial is ready - Leonardo/Micro
|
||||
Serial.println(F("setup(): begin"));
|
||||
|
||||
// Button uses the built-in pull up register.
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
button.init(BUTTON_PIN);
|
||||
|
||||
// Configure the ButtonConfig with the event handler and enable LongPress.
|
||||
// SupressAfterLongPress is optional since we don't do anything if we get it.
|
||||
ButtonConfig* buttonConfig = button.getButtonConfig();
|
||||
buttonConfig->setEventHandler(handleEvent);
|
||||
buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
|
||||
buttonConfig->setLongPressDelay(2000);
|
||||
|
||||
Serial.println(F("setup(): stopwatch ready"));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// We split the loop into an inner loop and an outer loop. The inner loop
|
||||
// allows us to measure the speed of button.check() without the overhead of
|
||||
// the outer loop. However, we must allow the outer loop() method to return
|
||||
// periodically to allow the microcontroller to its own stuff. This is
|
||||
// especially true on an ESP8266 board, where a Watch Dog Timer will
|
||||
// soft-reset the board if loop() doesn't return every few seconds.
|
||||
do {
|
||||
// button.check() Should be called every 20ms or faster for the default
|
||||
// debouncing time of ~50ms.
|
||||
button.check();
|
||||
|
||||
// increment loop counter
|
||||
if (stopwatchState == STOPWATCH_STARTED) {
|
||||
innerLoopCounter++;
|
||||
}
|
||||
} while (innerLoopCounter);
|
||||
|
||||
// Each time the innerLoopCounter rolls over (65536), increment the outer loop
|
||||
// counter, and return from loop(), to prevent WDT errors on ESP8266.
|
||||
outerLoopCounter++;
|
||||
}
|
||||
|
||||
// The event handler for the button.
|
||||
void handleEvent(AceButton* /* button */, uint8_t eventType,
|
||||
uint8_t /* buttonState */) {
|
||||
unsigned long now = millis();
|
||||
switch (eventType) {
|
||||
case AceButton::kEventPressed:
|
||||
if (stopwatchState == STOPWATCH_INIT) {
|
||||
|
||||
// enable or disable higher level events, to get different performance
|
||||
// numbers
|
||||
if (allEventsEnabled) {
|
||||
enableAllEvents();
|
||||
} else {
|
||||
disableAllEvents();
|
||||
}
|
||||
|
||||
Serial.println(F("handleEvent(): stopwatch started"));
|
||||
startMillis = now;
|
||||
innerLoopCounter = 0;
|
||||
outerLoopCounter = 0;
|
||||
stopwatchState = STOPWATCH_STARTED;
|
||||
} else if (stopwatchState == STOPWATCH_STARTED) {
|
||||
stopMillis = now;
|
||||
stopwatchState = STOPWATCH_STOPPED;
|
||||
unsigned long duration = stopMillis - startMillis;
|
||||
uint32_t loopCounter = ((uint32_t) outerLoopCounter << 16) +
|
||||
innerLoopCounter;
|
||||
float microsPerLoop = duration * 1000.0f / loopCounter;
|
||||
|
||||
// reenable all events after stopping
|
||||
enableAllEvents();
|
||||
|
||||
Serial.println(F("handleEvent(): stopwatch stopped"));
|
||||
Serial.print(F("handleEvent(): duration (ms): "));
|
||||
Serial.print(duration);
|
||||
Serial.print(F("; loopCount: "));
|
||||
Serial.print(loopCounter);
|
||||
Serial.print(F("; micros/loop: "));
|
||||
Serial.println(microsPerLoop);
|
||||
|
||||
// Setting 0 allows the loop() function to return periodically.
|
||||
innerLoopCounter = 0;
|
||||
}
|
||||
break;
|
||||
case AceButton::kEventLongPressed:
|
||||
if (stopwatchState == STOPWATCH_STOPPED) {
|
||||
stopwatchState = STOPWATCH_INIT;
|
||||
Serial.println(F("handleEvent(): stopwatch reset"));
|
||||
allEventsEnabled = !allEventsEnabled;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void enableAllEvents() {
|
||||
Serial.println(F("enabling high level events"));
|
||||
button.getButtonConfig()->setFeature(ButtonConfig::kFeatureClick);
|
||||
button.getButtonConfig()->setFeature(ButtonConfig::kFeatureDoubleClick);
|
||||
button.getButtonConfig()->setFeature(ButtonConfig::kFeatureLongPress);
|
||||
button.getButtonConfig()->setFeature(ButtonConfig::kFeatureRepeatPress);
|
||||
}
|
||||
|
||||
void disableAllEvents() {
|
||||
Serial.println(F("disabling high level events"));
|
||||
button.getButtonConfig()->clearFeature(ButtonConfig::kFeatureClick);
|
||||
button.getButtonConfig()->clearFeature(ButtonConfig::kFeatureDoubleClick);
|
||||
button.getButtonConfig()->clearFeature(ButtonConfig::kFeatureLongPress);
|
||||
button.getButtonConfig()->clearFeature(ButtonConfig::kFeatureRepeatPress);
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* Emulate the buttons of a car radio:
|
||||
*
|
||||
* - 3 preset buttons
|
||||
* - 1 tune-up button
|
||||
* - 1 tune-down button
|
||||
*
|
||||
* LongPress of a preset button stores the station.
|
||||
* LongPress of a tune-up or tune-down button scans the radio frequency.
|
||||
*/
|
||||
|
||||
#include <AceButton.h>
|
||||
|
||||
using namespace ace_button;
|
||||
|
||||
const int NUM_PRESETS = 3;
|
||||
|
||||
const int PRESET_1_PIN = 6;
|
||||
const int PRESET_2_PIN = 5;
|
||||
const int PRESET_3_PIN = 4;
|
||||
const int TUNE_DOWN_PIN = 3;
|
||||
const int TUNE_UP_PIN = 2;
|
||||
|
||||
// Frequency range of the FM radio.
|
||||
const uint16_t FM_FREQ_MIN = 879; // 87.9 MHz
|
||||
const uint16_t FM_FREQ_MAX = 1079; // 107.9 MHz
|
||||
const uint16_t FM_FREQ_DELTA = 2; // 0.2 MHz increments in USA
|
||||
|
||||
// Preset buttons use the SystemButtonConfig.
|
||||
AceButton presetButton1;
|
||||
AceButton presetButton2;
|
||||
AceButton presetButton3;
|
||||
|
||||
// Tune up or down buttons use a different ButtonConfig.
|
||||
ButtonConfig tuneConfig;
|
||||
AceButton tuneDownButton(&tuneConfig);
|
||||
AceButton tuneUpButton(&tuneConfig);
|
||||
|
||||
// Array to hold the stations associated with the button whose id is an
|
||||
// index into this array. The value is the 10 * station_frequency (e.g. 88.5 MHz
|
||||
// is stored as 885).
|
||||
//
|
||||
// NOTE: It might be tempting to subclass the AceButton to hold this information
|
||||
// but it's cleaner to keep this info separated from the AceButton class
|
||||
// hierarchy.
|
||||
uint16_t stations[NUM_PRESETS];
|
||||
|
||||
// The current station frequency.
|
||||
uint16_t currentStation = FM_FREQ_MIN;
|
||||
|
||||
void handlePresetEvent(AceButton*, uint8_t, uint8_t);
|
||||
void handleTuneEvent(AceButton*, uint8_t, uint8_t);
|
||||
|
||||
void setup() {
|
||||
delay(1000); // some boards reboot twice
|
||||
Serial.begin(115200);
|
||||
while (! Serial); // Wait until Serial is ready - Leonardo/Micro
|
||||
Serial.println(F("setup(): begin()"));
|
||||
|
||||
// Configs for the preset buttons. Need Released event to change the station,
|
||||
// and LongPressed to memorize the current station. Don't need Clicked.
|
||||
ButtonConfig* presetConfig = ButtonConfig::getSystemButtonConfig();
|
||||
presetConfig->setEventHandler(handlePresetEvent);
|
||||
presetConfig->setFeature(ButtonConfig::kFeatureLongPress);
|
||||
presetConfig->setFeature(ButtonConfig::kFeatureSuppressAfterLongPress);
|
||||
|
||||
// Configs for the tune-up and tune-down buttons. Need RepeatPress instead of
|
||||
// LongPress.
|
||||
tuneConfig.setEventHandler(handleTuneEvent);
|
||||
tuneConfig.setFeature(ButtonConfig::kFeatureClick);
|
||||
tuneConfig.setFeature(ButtonConfig::kFeatureRepeatPress);
|
||||
// These suppressions not really necessary but cleaner.
|
||||
tuneConfig.setFeature(ButtonConfig::kFeatureSuppressAfterClick);
|
||||
tuneConfig.setFeature(ButtonConfig::kFeatureSuppressAfterRepeatPress);
|
||||
|
||||
// Preset Button 1
|
||||
pinMode(PRESET_1_PIN, INPUT_PULLUP);
|
||||
presetButton1.init(PRESET_1_PIN, HIGH, 0 /* id */);
|
||||
|
||||
// Preset Button 2
|
||||
pinMode(PRESET_2_PIN, INPUT_PULLUP);
|
||||
presetButton2.init(PRESET_2_PIN, HIGH, 1 /* id */);
|
||||
|
||||
// Preset Button 3
|
||||
pinMode(PRESET_3_PIN, INPUT_PULLUP);
|
||||
presetButton3.init(PRESET_3_PIN, HIGH, 2 /* id */);
|
||||
|
||||
// Tune Down Button
|
||||
pinMode(TUNE_DOWN_PIN, INPUT_PULLUP);
|
||||
tuneDownButton.init(TUNE_DOWN_PIN);
|
||||
|
||||
// Tune Up Button
|
||||
pinMode(TUNE_UP_PIN, INPUT_PULLUP);
|
||||
tuneUpButton.init(TUNE_UP_PIN);
|
||||
|
||||
// init the stations associated with each button
|
||||
initStations();
|
||||
|
||||
Serial.println(F("setup(): tuner buttons ready"));
|
||||
printStation(currentStation);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
presetButton1.check();
|
||||
presetButton2.check();
|
||||
presetButton3.check();
|
||||
tuneUpButton.check();
|
||||
tuneDownButton.check();
|
||||
}
|
||||
|
||||
void initStations() {
|
||||
for (int i = 0; i < NUM_PRESETS; i++) {
|
||||
stations[i] = FM_FREQ_MIN;
|
||||
}
|
||||
}
|
||||
|
||||
void printStation(uint16_t frequency) {
|
||||
Serial.print(F("station: "));
|
||||
Serial.print(frequency / 10);
|
||||
Serial.print('.');
|
||||
Serial.println(frequency % 10);
|
||||
}
|
||||
|
||||
void handlePresetEvent(AceButton* button, uint8_t eventType,
|
||||
uint8_t /* buttonState */) {
|
||||
switch (eventType) {
|
||||
case AceButton::kEventReleased:
|
||||
// We trigger on the Released event not the Pressed event to distinguish
|
||||
// this event from the LongPressed event.
|
||||
retrievePreset(button->getId());
|
||||
break;
|
||||
case AceButton::kEventLongPressed:
|
||||
setPreset(button->getId());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void handleTuneEvent(AceButton* button, uint8_t eventType,
|
||||
uint8_t /* buttonState */) {
|
||||
switch (eventType) {
|
||||
case AceButton::kEventPressed:
|
||||
case AceButton::kEventRepeatPressed: {
|
||||
uint8_t pin = button->getPin();
|
||||
switch (pin) {
|
||||
case TUNE_UP_PIN:
|
||||
tuneUp();
|
||||
break;
|
||||
case TUNE_DOWN_PIN:
|
||||
tuneDown();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void retrievePreset(uint8_t id) {
|
||||
if (id < NUM_PRESETS) {
|
||||
currentStation = stations[id];
|
||||
Serial.print(F("memory "));
|
||||
Serial.print(id + 1);
|
||||
Serial.print(F(": "));
|
||||
printStation(currentStation);
|
||||
}
|
||||
}
|
||||
|
||||
void setPreset(uint8_t id) {
|
||||
if (id < NUM_PRESETS) {
|
||||
stations[id] = currentStation;
|
||||
|
||||
Serial.print(F("memory "));
|
||||
Serial.print(id + 1);
|
||||
Serial.print(F(" set: "));
|
||||
printStation(currentStation);
|
||||
}
|
||||
}
|
||||
|
||||
void tuneUp() {
|
||||
currentStation += FM_FREQ_DELTA;
|
||||
if (currentStation > FM_FREQ_MAX) {
|
||||
currentStation = FM_FREQ_MIN;
|
||||
}
|
||||
printStation(currentStation);
|
||||
}
|
||||
|
||||
void tuneDown() {
|
||||
currentStation -= FM_FREQ_DELTA;
|
||||
if (currentStation < FM_FREQ_MIN) {
|
||||
currentStation = FM_FREQ_MAX;
|
||||
}
|
||||
printStation(currentStation);
|
||||
}
|
||||
Reference in New Issue
Block a user