pidriver
Node.js bind of wiringPi lib
Installation
$ [sudo] npm install pidriver
Usage
Some functions may need root privileges
$ [sudo] node myapp
API is at most sync but any function that have callback(cb) can by async
//syncvar ret = input; //asyncinput
Using "mods"
var pi = ; piINPUT //'in' //all mods: var mods = //Gpio mode: INPUT: 'in' OUTPUT: 'out' //for internal use rather (best not to use) PWM_OUTPUT: 2 GPIO_CLOCK: 3 //Gpio states: LOW: '0' HIGH: '1' // Gpio pull PUD_OFF: 0 PUD_DOWN: 1 PUD_UP: 2 // PWM PWM_MODE_MS: 0 PWM_MODE_BAL: 1 // Interrupt levels INT_EDGE_NONE: 'none' INT_EDGE_SETUP: 0 INT_EDGE_FALLING: 'falling' INT_EDGE_RISING: 'rising' INT_EDGE_BOTH: 'both' ;
Gpio
var pi = led = 'GPIO27' mode: piOUTPUT input = 'GPIO24' pull: piPUD_UP; input; input;
new Gpio(gpio [,options])
gpio - pin number / 'GPIOx' / 'WPIOx' / 'special name'
You can use pin number or one of most popular names: 'GPIOx' for official gpio numbering, 'WPIOx' for wiringPi gpio numbering or its special name like 'PWM0' (for 12s pin) pin map
Full list of available names:
var pi = ; console /* example for 12s pin: { ... 12: {name: ['PWM0','GPIO18', 'WPIO1'], gpio: 18, able: true, pullable: true}, ... } Pin number: 12 Available names: PWM0, GPIO18, WPIO1 Usable as Gpio Pullable (see below)*/
options
//default edge: piINT_EDGE_BOTH mode: piINPUT pull: piPUD_OFF state: 0 //initial state
About mode and pull wiringPi doc, use "mods" for edge, mode, pull
Gpio is also Event Emitter:
Gpio.on('event', cb)
events: 'change' - state change 'up' - state 1 'down' - state 0
Gpio methods
-
Gpio.read([cb]) - read state
-
Gpio.write(state [,cb]) - set state
-
Gpio.up([cb]) - set state high
-
Gpio.down([cb]) - set state low
-
Gpio.setEdge(edge)
-
Gpio.setDirection(dir)
-
Gpio.setPull(pud)
-
Gpio.isPullable()
-
Gpio.isInput()
-
Gpio.isOutput()
-
Gpio.direction() - get direction
-
Gpio.options() - get options
-
Gpio.unexport()
Group
var pi = led0 = 'GPIO27' mode: _OUTPUT led1 = 'GPIO22' mode: _OUTPUT led2 = 'GPIO22' mode: _OUTPUT; var group = led0 led1 led2; group; //light up all leds group; // turn on leds: led0 and led2
new Group(gpios)
gpios - Array of Gpio
Group methods
-
Group.read([cb])
-
Group.write(value [,cb])
value is number (you can use hex(0xFF) also) number is formatted to bits array (85 -> 01010101) and asian to proper pin
-
Group.up([cb])
-
Group.down([cb])
PWM
var pi = pwm = 'PWM0' {}; console; var state = 0; ;
new PWM(gpio [,options])
gpio - gpio name, see Gpio
options
//default clock: 2 range: 1024 mode: modsPWM_MODE_BAL duty: 0
about range, clock, mode see PWM, for mode use "mods"
PWM methods
-
PWM.isHW() - is hardware PWM (only supported)
-
PWM.setRange(range)
-
PWM.setClock(clock)
-
PWM.setMode(mode)
-
PWM.write(value[, cb])
-
PWM.options()
-
PWM.unexport()
SPI
$ gpio load spi
var pi = spi = 0 { buff; buff; buff; return buff; } { return buff1&3<<8 + buff2; } ; //post and preBuffering code from http://www.raspberrypi-spy.co.uk/2013/10/analogue-sensors-on-the-raspberry-pi-using-an-mcp3008/ //example of reading channel 0 on MCP3008 ;
new SPI(channel [,options])
channel - there is only two channels (0 and 1 (CE0 and CE1 pin))
options
//default channel: 0 clock: 1000000 preBuffering: null postBuffering: null words: 3
SPI methods
-
SPI.options()
-
SPI.write(data, arg [,cb])
-
SPI.read(arg [,cb])
-
SPI.transfer(data, arg [,cb])
LCD
var pi = lcd = 'GPIO7''GPIO8''GPIO25''GPIO24''GPIO23''GPIO18'; lcd; var i = 0 t = '-''\\''|''/';
new LCD(gpios [,options])
gpios - array of GPIO name (first is rs, second strb, channel0, channel1, ...)
options
//default rows: 2 //number of LED's rows cols: 16 //number of LED's cols bits: 4 //number of LED's channel (4 or 8 depends on LCD and installation)
LCD methods
-
LCD.clear([cb])
-
LCD.home([cb])
-
LCD.write([cb])
-
LCD.setPosition(x, y [,cb])