Refactor source code, update example sketches.
This commit is contained in:
@@ -1,58 +1,52 @@
|
|||||||
/*----------------------------------------------------------------------*
|
// Arduino Button Library
|
||||||
* Example sketch for Arduino Button Library by Jack Christensen *
|
// https://github.com/JChristensen/JC_Button
|
||||||
* *
|
// Copyright (C) 2018 by Jack Christensen and licensed under
|
||||||
* An example that uses both short and long button presses. *
|
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
|
||||||
* *
|
//
|
||||||
* A simple state machine where a short press of the button turns the *
|
// Example sketch demonstrating short and long button presses.
|
||||||
* Arduino pin 13 LED on or off, and a long press causes the LED to *
|
//
|
||||||
* blink rapidly. Once in rapid blink mode, another long press goes *
|
// A simple state machine where a short press of the button turns the
|
||||||
* back to on/off mode. *
|
// Arduino pin 13 LED on or off, and a long press causes the LED to
|
||||||
* *
|
// blink rapidly. Once in rapid blink mode, another long press goes
|
||||||
* This work is licensed under the Creative Commons Attribution- *
|
// back to on/off mode.
|
||||||
* ShareAlike 3.0 Unported License. To view a copy of this license, *
|
|
||||||
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
|
|
||||||
* letter to Creative Commons, 171 Second Street, Suite 300, *
|
|
||||||
* San Francisco, California, 94105, USA. *
|
|
||||||
*----------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
|
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
|
||||||
|
|
||||||
#define BUTTON_PIN 7 //Connect a tactile button switch (or something similar) from this pin to ground.
|
// pin assignments
|
||||||
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
|
const byte
|
||||||
#define INVERT true //Since the pullup resistor will keep the pin high unless the
|
BUTTON_PIN(7), // connect a button switch from this pin to ground
|
||||||
//switch is closed, this is negative logic, i.e. a high state
|
LED_PIN(13); // the standard Arduino "pin 13" LED
|
||||||
//means the button is NOT pressed. (Assuming a normally open switch.)
|
|
||||||
#define DEBOUNCE_MS 25 //A debounce time of 20 milliseconds usually works well for tactile button switches.
|
|
||||||
|
|
||||||
#define LED_PIN 13 //The standard Arduino "Pin 13" LED.
|
Button myBtn(BUTTON_PIN); // define the button
|
||||||
#define LONG_PRESS 1000 //We define a "long press" to be 1000 milliseconds.
|
const unsigned long
|
||||||
#define BLINK_INTERVAL 100 //In the BLINK state, switch the LED every 100 milliseconds.
|
LONG_PRESS(1000), // we define a "long press" to be 1000 milliseconds.
|
||||||
|
BLINK_INTERVAL(100); // in the BLINK state, switch the LED every 100 milliseconds.
|
||||||
Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Define the button
|
|
||||||
|
|
||||||
//The list of possible states for the state machine. This state machine has a fixed
|
|
||||||
//sequence of states, i.e. ONOFF --> TO_BLINK --> BLINK --> TO_ONOFF --> ONOFF
|
|
||||||
//Note that while the user perceives two "modes", i.e. ON/OFF mode and rapid blink mode,
|
|
||||||
//two extra states are needed in the state machine to transition between these modes.
|
|
||||||
enum {ONOFF, TO_BLINK, BLINK, TO_ONOFF};
|
|
||||||
uint8_t STATE; //The current state machine state
|
|
||||||
boolean ledState; //The current LED status
|
|
||||||
unsigned long ms; //The current time from millis()
|
|
||||||
unsigned long msLast; //The last time the LED was switched
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(LED_PIN, OUTPUT); //Set the LED pin as an output
|
myBtn.begin(); // initialize the button object
|
||||||
|
pinMode(LED_PIN, OUTPUT); // set the LED pin as an output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the list of possible states for the state machine. This state machine has a fixed
|
||||||
|
// sequence of states, i.e. ONOFF --> TO_BLINK --> BLINK --> TO_ONOFF --> ONOFF
|
||||||
|
// note that while the user perceives two "modes", i.e. ON/OFF mode and rapid blink mode,
|
||||||
|
// two extra states are needed in the state machine to transition between these modes.
|
||||||
|
enum states_t {ONOFF, TO_BLINK, BLINK, TO_ONOFF};
|
||||||
|
|
||||||
|
bool ledState; // current LED status
|
||||||
|
unsigned long ms; // current time from millis()
|
||||||
|
unsigned long msLast; // last time the LED was switched
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
static states_t STATE; // current state machine state
|
||||||
ms = millis(); // record the current time
|
ms = millis(); // record the current time
|
||||||
myBtn.read(); //Read the button
|
myBtn.read(); // read the button
|
||||||
|
|
||||||
switch (STATE) {
|
switch (STATE)
|
||||||
|
{
|
||||||
//This state watches for short and long presses, switches the LED for
|
// this state watches for short and long presses, switches the LED for
|
||||||
// short presses, and moves to the TO_BLINK state for long presses.
|
// short presses, and moves to the TO_BLINK state for long presses.
|
||||||
case ONOFF:
|
case ONOFF:
|
||||||
if (myBtn.wasReleased())
|
if (myBtn.wasReleased())
|
||||||
@@ -61,7 +55,7 @@ void loop()
|
|||||||
STATE = TO_BLINK;
|
STATE = TO_BLINK;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//This is a transition state where we start the fast blink as feedback to the user,
|
// this is a transition state where we start the fast blink as feedback to the user,
|
||||||
// but we also need to wait for the user to release the button, i.e. end the
|
// but we also need to wait for the user to release the button, i.e. end the
|
||||||
// long press, before moving to the BLINK state.
|
// long press, before moving to the BLINK state.
|
||||||
case TO_BLINK:
|
case TO_BLINK:
|
||||||
@@ -71,10 +65,11 @@ void loop()
|
|||||||
fastBlink();
|
fastBlink();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//The fast-blink state. Watch for another long press which will cause us to
|
// the fast-blink state. Watch for another long press which will cause us to
|
||||||
// turn the LED off (as feedback to the user) and move to the TO_ONOFF state.
|
// turn the LED off (as feedback to the user) and move to the TO_ONOFF state.
|
||||||
case BLINK:
|
case BLINK:
|
||||||
if (myBtn.pressedFor(LONG_PRESS)) {
|
if (myBtn.pressedFor(LONG_PRESS))
|
||||||
|
{
|
||||||
STATE = TO_ONOFF;
|
STATE = TO_ONOFF;
|
||||||
digitalWrite(LED_PIN, LOW);
|
digitalWrite(LED_PIN, LOW);
|
||||||
ledState = false;
|
ledState = false;
|
||||||
@@ -83,7 +78,7 @@ void loop()
|
|||||||
fastBlink();
|
fastBlink();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//This is a transition state where we just wait for the user to release the button
|
// this is a transition state where we just wait for the user to release the button
|
||||||
// before moving back to the ONOFF state.
|
// before moving back to the ONOFF state.
|
||||||
case TO_ONOFF:
|
case TO_ONOFF:
|
||||||
if (myBtn.wasReleased())
|
if (myBtn.wasReleased())
|
||||||
@@ -92,7 +87,7 @@ void loop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Reverse the current LED state. If it's on, turn it off. If it's off, turn it on.
|
// reverse the current LED state. if it's on, turn it off. If it's off, turn it on.
|
||||||
void switchLED()
|
void switchLED()
|
||||||
{
|
{
|
||||||
msLast = ms; // record the last switch time
|
msLast = ms; // record the last switch time
|
||||||
@@ -100,7 +95,7 @@ void switchLED()
|
|||||||
digitalWrite(LED_PIN, ledState);
|
digitalWrite(LED_PIN, ledState);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Switch the LED on and off every BLINK_INETERVAL milliseconds.
|
// switch the LED on and off every BLINK_INETERVAL milliseconds.
|
||||||
void fastBlink()
|
void fastBlink()
|
||||||
{
|
{
|
||||||
if (ms - msLast >= BLINK_INTERVAL)
|
if (ms - msLast >= BLINK_INTERVAL)
|
||||||
|
|||||||
@@ -1,43 +1,35 @@
|
|||||||
/*----------------------------------------------------------------------*
|
// Arduino Button Library
|
||||||
* Example sketch for Arduino Button Library by Jack Christensen *
|
// https://github.com/JChristensen/JC_Button
|
||||||
* *
|
// Copyright (C) 2018 by Jack Christensen and licensed under
|
||||||
* The simplest example. Using a tactile button switch to turn *
|
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
|
||||||
* the Arduino's pin 13 LED on and off. Wire a switch from Arduino *
|
//
|
||||||
* pin 2 to ground. *
|
// Example sketch to turn an LED on and off with a tactile button switch.
|
||||||
* *
|
// Wire the switch from the Arduino pin to ground.
|
||||||
* This work is licensed under the Creative Commons Attribution- *
|
|
||||||
* ShareAlike 3.0 Unported License. To view a copy of this license, *
|
|
||||||
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
|
|
||||||
* letter to Creative Commons, 171 Second Street, Suite 300, *
|
|
||||||
* San Francisco, California, 94105, USA. *
|
|
||||||
*----------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
|
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
|
||||||
|
|
||||||
#define BUTTON_PIN 7 //Connect a tactile button switch (or something similar) from this pin to ground.
|
// pin assignments
|
||||||
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
|
const byte
|
||||||
#define INVERT true //Since the pullup resistor will keep the pin high unless the
|
BUTTON_PIN(7), // connect a button switch from this pin to ground
|
||||||
//switch is closed, this is negative logic, i.e. a high state
|
LED_PIN(13); // the standard Arduino "pin 13" LED
|
||||||
//means the button is NOT pressed. (Assuming a normally open switch.)
|
|
||||||
#define DEBOUNCE_MS 25 //A debounce time of 25 milliseconds usually works well for tactile button switches.
|
|
||||||
#define LED_PIN 13 //The standard Arduino "Pin 13" LED
|
|
||||||
|
|
||||||
Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Define the button
|
Button myBtn(BUTTON_PIN); // define the button
|
||||||
boolean ledState; //A variable that keeps the current LED status
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(LED_PIN, OUTPUT); //Set the LED pin as an output
|
myBtn.begin(); // initialize the button object
|
||||||
|
pinMode(LED_PIN, OUTPUT); // set the LED pin as an output
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
myBtn.read(); //Read the button
|
static bool ledState; // a variable that keeps the current LED status
|
||||||
|
myBtn.read(); // read the button
|
||||||
|
|
||||||
if (myBtn.wasReleased()) { //If the button was released, change the LED state
|
if (myBtn.wasReleased()) // if the button was released, change the LED state
|
||||||
|
{
|
||||||
ledState = !ledState;
|
ledState = !ledState;
|
||||||
digitalWrite(LED_PIN, ledState);
|
digitalWrite(LED_PIN, ledState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,59 +1,57 @@
|
|||||||
/*----------------------------------------------------------------------*
|
// Arduino Button Library
|
||||||
* Example sketch for Arduino Button Library by Jack Christensen *
|
// https://github.com/JChristensen/JC_Button
|
||||||
* *
|
// Copyright (C) 2018 by Jack Christensen and licensed under
|
||||||
* An example that uses both short and long button presses to adjust *
|
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
|
||||||
* a number up and down, between two limits. Short presses increment *
|
//
|
||||||
* or decrement by one, long presses repeat at a specified rate. *
|
// Example skletch that uses both short and long button presses to adjust
|
||||||
* Every time the number changes, it is written to the serial monitor. *
|
// a number up and down, between two limits. Short presses increment
|
||||||
* *
|
// or decrement by one, long presses repeat at a specified rate.
|
||||||
* This work is licensed under the Creative Commons Attribution- *
|
// Every time the number changes, it is written to the serial monitor.
|
||||||
* ShareAlike 3.0 Unported License. To view a copy of this license, *
|
|
||||||
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
|
|
||||||
* letter to Creative Commons, 171 Second Street, Suite 300, *
|
|
||||||
* San Francisco, California, 94105, USA. *
|
|
||||||
*----------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
|
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
|
||||||
|
|
||||||
#define DN_PIN 7 //Connect two tactile button switches (or something similar)
|
// pin assignments
|
||||||
#define UP_PIN 8 //from Arduino pin 7 to ground and from pin 8 to ground.
|
const byte
|
||||||
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
|
DN_PIN(7), // connect a button switch from this pin to ground
|
||||||
#define INVERT true //Since the pullup resistor will keep the pin high unless the
|
UP_PIN(8); // ditto
|
||||||
//switch is closed, this is negative logic, i.e. a high state
|
|
||||||
//means the button is NOT pressed. (Assuming a normally open switch.)
|
|
||||||
#define DEBOUNCE_MS 25 //A debounce time of 20 milliseconds usually works well for tactile button switches.
|
|
||||||
|
|
||||||
#define REPEAT_FIRST 500 //ms required before repeating on long press
|
Button btnUP(UP_PIN), btnDN(DN_PIN); // define the buttons
|
||||||
#define REPEAT_INCR 100 //repeat interval for long press
|
|
||||||
#define MIN_COUNT 0
|
|
||||||
#define MAX_COUNT 59
|
|
||||||
|
|
||||||
Button btnUP(UP_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Define the buttons
|
const unsigned long
|
||||||
Button btnDN(DN_PIN, PULLUP, INVERT, DEBOUNCE_MS);
|
REPEAT_FIRST(500), // ms required before repeating on long press
|
||||||
|
REPEAT_INCR(100); // repeat interval for long press
|
||||||
enum {WAIT, INCR, DECR}; //The possible states for the state machine
|
const int
|
||||||
uint8_t STATE; //The current state machine state
|
MIN_COUNT(0),
|
||||||
int count; //The number that is adjusted
|
MAX_COUNT(59);
|
||||||
int lastCount = -1; //Previous value of count (initialized to ensure it's different when the sketch starts)
|
|
||||||
unsigned long rpt = REPEAT_FIRST; //A variable time that is used to drive the repeats for long presses
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
btnUP.begin();
|
||||||
|
btnDN.begin();
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
static int
|
||||||
|
count, // the number that is adjusted
|
||||||
|
lastCount(-1); // previous value of count (initialized to ensure it's different when the sketch starts)
|
||||||
|
static unsigned long
|
||||||
|
rpt(REPEAT_FIRST); // a variable time that is used to drive the repeats for long presses
|
||||||
|
enum states_t {WAIT, INCR, DECR}; // states for the state machine
|
||||||
|
static states_t STATE; // current state machine state
|
||||||
|
|
||||||
btnUP.read(); // read the buttons
|
btnUP.read(); // read the buttons
|
||||||
btnDN.read();
|
btnDN.read();
|
||||||
|
|
||||||
if (count != lastCount) { //print the count if it has changed
|
if (count != lastCount) // print the count if it has changed
|
||||||
|
{
|
||||||
lastCount = count;
|
lastCount = count;
|
||||||
Serial.println(count, DEC);
|
Serial.println(count, DEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (STATE) {
|
switch (STATE)
|
||||||
|
{
|
||||||
case WAIT: // wait for a button event
|
case WAIT: // wait for a button event
|
||||||
if (btnUP.wasPressed())
|
if (btnUP.wasPressed())
|
||||||
STATE = INCR;
|
STATE = INCR;
|
||||||
@@ -63,11 +61,13 @@ void loop()
|
|||||||
rpt = REPEAT_FIRST;
|
rpt = REPEAT_FIRST;
|
||||||
else if (btnDN.wasReleased())
|
else if (btnDN.wasReleased())
|
||||||
rpt = REPEAT_FIRST;
|
rpt = REPEAT_FIRST;
|
||||||
else if (btnUP.pressedFor(rpt)) { //check for long press
|
else if (btnUP.pressedFor(rpt)) // check for long press
|
||||||
|
{
|
||||||
rpt += REPEAT_INCR; // increment the long press interval
|
rpt += REPEAT_INCR; // increment the long press interval
|
||||||
STATE = INCR;
|
STATE = INCR;
|
||||||
}
|
}
|
||||||
else if (btnDN.pressedFor(rpt)) {
|
else if (btnDN.pressedFor(rpt))
|
||||||
|
{
|
||||||
rpt += REPEAT_INCR;
|
rpt += REPEAT_INCR;
|
||||||
STATE = DECR;
|
STATE = DECR;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,12 @@
|
|||||||
/*----------------------------------------------------------------------*
|
// Arduino Button Library
|
||||||
* Arduino Button Library v1.0 *
|
// https://github.com/JChristensen/JC_Button
|
||||||
* Jack Christensen May 2011, published Mar 2012 *
|
// Copyright (C) 2018 by Jack Christensen and licensed under
|
||||||
* *
|
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
|
||||||
* Library for reading momentary contact switches like tactile button *
|
//
|
||||||
* switches. Intended for use in state machine constructs. *
|
// Library for reading momentary contact switches like tactile button
|
||||||
* Use the read() function to read all buttons in the main loop, *
|
// switches. Intended for use in state machine constructs.
|
||||||
* which should execute as fast as possible. *
|
// Use the read() function to read each button in the main loop,
|
||||||
* *
|
// which should execute as fast as possible.
|
||||||
* This work is licensed under the Creative Commons Attribution- *
|
|
||||||
* ShareAlike 3.0 Unported License. To view a copy of this license, *
|
|
||||||
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
|
|
||||||
* letter to Creative Commons, 171 Second Street, Suite 300, *
|
|
||||||
* San Francisco, California, 94105, USA. *
|
|
||||||
*----------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
#include "JC_Button.h"
|
#include "JC_Button.h"
|
||||||
|
|
||||||
@@ -29,53 +23,41 @@
|
|||||||
* (Note that invert cannot be implied from puEnable since an external *
|
* (Note that invert cannot be implied from puEnable since an external *
|
||||||
* pullup could be used.) *
|
* pullup could be used.) *
|
||||||
*----------------------------------------------------------------------*/
|
*----------------------------------------------------------------------*/
|
||||||
Button::Button(uint8_t pin, uint8_t puEnable, uint8_t invert, uint32_t dbTime)
|
|
||||||
|
void Button::begin()
|
||||||
{
|
{
|
||||||
_pin = pin;
|
pinMode(m_pin, m_puEnable ? INPUT_PULLUP : INPUT);
|
||||||
_puEnable = puEnable;
|
m_state = digitalRead(m_pin);
|
||||||
_invert = invert;
|
if (m_invert) m_state = !m_state;
|
||||||
_dbTime = dbTime;
|
m_time = millis();
|
||||||
if (_puEnable != 0)
|
m_lastState = m_state;
|
||||||
pinMode(_pin, INPUT_PULLUP); //enable pullup resistor
|
m_changed = false;
|
||||||
else
|
m_lastChange = m_time;
|
||||||
pinMode(_pin, INPUT);
|
|
||||||
_state = digitalRead(_pin);
|
|
||||||
if (_invert != 0) _state = !_state;
|
|
||||||
_time = millis();
|
|
||||||
_lastState = _state;
|
|
||||||
_changed = 0;
|
|
||||||
_lastChange = _time;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*
|
/*----------------------------------------------------------------------*
|
||||||
* read() returns the state of the button, 1==pressed, 0==released, *
|
/ returns the state of the button, true==pressed, false==released, *
|
||||||
* does debouncing, captures and maintains times, previous states, etc. *
|
/ does debouncing, captures and maintains times, previous states, etc. *
|
||||||
*----------------------------------------------------------------------*/
|
/-----------------------------------------------------------------------*/
|
||||||
uint8_t Button::read(void)
|
bool Button::read()
|
||||||
{
|
{
|
||||||
static uint32_t ms;
|
uint32_t ms = millis();
|
||||||
static uint8_t pinVal;
|
uint8_t pinVal = digitalRead(m_pin);
|
||||||
|
if (m_invert) pinVal = !pinVal;
|
||||||
ms = millis();
|
if (ms - m_lastChange < m_dbTime)
|
||||||
pinVal = digitalRead(_pin);
|
{
|
||||||
if (_invert != 0) pinVal = !pinVal;
|
m_time = ms;
|
||||||
if (ms - _lastChange < _dbTime) {
|
m_changed = false;
|
||||||
_time = ms;
|
return m_state;
|
||||||
_changed = 0;
|
|
||||||
return _state;
|
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
_lastState = _state;
|
{
|
||||||
_state = pinVal;
|
m_lastState = m_state;
|
||||||
_time = ms;
|
m_state = pinVal;
|
||||||
if (_state != _lastState) {
|
m_time = ms;
|
||||||
_lastChange = ms;
|
m_changed = (m_state != m_lastState);
|
||||||
_changed = 1;
|
if (m_changed) m_lastChange = ms;
|
||||||
}
|
return m_state;
|
||||||
else {
|
|
||||||
_changed = 0;
|
|
||||||
}
|
|
||||||
return _state;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,14 +66,14 @@ uint8_t Button::read(void)
|
|||||||
* read, and return false (0) or true (!=0) accordingly. *
|
* read, and return false (0) or true (!=0) accordingly. *
|
||||||
* These functions do not cause the button to be read. *
|
* These functions do not cause the button to be read. *
|
||||||
*----------------------------------------------------------------------*/
|
*----------------------------------------------------------------------*/
|
||||||
uint8_t Button::isPressed(void)
|
bool Button::isPressed()
|
||||||
{
|
{
|
||||||
return _state == 0 ? 0 : 1;
|
return m_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Button::isReleased(void)
|
bool Button::isReleased()
|
||||||
{
|
{
|
||||||
return _state == 0 ? 1 : 0;
|
return m_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*
|
/*----------------------------------------------------------------------*
|
||||||
@@ -100,37 +82,37 @@ uint8_t Button::isReleased(void)
|
|||||||
* true (!=0) accordingly. *
|
* true (!=0) accordingly. *
|
||||||
* These functions do not cause the button to be read. *
|
* These functions do not cause the button to be read. *
|
||||||
*----------------------------------------------------------------------*/
|
*----------------------------------------------------------------------*/
|
||||||
uint8_t Button::wasPressed(void)
|
bool Button::wasPressed()
|
||||||
{
|
{
|
||||||
return _state && _changed;
|
return m_state && m_changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Button::wasReleased(void)
|
bool Button::wasReleased()
|
||||||
{
|
{
|
||||||
return !_state && _changed;
|
return !m_state && m_changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*
|
/*----------------------------------------------------------------------*
|
||||||
* pressedFor(ms) and releasedFor(ms) check to see if the button is *
|
* pressedFor(ms) and releasedFor(ms) check to see if the button is *
|
||||||
* pressed (or released), and has been in that state for the specified *
|
* pressed (or released), and has been in that state for the specified *
|
||||||
* time in milliseconds. Returns false (0) or true (1) accordingly. *
|
* time in milliseconds. Returns false (0) or true (!=0) accordingly. *
|
||||||
* These functions do not cause the button to be read. *
|
* These functions do not cause the button to be read. *
|
||||||
*----------------------------------------------------------------------*/
|
*----------------------------------------------------------------------*/
|
||||||
uint8_t Button::pressedFor(uint32_t ms)
|
bool Button::pressedFor(uint32_t ms)
|
||||||
{
|
{
|
||||||
return (_state == 1 && _time - _lastChange >= ms) ? 1 : 0;
|
return m_state && m_time - m_lastChange >= ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Button::releasedFor(uint32_t ms)
|
bool Button::releasedFor(uint32_t ms)
|
||||||
{
|
{
|
||||||
return (_state == 0 && _time - _lastChange >= ms) ? 1 : 0;
|
return !m_state && m_time - m_lastChange >= ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*
|
/*----------------------------------------------------------------------*
|
||||||
* lastChange() returns the time the button last changed state, *
|
* lastChange() returns the time the button last changed state, *
|
||||||
* in milliseconds. *
|
* in milliseconds. *
|
||||||
*----------------------------------------------------------------------*/
|
*----------------------------------------------------------------------*/
|
||||||
uint32_t Button::lastChange(void)
|
uint32_t Button::lastChange()
|
||||||
{
|
{
|
||||||
return _lastChange;
|
return m_lastChange;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,42 +1,37 @@
|
|||||||
/*----------------------------------------------------------------------*
|
// Arduino Button Library
|
||||||
* Arduino Button Library v1.0 *
|
// https://github.com/JChristensen/JC_Button
|
||||||
* Jack Christensen Mar 2012 *
|
// Copyright (C) 2018 by Jack Christensen and licensed under
|
||||||
* *
|
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
|
||||||
* This work is licensed under the Creative Commons Attribution- *
|
|
||||||
* ShareAlike 3.0 Unported License. To view a copy of this license, *
|
#ifndef JC_BUTTON_H_INCLUDED
|
||||||
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
|
#define JC_BUTTON_H_INCLUDED
|
||||||
* letter to Creative Commons, 171 Second Street, Suite 300, *
|
|
||||||
* San Francisco, California, 94105, USA. *
|
|
||||||
*----------------------------------------------------------------------*/
|
|
||||||
#ifndef Button_h
|
|
||||||
#define Button_h
|
|
||||||
#if ARDUINO >= 100
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#else
|
|
||||||
#include <WProgram.h>
|
|
||||||
#endif
|
|
||||||
class Button
|
class Button
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Button(uint8_t pin, uint8_t puEnable, uint8_t invert, uint32_t dbTime);
|
Button::Button(uint8_t pin, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
|
||||||
uint8_t read();
|
: m_pin(pin), m_dbTime(dbTime), m_puEnable(puEnable), m_invert(invert) {}
|
||||||
uint8_t isPressed();
|
void begin();
|
||||||
uint8_t isReleased();
|
bool read();
|
||||||
uint8_t wasPressed();
|
bool isPressed();
|
||||||
uint8_t wasReleased();
|
bool isReleased();
|
||||||
uint8_t pressedFor(uint32_t ms);
|
bool wasPressed();
|
||||||
uint8_t releasedFor(uint32_t ms);
|
bool wasReleased();
|
||||||
|
bool pressedFor(uint32_t ms);
|
||||||
|
bool releasedFor(uint32_t ms);
|
||||||
uint32_t lastChange();
|
uint32_t lastChange();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t _pin; //arduino pin number
|
uint8_t m_pin; // arduino pin number connected to button
|
||||||
uint8_t _puEnable; //internal pullup resistor enabled
|
bool m_puEnable; // internal pullup resistor enabled
|
||||||
uint8_t _invert; //if 0, interpret high state as pressed, else interpret low state as pressed
|
bool m_invert; // if true, interpret logic low as pressed, else interpret logic high as pressed
|
||||||
uint8_t _state; //current button state
|
bool m_state; // current button state, true=pressed
|
||||||
uint8_t _lastState; //previous button state
|
bool m_lastState; // previous button state
|
||||||
uint8_t _changed; //state changed since last read
|
bool m_changed; // state changed since last read
|
||||||
uint32_t _time; //time of current state (all times are in ms)
|
uint32_t m_time; // time of current state (ms from millis)
|
||||||
uint32_t _lastChange; //time of last state change
|
uint32_t m_lastChange; // time of last state change (ms)
|
||||||
uint32_t _dbTime; //debounce time
|
uint32_t m_dbTime; // debounce time (ms)
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user