diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03f4a3c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.pio diff --git a/README.md b/README.md index 65209bb..5e9a8b2 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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) diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -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 diff --git a/keywords.txt b/keywords.txt index 24719f9..8618ba0 100644 --- a/keywords.txt +++ b/keywords.txt @@ -32,6 +32,7 @@ toggle KEYWORD2 setToggleState KEYWORD2 setToggleTrigger KEYWORD2 setSamplePeriodUs KEYWORD2 +clearTimer KEYWORD2 getElapsedMs KEYWORD2 isPressed KEYWORD2 isReleased KEYWORD2 diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -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 +#include + +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 diff --git a/library.json b/library.json index d9b4715..8d94117 100644 --- a/library.json +++ b/library.json @@ -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": diff --git a/library.properties b/library.properties index 8bbc87a..1dd68b6 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Toggle -version=3.1.5 +version=3.1.6 author=David Lloyd maintainer=David Lloyd 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. diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..ebc510b --- /dev/null +++ b/platformio.ini @@ -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 diff --git a/src/Toggle.cpp b/src/Toggle.cpp index 5f37a76..05759fd 100644 --- a/src/Toggle.cpp +++ b/src/Toggle.cpp @@ -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. ************************************************/ @@ -145,17 +145,24 @@ 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; } @@ -163,7 +170,6 @@ bool Toggle::pressedFor(uint16_t ms) { } bool Toggle::releasedFor(uint16_t ms) { - if (onChange() == 2) startUs = micros(); if (isReleased() && getElapsedMs() > ms) { return true; } @@ -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; @@ -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; } diff --git a/src/Toggle.h b/src/Toggle.h index 8acfc70..97a1d6f 100644 --- a/src/Toggle.h +++ b/src/Toggle.h @@ -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 diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -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