Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Add clearTimer() function and platformio files.
  • Loading branch information
Dlloydev committed Jun 11, 2022
1 parent 149e64c commit 86af999
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.pio
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,23 @@ None.

## Timer Functions

There are 4 timer functions to make timing operations simple to use in your code.
## clearTimer()

##### Description

Simply clears the ms timer used for the timer functions.

##### Syntax

`myInput.clearTimer();`

##### Parameters

None.

##### Returns

None.



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

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



## pressedFor(ms)

## releasedFor(ms)
Expand Down
39 changes: 39 additions & 0 deletions include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ toggle KEYWORD2
setToggleState KEYWORD2
setToggleTrigger KEYWORD2
setSamplePeriodUs KEYWORD2
clearTimer KEYWORD2
getElapsedMs KEYWORD2
isPressed KEYWORD2
isReleased KEYWORD2
Expand Down
46 changes: 46 additions & 0 deletions lib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.

The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").

For example, see a structure of the following two libraries `Foo` and `Bar`:

|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c

and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>

int main (void)
{
...
}

```

PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.

More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
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.1.5",
"version": "3.1.6",
"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.",
"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.1.5
version=3.1.6
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
9 changes: 9 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
26 changes: 14 additions & 12 deletions src/Toggle.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/************************************************
Toggle Library for Arduino - Version 3.1.5
Toggle Library for Arduino - Version 3.1.6
by dlloydev https://github.com/Dlloydev/Toggle
Licensed under the MIT License.
************************************************/
Expand Down Expand Up @@ -145,25 +145,31 @@ bool Toggle::getLastToggleState() { // private

/************* button timer functions ****************/

void Toggle::clearTimer() {
startUs = micros();
}

uint32_t Toggle::getElapsedMs() {
return (micros() - startUs) * 0.001;
}

bool Toggle::blink(uint16_t ms, uint8_t mode) {
if (mode == 2 && onChange() == 2) startUs = micros();
else if (mode == 1 && onChange() == 1) startUs = micros();
else if (mode == 0 && onChange()) startUs = micros();
if (mode == 2 && onChange() == 2) clearTimer();
else if (mode == 1 && onChange() == 1) clearTimer();
else if (mode == 0 && onChange()) clearTimer();
onPress();
onRelease();
return (bool)(ms > (getElapsedMs()));
}

bool Toggle::pressedFor(uint16_t ms) {
if (onChange() == 1) startUs = micros();
if (isPressed() && getElapsedMs() > ms) {
return true;
}
return false;
}

bool Toggle::releasedFor(uint16_t ms) {
if (onChange() == 2) startUs = micros();
if (isReleased() && getElapsedMs() > ms) {
return true;
}
Expand All @@ -172,16 +178,12 @@ bool Toggle::releasedFor(uint16_t ms) {

bool Toggle::retrigger(uint16_t ms) {
if (isPressed() && getElapsedMs() > ms) {
startUs = micros();
clearTimer();
return true;
}
return false;
}

uint32_t Toggle::getElapsedMs() {
return (micros() - startUs) * 0.001;
}

uint8_t Toggle::pressCode(bool debug) {
static uint8_t pCode = 0, code = 0;
static uint32_t elapsedMs = 0;
Expand All @@ -190,7 +192,7 @@ uint8_t Toggle::pressCode(bool debug) {
case PB_DEFAULT:
elapsedMs = getElapsedMs();
if (pCode && isReleased() && (elapsedMs > (CLICK::LONG + CLICK::MULTI))) _state = PB_DONE;
if (onChange()) startUs = micros();
if (onChange()) clearTimer();
if (onPress()) {
_state = PB_ON_PRESS;
}
Expand Down
1 change: 1 addition & 0 deletions src/Toggle.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Toggle {
void setToggleState(bool toggleState); // set toggle state
void setToggleTrigger(bool change); // set toggle trigger mode: onPress(0), onRelease(1)
void setSamplePeriodUs(uint16_t samplePeriodUs); // sample period in microseconds
void clearTimer(); // clear ms timer
uint32_t getElapsedMs(); // get elapsed ms since the last state change selected by timer mode
bool isPressed(uint8_t bit = 0); // returns true if pressed
bool isReleased(uint8_t bit = 0); // returns true if released
Expand Down
11 changes: 11 additions & 0 deletions test/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

This directory is intended for PlatformIO Test Runner and project tests.

Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.

More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html

0 comments on commit 86af999

Please sign in to comment.