Latest Version: 1.2.0
This library implements debouncing for buttons connected to an imp. It requires just two lines of code in your device firmware: one to require the library, the second to instantiate the class. Instantiation involves providing callback functions that will be executed, respectively, when the button is pressed and then released. The class automatically handles bounces, ensuring your callbacks are only run when the button has been intentionally pressed and released.
You can view the library’s source code on GitHub. Click here to see information on the available versions of this library.
To add this library to your project, add #require "Button.class.nut:1.2.0"
to the top of your device code
The Button constructor has two required parameters — pin and pull — and three optional parameters: polarity, pressCallback and releaseCallback.
Neither callback function should include any parameters:
#require "Button.class.nut:1.2.0"
// Button on pin7
button <- Button(hardware.pin7, DIGITAL_IN_PULLUP, Button.NORMALLY_HIGH,
function() {
server.log("Button pressed");
}
);
The onPress() method sets the callback for the button’s onPress event (ie. when the button is depressed). If an onPress callback function is already registered, it will be replaced with the new callback. Setting the callback to null
(or passing no reference) will result in no action being taken when the button is pressed. This is the default behavior.
button.onPress(function() {
server.log("Button Pressed!");
});
The onRelease() method sets the callback for the button’s onRelease event (ie. when the button is released). If an onRelease callback function is already registered, it will be replaced with the new callback. Setting the callback to null
(or passing no reference) will result in no action being taken when the button is released. This is the default behavior.
button.onRelease(function() {
server.log("Button Released!");
});
All methods return the this
, the instance of the instantiated object. This allows method chaining as shown below:
Button(hardware.pin1, DIGITAL_IN_WAKEUP)
.onPress(function() {
server.log("Pressed");
}).onRelease(function() {
server.log("Released.. going to sleep");
imp.onidle(function() { server.sleepfor(3600); });
});
The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.
Version | Source Code | Notes |
---|---|---|
1.1.0 | GitHub | Initial release |
1.1.1 | GitHub | Minor code changes |
1.2.0 | GitHub | onRelease() and onPress() callback parameters now optional |
The Button library is licensed under the MIT License.