Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

parallel.js: added example of parallel loops

* Cleaned up termination
* Calculated gpioFile for digitalWrite, even on unallocated pins
  (This needs to be revisited.  The states should be messaged between
   the various running processes to manage allocation.)
* Removed unused module 'events' from bonescript/index.js
  • Loading branch information...
commit 6034b493253c5ac9ac59215991c424ba68422ca0 1 parent 7fc6d08
Jason Kridner authored
7 node_modules/bonescript/gpioint.js
View
@@ -16,7 +16,12 @@ if(process.argv.length > 2) {
var gpioPoll = new misc.Pollpri(m.file);
var gpioHandler = function(value) {
//console.log('Got interrupt event');
- process.send({'value': value});
+ try {
+ process.send({'value': value});
+ } catch(ex) {
+ console.error('Unable to send message to parent process: ' + ex);
+ process.exit(1);
+ }
};
gpioPoll.on('edge', gpioHandler);
};
44 node_modules/bonescript/index.js
View
@@ -6,7 +6,6 @@ var child_process = require('child_process');
var http = require('http');
var url = require('url');
var path = require('path');
-var events = require('events');
var eeprom = require('./eeprom');
bone = require('./bone').bone;
@@ -236,10 +235,14 @@ pinMode = exports.pinMode = function(pin, direction, mux, pullup, slew, callback
};
digitalWrite = exports.digitalWrite = function(pin, value, callback) {
+ var gpioFile = '/sys/class/gpio/gpio' + pin.gpio + '/value';
+ if(pin.led) {
+ gpioFile = '/sys/class/leds/beaglebone::' + pin.led + '/brightness';
+ }
if(callback) {
- fs.writeFile(gpio[pin.gpio].path, '' + value, null, callback);
+ fs.writeFile(gpioFile, '' + value, null, callback);
} else {
- fs.writeFileSync(gpio[pin.gpio].path, '' + value, null);
+ fs.writeFileSync(gpioFile, '' + value, null);
}
return(true);
};
@@ -311,6 +314,9 @@ attachInterrupt = exports.attachInterrupt = function(pin, mode, callback) {
intProc.on('message', intHandler);
intProc.send({'pin': pin, 'mode': mode, 'file': gpioFile});
gpio[pin.gpio].intProc = intProc;
+ process.on('SIGTERM', function() {
+ intProc.kill();
+ });
};
// See http://processors.wiki.ti.com/index.php/AM335x_PWM_Driver's_Guide
@@ -334,7 +340,7 @@ analogWrite = exports.analogWrite = function(pin, value, freq, callback) {
}
} else {
pwm[pin.pwm.path] = {};
- pwm[pin.pwm.path].key = pin.key;
+ pwm[pin.pwm.path].key = '' + pin.key;
pwm[pin.pwm.path].freq = freq;
pinMode(pin, OUTPUT, pin.pwm.muxmode, 'disabled', 'fast');
@@ -415,6 +421,8 @@ if(fibers.exists) {
}, 0);
yield(null);
}
+ } else {
+ throw("Not yet supported");
}
}).run();
};
@@ -422,14 +430,40 @@ if(fibers.exists) {
run = exports.run = function() {
if(cluster.isMaster) {
setup();
- cluster.fork();
+ var workers = [];
+ if(typeof loop === "object") {
+ for(var n = 0; n < loop.length; n++) {
+ console.log('Adding loop ' + (n+1));
+ workers[n] = cluster.fork();
+ }
+ } else {
+ workers[0] = cluster.fork();
+ }
+ cluster.on('death', function(worker) {
+ console.log('Loop with PID ' + worker.pid + ' died');
+ });
+ process.on('SIGTERM', function() {
+ for(var n = 0; n < workers.length; n++) {
+ console.log('Killing loop ' + (n+1));
+ workers[n].kill();
+ }
+ process.exit(0);
+ });
} else {
if(typeof loop === "function") {
+ console.log('Starting loop');
var repeat = function repeat() {
loop();
process.nextTick(repeat);
};
repeat();
+ } else if(typeof loop === "object") {
+ console.log('Starting loop ' + process.env.NODE_WORKER_ID);
+ var repeat = function repeat() {
+ loop[process.env.NODE_WORKER_ID-1]();
+ process.nextTick(repeat);
+ };
+ repeat();
}
}
};
41 parallel.js
View
@@ -0,0 +1,41 @@
+var bb = require('bonescript');
+
+var ledPin = bone.USR3;
+var outputPin = bone.P8_3;
+var eventPin = bone.P8_5;
+var ainPin = bone.P9_39;
+var pwmPin = bone.P9_14;
+
+setup = function() {
+ console.log('Please connect ' + outputPin.key + ' to ' + eventPin.key +
+ ' with a 1kohm resistor');
+ pinMode(ledPin, OUTPUT);
+ pinMode(outputPin, OUTPUT);
+ pinMode(pwmPin, OUTPUT);
+ pinMode(eventPin, INPUT);
+ digitalWrite(outputPin, LOW);
+ attachInterrupt(eventPin, CHANGE, function(pin, value) {
+ console.log(pin.key + ' changed to ' +
+ ((value == HIGH) ? 'HIGH' : 'LOW'));
+ });
+};
+
+loop = [
+ function() {
+ //console.log("Setting " + outputPin.key + " HIGH");
+ digitalWrite(ledPin, HIGH);
+ digitalWrite(outputPin, HIGH);
+ delay(500);
+ digitalWrite(ledPin, LOW);
+ digitalWrite(outputPin, LOW);
+ delay(500);
+ },
+ function() {
+ //console.log("Reading " + ainPin.key);
+ var value = analogRead(ainPin);
+ analogWrite(pwmPin, value);
+ delay(100);
+ }
+];
+
+bb.run();
Please sign in to comment.
Something went wrong with that request. Please try again.