[sensehatsim] Add support for scrolling text

This commit is contained in:
Nick O'Leary 2020-06-18 00:44:17 +01:00
parent 696a99eaa3
commit 98d5df965c
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
5 changed files with 212 additions and 124 deletions

View File

@ -1,8 +1,7 @@
node-red-node-pi-sense-hat-simulator
====================================
A <a href="http://nodered.org" target="_new">Node-RED</a> 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<axis>`
#### 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.

View File

@ -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"
}
},

View File

@ -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 @@
</div>
<script>
var fontData = {" ":[0,0,0,0,0,0,0,0],"+":[0,4,4,31,4,4,0,0],"-":[0,0,0,31,0,0,0,0],"*":[0,4,21,14,21,4,0,0],"/":[0,16,8,4,2,1,0,0],"!":[4,0,4,4,4,4,4,0],"\"":[0,0,0,0,10,10,10,0],"#":[10,10,31,10,31,10,10,0],"$":[4,30,5,14,20,15,4,0],">":[8,4,2,1,2,4,8,0],"<":[2,4,8,16,8,4,2,0],"0":[14,17,25,21,19,17,14,0],"1":[14,4,4,4,4,12,4,0],"2":[31,8,4,2,1,17,14,0],"3":[14,17,1,2,4,2,31,0],"4":[2,2,31,18,10,6,2,0],"5":[14,17,1,1,30,16,31,0],"6":[14,17,17,30,16,8,6,0],"7":[4,4,4,4,2,1,31,0],"8":[14,17,17,14,17,17,14,0],"9":[12,2,1,15,17,17,14,0],".":[12,12,0,0,0,0,0,0],"=":[0,0,31,0,31,0,0,0],")":[8,4,2,2,2,4,8,0],"(":[2,4,8,8,8,4,2,0],"A":[17,17,17,31,17,17,14,0],"B":[30,17,17,30,17,17,30,0],"C":[14,17,16,16,16,17,14,0],"D":[28,18,17,17,17,18,28,0],"E":[31,16,16,30,16,16,31,0],"F":[16,16,16,30,16,16,31,0],"G":[15,17,17,23,16,17,14,0],"H":[17,17,17,31,17,17,17,0],"I":[14,4,4,4,4,4,14,0],"J":[12,18,2,2,2,2,7,0],"K":[17,18,20,24,20,18,17,0],"L":[31,16,16,16,16,16,16,0],"M":[17,17,17,21,21,27,17,0],"N":[17,17,19,21,25,17,17,0],"O":[14,17,17,17,17,17,14,0],"P":[16,16,16,30,17,17,30,0],"Q":[13,18,21,17,17,17,14,0],"R":[17,18,20,30,17,17,30,0],"S":[30,1,1,14,16,16,15,0],"T":[4,4,4,4,4,4,31,0],"U":[14,17,17,17,17,17,17,0],"V":[4,10,17,17,17,17,17,0],"W":[17,27,21,21,17,17,17,0],"X":[17,17,10,4,10,17,17,0],"Y":[4,4,4,4,10,17,17,0],"Z":[31,16,8,4,2,1,31,0],"a":[15,17,15,1,14,0,0,0],"b":[14,17,17,25,22,16,16,0],"c":[14,17,16,16,14,0,0,0],"d":[15,17,17,19,13,1,1,0],"e":[14,16,31,17,14,0,0,0],"f":[4,4,4,14,4,5,2,0],"g":[14,1,15,17,15,0,0,0],"h":[17,17,17,25,22,16,16,0],"i":[14,4,4,4,12,0,4,0],"j":[12,18,2,2,2,0,2,0],"k":[9,10,12,10,9,8,8,0],"l":[14,4,4,4,4,4,12,0],"m":[21,21,21,21,26,0,0,0],"n":[17,17,17,25,22,0,0,0],"o":[14,17,17,17,14,0,0,0],"p":[16,16,30,17,30,0,0,0],"q":[1,1,15,17,15,0,0,0],"r":[8,8,8,12,11,0,0,0],"s":[30,1,14,16,15,0,0,0],"t":[2,5,4,4,14,4,0,0],"u":[13,19,17,17,17,0,0,0],"v":[4,10,17,17,17,0,0,0],"w":[10,21,21,17,17,0,0,0],"x":[19,12,4,6,25,0,0,0],"y":[24,4,6,9,17,0,0,0],"z":[31,8,4,2,31,0,0,0],"?":[4,0,4,2,1,17,14,0],",":[8,4,12,0,0,0,0,0],";":[8,4,12,0,12,12,0,0],":":[0,12,12,0,12,12,0,0],"|":[4,4,4,4,4,4,4,0],"@":[14,21,21,13,1,17,14,0],"%":[3,19,8,4,2,25,24,0],"[":[14,8,8,8,8,8,14,0],"&":[13,18,21,8,20,18,12,0],"_":[31,0,0,0,0,0,0,0],"'":[0,0,0,0,8,4,12,0],"]":[14,2,2,2,2,2,14,0],"\\":[0,1,2,4,8,16,0,0],"~":[0,0,0,22,13,0,0,0]};
var temperature = 29;
var humidity = 80;
var pressure = 1000;
@ -256,8 +262,10 @@ function connect() {
var location = document.location.toString().replace(/^http/,"ws")+"/ws";
ws = new WebSocket(location);
ws.onopen = function() {
console.log("Connected to Node-RED")
}
ws.onclose = function() {
console.log("Lost connection, reconnecting in 5 seconds")
setTimeout(connect,5000);
}
ws.onmessage = function(msg) {
@ -265,102 +273,180 @@ function connect() {
var data = msg.data.substring(1);
var parts;
var x,y,t;
if (command === 'P') {
parts = data.split(",");
for (var i=0;i<parts.length;i+=5) {
x = parts[i];
y = parts[i+1];
if (flipH) { x = 7-x; }
if (flipV) { y = 7-y; }
if (rotation === 180) {
x = 7-x;
y = 7-y;
} else if (rotation === 90) {
if (y<4 && x<4) { t = y; y = x; x = 7-t; }
else if (y<4 && x>3) { t=x; x = 7-y; y = t; }
else if (y>3 && x>3) { t=x; x = 7-y; y = t; }
else if (y>3 && x<4) { t=y; y = x; x = 7-t; }
} else if (rotation === 270) {
if (y<4 && x<4) { t = x; x = y; y = 7-t; }
else if (y<4 && x>3) { t=y; y = 7-x; x = t; }
else if (y>3 && x>3) { t=y; y = 7-x; x = t; }
else if (y>3 && x<4) { t=x; x = y; y = 7-t; }
}
cells[y][x].style.backgroundColor = "rgb("+parts[i+2]+","+parts[i+3]+","+parts[i+4]+")"
}
} else if (command === 'Y') {
if (command === 'Y') {
parts = data.split(",");
updateTemperature(Number(parts[0]));
updateHumidity(Number(parts[1]));
updatePressure(Number(parts[2]));
} else if (command === 'R') {
var delta = Number(data) - rotation;
if (delta < 0) {
delta += 360;
} else if (command === 'D') {
if (data === '0') {
document.getElementById("display").classList.add("low_brightness");
} else if (data === '1') {
document.getElementById("display").classList.remove("low_brightness");
}
if (delta === 90) {
for (y=0;y<4;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = cells[7-x][7-y].style.backgroundColor;
cells[7-x][7-y].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
} else {
clearTimeout(scrollMessageTimeout);
if (command === 'P') {
parts = data.split(",");
for (var i=0;i<parts.length;i+=5) {
x = parts[i];
y = parts[i+1];
if (flipH) { x = 7-x; }
if (flipV) { y = 7-y; }
if (rotation === 180) {
x = 7-x;
y = 7-y;
} else if (rotation === 90) {
if (y<4 && x<4) { t = y; y = x; x = 7-t; }
else if (y<4 && x>3) { t=x; x = 7-y; y = t; }
else if (y>3 && x>3) { t=x; x = 7-y; y = t; }
else if (y>3 && x<4) { t=y; y = x; x = 7-t; }
} else if (rotation === 270) {
if (y<4 && x<4) { t = x; x = y; y = 7-t; }
else if (y<4 && x>3) { t=y; y = 7-x; x = t; }
else if (y>3 && x>3) { t=y; y = 7-x; x = t; }
else if (y>3 && x<4) { t=x; x = y; y = 7-t; }
}
cells[y][x].style.backgroundColor = "rgb("+parts[i+2]+","+parts[i+3]+","+parts[i+4]+")"
}
} else if (delta === 270) {
for (y=0;y<4;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = cells[7-x][7-y].style.backgroundColor;
cells[7-x][7-y].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = t;
}
} else if (command === 'R') {
var delta = Number(data) - rotation;
if (delta < 0) {
delta += 360;
}
} else if (delta === 180) {
for (y=0;y<4;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][7-x].style.backgroundColor;
cells[7-y][7-x].style.backgroundColor = t;
t = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
}
}
}
rotation = Number(data);
//data = 0,90,180,270
} else if (command === 'F') {
if (data === 'H') {
flipH = !flipH;
for (y=0;y<8;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
}
}
} else {
flipV = !flipV;
for (x=0;x<8;x++) {
if (delta === 90) {
for (y=0;y<4;y++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = t;
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = cells[7-x][7-y].style.backgroundColor;
cells[7-x][7-y].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
}
}
} else if (delta === 270) {
for (y=0;y<4;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = cells[7-x][7-y].style.backgroundColor;
cells[7-x][7-y].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = t;
}
}
} else if (delta === 180) {
for (y=0;y<4;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][7-x].style.backgroundColor;
cells[7-y][7-x].style.backgroundColor = t;
t = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
}
}
}
rotation = Number(data);
//data = 0,90,180,270
} else if (command === 'F') {
if (data === 'H') {
flipH = !flipH;
for (y=0;y<8;y++) {
for (x=0;x<4;x++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[y][7-x].style.backgroundColor;
cells[y][7-x].style.backgroundColor = t;
}
}
} else {
flipV = !flipV;
for (x=0;x<8;x++) {
for (y=0;y<4;y++) {
t = cells[y][x].style.backgroundColor;
cells[y][x].style.backgroundColor = cells[7-y][x].style.backgroundColor;
cells[7-y][x].style.backgroundColor = t;
}
}
}
} else if (command === 'T'){
var splitAt = data.indexOf(":");
var colors = data.substring(0,splitAt);
var message = data.substring(splitAt+1);
var colorParts = colors.split(",");
var foreground = "rgb("+colorParts[0]+","+colorParts[1]+","+colorParts[2]+")";
var background = "rgb("+colorParts[3]+","+colorParts[4]+","+colorParts[5]+")";
if (message.length === 1) {
drawCharAt(message[0],0,foreground,background)
} else {
currentMessage = {
foreground: foreground,
background: background,
text: " "+message +" ",
speed: colors.length === 7?colors[6]:0.1,
pos: 0,
subpos: 0
}
stepMessage();
}
}
}
}
}
function drawCharAt(char,cx,foreground,background) {
var charData = fontData[char]||fontData["?"];
for (var y=0;y<8;y++) {
var line = charData[y].toString(2);
while(line.length < 5) {
line = "0"+line;
}
for (var j=0;j<6;j++) {
var x = cx + j;
if (x >= 0 && x < 8) {
if (j === 5) {
cells[7-y][x].style.backgroundColor = background;
} else {
if (line[j] === "1") {
cells[7-y][x].style.backgroundColor = foreground;
} else {
cells[7-y][x].style.backgroundColor = background;
}
}
}
}
}
}
var currentMessage;
var scrollMessageTimeout;
function stepMessage() {
var x = 0;
var subpos = currentMessage.subpos;
var colCount = 0;
for (var i=currentMessage.pos;i<Math.min(currentMessage.pos+3,currentMessage.text.length);i++) {
var char = currentMessage.text[i];
drawCharAt(char,x-subpos,currentMessage.foreground,currentMessage.background)
x += 5-subpos;
subpos = 0;
}
currentMessage.subpos++;
if (currentMessage.subpos === 5) {
currentMessage.subpos = 0;
currentMessage.pos++;
}
if (currentMessage.pos < currentMessage.text.length) {
scrollMessageTimeout = setTimeout(stepMessage,currentMessage.speed*1000);
}
}
connect();
</script>
</body>

View File

@ -32,7 +32,8 @@
<p>Raspberry Pi Sense HAT Simulator input node.</p>
<p>This node simulates readings from the various sensors on the Sense HAT,
grouped into three sets; motion events, environment events and joystick events.</p>
<p>Once deployed, the simulator can be accessed <a href="sensehat-simulator" target="_new">here</a>.</p>
<p>Once deployed, click this button to open the simulator:</p>
<p style="text-align: center"><a href="sensehat-simulator" target="_new" class="red-ui-button">Open Simulator <i class="fa fa-external-link"></i></a></p>
<p><b>Motion events</b> - <i>not currently supported by the simulator</i></p>
<p>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:</p>
<script type="text/x-red" data-help-name="rpi-sensehatsim out">
<p>Raspberry Pi Sense HAT Simulator output node.</p>
<p>This node sends commands to the 8x8 LED display on the Sense HAT simulator.</p>
<p>Once deployed, the simulator can be accessed <a href="sensehat-simulator" target="_new">here</a>.</p>
<p>Once deployed, click this button to open the simulator:</p>
<p style="text-align: center"><a href="sensehat-simulator" target="_new" class="red-ui-button">Open Simulator <i class="fa fa-external-link"></i></a></p>
<p>Commands are sent to the node in <code>msg.payload</code>. Multiple commands can
be sent in a single message by separating them with newline (\n) characters.<p>
@ -108,7 +110,7 @@ be sent in a single message by separating them with newline (\n) characters.<p>
<p><code>axis</code> must be either <code>H</code> or <code>V</code> to flip on
the horizontal or vertical axis respectively.</p>
<p><b>Scroll a message</b> - <i>not currently supported by the simulator</i></p>
<p><b>Scroll a message</b></p>
<p>If <code>msg.payload</code> is not recognised as any of the above commands,
it is treated as a text message to be scrolled across the screen.</p>
<p>If the text is a single character, it will be displayed without scrolling.
@ -120,7 +122,7 @@ To scroll a single character, append a blank space after it - <code>"A "</code>.
<li><code>msg.speed</code> - the scroll speed. A value in the range 1 (slower) to 5 (faster), default: <code>3</code></li>
</ul>
<p><b>Set the screen brightness</b> - <i>not currently supported by the simulator</i></p>
<p><b>Set the screen brightness</b></p>
<p>Format: <code>D&lt;level&gt;</code></p>
<p><code>level</code> must be 0 (low) or 1 (high).</p>
</script>

View File

@ -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;