1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Add better error handling and update Compass

This commit is contained in:
Nick O'Leary 2016-03-06 21:54:30 +00:00
parent 53c9c834b9
commit d5446ee117
3 changed files with 42 additions and 27 deletions

View File

@ -1 +1 @@
[{"id":"8a52215e.75ade","type":"rpi-sensehat out","z":"ec37e354.13c82","name":"","x":460,"y":39,"wires":[]},{"id":"f0e76645.0f1898","type":"rpi-sensehat in","z":"ec37e354.13c82","name":"","motion":true,"env":false,"stick":false,"x":90,"y":39,"wires":[["a96a3875.5695c8"]]},{"id":"a96a3875.5695c8","type":"function","z":"ec37e354.13c82","name":"Compass","func":"// A port of the Compass example provided by\n// the Sense HAT python library\n// https://github.com/RPi-Distro/python-sense-hat/tree/master/examples\n\nvar led_loop = [4, 5, 6, 7, 15, 23, 31, 39, 47, 55, 63, 62, 61, 60, 59, 58, 57, 56, 48, 40, 32, 24, 16, 8, 0, 1, 2, 3];\nvar led_degree_ratio = led_loop.length / 360.0;\nvar dir_inverted = 360 - msg.payload.compass;\nled_index = Math.floor(led_degree_ratio * dir_inverted);\noffset = led_loop[led_index]\nvar y = Math.floor(offset / 8);\nvar x = offset % 8;\nvar prev_x = context.get('prev_x');\nvar prev_y = context.get('prev_y');\n\nmsg.payload = '';\nif (x != prev_x || y != prev_y) {\n msg.payload = prev_x+\",\"+prev_y+\",off,\"\n}\nmsg.payload += x+\",\"+y+\",blue\";\n\ncontext.set('prev_x',x);\ncontext.set('prev_y',y);\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":270,"y":39,"wires":[["8a52215e.75ade"]]}]
[{"id":"b3b88507.4c4778","type":"rpi-sensehat out","z":"ec37e354.13c82","name":"","x":469,"y":30,"wires":[]},{"id":"e00b82a.f1ff48","type":"rpi-sensehat in","z":"ec37e354.13c82","name":"","motion":true,"env":false,"stick":false,"x":99,"y":30,"wires":[["3571a1b3.ca8e5e"]]},{"id":"3571a1b3.ca8e5e","type":"function","z":"ec37e354.13c82","name":"Compass","func":"// Based on the Compass example provided by\n// the Sense HAT python library\n// https://github.com/RPi-Distro/python-sense-hat/tree/master/examples\n\nvar led_loop = [4, 5, 6, 7, 15, 23, 31, 39, 47, 55, 63, 62, 61, 60, 59, 58, 57, 56, 48, 40, 32, 24, 16, 8, 0, 1, 2, 3];\nvar led_degree_ratio = led_loop.length / 360.0;\nvar dir = (360 - msg.payload.compass)%360;\n\nfunction getXY(dir) {\n var led_index = Math.floor(led_degree_ratio * (dir%360));\n var offset = led_loop[led_index];\n return [offset % 8,Math.floor(offset / 8)];\n}\n\nvar previous = context.get('previous');\nmsg.payload = '';\n\nvar position = getXY(dir);\nif (!previous || position[0] != previous[0][0] || position[1] != previous[0][1]) {\n if (previous) {\n msg.payload = previous[0][0]+\",\"+previous[0][1]+\",off,\"+\n previous[1][0]+\",\"+previous[1][1]+\",off,\";\n } else {\n msg.payload = \"*,*,off,\";\n }\n previous = [\n position,\n getXY(dir+180)\n ];\n msg.payload += previous[0][0]+\",\"+previous[0][1]+\",red,\"+\n previous[1][0]+\",\"+previous[1][1]+\",white\"\n \n context.set('previous',previous);\n return msg;\n}\nreturn null;\n\n","outputs":1,"noerr":0,"x":279,"y":30,"wires":[["b3b88507.4c4778"]]}]

