-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpio_base.js
157 lines (124 loc) · 3.79 KB
/
gpio_base.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"use strict";
var fs = require("fs");
var path = require("path");
var extend = require('./helpers').extend;
var GPIOModule = (function(){
// TODO: move these paths to a config file
var _export = "/sys/class/gpio/gpiochip0/subsystem/export";
var _unexport = "/sys/class/gpio/gpiochip0/subsystem/unexport";
var _gpioPath = "/sys/class/gpio/";
// TODO: make this platform agnostic so we can specify pin mappings
var _validPins = [0,1,6,7,12,13,14,18,19,20,21,23,26];
var _configuredPins = {};
var _INPUT = "in";
var _OUTPUT = "out";
function _isNumber(number){
return !isNaN(parseInt(number, 10));
}
function _verifyPinIsValid(pinNumber){
if(!_isNumber(pinNumber) || _validPins.indexOf(pinNumber) == -1){
throw new Error("Invalid pin number: " + pinNumber);
}
}
function _verifyPinIsNotInUse(pinNumber){
if (fs.existsSync(path.join(_gpioPath, "gpio" + pinNumber)))
throw new Error("Pin " + pinNumber + " is already in use");
}
// Pin object used for tracking pins
var GPIOPin = function(pinNumber, direction, opts){
this.options = opts || {};
_verifyPinIsValid(pinNumber);
// Skipping verification is used for testing purposes
if(!this.options.hasOwnProperty('skipVerify'))
_verifyPinIsNotInUse(pinNumber);
fs.writeFileSync(_export, pinNumber);
this.pin = pinNumber;
this.valuePath = path.join(_gpioPath, "gpio" + pinNumber, "value");
this.directionPath = path.join(_gpioPath, "gpio" + pinNumber, "direction");
this.setDirection(direction);
this.value = null;
};
// "static" members of pin
GPIOPin.INPUT = "in";
GPIOPin.OUTPUT = "out";
GPIOPin.prototype.setDirection = function(direction){
fs.writeFileSync(this.directionPath, direction);
this.direction = direction;
};
GPIOPin.prototype.read = function(){
if(this.direction === _OUTPUT){
throw new Error("Cannot read from a pin that is configured as an output");
}
this.value = parseInt(fs.readFileSync(this.valuePath), 10);
return this.value;
};
GPIOPin.prototype.write = function(value){
if(this.direction === _INPUT){
throw new Error("Cannot write to a pin that is configured as an input");
}
value = !!value ? "1" : "0";
fs.writeFileSync(this.valuePath, value);
this.value = value === "1" ? 1 : 0;
};
GPIOPin.prototype.destroy = function(){
this.setDirection(_INPUT);
fs.writeFileSync(_unexport, this.pin);
}
var InputPin = function(pinNumber, opts){
GPIOPin.call(this, pinNumber, _INPUT, opts || {});
// Set initial value;
this.value = this.read();
};
InputPin.prototype = Object.create(GPIOPin.prototype);
var OutputPin = function(pinNumber, opts){
GPIOPin.call(this, pinNumber, _OUTPUT, opts || {});
var defaults = {
initial_state: 'off',
active_low: false
};
this.options = extend(defaults, this.options);
if((/on/).test(this.options.initial_state)){
this.is_on = true;
}
else{
this.is_on = false;
}
// Set the pin to the right initial state
if(this.options.active_low){
this.value = Number(!this.is_on);
}
else{
this.value = Number(this.is_on);
}
this.write(this.value);
};
OutputPin.prototype = Object.create(GPIOPin.prototype);
OutputPin.prototype.on = function(){
if(this.options.active_low){
this.write(0);
}
else{
this.write(1);
}
this.is_on = true;
};
OutputPin.prototype.off = function(){
if(this.options.active_low){
this.write(1);
}
else{
this.write(0);
}
this.is_on = false;
};
OutputPin.prototype.toggle = function(){
this.value ^= 1;
this.write(this.value);
};
return {
GPIOPin: GPIOPin,
InputPin: InputPin,
OutputPin: OutputPin
};
})();
module.exports = GPIOModule;