Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
- Fixed toggle function
- Added `begin()` function to setup in examples
- Updated readme
  • Loading branch information
Dlloydev committed May 21, 2022
1 parent c333169 commit 1755ce9
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 42 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Toggle(*in);
##### Description
The constructor defines a button object. If the default constructor is used when declaring an array of pointers to button objects, then it must be followed by a call to begin in setup. Otherwise, begin() is automatically called and not needed in setup.
The constructor defines a button object. If the default constructor is used when declaring an array of pointers to button objects, then it must be followed by a call to begin in setup.
##### Syntax
Expand Down Expand Up @@ -293,7 +293,7 @@ else

##### Description

This function will toggle the return value after each state change. Useful to easily toggle an LED responding to every state change.
This function will toggle the return value after each `onPress` state change. Useful to easily toggle an LED.

##### Syntax

Expand All @@ -305,7 +305,7 @@ This function will toggle the return value after each state change. Useful to ea

##### Returns

*true* or *false*, toggles after any state change. *(bool)
*true* or *false*, toggles after each `onPress` state change. *(bool)



Expand Down
7 changes: 3 additions & 4 deletions examples/Input_Bit/Input_Bit.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <Toggle.h>

const byte ledPin = LED_BUILTIN;
byte ledState = LOW;

byte bit = 0; // choose bit(0-7)
byte Input;
Expand All @@ -20,6 +19,7 @@ void setup() {
pinMode(ledPin, OUTPUT);
while (!Serial) { }; // Leonardo
Serial.begin(115200);
myInput.begin(Input);
myInput.setInputMode(myInput.inMode::input_bit);
myInput.setSamplePeriodUs(20); // 0-65535μs
}
Expand All @@ -28,10 +28,9 @@ void loop() {
myInput.poll(bit);
if (myInput.onPress()) {
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": OFF⇒ON"));
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
}
if (myInput.onRelease()) {
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": ON⇒OFF"));
}
digitalWrite(ledPin, myInput.toggle(bit));
}
2 changes: 1 addition & 1 deletion examples/Input_Bit_Test/Input_Bit_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Toggle myInput(&Input);
void setup() {
while (!Serial) { }; // Leonardo
Serial.begin(115200);

myInput.begin(Input);
myInput.setInputMode(myInput.inMode::input_port);
myInput.setAlgorithm(2); // ignore(2), (1) or (0) glitches for robust, normal and quick response
myInput.setSamplePeriodUs(20); // 0-65535μs
Expand Down
5 changes: 2 additions & 3 deletions examples/Input_Port/Input_Port.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <Toggle.h>

const byte ledPin = LED_BUILTIN;
byte ledState = LOW;

byte bit = 0; // choose bit(0-7)
byte Input;
Expand All @@ -20,6 +19,7 @@ void setup() {
pinMode(ledPin, OUTPUT);
while (!Serial) { }; // Leonardo
Serial.begin(115200);
myInput.begin(Input);
myInput.setInputMode(myInput.inMode::input_port); // debounce all bits
myInput.setSamplePeriodUs(20); // 0-65535μs
}
Expand All @@ -28,10 +28,9 @@ void loop() {
myInput.poll();
if (myInput.onPress()) {
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": OFF⇒ON"));
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
if (myInput.onRelease()) {
Serial.print(F("b")); Serial.print(bit); Serial.println(F(": ON⇒OFF"));
}
digitalWrite(ledPin, myInput.toggle(bit));
}
2 changes: 1 addition & 1 deletion examples/Input_Port_Test/Input_Port_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Toggle myInput(&Input);
void setup() {
while (!Serial) { }; // Leonardo
Serial.begin(115200);

myInput.begin(Input);
myInput.setInputMode(myInput.inMode::input_port);
myInput.setAlgorithm(2); // ignore(2), (1) or (0) glitches for robust, normal and quick response
myInput.setSamplePeriodUs(20); // 0-65535μs
Expand Down
9 changes: 3 additions & 6 deletions examples/Input_Pulldown/Input_Pulldown.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@

const byte buttonPin = 2;
const byte ledPin = LED_BUILTIN;
byte ledState = LOW;

Toggle sw1(buttonPin);

void setup() {
pinMode(ledPin, OUTPUT);
while (!Serial) { }; // Leonardo
Serial.begin(115200);
sw1.begin(buttonPin);
sw1.setInputMode(sw1.inMode::input_pulldown);
sw1.setInvertMode(true);
}

void loop() {
sw1.poll();
if (sw1.onPress()) {
Serial.println(F("sw1: OFF⇒ON"));
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
if (sw1.onPress()) Serial.println(F("sw1: OFF⇒ON"));
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
digitalWrite(ledPin, sw1.toggle());
}
12 changes: 9 additions & 3 deletions examples/One_Button_One_Switch/One_Button_One_Switch.ino
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
/************************************************************************
/******************************************************************
One Button and One Three Position Switch:
=========================================
This example checks each time a button or 3-position switch
This example that blinks an LED each time the button
has transitioned. All input pins will have their pullups enabled.
Open the Serial Monitor to view transition status.
***********************************************************************/
*****************************************************************/

#include <Toggle.h>

const byte buttonPin = 2;
const byte swPinA = 3;
const byte swPinB = 4;
const byte ledPin = LED_BUILTIN;

Toggle sw1(buttonPin); // button
Toggle sw2(swPinA, swPinB); // 3-position switch

void setup() {
pinMode(ledPin, OUTPUT);
while (!Serial) { }; // Leonardo
Serial.begin(115200);
sw1.begin(buttonPin);
sw2.begin(swPinA, swPinB);
}

void loop() {
sw1.poll();
sw2.poll();

if (sw1.onPress()) Serial.println(F("sw1: OFF⇒ON"));
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
if (sw2.UPtoMID()) Serial.println(F("sw2: UP⇒MID"));
if (sw2.MIDtoDN()) Serial.println(F("sw2: MID⇒DN"));
if (sw2.DNtoMID()) Serial.println(F("sw2: DN⇒MID"));
if (sw2.MIDtoUP()) Serial.println(F("sw2: MID⇒UP"));
digitalWrite(ledPin, sw1.toggle());
}
13 changes: 6 additions & 7 deletions examples/Three_Position_Switch/Three_Position_Switch.ino
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
/************************************************************************
/***********************************************************************
Using Three Position Switches:
==============================
A simple example that toggles an LED each time a 3-position switch has
transitioned. Both input pins will have its pullup enabled. The
switch is in the disconnected MID position if both inpits read high.
• open Serial Monitor to view transition status.
***********************************************************************/
Open the Serial Monitor to view transition status.
**********************************************************************/

#include <Toggle.h>

const byte pinA = 2;
const byte pinB = 3;
const byte ledPin = LED_BUILTIN;

Toggle sw1(pinA, pinB); // 3-position switch
Toggle sw1(pinA, pinB); // 3-position switch

void setup() {
pinMode(ledPin, OUTPUT);
while (!Serial) { }; // Leonardo
Serial.begin(115200);
sw1.begin(pinA, pinB);
}

void loop() {
sw1.poll();
// call toggle() just after poll(). Toggles on MID⇒UP transitions only
digitalWrite(ledPin, sw1.toggle());

if (sw1.UPtoMID()) Serial.println(F("sw1: UP⇒MID"));
if (sw1.MIDtoDN()) Serial.println(F("sw1: MID⇒DN"));
if (sw1.DNtoMID()) Serial.println(F("sw1: DN⇒MID"));
if (sw1.MIDtoUP()) Serial.println(F("sw1: MID⇒UP"));
digitalWrite(ledPin, sw1.toggle()); // toggles on MID⇒UP transition only
}
5 changes: 3 additions & 2 deletions examples/Toggle_Basic/Toggle_Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ const byte ledPin = LED_BUILTIN;
Toggle sw1(buttonPin);

void setup() {
pinMode(ledPin, OUTPUT);
while (!Serial) { }; // Leonardo
Serial.begin(115200);
sw1.begin(buttonPin);
pinMode(ledPin, OUTPUT);
}

void loop() {
sw1.poll();
digitalWrite(ledPin, sw1.toggle()); // call toggle() just after poll()
if (sw1.onPress()) Serial.println(F("sw1: OFF⇒ON"));
if (sw1.onRelease()) Serial.println(F("sw1: ON⇒OFF"));
digitalWrite(ledPin, sw1.toggle());
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Toggle",
"version": "3.0.0",
"version": "3.0.1",
"description": "Arduino bounce library for deglitching and debouncing hardware, signals and data. Works with all switch types, port expander and other 8-bit data sources. Flexible algorithm with Robust, Normal and Quick response modes.",
"keywords": "debounce, toggle, button, switch, data, deglitch",
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Toggle
version=3.0.0
version=3.0.1
author=David Lloyd
maintainer=David Lloyd <[email protected]>
sentence=Arduino bounce library for deglitching and debouncing hardware, signals and data. Works with all switch types, port expander and other 8-bit data sources.
Expand Down
18 changes: 11 additions & 7 deletions src/Toggle.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/************************************************
Toggle Library for Arduino - Version 3.0.0
Toggle Library for Arduino - Version 3.0.1
by dlloydev https://github.com/Dlloydev/Toggle
Licensed under the MIT License.
************************************************/
Expand Down Expand Up @@ -31,7 +31,10 @@ void Toggle::begin(uint8_t inA, uint8_t inB) {
}

void Toggle::poll(uint8_t bit) {
// begin();
if (csr & 0b00001000) { // first run
csr &= ~0b00001000; // clear first run
begin(_inA, _inB);
}
if (micros() - sampleUs > _samplePeriodUs) {
sampleUs += _samplePeriodUs;
sampleCount++;
Expand Down Expand Up @@ -138,12 +141,13 @@ uint8_t Toggle::onChange() {
return 0;
}

bool Toggle::toggle(bool invert) {
if (onChange() == 1) {
if (lsr & 0b00010000) lsr &= ~0b00010000;
else lsr |= 0b00010000;
bool Toggle::toggle(bool invert, uint8_t bit) {
if (isPressed(bit) && !(lsr & 0b00100000)) {
lsr |= 0b00100000; // set lastState
(lsr & 0b00010000) ? lsr &= ~0b00010000 : lsr |= 0b00010000; // negate toggleFlag
}
return (invert) ? !(lsr & 0b00010000) : (lsr & 0b00010000);
if (isReleased(bit) && (lsr & 0b00100000)) lsr &= ~0b00100000; // clear lastState
return (invert) ? !(lsr & 0b00010000) : (lsr & 0b00010000);
}

uint16_t Toggle::getElapsedMs() {
Expand Down
6 changes: 3 additions & 3 deletions src/Toggle.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Toggle {
uint16_t getElapsedMs(); // get elapsed ms since the last state change
uint8_t debounceInput(uint8_t bit = 0); // the input debouncer
uint8_t getDebounceCount(); // counts up to 10 samples for debounce period
bool toggle(bool invert = false); // returns true/false (toggle) at each state change
bool toggle(bool invert = false, uint8_t bit = 0); // returns true/false (toggle) at each state change
bool isPressed(uint8_t bit = 0); // returns true if pressed
bool isReleased(uint8_t bit = 0); // returns true if released
bool onPress(); // returns true if just pressed
Expand Down Expand Up @@ -54,8 +54,8 @@ class Toggle {
uint16_t sampleCount; // sample count
uint32_t sampleUs; // sample time μs
uint8_t out = 0xFF, pOut = 0xFF; // debounced output and previous debounced output
uint8_t csr = 0b10101010; // B7-B4: debounceCount, B2: invert, B1-B0 algorithm
uint8_t lsr = 0b00000000; // B4 toggle, B3 releasedFor, B2 pressedFor, B1 onRelease, B0 onPress
uint8_t csr = 0b10100010; // B7-B4: debounceCount, B2: invert, B1-B0 algorithm
uint8_t lsr = 0b00000000; // B5 lastState, B4 toggle, B3 releasedFor, B2 pressedFor, B1 onRelease, B0 onPress

};
#endif

0 comments on commit 1755ce9

Please sign in to comment.