Bounce2 icon indicating copy to clipboard operation
Bounce2 copied to clipboard

Hardware abstraction should be by composition rather than inheritance

Open jamesmyatt opened this issue 5 years ago • 6 comments

Especially since there is a Button class now, the hardware abstraction should be done by composition rather than inheritance. For example, it's not possible to define a Button instance that uses something other than digitalRead to read the state. What do you think?

https://en.wikipedia.org/wiki/Composition_over_inheritance

jamesmyatt avatar Aug 26 '20 13:08 jamesmyatt

Hum, interesting. But how would this be implemented?

thomasfredericks avatar Aug 27 '20 01:08 thomasfredericks

I also want to find a way to do this, id like to be able to define a button instance with an input that is not digitalRead, maybe add in an override?

KenwoodFox avatar Dec 21 '21 19:12 KenwoodFox

@KenwoodFox this can already be implemented. See the source for class Bounce in Bounce2.h.

You can set your custom override for digitalRead : virtual bool readCurrentState() { return digitalRead(pin); }

thomasfredericks avatar Dec 22 '21 14:12 thomasfredericks

@KenwoodFox this can already be implemented. See the source for class Bounce in Bounce2.h.

You can set your custom override for digitalRead : virtual bool readCurrentState() { return digitalRead(pin); }

This is pretty awesome! thanks so much.

KenwoodFox avatar Dec 24 '21 03:12 KenwoodFox

@KenwoodFox this can already be implemented. See the source for class Bounce in Bounce2.h.

You can set your custom override for digitalRead : virtual bool readCurrentState() { return digitalRead(pin); }

Sorry to ask, but where would you implement this? Should this be done for every button instance you create or do i need to make my own button class using inheriting from bounce?

KenwoodFox avatar Dec 27 '21 21:12 KenwoodFox

It isn't a complete replacement because it doesn't have complex debouncing techniques, but the Debouncer library allows passing in the value to use.

Thynix avatar Feb 28 '22 12:02 Thynix