Skip to content

Commit

Permalink
updated inital state setting of state & prev_state
Browse files Browse the repository at this point in the history
  • Loading branch information
LennartHennigs committed Aug 31, 2021
1 parent 6c0e29d commit 1262409
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
39 changes: 20 additions & 19 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,77 @@
# Changelog

## Unreleased
- added two new examples: [```MultiHandlerTwoButtons.ino```](https://github.com/LennartHennigs/Button2/blob/master/examples/MultiHandlerTwoButtons/MultiHandlerTwoButtons.ino) and [```TrackDualButtonClick.ino```](https://github.com/LennartHennigs/Button2/blob/master/examples/TrackDualButtonClick/TrackDualButtonClick.ino.ino)
- added two new examples: [`MultiHandlerTwoButtons.ino`](https://github.com/LennartHennigs/Button2/blob/master/examples/MultiHandlerTwoButtons/MultiHandlerTwoButtons.ino) and [`TrackDualButtonClick.ino`](https://github.com/LennartHennigs/Button2/blob/master/examples/TrackDualButtonClick/TrackDualButtonClick.ino.ino)
- added examples to the [README.md](https://github.com/LennartHennigs/Button2/blob/master/README.md)
- a bit of variable clean up
- updated setting of inital state of `state` and `prev_state`

## [1.6.2] - 2021-06-22
- initialized ```pin``` property to 255 instead of -1, as pointed out by [rin67630](https://github.com/rin67630) in issue [#26](https://github.com/LennartHennigs/Button2/issues/26)
- initialized `pin` property to 255 instead of -1, as pointed out by [rin67630](https://github.com/rin67630) in issue [#26](https://github.com/LennartHennigs/Button2/issues/26)


## [1.6.1] - 2021-03-22
- updated [README.md](https://github.com/LennartHennigs/Button2/blob/master/README.md)
- added ```const``` to getter functions
- added `const` to getter functions


## [1.6.0] - 2021-02-10
- added getter/setter functions for debounce, longclick and doubleclick timeouts
- removed debounce timeout parameter from ```contructor``` and ```begin()```
- removed debounce timeout parameter from `contructor` and `begin()`


## [1.5.4] - 2021-02-08
- Added ```getAttachPin()``` function, as suggested by [madivad](https://github.com/madivad) in issue [#23](https://github.com/LennartHennigs/Button2/issues/23)
- Added `getAttachPin()` function, as suggested by [madivad](https://github.com/madivad) in issue [#23](https://github.com/LennartHennigs/Button2/issues/23)


## [1.5.3] - 2021-01-26
- Fixed a bug in the constructor, as suggested by [alex-s-v](https://github.com/alex-s-v) in pull request [#22](https://github.com/LennartHennigs/Button2/pull/22)


## [1.5.2] - 2021-01-26
- Fixed a bug in the ```isPressed()``` function, as suggested by [zenturacp](https://github.com/zenturacp) in [#21](https://github.com/LennartHennigs/Button2/issues/21)
- Fixed a bug in the `isPressed()` function, as suggested by [zenturacp](https://github.com/zenturacp) in [#21](https://github.com/LennartHennigs/Button2/issues/21)


## [1.5.1] - 2021-01-04
- Fixed a bug in the ```loop()``` function
- Fixed a bug in the `loop()` function


## [1.5.0] - 2021-01-03
- Added default constructor and ```begin()``` function
- Added pull request by [skelstar](https://github.com/skelstar) to add the ```setLongClickDetectedHandler()``` function which is triggered as soon as the longclick timeout has passed
- Added default constructor and `begin()` function
- Added pull request by [skelstar](https://github.com/skelstar) to add the `setLongClickDetectedHandler()` function which is triggered as soon as the longclick timeout has passed


## [1.4.1] - 2020-12-19
- Moved activeLow outside of isCapacitive condition (as suggested by [Wai Lin](https://github.com/w4ilun) in [#18](https://github.com/LennartHennigs/Button2/pull/18)


## [1.4.0] - 2020-11-06
- Updated LongpressHandler example - changed variable name to from ```buttonA``` to ```button```
- toggled ```pressed``` and ```released``` (as suggesed by [TommyC81](https://github.com/TommyC81) in [#16](https://github.com/LennartHennigs/Button2/issues/16))
- added debug function ```isPressedRaw()``` (as suggesed by [TommyC81](https://github.com/TommyC81) in [#16](https://github.com/LennartHennigs/Button2/issues/16))
- fixed bug with ```click_count``` (as suggesed by [TommyC81](https://github.com/TommyC81) in [#16](https://github.com/LennartHennigs/Button2/issues/16))
- changed return types of ```getNumberOfClicks()``` and ```getClickType()``` to ```byte```
- Updated LongpressHandler example - changed variable name to from `button` to `button`
- toggled `pressed` and `released` (as suggesed by [TommyC81](https://github.com/TommyC81) in [#16](https://github.com/LennartHennigs/Button2/issues/16))
- added debug function `isPressedRaw()` (as suggesed by [TommyC81](https://github.com/TommyC81) in [#16](https://github.com/LennartHennigs/Button2/issues/16))
- fixed bug with `click_count` (as suggesed by [TommyC81](https://github.com/TommyC81) in [#16](https://github.com/LennartHennigs/Button2/issues/16))
- changed return types of `getNumberOfClicks()` and `getClickType()` to `byte`


## [1.3.0] - 2020-11-06
- Added capacitive touch sensor capabilties (for ESP32) (as suggested by [qubolino](https://github.com/qubolino) in [#11](https://github.com/LennartHennigs/Button2/issues/11))
- Removed deprecated entry in the library.properties file (as suggested by [SangLe](https://github.com/SNL5943)) in [#15](https://github.com/LennartHennigs/Button2/issues/15)
- Added ```const``` modifier to functions (as suggested by [Anton-V-K](https://github.com/Anton-V-K) in [#13](https://github.com/LennartHennigs/Button2/issues/13))
- Added `const` modifier to functions (as suggested by [Anton-V-K](https://github.com/Anton-V-K) in [#13](https://github.com/LennartHennigs/Button2/issues/13))


## [1.2.0] - 2020-04-16
- Added possibility to define your own timeouts for clicks (as suggested by [cmeldas](https://github.com/cmeldas) in [#10](https://github.com/LennartHennigs/Button2/issues/10))
- Removed ```yield()``` in main ```loop()``` since it caused some problems
- Removed `yield()` in main `loop()` since it caused some problems
- Created and added CHANGELOG.md


## [1.1.0] - 2020-03-27
- Changed the private functions to protected (as suggested by [Nagymadar](https://github.com/Nagymadar) in [#9](https://github.com/LennartHennigs/Button2/issues/9))
- Added support for active high buttons (as suggested by [Nagymadar](https://github.com/Nagymadar) in [#8](https://github.com/LennartHennigs/Button2/issues/8))
- Added ```reset()``` function to unset all functions (as suggested by [Nagymadar](https://github.com/Nagymadar) in [#7](https://github.com/LennartHennigs/Button2/issues/7))
- Added a ```yield()``` command to the main ```loop()```
- Added `reset()` function to unset all functions (as suggested by [Nagymadar](https://github.com/Nagymadar) in [#7](https://github.com/LennartHennigs/Button2/issues/7))
- Added a `yield()` command to the main `loop()`
- Changed the times for double and triple click
- Fixed error in ```SingleButton.ino``` (as suggested by [alexthe-red](https://github.com/alexthe-red) in [#3](https://github.com/LennartHennigs/Button2/issues/3))
- Fixed error in `SingleButton.ino` (as suggested by [alexthe-red](https://github.com/alexthe-red) in [#3](https://github.com/LennartHennigs/Button2/issues/3))
- Added the library to the Arduino IDE


Expand Down
19 changes: 14 additions & 5 deletions src/Button2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ void Button2::begin(byte attachTo, byte buttonMode /* = INPUT_PULLUP */, boolean
setDebounceTime(DEBOUNCE_MS);
setLongClickTime(LONGCLICK_MS);
setDoubleClickTime(DOUBLECLICK_MS);
state = activeLow ? HIGH : LOW;
if (!isCapacitive) {
pinMode(attachTo, buttonMode);
} else {
is_capacitive = true;
}
state = _getState();
prev_state = state ;
}

/////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -175,10 +176,8 @@ byte Button2::getClickType() const {

/////////////////////////////////////////////////////////////////

void Button2::loop() {
if (pin != UNDEFINED_PIN) {
prev_state = state;
unsigned long now = millis();
byte Button2::_getState() {
byte state;
if (!is_capacitive) {
state = digitalRead(pin);
} else {
Expand All @@ -187,6 +186,16 @@ void Button2::loop() {
state = capa < CAPACITIVE_TOUCH_THRESHOLD ? LOW : HIGH;
#endif
}
return state;
}

/////////////////////////////////////////////////////////////////

void Button2::loop() {
if (pin != UNDEFINED_PIN) {
prev_state = state;
unsigned long now = millis();
state = _getState();
// is button pressed?
if (state == _pressedState && prev_state != _pressedState) {
down_ms = now;
Expand Down
4 changes: 4 additions & 0 deletions src/Button2.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class Button2 {
bool operator == (Button2 &rhs);

void loop();

private:
byte _getState();

};
/////////////////////////////////////////////////////////////////
#endif
Expand Down

0 comments on commit 1262409

Please sign in to comment.