forked from jheising/node.pcduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.js
executable file
·142 lines (122 loc) · 2.71 KB
/
gpio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* A module to access the GPIO (General Purpose Input and Output) pins of the pcDuino Microcontroller.
*
* @module gpio
*/
var fs = require('fs');
var pinModes = [];
var pinStates = [];
var gpio = {};
gpio.INPUT = 0;
gpio.OUTPUT = 1;
gpio.SERIAL = 3;
gpio.INPUT_PU = 8;
gpio.LOW = 0;
gpio.HIGH = 1;
gpio.PIN_COUNT = 18;
function init(simulate)
{
pinModes = [];
pinStates = [];
var gpioModePath = "/sys/devices/virtual/misc/gpio/mode/";
var gpioStatePath = "/sys/devices/virtual/misc/gpio/pin/";
if(simulate)
{
// Make a directory to hold our simulated files
if(!fs.existsSync(__dirname + "/sim"))
{
fs.mkdirSync(__dirname + "/sim");
if(!fs.existsSync(__dirname + "/sim/gpio"))
{
fs.mkdirSync(__dirname + "/sim/gpio");
}
}
gpioModePath = __dirname + "/sim/gpio/mode.";
gpioStatePath = __dirname + "/sim/gpio/pin.";
}
// Create paths to our individual pins
for(var index = 0; index < gpio.PIN_COUNT; index++)
{
pinModes.push(gpioModePath + "gpio" + index);
pinStates.push(gpioStatePath + "gpio" + index);
if(simulate)
{
if(!fs.existsSync(pinModes[index]))
{
fs.writeFileSync(pinModes[index], "0");
}
if(!fs.existsSync(pinStates[index]))
{
fs.writeFileSync(pinStates[index], "0");
}
}
}
}
function validatePin(pin)
{
if(pin < 0 || pin >= gpio.PIN_COUNT)
{
throw "Invalid pin";
}
}
/**
* Simulate reading or writing values to the GPIO ports. Can be used to simulate values when running on a non pcDuino system.
*
* @param {boolean} simulate - Set to true to simulate, or false to not simulate.
*/
gpio.simulate = init;
/**
* Set the mode for a GPIO pin.
*
* @param {!number} pin - A pin number from 0 to PIN_COUNT.
* @param mode
*/
gpio.pinMode = function(pin, mode)
{
validatePin(pin);
fs.writeFileSync(pinModes[pin], String(mode));
}
/**
* Read the state for a GPIO pin.
*
* @param {!number} pin - A pin number from 0 to PIN_COUNT.
*/
gpio.digitalRead = function(pin)
{
validatePin(pin);
return Number(fs.readFileSync(pinStates[pin], "utf8"));
}
/**
* Check if the GPIO pin is HIGH.
*
* @param {!number} pin - A pin number from 0 to PIN_COUNT.
* @returns {boolean}
*/
gpio.isHIGH = function(pin)
{
return (gpio.digitalRead(pin) === gpio.HIGH);
}
/**
* Check if the GPIO pin is LOW.
*
* @param {!number} pin - A pin number from 0 to PIN_COUNT.
* @returns {boolean}
*/
gpio.isLOW = function(pin)
{
return (gpio.digitalRead(pin) === gpio.LOW);
}
/**
* Set the state of a GPIO pin.
*
* @param {!number} pin - A pin number from 0 to PIN_COUNT.
* @param value
*/
gpio.digitalWrite = function(pin, value)
{
validatePin(pin);
fs.writeFileSync(pinStates[pin], String(value));
}
// Init without simulating
init(false);
module.exports = gpio;