UNPKG

4.08 kBMarkdownView Raw
1gpio
2====
3Talk to your Raspberry Pi's GPIO
4
5* demo using LED: http://www.youtube.com/watch?v=2Juo-CJ6eu4
6* demo using RC car: http://www.youtube.com/watch?v=klQdX8-YVaI
7
8##Installation
9Get node.js on your Raspberry Pi - https://github.com/gflarity/node_pi
10
11## Usage
12
13This library is an npm package, just define "gpio" in your package.json dependencies or
14```js
15npm install gpio
16```
17
18##### Standard usage
19
20```js
21var gpio = require("gpio");
22
23// Calling export with a pin number will export that header and return a gpio header instance
24var gpio4 = gpio.export(4, {
25 // When you export a pin, the default direction is out. This allows you to set
26 // the pin value to either LOW or HIGH (3.3V) from your program.
27 direction: 'out',
28
29 // set the time interval (ms) between each read when watching for value changes
30 // note: this is default to 100, setting value too low will cause high CPU usage
31 interval: 200,
32
33 // Due to the asynchronous nature of exporting a header, you may not be able to
34 // read or write to the header right away. Place your logic in this ready
35 // function to guarantee everything will get fired properly
36 ready: function() {
37 gpio4.set(); // sets pin to high
38 console.log(gpio4.value); // should log 1
39
40 gpio4.set(0); // sets pin to low (can also call gpio4.reset()
41 console.log(gpio4.value); // should log 0
42
43 gpio4.unexport(); // all done
44 }
45});
46```
47
48##### Header direction "in" and event binding
49If you plan to set the header voltage externally, use direction 'in' and read value from your program.
50This library uses node's EventEmitter (http://nodejs.org/api/events.html) which allows you to watch
51for value changes and fire a callback. Event bindings also work when direction is 'out'.
52```js
53var gpio = require("gpio");
54
55// creates pin instance with direction "in"
56var gpio4 = gpio.export(4, {
57 direction: "in",
58 ready: function() {
59
60 // bind to the "change" event
61 // see nodejs's EventEmitter
62 gpio4.on("change", function(val) {
63 // value will report either 1 or 0 (number) when the value changes
64 console.log(val)
65 });
66
67 // you can bind multiple events
68 var processPin4 = function(val) { console.log(val); };
69 gpio4.on("change", processPin4);
70
71 // unbind a particular callback from the "change" event
72 gpio4.removeListener("change", processPin4);
73
74 // unbind all callbacks from the "change" event
75 gpio4.removeAllListeners("change");
76
77 // you can also manually change the direction anytime after instantiation
78 gpio4.setDirection("out");
79 gpio4.setDirection("in");
80 }
81});
82```
83
84## Example
85##### Cycle voltage every half a second
86```js
87var gpio = require("gpio");
88var gpio22, gpio4, intervalTimer;
89
90// Flashing lights if LED connected to GPIO22
91gpio22 = gpio.export(22, {
92 ready: function() {
93 inervalTimer = setInterval(function() {
94 gpio22.set();
95 setTimeout(function() { gpio22.reset(); }, 500);
96 }, 1000);
97 }
98});
99
100// Lets assume a different LED is hooked up to pin 4, the following code
101// will make that LED blink inversely with LED from pin 22
102gpio4 = gpio.export(4, {
103 ready: function() {
104 // bind to gpio22's change event
105 gpio22.on("change", function(val) {
106 gpio4.set(1 - val); // set gpio4 to the opposite value
107 });
108 }
109});
110
111// reset the headers and unexport after 10 seconds
112setTimeout(function() {
113 clearInterval(intervalTimer); // stops the voltage cycling
114 gpio22.removeAllListeners('change'); // unbinds change event
115 gpio22.reset(); // sets header to low
116 gpio22.unexport(); // unexport the header
117
118 gpio4.reset();
119 gpio4.unexport(function() {
120 // unexport takes a callback which gets fired as soon as unexporting is done
121 process.exit(); // exits your node program
122 });
123}, 10000)
124```
125
126
127##### Controlling an RC car
128Source code here: https://github.com/EnotionZ/node-rc
129