Comments (5)
Did you ever figure this out?
from onebutton.
Hello, so I wanted to use the OneButton library with the mcp23017 pins, as I didn't find anything about it in the topics, I ended up giving up
from onebutton.
The library is not made for using input signals from other sources but the DigitalIn Pins.
If you like to use the MCP23017 as input you have to re-write parts of the library.
It can be done like I did it in https://github.com/HomeDing/HomeDing/blob/develop/src/ButtonElement.cpp.
The loop() function in this implementation is doing almost the same as the button library but is not using the digital read directly.
As a general comment, this library can be used under conditions and assumptions and this is true to many other libraries. Coding skills are required.
Feel free to just use part of the library code for your specific problem.
Regarding the Homeding library and eco-system:
An Element for the mcp23017 can be implemented to create actions based on input levels and send them to the button element. This is a action based implementation approach that fits well to IoT Implementations.
from onebutton.
Yes. Look:
/*
* MCP23017Encoder_OneButton.ino - Example for the OneButtonLibrary library.
* This is a sample sketch to show how to use the OneClick library on MCP23017 pins.
*
* The library internals are explained at
* http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
*
* Setup a test circuit:
* * Connect 2 pushbuttons to pin 2 at mcp0 and ground, and pin 5 at mcp1 and ground.
*
* The sketch shows how to setup the library and bind the functions (singleClick, doubleClick) to the events.
* In the loop function the button.tick function must be called as often as you like.
*
* * 22.01.2021 created by Matthias Hertel
*/
#include "OneButton.h"
#include <Adafruit_MCP23X17.h>
Adafruit_MCP23X17 mcp0;
Adafruit_MCP23X17 mcp1;
// This is an example on how to use the OneClick library on other input sources than standard digital pins.
// 1. do not use a pin in the initialization of the OneClick library.
// 2. pass the input state to the tick function.
// You can also find how to create an instance in setup and not by declaration.
// You can also find how to use inline callback functions.
// OneButton instance will be created in setup.
OneButton *button1;
OneButton *button2;
void setup() {
mcp0.begin_I2C(0x20); // i2c address 0x20
mcp1.begin_I2C(0x21); // i2c address 0x21
mcp0.pinMode(2, INPUT_PULLUP);
mcp1.pinMode(5, INPUT_PULLUP);
Serial.begin(115200);
delay(1000);
Serial.println("One Button Example with custom input.");
// create the OneButton instance without a pin.
button1 = new OneButton();
button2 = new OneButton();
// Here is an example on how to use a parameter to the registered function:
button1->attachClick([]() { click(1); });
button1->attachDoubleClick([]() { doubleclick(1); });
button1->attachMultiClick([]() { multiClick(1, button1->getNumberClicks()); });
button1->attachLongPressStart([]() { longPress(1); });
button2->attachClick([]() { click(2); });
button2->attachDoubleClick([]() { doubleclick(2); });
button2->attachMultiClick([]() { multiClick(2, button2->getNumberClicks()); });
button2->attachLongPressStart([]() { longPress(2); });
} // setup()
void loop() {
// read your own source of input:
bool isPressed1 = (mcp0.digitalRead(2) == LOW);
bool isPressed2 = (mcp1.digitalRead(5) == LOW);
// call tick frequently with current push-state of the input
button1->tick(isPressed1);
button2->tick(isPressed2);
} // loop()
void click(int b) { // Parameter to identfy wich button pressed
Serial.print("Button");
Serial.print(b);
Serial.println(" click");
} // click
void doubleclick(int b) { // Parameter to identfy wich button pressed
Serial.print("Button");
Serial.print(b);
Serial.println(" doubleclick");
} // doubleclick
// this function will be called when the button was pressed multiple times in a short timeframe.
void multiClick(int b, int n) { // Parameter to identfy wich button pressed and how often
Serial.print("Button");
Serial.print(b);
Serial.print(": ");
if (n == 3) {
Serial.println("tripleClick detected.");
} else if (n == 4) {
Serial.println("quadrupleClick detected.");
} else {
Serial.print("multiClick(");
Serial.print(n);
Serial.println(") detected.");
}
} // multiClick
void longPress(int b) { // Parameter to identfy wich button pressed
Serial.print("Button");
Serial.print(b);
Serial.println(" long press");
} // longPress
from onebutton.
Thanks for the code.
Yes, seems correct working.
Can you create a pull request to add the example ?
from onebutton.
Related Issues (20)
- ESP32CAM only pin 4 working HOT 1
- attachPressStart removed, attachLongPressStart can be used instead, but how?? HOT 1
- type mismatch for setPressTicks HOT 1
- Update Published PlatformIO version HOT 7
- OneButton stop working after ESP32 deep sleep HOT 3
- Arduino Library Manager doesn't download latest version HOT 2
- Lag after button press HOT 4
- Library not working on Nano ESP32 Board HOT 14
- Detecting multiple clicks returns wrong values for > 2
- HOMING
- Add isPressed function to support latching switch. HOT 1
- In the special input example, is it correct that click is fired first? HOT 6
- TwoButtons , only the first button work, the second button can not work。两个按键错误。 HOT 4
- event to fire immediately, when button is pressed HOT 1
- Deprecation warning on stm32 Arduino HOT 1
- How to get rid of #define PIN_INPUT? HOT 2
- Button.setLongPressIntervalMS(100) not working? HOT 1
- Does one button support i2c IO expander such as TCA9548A? HOT 2
- ERRROR compiling example SimpleOnButton HOT 1
- You actually fixed this issue while I was reporting it - thank you! HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from onebutton.