From a3e2365ab91ce2f22dad53dc861ffc67e72abc87 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 4 Mar 2016 17:26:21 +0000 Subject: [PATCH] SenseHAT: accept number payload as text message --- hardware/sensehat/package.json | 2 +- hardware/sensehat/sensehat.js | 50 +++++++++++++++++++--------------- hardware/sensehat/sensehat.py | 8 ++++-- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/hardware/sensehat/package.json b/hardware/sensehat/package.json index 864c36d6..a2971c48 100644 --- a/hardware/sensehat/package.json +++ b/hardware/sensehat/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-pi-sense-hat", - "version" : "0.0.3", + "version" : "0.0.4", "description" : "A Node-RED node to interact with a Raspberry Pi Sense HAT", "repository" : { "type":"git", diff --git a/hardware/sensehat/sensehat.js b/hardware/sensehat/sensehat.js index d86327be..7ce45d35 100644 --- a/hardware/sensehat/sensehat.js +++ b/hardware/sensehat/sensehat.js @@ -231,12 +231,38 @@ module.exports = function(RED) { node.on("close", function(done) { HAT.close(this,done); }); + var handleTextMessage = function(line,msg) { + var textCol = colours.getRGB(msg.color); + var backCol = colours.getRGB(msg.background); + var speed = null; + if (!isNaN(msg.speed)) { + speed = msg.speed; + } + var command = "T"; + if (textCol) { + command += textCol; + if (backCol) { + command += ","+backCol; + } + } + if (speed) { + var s = parseInt(speed); + if (s >= 1 && s <= 5) { + s = 0.1 + (3-s)*0.03; + } + command = command + ((command.length === 1)?"":",") + s; + } + command += ":" + line; + return command; + } node.on("input",function(msg) { var command; var parts; var col; - if (typeof msg.payload === 'string') { + if (typeof msg.payload === 'number') { + HAT.send(handleTextMessage(""+msg.payload,msg)); + } else if (typeof msg.payload === 'string') { var lines = msg.payload.split("\n"); lines.forEach(function(line) { command = null; @@ -300,27 +326,7 @@ module.exports = function(RED) { } else if (/^F(H|V)$/i.test(line)) { command = line.toUpperCase(); } else { - var textCol = colours.getRGB(msg.color); - var backCol = colours.getRGB(msg.background); - var speed = null; - if (!isNaN(msg.speed)) { - speed = msg.speed; - } - command = "T"; - if (textCol) { - command += textCol; - if (backCol) { - command += ","+backCol; - } - } - if (speed) { - var s = parseInt(speed); - if (s >= 1 && s <= 5) { - s = 0.1 + (3-s)*0.03; - } - command = command + ((command.length === 1)?"":",") + s; - } - command += ":" + line; + command = handleTextMessage(line,msg); } } if (command) { diff --git a/hardware/sensehat/sensehat.py b/hardware/sensehat/sensehat.py index e8a87195..24f807d8 100644 --- a/hardware/sensehat/sensehat.py +++ b/hardware/sensehat/sensehat.py @@ -89,9 +89,11 @@ class ScrollThread(threading.Thread): try: SH.show_message(self.message,text_colour=self.fcol,back_colour=self.bcol,scroll_speed=self.speed) except: - SH.set_rotation(old_rotation,False) - SH.clear(self.bcol); - pass + try: + SH.set_rotation(old_rotation,False) + SH.clear(self.bcol); + except: + pass def interrupt(self): if not self.isAlive():