From 98d5df965c7b335d5ba2dff0521ee6a1f30fa68d Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 18 Jun 2020 00:44:17 +0100 Subject: [PATCH] [sensehatsim] Add support for scrolling text --- hardware/sensehatsim/README.md | 5 +- hardware/sensehatsim/package.json | 24 ++- hardware/sensehatsim/public/index.html | 248 +++++++++++++++++-------- hardware/sensehatsim/sensehatsim.html | 10 +- hardware/sensehatsim/sensehatsim.js | 49 +++-- 5 files changed, 212 insertions(+), 124 deletions(-) diff --git a/hardware/sensehatsim/README.md b/hardware/sensehatsim/README.md index c254e20d..c7ce03e4 100644 --- a/hardware/sensehatsim/README.md +++ b/hardware/sensehatsim/README.md @@ -1,8 +1,7 @@ node-red-node-pi-sense-hat-simulator ==================================== -A Node-RED node to simulate a -Raspberry Pi Sense HAT. +A node to simulate a Raspberry Pi Sense HAT. This allows you to create flows that interact with a virtual Sense HAT without the actual hardware - whether you're running on a Raspberry Pi, a laptop or elsewhere. @@ -105,8 +104,6 @@ Format: `R` #### Scroll a message -**The current version of the simulator does not support displaying text** - If `msg.payload` is not recognised as any of the above commands, it is treated as a text message to be scrolled across the screen. diff --git a/hardware/sensehatsim/package.json b/hardware/sensehatsim/package.json index 0a625a08..15fca0be 100644 --- a/hardware/sensehatsim/package.json +++ b/hardware/sensehatsim/package.json @@ -1,18 +1,22 @@ { - "name" : "node-red-node-pi-sense-hat-simulator", - "version" : "0.0.2", - "description" : "A Node-RED node to simulate a Raspberry Pi Sense HAT", - "repository" : { - "type":"git", - "url":"https://github.com/node-red/node-red-nodes/tree/master/hardware/sensehatsim" + "name": "node-red-node-pi-sense-hat-simulator", + "version": "0.1.0", + "description": "A Node-RED node to simulate a Raspberry Pi Sense HAT", + "repository": { + "type": "git", + "url": "https://github.com/node-red/node-red-nodes/tree/master/hardware/sensehatsim" }, "dependencies": { - "ws": "0.8.1" + "ws": "~6.2.1" }, "license": "Apache-2.0", - "keywords": [ "node-red", "sensehat", "astropi" ], - "node-red" : { - "nodes" : { + "keywords": [ + "node-red", + "sensehat", + "astropi" + ], + "node-red": { + "nodes": { "sensehatsim": "sensehatsim.js" } }, diff --git a/hardware/sensehatsim/public/index.html b/hardware/sensehatsim/public/index.html index 8267bf49..c419169a 100644 --- a/hardware/sensehatsim/public/index.html +++ b/hardware/sensehatsim/public/index.html @@ -25,6 +25,9 @@ box-shadow: 2px 2px 1px #99cc99; } + #display.low_brightness .led { + filter: brightness(0.6); + } .led { background-color: #000; border: 1px solid #eee; @@ -162,6 +165,9 @@ diff --git a/hardware/sensehatsim/sensehatsim.html b/hardware/sensehatsim/sensehatsim.html index 0edfeade..67168cb5 100644 --- a/hardware/sensehatsim/sensehatsim.html +++ b/hardware/sensehatsim/sensehatsim.html @@ -32,7 +32,8 @@

Raspberry Pi Sense HAT Simulator input node.

This node simulates readings from the various sensors on the Sense HAT, grouped into three sets; motion events, environment events and joystick events.

-

Once deployed, the simulator can be accessed here.

+

Once deployed, click this button to open the simulator:

+

Open Simulator

Motion events - not currently supported by the simulator

Motion events include readings from the accelerometer, gyroscope and magnetometer, as well as the current compass heading. They are sent at a rate of approximately 10 @@ -73,7 +74,8 @@ is an object with the following values:

diff --git a/hardware/sensehatsim/sensehatsim.js b/hardware/sensehatsim/sensehatsim.js index bcdb8cd6..42439fa9 100644 --- a/hardware/sensehatsim/sensehatsim.js +++ b/hardware/sensehatsim/sensehatsim.js @@ -3,6 +3,7 @@ module.exports = function(RED) { "use strict"; var path = require("path"); var ws = require("ws"); + var url = require("url"); var colours = require('./colours'); // Xaccel.x,y,z,gyro.x,y,z,orientation.roll,pitch,yaw,compass @@ -25,7 +26,7 @@ module.exports = function(RED) { var currentDisplay = []; var HAT = (function() { var hatWS = null; - var wsServerListeners = {}; + var wsServerListener; var wsConnections = {}; var currentEnvironment = {temperature: 20, humidity: 80, pressure: 1000}; var hat = null; @@ -56,19 +57,15 @@ module.exports = function(RED) { wsServerListeners[event] = listener; } } - RED.server.addListener('newListener',storeListener); // Create a WebSocket Server - hatWS = new ws.Server({ - server:RED.server, - path:wsPath, - // Disable the deflate option due to this issue - // https://github.com/websockets/ws/pull/632 - // that is fixed in the 1.x release of the ws module - // that we cannot currently pickup as it drops node 0.10 support - perMessageDeflate: false - }); - RED.server.removeListener('newListener',storeListener); + hatWS = new ws.Server({noServer: true}); + hatWS.on('error', function(err) { + + }) hatWS.on('connection', function(socket) { + socket.on('error', function(err) { + delete wsConnections[id]; + }); var id = (1+Math.random()*4294967295).toString(16); wsConnections[id] = socket; socket.send("Y"+currentEnvironment.temperature+","+currentEnvironment.humidity+","+currentEnvironment.pressure); @@ -119,25 +116,27 @@ module.exports = function(RED) { } }); - socket.on('error', function(err) { - delete wsConnections[id]; - }); + }); + + wsServerListener = function upgrade(request, socket, head) { + const pathname = url.parse(request.url).pathname; + if (pathname === wsPath) { + hatWS.handleUpgrade(request, socket, head, function done(ws) { + hatWS.emit('connection', ws, request); + }); + } + // Don't destroy the socket as other listeners may want to handle the + // event. + }; + RED.server.on('upgrade', wsServerListener) + } } var disconnect = function(done) { if (hatWS !== null) { - var listener = null; - for (var event in wsServerListeners) { - if (wsServerListeners.hasOwnProperty(event)) { - listener = wsServerListeners[event]; - if (typeof listener === "function") { - RED.server.removeListener(event,listener); - } - } - } - wsServerListeners = {}; + RED.server.removeListener('upgrade', wsServerListener) wsConnections = {}; hatWS.close(); hatWS = null;