wireServer = require('child_process').fork('wireServer.js',
[global.config.i2cDevice, global.config.i2cAddress, global.config.i2cCommand]);
wireServerPid = wireServer.pid;
utilities.trace(false, "test - wireServer started as PID " + wireServerPid);
eventEmitter.on('startObserve', function(message){
wireServer.send({"message":"init",
"i2cDevice":global.config.i2cDevice,
"i2cAddress":global.config.i2cAddress,
"i2cCommand":global.config.i2cCommand});
// Each message will be a voltage reading that we then pass to the below routine
wireServer.on('message', function (message) {
observe(message.voltageIn);
});
});
execSync('kill ' + wireServerPid);
utilities.trace(false, "test - Current wireServer running as PID " + wireServerPid + " killed")
eventEmitter.emit('startObserve', '');
The background process that the above launches, and then communicates with, is shown below:
// Load the library we need to get voltage measurements using i2c
var i2c = require('i2c');
var i2cDevice = "/dev/i2c-" + process.argv[2];
var i2cAddress = process.argv[3];
var i2cCommand = Number(process.argv[4]);
// Convert the i2c address from character to hex
i2cAddress = parseInt("0x" + i2cAddress);
var wire = new i2c(i2cAddress, {device: i2cDevice, debug: false});
var seq = 0;
wire.on('data', function(data) {
observe(data.data);
});
// Look for an init message from test to start our observations
process.on('message', function (message) {
wire.stream(i2cCommand, 4, 3);
});
// Take a voltage reading and send it on to test
function observe(buffer) {
var voltageIn = 0;
voltageIn = (buffer[0] << 8) + buffer[1];
// Did we get a negative value? If so make it a zero.
if (voltageIn > 2048) {
voltageIn = 0;
}
else {
voltageIn = voltageIn / 2048;
}
if (seq == 5000 || seq === 0) {
console.log(datetimeStamp() + " - wireServer - active");
seq = 1;
}
process.send({seq:seq++, voltageIn: voltageIn });
}
No comments:
Post a Comment