View File

@ -24,7 +24,7 @@ module.exports = function(RED) {
if ( !(1 & parseInt ((fs.statSync(hatCommand).mode & parseInt ("777", 8)).toString (8)[0]) )) {
RED.log.error(hatCommand + " command is not executable");
throw "Error : "+RED._("rpi-gpio.errors.mustbeexecutable");
throw "Error : "+RED._("node-red:rpi-gpio.errors.mustbeexecutable");
}
// the magic to make python print stuff immediately
@ -104,24 +104,28 @@ module.exports = function(RED) {
for (var j=0;j<users.length;j++) {
var node = users[j];
if (node.motion && msg.topic === "motion") {
node.send(msg);
node.send(RED.util.cloneMessage(msg));
} else if (node.env && msg.topic === 'environment') {
node.send(msg);
node.send(RED.util.cloneMessage(msg));
} else if (node.stick && msg.topic === 'joystick') {
node.send(msg);
node.send(RED.util.cloneMessage(msg));
}
}
}
}
});
hat.stderr.on('data', function (data) {
if (RED.settings.verbose) { RED.log.error("err: "+data+" :"); }
});
hat.stderr.on('error', function(err) { });
hat.stdin.on('error', function(err) { });
hat.on('close', function (code) {
hat = null;
if (RED.settings.verbose) { RED.log.info(RED._("rpi-gpio.status.closed")); }
users.forEach(function(node) {
node.status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
});
if (RED.settings.verbose) { RED.log.info(RED._("node-red:rpi-gpio.status.closed")); }
if (onclose) {
onclose();
onclose = null;
@ -133,9 +137,11 @@ module.exports = function(RED) {
});
hat.on('error', function (err) {
if (err.errno === "ENOENT") { RED.log.error(RED._("rpi-gpio.errors.commandnotfound")); }
else if (err.errno === "EACCES") { RED.log.error(RED._("rpi-gpio.errors.commandnotexecutable")); }
else { RED.log.error(RED._("rpi-gpio.errors.error")+': ' + err.errno); }
if (err.errno === "ENOENT") { RED.log.error(RED._("node-red:rpi-gpio.errors.commandnotfound")); }
else if (err.errno === "EACCES") { RED.log.error(RED._("node-red:rpi-gpio.errors.commandnotexecutable")); }
else {
RED.log.error(RED._("node-red:rpi-gpio.errors.error")+': ' + err.errno);
}
});
if (motionUsers > 0) {
@ -165,6 +171,9 @@ module.exports = function(RED) {
if (!hat) {
connect();
}
if (!reconnectTimer) {
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
}
if (node.motion) {
if (motionUsers === 0) {
@ -215,6 +224,7 @@ module.exports = function(RED) {
this.env = n.env;
this.stick = n.stick;
var node = this;
node.status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
HAT.open(this);
node.on("close", function(done) {
@ -226,6 +236,8 @@ module.exports = function(RED) {
function SenseHatOutNode(n) {
RED.nodes.createNode(this,n);
var node = this;
node.status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
HAT.open(this);
node.on("close", function(done) {

View File

@ -53,9 +53,9 @@ def get_stick():
if f.read().strip() == 'Raspberry Pi Sense HAT Joystick':
return os.path.join('/dev', 'input', os.path.basename(evdev))
except IOError as e:
if e.errno != errno.ENOENT:
raise
raise RuntimeError('unable to locate SenseHAT joystick device')
sys.exit(1)
sys.exit(1)
stick_file = io.open(get_stick(),'rb')
@ -217,6 +217,7 @@ def process_joystick():
def main_loop():
# while still waiting for input on at least one file
try:
while files:
ready = select.select(files, [], [], 0.01)[0]
if not ready:
@ -231,6 +232,8 @@ def main_loop():
process_command(line)
else:
process_joystick()
except:
sys.exit(0)
try:
main_loop()