-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
697 lines (650 loc) · 23.4 KB
/
index.js
File metadata and controls
697 lines (650 loc) · 23.4 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Copyright (C) 2011 - Texas Instruments, Jason Kridner
//
//
var fs = require('fs');
var child_process = require('child_process');
var winston = require('winston');
var eeprom = require('./eeprom');
var parse = require('./parse');
var bone = require('./bone');
var functions = require('./functions');
winston.setLevels(winston.config.syslog.levels);
//winston.add(winston.transports.File, {
// filename: '/var/lib/cloud9/bonescript.log',
// level: 'warn'
//});
var f = {};
var capemgr;
var file_exists = fs.exists;
var file_existsSync = fs.existsSync;
if(typeof file_exists == 'undefined') {
var path = require('path');
file_exists = path.exists;
file_existsSync = path.existsSync;
}
function file_find(path, prefix, attempts) {
if(typeof attempts == 'undefined') attempts = 1;
for(var i = 0; i < attempts; i++) {
try {
var files = fs.readdirSync(path);
for(var j in files) {
if(files[j].indexOf(prefix) == 0) {
return(path + '/' + files[j]);
}
}
} catch(ex) {
}
}
}
function is_capemgr() {
if(typeof capemgr == 'undefined') {
capemgr = file_find('/sys/devices', 'bone_capemgr.');
if(typeof capemgr == 'undefined') capemgr = false;
}
return(capemgr);
}
// Note, this just makes sure there was an attempt to load the
// devicetree fragment, not if it was successful
function load_dt(name) {
if(!is_capemgr()) return(false);
var slots = fs.readFileSync(capemgr + '/slots', 'ascii');
if(slots.indexOf(name) < 0) {
fs.writeFileSync(capemgr + '/slots', name);
}
for(var i = 0; i < 10000; i++) {
slots = fs.readFileSync(capemgr + '/slots', 'ascii');
if(slots.indexOf(name) >= 0) return(true);
}
return(false);
}
function myeval(x) {
winston.debug('eval("' + x + '");');
var y;
try {
y = eval(x);
} catch(ex) {
y = undefined;
winston.error('myeval error: ' + ex);
throw('myeval error: ' + ex);
}
winston.debug('result = ' + y);
return(y);
}
function myrequire(packageName, onfail) {
var y = {};
try {
y = require(packageName);
y.exists = true;
} catch(ex) {
y.exists = false;
winston.debug("Optional package '" + packageName + "' not loaded");
if(onfail) onfail();
}
return(y);
}
function getpin(pin) {
if(typeof pin == 'object') return(pin);
else if(typeof pin == 'string') return(bone.pins[pin]);
else if(typeof pin == 'number') return(bone.pinIndex[pin]);
else throw("Invalid pin: " + pin);
}
var gOUTPUT = "out";
var gINPUT = "in";
var gINPUT_PULLUP = "in_pullup";
var gHIGH = 1;
var gLOW = 0;
var gLSBFIRST = 1; // used in: shiftOut(dataPin, clockPin, bitOrder, val)
var gMSBFIRST = 0;
var gCHANGE = "both";
var gRISING = "rising";
var gFALLING = "falling";
// Keep track of allocated resources
var gpio = [];
var pwm = {};
// returned object has:
// mux: index of mux mode
// options: array of mode names
// slew: 'fast' or 'slow'
// rx: 'enabled' or 'disabled'
// pullup: 'diabled', 'pullup' or 'pulldown'
// pin: key string for pin
// name: pin name
f.getPinMode = function(pin, callback) {
winston.debug('getPinMode(' + pin + ');');
pin = getpin(pin);
var mode = {'pin': pin.key, 'name': pin.name};
if(pin.options) mode.options = pin.options;
var muxFile = '/sys/kernel/debug/omap_mux/' + pin.mux;
var pinctrlFile = '/sys/kernel/debug/pinctrl/44e10800.pinmux/pins';
var muxRegOffset = parseInt(pin.muxRegOffset, 16);
var readOmapMux = function(err, data) {
if(err) winston.debug('readOmapMux error: ' + err);
mode = parse.modeFromOmapMux(data, mode);
callback(mode);
};
var readPinctrl = function(err, data) {
if(err) winston.debug('readPinctrl error: ' + err);
mode = parse.modeFromPinctrl(data, muxRegOffset, 0x44e10800, mode);
callback(mode);
};
var tryPinctrl = function(exists) {
if(exists) {
fs.readFile(pinctrlFile, 'utf8', readPinctrl);
} else {
winston.debug('getPinMode(' + pin.key + '): no valid mux data');
callback(mode);
}
};
var tryOmapMux = function(exists) {
if(exists) {
fs.readFile(muxFile, 'utf8', readOmapMux);
} else {
file_exists(pinctrlFile, tryPinctrl);
}
};
if(callback) {
file_exists(muxFile, tryOmapMux);
} else {
try {
var data = fs.readFileSync(muxFile, 'utf8');
mode = parse.modeFromOmapMux(data, mode);
} catch(ex) {
try {
var data2 = fs.readFileSync(pinctrlFile, 'utf8');
mode = parse.modeFromPinctrl(data2, muxRegOffset, 0x44e10800, mode);
} catch(ex2) {
winston.debug('getPinMode(' + pin.key + '): ' + ex2);
}
}
return(mode);
}
};
f.getPinMode.args = ['pin', 'callback'];
f.pinMode = function(pin, direction, mux, pullup, slew, callback) {
winston.debug('pinMode(' + [pin, direction, mux, pullup, slew] + ');');
// if(is_capemgr()) {
// var x = {
// value: false,
// err: "pinMode not functional or necessary on 'capemgr' enabled kernel"
// };
// if(callback) callback(x);
// return(false);
// }
pin = getpin(pin);
if(direction == gINPUT_PULLUP) pullup = 'pullup';
pullup = pullup || ((direction == gINPUT) ? 'pulldown' : 'disabled');
slew = slew || 'fast';
mux = mux || 7; // default to GPIO mode
if(!pin.mux) {
winston.debug('Invalid pin object for pinMode: ' + pin);
throw('Invalid pin object for pinMode: ' + pin);
}
var muxFile = '/sys/kernel/debug/omap_mux/' + pin.mux;
var gpioFile = '/sys/class/gpio/gpio' + pin.gpio + '/value';
// Handle case where pin is allocated as a gpio-led
if(pin.led) {
if((direction != gOUTPUT) || (mux != 7)) {
var err = 'pinMode only supports GPIO output for LEDs: ' + pin.key;
winston.info(err);
if(callback) callback({value:false, err:err});
return(false);
}
gpioFile = '/sys/class/leds/beaglebone::' + pin.led + '/brightness';
var pathA = "/sys/class/leds/beaglebone:";
var pathB = pathA;
pathA += ":" + pin.led + "/trigger";
pathB += "green:" + pin.led + "/trigger";
if(file_existsSync(pathA)) {
fs.writeFileSync(pathA, "gpio");
} else {
if(file_existsSync(pathB)) {
fs.writeFileSync(pathB, "gpio");
} else {
winston.error("Unable to find LED: " + pin.led);
}
}
gpio[n] = {'path': gpioFile};
if(callback) callback({value:true});
return(true);
}
// Figure out the desired value
var pinData = 0;
if(slew == 'slow') pinData |= 0x40;
if(direction != gOUTPUT) pinData |= 0x20;
switch(pullup) {
case 'disabled':
pinData |= 0x08;
break;
case 'pullup':
pinData |= 0x10;
break;
default:
break;
}
pinData |= (mux & 0x07);
if(!is_capemgr()) {
try {
var fd = fs.openSync(muxFile, 'w');
fs.writeSync(fd, pinData.toString(16), null);
} catch(ex) {
winston.debug('Unable to configure mux for pin ' + pin + ': ' + ex);
// Don't exit yet --- need to try using pinmux-helper with devicetree
// ... and it might work if the pin is already muxed to 7
winston.debug('mode = ' + JSON.stringify(f.getPinMode(pin)));
var currentMode = f.getPinMode(pin);
if(currentMode.mux != mux) {
var err2 = 'Unable to configure mux for pin ' + pin.key + ': ' + ex;
winston.info(err2);
gpio[n] = {};
if(callback) callback({value:false, err:err2});
return(false);
}
}
}
// Enable GPIO, if not already done
var n = pin.gpio;
if(mux == 7) {
if(!gpio[n] || !gpio[n].path) {
gpio[n] = {'path': gpioFile};
// Export the GPIO controls
var exists = file_existsSync(gpioFile);
if(exists) {
winston.debug("gpio: " + n + " already exported.");
fs.writeFileSync("/sys/class/gpio/gpio" + n + "/direction",
direction, null);
} else {
try {
winston.debug("exporting gpio: " + n);
fs.writeFileSync("/sys/class/gpio/export", "" + n, null);
winston.debug("setting gpio " + n +
" direction to " + direction);
fs.writeFileSync("/sys/class/gpio/gpio" + n + "/direction",
direction, null);
} catch(ex2) {
var pmerr = 'Unable to export gpio-' + n + ': ' + ex2;
winston.debug(pmerr);
var gpioUsers =
fs.readFileSync('/sys/kernel/debug/gpio', 'utf-8');
gpioUsers = gpioUsers.split('\n');
for(var x in gpioUsers) {
var y = gpioUsers[x].match(/gpio-(\d+)\s+\((\S+)\s*\)/);
if(y && y[1] == n) {
var pmerr2 = 'gpio-' + n + ' consumed by ' + y[2];
pmerr += '\n' + pmerr2;
winston.error(pmerr);
}
}
gpio[n] = {};
if(callback) callback({value:false, err:pmerr});
return(false);
}
}
}
} else {
gpio[n] = {};
}
if(callback) callback({value:true});
return(true);
};
f.pinMode.args = ['pin', 'direction', 'mux', 'pullup', 'slew', 'callback'];
f.digitalWrite = function(pin, value, callback) {
winston.debug('digitalWrite(' + [pin, value] + ');');
pin = getpin(pin);
value = parseInt(value, 2) ? 1 : 0;
var gpioFile = '/sys/class/gpio/gpio' + pin.gpio + '/value';
if(pin.led) {
var pathA = "/sys/class/leds/beaglebone:";
var pathB = pathA;
pathA += ":" + pin.led + "/brightness";
pathB += "green:" + pin.led + "/brightness";
if(file_existsSync(pathA)) {
gpioFile = pathA;
} else {
if(file_existsSync(pathB)) {
gpioFile = pathB;
} else {
winston.error("Unable to find LED: " + pin.led);
}
}
}
winston.debug("gpioFile = " + gpioFile);
if(callback) {
fs.writeFile(gpioFile, '' + value, null, callback);
} else {
try {
fs.writeFileSync(gpioFile, '' + value, null);
} catch(ex) {
winston.error("Unable to write to " + gpioFile);
}
}
return(true);
};
f.digitalWrite.args = ['pin', 'value', 'callback'];
f.digitalRead = function(pin, callback) {
winston.debug('digitalRead(' + [pin, value] + ');');
pin = getpin(pin);
var gpioFile = '/sys/class/gpio/gpio' + pin.gpio + '/value';
if(callback) {
var readFile = function(err, data) {
if(err) winston.error('digitalRead error: ' + err);
var value = parseInt(data, 2);
callback({'value':value});
};
fs.readFile(gpioFile, readFile);
return(true);
}
var value = parseInt(fs.readFileSync(gpioFile), 2);
return(value);
};
f.digitalRead.args = ['pin', 'callback'];
f.analogRead = function(pin, callback) {
winston.debug('analogRead(' + [pin] + ');');
pin = getpin(pin);
if(typeof this.ainPrefix == 'undefined') {
if(load_dt('cape-bone-iio')) {
var helper = file_find('/sys/devices/ocp.2', 'helper.', 10000);
this.ainPrefix = helper + '/AIN';
this.indexOffset = 0;
this.scale = 1800;
} else {
this.ainPrefix = '/sys/bus/platform/devices/tsc/ain';
this.indexOffset = 1;
this.scale = 4096;
}
}
var ainPrefix = this.ainPrefix;
var indexOffset = this.indexOffset;
var scale = this.scale;
var ainFile = ainPrefix + (pin.ain + indexOffset).toString();
if(callback) {
var readFile = function(err, data) {
if(err) {
delete this.ainPrefix;
winston.error('analogRead error: ' + err);
}
var value = parseInt(data, 10);
if(f.getPlatform().name == 'Beaglebone')
value = value / scale;
callback({'value': value});
};
fs.readFile(ainFile, readFile);
return(true);
}
var data = parseInt(fs.readFileSync(ainFile), 10);
if(isNaN(data)) {
delete this.ainPrefix;
throw('analogRead(' + pin.key + ') returned ' + data);
}
if(f.getPlatform().name == 'Beaglebone')
data = data / scale;
if(isNaN(data)) {
delete this.ainPrefix;
throw('analogRead(' + pin.key + ') scaled to ' + data);
}
return(data);
};
f.analogRead.args = ['pin', 'callback'];
f.shiftOut = function(dataPin, clockPin, bitOrder, val, callback) {
winston.debug('shiftOut(' + [dataPin, clockPin, bitOrder, val] + ');');
dataPin = getpin(dataPin);
clockPin = getpin(clockPin);
var i;
var bit;
for (i = 0; i < 8; i++)
{
if (bitOrder == gLSBFIRST)
{
bit = val & (1 << i);
} else {
bit = val & (1 << (7 - i));
}
f.digitalWrite(dataPin, bit);
f.digitalWrite(clockPin, gHIGH);
f.digitalWrite(clockPin, gLOW);
if(callback) callback();
}
};
f.shiftOut.args = ['dataPin', 'clockPin', 'bitOrder', 'val', 'callback'];
f.attachInterrupt = function(pin, handler, mode, callback) {
winston.debug('attachInterrupt(' + [pin, handler, mode] + ');');
pin = getpin(pin);
if(!gpio[pin.gpio]) {
if(callback) callback({'pin':pin, 'attached':false, 'configured':false});
return(false);
}
if(gpio[pin.gpio].intProc) {
if(callback) callback({'pin':pin, 'attached':false, 'configured':true});
return(false);
}
var gpioFile = '/sys/class/gpio/gpio' + pin.gpio + '/value';
fs.writeFileSync('/sys/class/gpio/gpio' + pin.gpio + '/edge', mode);
handler = (typeof handler === "string") ? myeval('(' + handler + ')') : handler;
var intHandler = function(m) {
if(typeof handler =='function') m.output = handler({'pin':pin, 'value':m.value});
else m.output = {handler:handler};
if(m.output.handler && (typeof callback == 'function')) callback(m);
};
var intProc;
if(child_process.fork) {
intProc = child_process.fork(__dirname + '/gpioint.js');
} else {
var fork = require('fork');
intProc = fork.fork(__dirname + '/gpioint.js');
}
intProc.on('message', intHandler);
intProc.on('exit', function(code, signal) {
if(callback) callback({
'pin':pin,
'code':code,
'signal':signal,
'died':true
});
});
intProc.send({'pin':pin, 'mode':mode, 'file':gpioFile});
gpio[pin.gpio].intProc = intProc;
process.on('SIGTERM', function() {
intProc.kill();
if(callback) callback({'pin':pin, 'died':true});
});
if(callback) callback({'pin':pin, 'attached':true});
return(true);
};
f.attachInterrupt.args = ['pin', 'hander', 'mode', 'callback'];
f.detachInterrupt = function(pin, callback) {
winston.debug('detachInterrupt(' + [pin] + ');');
pin = getpin(pin);
if(!gpio[pin.gpio] || !gpio[pin.gpio].intProc) {
if(callback) callback({'pin':pin, 'detached':false});
return(false);
}
gpio[pin.gpio].intProc.kill();
delete gpio[pin.gpio].intProc;
if(callback) callback({'pin':pin, 'detached':true});
return(true);
};
f.detachInterrupt.args = ['pin', 'callback'];
// See http://processors.wiki.ti.com/index.php/AM335x_PWM_Driver's_Guide
// That guide isn't useful for the new pwm_test interface
f.analogWrite = function(pin, value, freq, callback) {
winston.debug('analogWrite(' + [pin,value,freq] + ');');
pin = getpin(pin);
freq = freq || 2000.0;
// Make sure the pin has a pwm associated
if(typeof pin.pwm == 'undefined') {
throw(pin.key + ' does not support analogWrite()');
}
// Make sure it no one else who has the pwm
if((typeof pwm[pin.pwm.name] != 'undefined') && (pin.key != pwm[pin.pwm.name].key)) {
throw(pin.key + ' requires pwm ' + pin.pwm.name +
' but it is already in use by ' +
pwm[pin.pwm].key
);
}
// Make sure pwm[].key and pwm[].(pwm_test_path|old_pwm_path) are valid
if(typeof pwm[pin.pwm.name] == 'undefined') {
pwm[pin.pwm.name] = {};
pwm[pin.pwm.name].key = pin.key;
var fragment = 'bone_pwm_' + pin.key;
if(load_dt('am33xx_pwm') && load_dt(fragment)) {
var ocp = file_find('/sys/devices', 'ocp.');
var pwm_test = file_find(ocp, 'pwm_test_' + pin.key + '.', 10000);
file_find(pwm_test, 'period', 10000);
pwm[pin.pwm.name].pwm_test_path = pwm_test;
pwm[pin.pwm.name].freq = 0;
} else {
pwm[pin.pwm.name].old_pwm_path = '/sys/class/pwm/' + pin.pwm.path;
f.pinMode(pin, gOUTPUT, pin.pwm.muxmode, 'disabled', 'fast');
// Clear up any unmanaged usage
fs.writeFileSync(path+'/request', '0');
// Allocate and configure the PWM
fs.writeFileSync(path+'/request', '1');
fs.writeFileSync(path+'/period_freq', Math.round(freq));
fs.writeFileSync(path+'/polarity', '0');
fs.writeFileSync(path+'/run', '1');
pwm[pin.pwm.name].freq = freq;
}
pwm[pin.pwm.name].key = pin.key;
}
// Perform update only
if(typeof pwm[pin.pwm.name].pwm_test_path === 'string') {
if(pwm[pin.pwm.name].freq != freq) {
var period = Math.round( 1.0e9 / freq ); // period in ns
fs.writeFileSync(pwm[pin.pwm.name].pwm_test_path+'/period', period);
}
var duty = Math.round( period * value );
fs.writeFileSync(pwm[pin.pwm.name].pwm_test_path+'/duty', duty);
} else {
if(pwm[pin.pwm.name].freq != freq) {
fs.writeFileSync(pwm[pin.pwm.name].old_pwm_path+'/run', '0');
fs.writeFileSync(pwm[pin.pwm.name].old_pwm_path+'/duty_percent', '0');
fs.writeFileSync(pwm[pin.pwm.name].old_pwm_path+'/period_freq', Math.round(freq));
fs.writeFileSync(pwm[pin.pwm.name].old_pwm_path+'/run', '1');
pwm[pin.pwm.name].freq = freq;
}
fs.writeFileSync(pwm[pin.pwm.name].old_pwm_path+'/duty_percent', Math.round(value*100));
}
// All done
if(callback) callback({value:true});
return(true);
};
f.analogWrite.args = ['pin', 'value', 'freq', 'callback'];
f.getEeproms = function(callback) {
var eeproms = {};
if(!is_capemgr()) {
var EepromFiles = {
'/sys/bus/i2c/drivers/at24/1-0050/eeprom': { type: 'bone' },
'/sys/bus/i2c/drivers/at24/3-0054/eeprom': { type: 'cape' },
'/sys/bus/i2c/drivers/at24/3-0055/eeprom': { type: 'cape' },
'/sys/bus/i2c/drivers/at24/3-0056/eeprom': { type: 'cape' },
'/sys/bus/i2c/drivers/at24/3-0057/eeprom': { type: 'cape' }
};
eeproms = eeprom.readEeproms(EepromFiles);
if(eeproms == {}) {
winston.debug('No valid EEPROM contents found');
}
} else {
var boardName = fs.readFileSync(capemgr + '/baseboard/board-name', 'ascii');
var version = fs.readFileSync(capemgr + '/baseboard/revision', 'ascii');
var serialNumber = fs.readFileSync(capemgr + '/baseboard/serial-number', 'ascii');
eeproms['/sys/bus/i2c/drivers/at24/1-0050/eeprom'] = {};
eeproms['/sys/bus/i2c/drivers/at24/1-0050/eeprom'].boardName = boardName;
eeproms['/sys/bus/i2c/drivers/at24/1-0050/eeprom'].version = version;
eeproms['/sys/bus/i2c/drivers/at24/1-0050/eeprom'].serialNumber = serialNumber;
}
if(callback) {
callback(eeproms);
}
return(eeproms);
};
f.getEeproms.args = ['callback'];
f.readTextFile = function(filename, callback) {
if(typeof callback == 'function') {
var cb = function(err, data) {
callback({'err':err, 'data':data});
};
fs.readFile(filename, 'ascii', cb);
} else {
return fs.readFileSync(filename, 'ascii');
}
};
f.readTextFile.args = ['filename', 'callback'];
f.writeTextFile = function(filename, data, callback) {
if(typeof callback == 'function') {
var cb = function(err) {
callback({'err':err});
};
fs.writeFile(filename, data, 'ascii', cb);
} else {
return fs.writeFileSync(filename, data, 'ascii');
}
};
f.writeTextFile.args = ['filename', 'data', 'callback'];
f.getPlatform = function(callback) {
var platform = {'platform': bone, 'name': "BeagleBone", 'bonescript': "0.2"};
if(file_existsSync(is_capemgr() + '/baseboard/board-name')) {
platform.name = fs.readFileSync(capemgr + '/baseboard/board-name', 'ascii').trim();
if(platform.name == 'A335BONE') platform.name = 'BeagleBone';
if(platform.name == 'A335BNLT') platform.name = 'BeagleBone Black';
platform.version = fs.readFileSync(capemgr + '/baseboard/revision', 'ascii').trim();
if(!platform.version.match(/^[\040-\176]*$/)) delete platform.version;
platform.serialNumber = fs.readFileSync(capemgr + '/baseboard/serial-number', 'ascii').trim();
if(!platform.serialNumber.match(/^[\040-\176]*$/)) delete platform.serialNumber;
}
if(callback) callback(platform);
return(platform);
};
f.getPlatform.args = ['callback'];
f.echo = function(data, callback) {
winston.info(data);
callback({'data': data});
return(data);
};
f.echo.args = ['data', 'callback'];
f.setDate = function(date, callback) {
child_process.exec('date -s "' + date + '"', dateResponse);
function dateResponse(error, stdout, stderr) {
if(typeof callback != 'function') return;
if(error) callback('error: ' + error);
if(stdout) callback('stdout: ' + error);
if(stderr) callback('stderr: ' + error);
}
};
f.setDate.args = ['date', 'callback'];
// Exported variables
exports.OUTPUT = gOUTPUT;
exports.INPUT = gINPUT;
exports.INPUT_PULLUP = gINPUT_PULLUP;
exports.HIGH = gHIGH;
exports.LOW = gLOW;
exports.LSBFIRST = gLSBFIRST;
exports.MSBFIRST = gMSBFIRST;
exports.CHANGE = gCHANGE;
exports.RISING = gRISING;
exports.FALLING = gFALLING;
exports.bone = bone; // this likely needs to be platform and be detected
for(var x in f) {
exports[x] = f[x];
}
for(var x in functions) {
exports[x] = functions[x];
}
// Global variable assignments
// This section is broken out because it will eventually be deprecated
var alreadyRan = false;
function setGlobals() {
for(var x in exports) {
global[x] = exports[x];
}
global.run = run;
process.nextTick(run);
function run() {
if(alreadyRan) return(false);
alreadyRan = true;
// 'setup' and 'loop' are globals that may or may not be defined
if(typeof global.setup == 'function') global.setup();
while(1) {
if(typeof global.loop == 'function') global.loop();
}
}
}
exports.setGlobals = setGlobals;