Skip to content

Commit 2811e35

Browse files
committed
Update
Uses much less memory than other libraries (only 19 bytes per button). Updated documentation.
1 parent 7f73a4c commit 2811e35

File tree

6 files changed

+150
-76
lines changed

6 files changed

+150
-76
lines changed

README.md

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
# Toggle [![arduino-library-badge](https://www.ardu-badge.com/badge/Toggle.svg?)](https://www.ardu-badge.com/Toggle) [![PlatformIO Registry](https://badges.registry.platformio.org/packages/dlloydev/library/Toggle.svg)](https://registry.platformio.org/libraries/dlloydev/Toggle)
66

7-
Arduino switch and button library for SPST, SPDT or SP3T contacts. Simple to use, provides debouncing, deglitching and uses very little memory. Status indicates one-shot transitions (depicting direction) and current position status.
7+
Arduino switch and button library for SPST, SPDT or SP3T contacts. Simple to use, provides debouncing, deglitching and uses very little memory. Captures one-shot transitions (depicting direction) and current position status.
88

99
#### Features
1010

1111
- Performs both debouncing and deglitching.
1212
- External pull-up resistors not required.
1313
- Very simple to use.
14-
- Very low memory use.
14+
- Ultra low memory use (14 bytes per button).
1515

1616
## Using Toggle
1717

