1.0.1 • Published 9 years ago

t2-button v1.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
9 years ago

t2-button

Quick start

Hardware

Connect your button between the 3v3 and the GND inputs on one of the GPIO pins on your tessel.

Code

The following code will light the t2's green LED while the button is pressed.

var tessel     = require('tessel');
var Button     = require('t2-button');
var buttonPin  = tessel.port.A.pin[0];
var greenLight = tessel.led[2];

var pushButton = Object.create(Button);

pushButton
.listen({ frequency: 100, pin: buttonPin })
.on('press', function () {
  green.on();
})
.on('release', function () {
  green.off();
})
.on('error', function (err) {
  console.log("Uh oh: ", err);
});

More information

Configuration

Button.listen should be called with an options object, which may have the following keys.

KeyDescriptionDefault / requried?
pinThe tessel GPIO pin that the button is connected to.No default - requried unless you have separately called Button.setPin
delayThe delay between polling the state of the button in millisecondsNo default - see frequency property below.
frequencyThe frequency at which the button should be polled in Hz. Note: this will be ignored if you have also specified a delayDefault: 100
pullDownBoolean indicating whether the pin is high or low when pressed. If true, the button is expected to be low when pressed.Default: true

Button properties

PropertyTypeDescription
setPinfunctionSets the GPIO pin to listen on.
listenfunctionStarts an interval to regularly poll the input and trigger the press and release events.
checkPinfunctionThis is the function called by the listen interval. If the pin value has changed since this was last called, the press or release event will be emitted.
stopListeningfunctionCancels the checkPin interval.
valnumberThe current value of the pin (1 or 0).

All methods are chainable. Button's prototype is an EventEmitter.

What's with Object.create(Button)?

This library was made using the "behaviour delegation" design pattern discussed by Kyle Simpson in his excellent book series You Don't know JS.