Skip to content

Commit 86af999

Browse files
committed
Update
Add clearTimer() function and platformio files.
1 parent 149e64c commit 86af999

File tree

11 files changed

+143
-15
lines changed

11 files changed

+143
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.pio

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,23 @@ None.
278278

279279
## Timer Functions
280280

281-
There are 4 timer functions to make timing operations simple to use in your code.
281+
## clearTimer()
282+
283+
##### Description
284+
285+
Simply clears the ms timer used for the timer functions.
286+
287+
##### Syntax
288+
289+
`myInput.clearTimer();`
290+
291+
##### Parameters
292+
293+
None.
294+
295+
##### Returns
296+
297+
None.
282298

283299

284300

@@ -306,6 +322,8 @@ This function sets the duration in milliseconds that the returned value is true.
306322

307323
[Toggle_Basic.ino](https://github.com/Dlloydev/Toggle/blob/main/examples/Toggle_Basic/Toggle_Basic.ino)
308324

325+
326+
309327
## pressedFor(ms)
310328

311329
## releasedFor(ms)

include/README

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ toggle KEYWORD2
3232
setToggleState KEYWORD2
3333
setToggleTrigger KEYWORD2
3434
setSamplePeriodUs KEYWORD2
35+
clearTimer KEYWORD2
3536
getElapsedMs KEYWORD2
3637
isPressed KEYWORD2
3738
isReleased KEYWORD2

lib/README

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Toggle",
3-
"version": "3.1.5",
3+
"version": "3.1.6",
44
"description": "Arduino bounce library for debouncing hardware, signals and data. Works with all switch types, port expander and other 8-bit data sources. Robust debounce algorithm.",
55
"keywords": "debounce, toggle, button, switch, data, deglitch",
66
"repository":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Toggle
2-
version=3.1.5
2+
version=3.1.6
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
55
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.

platformio.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html

src/Toggle.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/************************************************
2-
Toggle Library for Arduino - Version 3.1.5
2+
Toggle Library for Arduino - Version 3.1.6
33
by dlloydev https://github.com/Dlloydev/Toggle
44
Licensed under the MIT License.
55
************************************************/
@@ -145,25 +145,31 @@ bool Toggle::getLastToggleState() { // private
145145

146146
/************* button timer functions ****************/
147147

148+
void Toggle::clearTimer() {
149+
startUs = micros();
150+
}
151+
152+
uint32_t Toggle::getElapsedMs() {
153+
return (micros() - startUs) * 0.001;
154+
}
155+
148156
bool Toggle::blink(uint16_t ms, uint8_t mode) {
149-
if (mode == 2 && onChange() == 2) startUs = micros();
150-
else if (mode == 1 && onChange() == 1) startUs = micros();
151-
else if (mode == 0 && onChange()) startUs = micros();
157+
if (mode == 2 && onChange() == 2) clearTimer();
158+
else if (mode == 1 && onChange() == 1) clearTimer();
159+
else if (mode == 0 && onChange()) clearTimer();
152160
onPress();
153161
onRelease();
154162
return (bool)(ms > (getElapsedMs()));
155163
}
156164

157165
bool Toggle::pressedFor(uint16_t ms) {
158-
if (onChange() == 1) startUs = micros();
159166
if (isPressed() && getElapsedMs() > ms) {
160167
return true;
161168
}
162169
return false;
163170
}
164171

165172
bool Toggle::releasedFor(uint16_t ms) {
166-
if (onChange() == 2) startUs = micros();
167173
if (isReleased() && getElapsedMs() > ms) {
168174
return true;
169175
}
@@ -172,16 +178,12 @@ bool Toggle::releasedFor(uint16_t ms) {
172178

173179
bool Toggle::retrigger(uint16_t ms) {
174180
if (isPressed() && getElapsedMs() > ms) {
175-
startUs = micros();
181+
clearTimer();
176182
return true;
177183
}
178184
return false;
179185
}
180186

181-
uint32_t Toggle::getElapsedMs() {
182-
return (micros() - startUs) * 0.001;
183-
}
184-
185187
uint8_t Toggle::pressCode(bool debug) {
186188
static uint8_t pCode = 0, code = 0;
187189
static uint32_t elapsedMs = 0;
@@ -190,7 +192,7 @@ uint8_t Toggle::pressCode(bool debug) {
190192
case PB_DEFAULT:
191193
elapsedMs = getElapsedMs();
192194
if (pCode && isReleased() && (elapsedMs > (CLICK::LONG + CLICK::MULTI))) _state = PB_DONE;
193-
if (onChange()) startUs = micros();
195+
if (onChange()) clearTimer();
194196
if (onPress()) {
195197
_state = PB_ON_PRESS;
196198
}

src/Toggle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Toggle {
2424
void setToggleState(bool toggleState); // set toggle state
2525
void setToggleTrigger(bool change); // set toggle trigger mode: onPress(0), onRelease(1)
2626
void setSamplePeriodUs(uint16_t samplePeriodUs); // sample period in microseconds
27+
void clearTimer(); // clear ms timer
2728
uint32_t getElapsedMs(); // get elapsed ms since the last state change selected by timer mode
2829
bool isPressed(uint8_t bit = 0); // returns true if pressed
2930
bool isReleased(uint8_t bit = 0); // returns true if released

test/README

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This directory is intended for PlatformIO Test Runner and project tests.
3+
4+
Unit Testing is a software testing method by which individual units of
5+
source code, sets of one or more MCU program modules together with associated
6+
control data, usage procedures, and operating procedures, are tested to
7+
determine whether they are fit for use. Unit testing finds problems early
8+
in the development cycle.
9+
10+
More information about PlatformIO Unit Testing:
11+
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html

0 commit comments

Comments
 (0)