@@ -36,30 +36,30 @@ sw1.poll();
3636
sw2.poll();
3737
```
3838

39-
###### The switch status options when using 1 input pin:
39+
###### The switch functions when using 1 input pin:
4040

4141
```c++
42-
sw.isON;
43-
sw.isOFF;
44-
sw.ONtoOFF;
45-
sw.OFFtoON;
42+
bool isOFF();
43+
bool isON();
44+
bool OFFtoON();
45+
bool ONtoOFF();
4646
```
4747

48-
The switch has 2 positions referred to as OFF (input is high) and ON (input is low). The first 2 status options will continuously return true if the switch is at that current position. The last 2 status options return true (once only) if the switch has just transitioned to the checked position. This is very handy to execute code based on direction of switched operation or for any one-shot processing of code.
48+
The switch has 2 positions referred to as OFF (input is high) and ON (input is low). The first 2 functions will continuously return true if the switch is at that current position. The last 2 functions return true (once only) if the switch has just transitioned to the checked position. This is very handy to execute code based on direction of switched operation or for any one-shot processing of code.
4949

50-
###### The switch status options when using 2 input pins:
50+
###### The switch functions when using 2 input pins:
5151

5252
```c++
53-
sw.isUP;
54-
sw.isMID;
55-
sw.isDN;
56-
sw.UPtoMID;
57-
sw.MIDtoDN;
58-
sw.DNtoMID;
59-
sw.MIDtoUP;
53+
bool isUP();
54+
bool isMID();
55+
bool isDN();
56+
bool UPtoMID();
57+
bool MIDtoDN();
58+
bool DNtoMID();
59+
bool MIDtoUP();
6060
```
6161

62-
The switch has 3 positions referred to as UP, MID (center) and DN (down). The first 3 status options will continuously return true if the switch is at that position. The last 4 status options return true (once only) if the switch has just transitioned to that position. This is very handy to execute code based on direction of switched operation or for any one-shot processing of code.
62+
The switch has 3 positions referred to as UP, MID (center) and DN (down). The first 3 functions will continuously return true if the switch is at that position. The last 4 functions return true (once only) if the switch has just transitioned to that position. This is very handy to execute code based on direction of switched operation or for any one-shot processing of code.
6363

6464
#### Example Sketch
6565

@@ -155,7 +155,18 @@ Using the input pullups provides a high 20K-50K impedance that makes the signals
155155
156156
#### Debouncing
157157
158-
Debouncing requires the shift register to be completely filled with 1's or 0's to signify a stable state. This occurs 80ms after the last transition. Contact closure will be detected after at least 10ms have elapsed (de-glitch period). Contact release is detected in at least 80ms.
158+
Debouncing requires the shift register to be completely filled with 1's or 0's to signify a stable state. This occurs 80ms after the last transition. Contact closure will be detected after at least 10ms have elapsed (de-glitch period). Contact release is detected in at least 80ms.
159+
160+
#### Memory Comparison on Leonardo with 2 buttons attached :
161+
162+
| Library | Version | Bytes | Bytes Used |
163+
| ------------ | --------- | ------- | ---------- |
164+
| Empty sketch | -- | 149 | -- |
165+
| **Toggle.h** | **2.0.1** | **177** | **28** |
166+
| JC_Button.h | 2.1.2 | 186 | 37 |
167+
| Bounce2.h | 2.71.0 | 193 | 44 |
168+
| AceButton.h | 1.9.2 | 205 | 56 |
169+
| ezButton.h | 1.0.3 | 331 | 182 |
159170
160171
### References
161172

keywords.txt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,18 @@ sw9 KEYWORD1
2323
##########################################
2424

2525
poll KEYWORD2
26+
isUP KEYWORD2
27+
isMID KEYWORD2
28+
isDN KEYWORD2
29+
UPtoMID KEYWORD2
30+
MIDtoDN KEYWORD2
31+
DNtoMID KEYWORD2
32+
MIDtoUP KEYWORD2
33+
isON KEYWORD2
34+
isOFF KEYWORD2
35+
ONtoOFF KEYWORD2
36+
OFFtoON KEYWORD2
2637

2738
##########################################
2839
# Constants (LITERAL1)
2940
##########################################
30-
31-
isUP LITERAL1
32-
isMID LITERAL1
33-
isDN LITERAL1
34-
UPtoMID LITERAL1
35-
MIDtoDN LITERAL1
36-
DNtoMID LITERAL1
37-
MIDtoUP LITERAL1
38-
isON LITERAL1
39-
isOFF LITERAL1
40-
ONtoOFF LITERAL1
41-
OFFtoON LITERAL1

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Toggle",
3-
"version": "2.0.0",
4-
"description": "Arduino switch and button library for SPST, SPDT or SP3T contacts. Simple to use, provides debouncing, deglitching and uses very little memory. Status indicates one-shot transitions (depicting direction) and current position status.",
3+
"version": "2.1.0",
4+
"description": "Arduino switch and button library for SPST, SPDT or SP3T contacts. Simple to use, provides debouncing, deglitching and uses very little memory. Captures one-shot transitions (depicting direction) and current position status.",
55
"keywords": "toggle, switch",
66
"repository":
77
{

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Toggle
2-
version=2.0.0
2+
version=2.1.0
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
55
sentence=Arduino switch and button library for SPST, SPDT or SP3T contacts. Simple to use, provides debouncing, deglitching and uses very little memory.
6-
paragraph=Status indicates one-shot transitions (depicting direction) and current position status.
6+
paragraph=Captures one-shot transitions (depicting direction) and current position status.
77
category=Signal Input/Output
88
url=https://github.com/Dlloydev/Toggle
99
architectures=*

src/Toggle.cpp

Lines changed: 90 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************
2-
Toggle Library for Arduino - Version 2.0.0
2+
Toggle Library for Arduino - Version 2.1.0
33
by dlloydev https://github.com/Dlloydev/Toggle
44
Licensed under the MIT License.
55
****************************************************/
@@ -13,69 +13,122 @@ Toggle::Toggle(uint8_t pinA, uint8_t pinB)
1313
}
1414

1515
void Toggle::poll() {
16-
1716
if (firstRun) {
1817
firstRun = false;
1918
if (_pinA) pinMode(_pinA, INPUT_PULLUP);
2019
if (_pinB) {
2120
pinMode(_pinB, INPUT_PULLUP);
22-
} else {
23-
lastRegA = 0xFF;
24-
regA = 0xFF;
25-
}
21+
}
2622
}
27-
ms = millis();
28-
2923
lastRegA = regA;
3024
lastRegB = regB;
3125

32-
if (ms - sampleTime > 10) {
26+
if (millis() - sampleTime > 10) {
27+
sampleTime = millis();
3328
regA = regA << 1 | digitalRead(_pinA);
3429
if (_pinB) {
3530
regB = regB << 1 | digitalRead(_pinB);
3631
}
3732
else regB = 0xFF;
38-
sampleTime = ms;
3933
}
4034

35+
// b3:ONtoOFF, b2:OFFtoON, b1:isON, b0:isOFF;
36+
4137
if (_pinA && !_pinB) {
42-
if (regA == 0xFF) {
43-
isOFF = true;
44-
isON = false;
38+
if (lastRegA == 0xFF) {
39+
statusOne |= 0b00000001; // isOFF = 1
40+
statusOne &= 0b11111101; // isON = 0
4541
}
4642
if (regA == 0xFC) {
47-
isON = true;
48-
isOFF = false;
43+
statusOne |= 0b00000010; // isON = 1
44+
statusOne &= 0b11111110; // isOFF = 0
4945
}
50-
OFFtoON = false;
51-
if (lastRegA == 0xFE && regA == 0xFC) OFFtoON = true;
52-
ONtoOFF = false;
53-
if (lastRegA == 0x7F && regA == 0xFF) ONtoOFF = true;
46+
statusOne &= 0b11111011; // OFFtoON = 0
47+
if (lastRegA == 0xFF && regA == 0xFE) statusOne |= 0b00000100; // OFFtoON = 1
48+
statusOne &= 0b11110111; // ONtoOFF = 0
49+
if (lastRegA == 0x7F && regA == 0xFF) statusOne |= 0b00001000; // ONtoOFF = 1
5450
}
5551

52+
// b6:MIDtoUP, b5:DNtoMID, b4:MIDtoDN, b3:UPtoMID, b2:isDN, b1:isMID, b0:isUP;
53+
5654
if (_pinA && _pinB) {
5755
if (lastRegA == 0xFE && regA == 0xFC && regB == 0xFF) {
58-
isUP = true;
59-
isMID = false;
60-
isDN = false;
56+
statusTwo |= 0b00000001; // isUP = 1
57+
statusTwo &= 0b11111101; // isMID = 0
58+
statusTwo &= 0b11111011; // isDN = 0
6159
}
62-
if (regA == 0xFF && regB == 0xFF) {
63-
isUP = false;
64-
isMID = true;
65-
isDN = false;
60+
if (lastRegA == 0xFF && lastRegB == 0xFF) {
61+
statusTwo &= 0b11111110; // isUP = 0
62+
statusTwo |= 0b00000010; // isMID = 1
63+
statusTwo &= 0b11111011; // isDN = 0
6664
}
6765
if (lastRegB == 0xFE && regB == 0xFC && regA == 0xFF) {
68-
isUP = false;
69-
isMID = false;
70-
isDN = true;
66+
statusTwo &= 0b11111110; // isUP = 0
67+
statusTwo &= 0b11111101; // isMID = 0
68+
statusTwo |= 0b00000100; // isDN = 1
7169
}
72-
UPtoMID = false;
73-
if (lastRegA == 0x1 && regA == 0x3 && regB == 0xFF) UPtoMID = true;
74-
MIDtoDN = false;
75-
if (lastRegB == 0xFE && regB == 0xFC && regA == 0xFF) MIDtoDN = true;
76-
DNtoMID = false;
77-
if (lastRegB == 0x1 && regB == 0x3 && regA == 0xFF) DNtoMID = true;
78-
MIDtoUP = false;
79-
if (lastRegA == 0xFE && regA == 0xFC && regB == 0xFF) MIDtoUP = true;
70+
statusTwo &= 0b10111111; // MIDtoUP = 0
71+
if (lastRegA == 0xFF && regA == 0xFE) statusTwo |= 0b01000000; // MIDtoUP = 1
72+
statusTwo &= 0b11110111; // UPtoMID = 0
73+
if (lastRegA < 0xFF && regA == 0xFF) statusTwo |= 0b00001000; // UPtoMID = 1
74+
statusTwo &= 0b11101111; // MIDtoDN = 0
75+
if (lastRegB == 0xFF && regB == 0xFE) statusTwo |= 0b00010000; // MIDtoDN = 1
76+
statusTwo &= 0b11011111; // DNtoMID = 0
77+
if (lastRegB < 0xFF && regB == 0xFF) statusTwo |= 0b00100000; // DNtoMID = 1
8078
}
8179
}
80+
81+
bool Toggle::isOFF() {
82+
if ((statusOne & 0b00000001) == 0b00000001) return true;
83+
return false;
84+
}
85+
86+
bool Toggle::isON() {
87+
if ((statusOne & 0b00000010) == 0b00000010) return true;
88+
return false;
89+
}
90+
91+
bool Toggle::OFFtoON() {
92+
if ((statusOne & 0b00000100) == 0b00000100) return true;
93+
return false;
94+
}
95+
96+
bool Toggle::ONtoOFF() {
97+
if ((statusOne & 0b00001000) == 0b00001000) return true;
98+
return false;
99+
}
100+
101+
bool Toggle::isUP() {
102+
if ((statusTwo & 0b00000001) == 0b00000001) return true;
103+
return false;
104+
}
105+
106+
bool Toggle::isMID() {
107+
if ((statusTwo & 0b00000010) == 0b00000010) return true;
108+
return false;
109+
}
110+
111+
bool Toggle::isDN() {
112+
if ((statusTwo & 0b00000100) == 0b00000100) return true;
113+
return false;
114+
}
115+
116+
bool Toggle::UPtoMID() {
117+
if ((statusTwo & 0b00001000) == 0b00001000) return true;
118+
return false;
119+
}
120+
121+
bool Toggle::MIDtoDN() {
122+
if ((statusTwo & 0b00010000) == 0b00010000) return true;
123+
return false;
124+
}
125+
126+
bool Toggle::DNtoMID() {
127+
if ((statusTwo & 0b00100000) == 0b00100000) return true;
128+
return false;
129+
}
130+
131+
bool Toggle::MIDtoUP() {
132+
if ((statusTwo & 0b01000000) == 0b01000000) return true;
133+
return false;
134+
}

src/Toggle.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
#pragma once
22
#ifndef Toggle_h
33
#define Toggle_h
4-
#include "Arduino.h"
4+
#include <Arduino.h>
55

66
class Toggle {
77

88
public:
99

1010
Toggle(uint8_t pinA, uint8_t pinB = 0);
11+
1112
void poll();
13+
bool isOFF();
14+
bool isON();
15+
bool OFFtoON();
16+
bool ONtoOFF();
17+
bool isUP();
18+
bool isMID();
19+
bool isDN();
20+
bool UPtoMID();
21+
bool MIDtoDN();
22+
bool DNtoMID();
23+
bool MIDtoUP();
1224

13-
bool isUP, isMID, isDN, UPtoMID, MIDtoDN, DNtoMID, MIDtoUP, isON, isOFF, ONtoOFF, OFFtoON;
25+
uint8_t statusOne, statusTwo;
1426

1527
private:
1628

17-
uint8_t regA = 0xAA, regB = 0x55, lastRegA = 0xAA, lastRegB = 0x55;
18-
uint32_t sampleTime, ms;
29+
uint8_t regA = 0xFF, regB = 0xFF, lastRegA = 0xFF, lastRegB = 0xFF;
30+
uint32_t sampleTime;
1931
uint8_t _pinA, _pinB;
2032
bool firstRun = true;
21-
2233
};
2334
#endif

0 commit comments

Comments
 (0)