BounceButton - a class for buttons #61

Closed
NoobTracker wants to merge 3 commits from master into master
NoobTracker commented 2020-05-08 17:51:37 +03:00 (Migrated from github.com)

The BounceButton class adds the setInvert() function to the Bounce class, which can be used to invert the new pressed() and released() functions. read() is also adjusted so that you can use setInvert() with it. This class is intended for inverting a button when using INPUT_PULLUP.

The BounceButton class adds the setInvert() function to the Bounce class, which can be used to invert the new pressed() and released() functions. read() is also adjusted so that you can use setInvert() with it. This class is intended for inverting a button when using INPUT_PULLUP.
todo[bot] commented 2020-05-08 17:51:40 +03:00 (Migrated from github.com)

Make Bounce2 more abstract. Split it from the hardware layer.

Remove deboucing code from Bounce2 and make a new Debounce class from that code. Bounce2 should extend Debounce.


https://github.com/thomasfredericks/Bounce2/blob/7e1cb95a9d289fd178b35f43ac9dd7a3b6ad1a5a/src/Bounce2.h#L30-L35


This comment was generated by todo based on a todo comment in 7e1cb95a9d in #61. cc @NoobTracker.
## Make Bounce2 more abstract. Split it from the hardware layer. Remove deboucing code from Bounce2 and make a new Debounce class from that code. Bounce2 should extend Debounce. --- https://github.com/thomasfredericks/Bounce2/blob/7e1cb95a9d289fd178b35f43ac9dd7a3b6ad1a5a/src/Bounce2.h#L30-L35 --- ###### This comment was generated by [todo](https://todo.jasonet.co) based on a `todo` comment in 7e1cb95a9d289fd178b35f43ac9dd7a3b6ad1a5a in #61. cc @NoobTracker.
thomasfredericks commented 2020-05-19 21:36:56 +03:00 (Migrated from github.com)

Hi @NoobTracker,

I made a branch ( https://github.com/thomasfredericks/Bounce2/tree/Button ) with the basic idea behind your merge request. Please check the buttonClass example. I did not test it yet. I included the class directly into Bounce2.h

Hi @NoobTracker, I made a branch ( https://github.com/thomasfredericks/Bounce2/tree/Button ) with the basic idea behind your merge request. Please check the buttonClass example. I did not test it yet. I included the class directly into Bounce2.h
NoobTracker commented 2020-05-19 21:53:12 +03:00 (Migrated from github.com)

Hi,
the idea with setPressedState () is really good, it is more understandable than setInvert (). The program should work. With stateForPressed, however, the default value is 0, which immediately activates the inversion. That should be changed. I would also return !(getStateFlag(DEBOUNCED_STATE) ^ stateForPressed) && getStateFlag(CHANGED_STATE); use for pressed rather than the current program and maybe outsource the functions to the .cpp file. But that's your decision. In any case, thank you for taking care of the program.

Hi, the idea with setPressedState () is really good, it is more understandable than setInvert (). The program should work. With stateForPressed, however, the default value is 0, which immediately activates the inversion. That should be changed. I would also `return !(getStateFlag(DEBOUNCED_STATE) ^ stateForPressed) && getStateFlag(CHANGED_STATE);` use for pressed rather than the current program and maybe outsource the functions to the .cpp file. But that's your decision. In any case, thank you for taking care of the program.
NoobTracker commented 2020-05-19 22:08:23 +03:00 (Migrated from github.com)

Oh, and the example doesn't work because you only switch on the LED if the button was pressed exactly during the last update. Then it goes out again. You would have to make

if ( buttonA.pressed()  || buttonB.pressed()  ) {
  digitalWrite(LED_PIN, HIGH );
} 
else {
  digitalWrite(LED_PIN, LOW );
}
if ( buttonA.pressed()  || buttonB.pressed()  ) {
  digitalWrite(LED_PIN, HIGH );
} 
if ( buttonA.released()  || buttonB.released()  ) {
  digitalWrite(LED_PIN, LOW );
}

You could also make #define LED_PIN 13 #define LED_PIN LED_BUILTIN.

Oh, and the example doesn't work because you only switch on the LED if the button was pressed exactly during the last update. Then it goes out again. You would have to make ``` if ( buttonA.pressed() || buttonB.pressed() ) { digitalWrite(LED_PIN, HIGH ); } else { digitalWrite(LED_PIN, LOW ); } ``` ``` if ( buttonA.pressed() || buttonB.pressed() ) { digitalWrite(LED_PIN, HIGH ); } if ( buttonA.released() || buttonB.released() ) { digitalWrite(LED_PIN, LOW ); } ``` You could also make `#define LED_PIN 13` `#define LED_PIN LED_BUILTIN`.
thomasfredericks commented 2020-05-20 00:16:56 +03:00 (Migrated from github.com)

Wow, thanks for the error checking. I really messed up ;) I will correct it.
I prefer not to create additional header files to reduce potential conflicts with other libraries. Although they could be hidden in a subfolder inside «src».

Wow, thanks for the error checking. I really messed up ;) I will correct it. I prefer not to create additional header files to reduce potential conflicts with other libraries. Although they could be hidden in a subfolder inside «src».
thomasfredericks commented 2020-05-21 22:46:39 +03:00 (Migrated from github.com)

Corrections were uploaded

Corrections were uploaded
jamesmyatt commented 2020-07-25 00:04:15 +03:00 (Migrated from github.com)

"setPressedState" is much clearer. I have a class in my current project that's very similar 😉 . I think you can re-use the state attribute to avoid increasing memory usage. Would it be possible to close this PR and open a new one for your "Button" branch?

"setPressedState" is much clearer. I have a class in my current project that's very similar 😉 . I think you can re-use the state attribute to avoid increasing memory usage. Would it be possible to close this PR and open a new one for your "Button" branch?
thomasfredericks commented 2020-08-03 16:50:46 +03:00 (Migrated from github.com)

Would it be possible to close this PR and open a new one for your "Button" branch?

Yes @jamesmyatt

> Would it be possible to close this PR and open a new one for your "Button" branch? Yes @jamesmyatt

Pull request closed

Please reopen this pull request to perform a merge.
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Max/thomasfredericks_Bounce2#61