-
Notifications
You must be signed in to change notification settings - Fork 0
2. Library API
Caio Frota edited this page Mar 5, 2021
·
1 revision
void begin(); // Initial Setup.
void loop(); // Program Loop.
int getState(); // Get button state.
bool isPulledUp(); // True if button is pulled up.
void setPulledUp(); // Define button is pulled up.
void setPulledDown(); // Define button is pulled down.
int getShortPressTime(); // Get short time.
void setShortPressTime(int shortPressTime); // Define short time.
int getLongPressTime(); // Get long time.
void setLongPressTime(int shortPressTime); // Define long time.
void setOnShortPressReachedCallback(std::function<void()> func); // Define short time reached callback.
void setOnLongPressReachedCallback(std::function<void()> func); // Define long time reached callback.
void setOnPressCallback(std::function<void()> func); // Define button press callback.
void setOnShortPressCallback(std::function<void()> func); // Define button short press callback.
void setOnLongPressCallback(std::function<void()> func); // Define button long press callback.
You can check the button status whenever you want.
void loop() {
// ... YOUR CODE ...
pushButton.getState(); // It will return the value of "digitalRead(YOUR_BUTTON_PIN)"
// ... YOUR CODE ...
}
- Schematic
By default, PULL DOWN is defined for reading HIGH as pressed. But if you need to change the configuration and read LOW as pressed, you need to put the following line:
void setup() {
// ... YOUR CODE ...
pushButton.setPulledUp(); // Define a PULL UP resistor.
// ... YOUR CODE ...
}
In addition, you also can define it back as pulled DOWN whenever you want or check if it's pulled UP or DOWN with the following methods:
void loop() {
// ... YOUR CODE ...
pushButton.setPulledDown(); // Define a PULL DOWN resistor.
pushButton.setPulledUp(); // Define a PULL UP resistor.
pushButton.isPulledUp(); // Return true if your code is pulled up.
// ... YOUR CODE ...
}
Sometimes we need to take different actions when the button is just pressed and when it's hold for a couple time. So, it was developed a functionality where you can have 3 actions in the same button:
- Singles Press: When the button is just pressed and released quickly.
- Short Press: When the button is pressed and held for a short time (3 seconds by default).
- Long Press: When the button is pressed and held for a long time (10 seconds by default).
If you need to change the amount of time it should be pressed:
void setup() {
// ... YOUR CODE ...
setShortPressTime(2000); // Change the short time to 2 seconds.
getShortPressTime(); // Get the amount of time that it should be held.
// ... YOUR CODE ...
}
void setup() {
// ... YOUR CODE ...
setLongPressTime(5000); // Change the long time to 5 seconds.
getLongPressTime(); // Get the amount of time that it should be held.
// ... YOUR CODE ...
}
Define your callback functions for each action you need.
- This callback is called when the defined short press time is reached (not released). The default is 3 seconds.
void setup() {
// ... YOUR CODE ...
setOnShortPressReachedCallback(onShortPressCallback);
// ... YOUR CODE ...
}
void onShortPressCallback() {
// Do something...
}
- This callback is called when the defined long press time is reached (not released). The default is 10 seconds.
void setup() {
// ... YOUR CODE ...
setOnLongPressReachedCallback(onLongPressCallback);
// ... YOUR CODE ...
}
void onLongPressCallback() {
// Do something...
}
- This callback is called when the button is released but it didn't reach either short or long press time.
void setup() {
// ... YOUR CODE ...
setOnPressCallback(onPressCallback);
// ... YOUR CODE ...
}
void onPressCallback() {
// Do something...
}
- This callback is called when the button is released and reached the short press time but it didn't reach the long press time.
void setup() {
// ... YOUR CODE ...
setOnShortPressCallback(onShortPressCallback);
// ... YOUR CODE ...
}
void onShortPressCallback() {
// Do something...
}
- This callback is called when the button is released and reached the long press time.
void setup() {
// ... YOUR CODE ...
setOnLongPressCallback(onLongPressCallback);
// ... YOUR CODE ...
}
void onLongPressCallback() {
// Do something...
}