-
Notifications
You must be signed in to change notification settings - Fork 5
/
gpio-galileo-helper.js
242 lines (217 loc) · 6.53 KB
/
gpio-galileo-helper.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* Crafted by Obero on 02/14/14
* For Epitech Innovation Hub
* Edited by Obero on 29/09/14
*/
"use strict";
var fs = require('fs');
/**
* GalileoGpio helps you to manipulate GPIO on Intel Galileo board.
*
* @class GalileoGpio
* @constructor
*/
var GalileoGpio = function()
{
/**
* Helps you to work directly on a given IO pin with its number,
* mapping its number to the matching GPIO id.
* @property pin
* @type Object
*/
this.pin =
{
// Analogic
'A0': {"id" : "37", "index" :0},
'A1': {"id" : "36", "index" :1},
'A2': {"id" : "23", "index" :2},
'A3': {"id" : "22", "index" :3},
'A4': {"id" : "21", "index" :4},
'A5': {"id" : "20", "index" :5},
// Digital
0: {"id" : "50"},
1: {"id" : "51"},
2: {"id" : "32"},
4: {"id" : "28"},
7: {"id" : "27"},
8: {"id" : "26"},
12: {"id" : "38"},
13: {"id" : "39"},
// PWM ready
3: {"id" : "18", "index": 3, "period": 0},
5: {"id" : "17", "index": 5, "period": 0},
6: {"id" : "24", "index": 6, "period": 0},
9: {"id" : "19", "index": 1, "period": 0},
10: {"id" : "16", "index": 7, "period": 0},
11: {"id" : "25", "index": 4, "period": 0}
};
/**
* Pwm states helper
* @property pwmState
* @property Object
*/
this.pwmState =
{
'ON': "1",
'OFF': "0"
};
/**
* GPIO system path
* @property gpioPath
* @type Object
*/
this.gpioPath = "/sys/class/gpio";
this.pwmPath = "/sys/class/pwm/pwmchip0"
};
GalileoGpio.prototype.openGPIO = function()
{
//TODO: direct use of GPIO number, it can be useful
};
/**
* Exports a GPIO to make it reachable on the system.
*
* @method openPin
* @param {Number} pin_number Number of the pin on the Galileo board.
*/
GalileoGpio.prototype.openPin = function(pin_number)
{
//TODO: use rough GPIO method once available
if (!fs.existsSync(this.gpioPath + "/gpio" + this.pin[pin_number].id))
{
fs.writeFileSync(this.gpioPath + "/export", this.pin[pin_number].id);
}
};
/**
* Set the direction of the pin.
*
* @method setPinDirection
* @param {Number} pin_number Number of the pin on the Galileo board.
* @param {String} direction Either `in` or `out`
*/
GalileoGpio.prototype.setPinDirection = function(pin_number, direction)
{
fs.writeFileSync(this.gpioPath + "/gpio" + this.pin[pin_number].id + "/direction", direction);
};
/**
* Set the configuration type of the pin
*
* @method setPinPortDrive
* @param {Number} pin_number Number of the pin on the Galileo board.
* @param {String} config_type Either `pullup`, `pulldown`, `strong`, `hiz`
*/
GalileoGpio.prototype.setPinPortDrive = function(pin_number, config_type)
{
fs.writeFileSync(this.gpioPath + "/gpio" + this.pin[pin_number].id + "/drive", config_type);
};
/**
* Write data on the pin
*
* @method writePin
* @param {Number} pin_number Number of the pin on the Galileo board.
* @param {String} data What does the pin says ?
*/
GalileoGpio.prototype.writePin = function(pin_number, data)
{
fs.writeFileSync(this.gpioPath + "/gpio" + this.pin[pin_number].id + "/value", data);
};
/**
* Read data from an analog pin
*
* @method readAnalogPin
* @param {Number} pin_number Number of the analog pin on the Galileo board.
*/
//TODO: pass 'raw' in the parameters
GalileoGpio.prototype.readAnalogPin = function(pin_number)
{
var data = fs.readFileSync("/sys/bus/iio/devices/iio:device0/in_voltage" + this.pin[pin_number].index + "_raw");
//console.log('Reading on pin ' + this.pin[pin_number].index)
return data;
};
// TODO: test readAnalogScalePin
GalileoGpio.prototype.readAnalogScalePin = function(pin_number)
{
var data = fs.readFileSync("/sys/bus/iio/devices/iio:device0/in_voltage" /*+ pin_number*/ + "_scale");
return data;
};
/**
* Read data from a digital pin
*
* @method readDigitalPin
* @param {Number} pin_number Number of the digital pin on the Galileo board.
*/
GalileoGpio.prototype.readDigitalPin = function(pin_number)
{
var data = fs.readFileSync(this.gpioPath + "/gpio" + this.pin[pin_number].id + "/value");
return data;
};
/**
* Close system access to the pin
*
* @method closePin
* @param {Number} pin_number Number of the pin on the Galileo board.
*/
GalileoGpio.prototype.closePin = function(pin_number)
{
if (fs.existsSync(this.gpioPath + "/gpio" + this.pin[pin_number].id))
{
fs.writeFileSync(this.gpioPath + "/unexport", this.pin[pin_number].id);
}
};
/**
* Exports a pwm GPIO to make it reachable on the system.
*
* @method openPwmPin
* @param {Number} pin_number Number of the pwm pin on the Galileo board.
*/
GalileoGpio.prototype.openPwmPin = function(pin_number)
{
fs.writeFileSync(this.pwmPath + "/export", this.pin[pin_number].index);
};
/**
* Activate or deactivate a pwm pin
*
* @method setPwmPinActivation
* @param {Number} pin_number Number of the pwm pin on the Galileo board.
* @param {String} activation Defines if it emits or not ("0" or "1")
*/
GalileoGpio.prototype.setPwmPinActivation = function(pin_number, activation)
{
fs.writeFileSync(this.pwmPath + "/pwm" + this.pin[pin_number].index + "/enable", activation);
};
/**
* Sets the pwm period in nanoseconds (i.e. how long the cycle lasts)
*
* @method setPwmPinPeriod
* @param {Number} pin_number Number of the pwm pin on the Galileo board.
* @param {Number} period Pwm period in nanoseconds.
*/
GalileoGpio.prototype.setPwmPinPeriod = function(pin_number, period)
{
fs.writeFileSync(this.pwmPath + "/pwm" + this.pin[pin_number].index + "/period", period.toString());
this.pin[pin_number].period = period;
};
/**
* Sets the pwm duty cycle (i.e. how long it is activated during a cycle)
*
* @method setPwmPinCycle
* @param {Number} pin_number Number of the pwm pin on the Galileo board.
* @param {Number} cycle Pwm cycle in percentage.
*/
GalileoGpio.prototype.setPwmPinCycle = function(pin_number, cycle)
{
var cycle_value = cycle / 100 * this.pin[pin_number].period;
console.log(cycle_value);
fs.writeFileSync(this.pwmPath + "/pwm" + this.pin[pin_number].index + "/duty_cycle", cycle_value.toString());
};
/**
* Unexport a pwm GPIO.
*
* @method closePwmPin
* @param {Number} pin_number Number of the pwm pin on the Galileo board.
*/
GalileoGpio.prototype.closePwmPin = function(pin_number)
{
fs.writeFileSync(this.pwmPath + "/unexport", this.pin[pin_number].index);
this.pin[pin_number].period = 0;
};
module.exports = new GalileoGpio;