SenseHat: Add clock example and support for x0-x1 ranges

This commit is contained in:
Nick O'Leary 2016-03-28 22:53:28 +01:00
parent 73e84c7ff9
commit a4daf91a30
6 changed files with 50 additions and 20 deletions

View File

@ -77,7 +77,8 @@ be sent in a single message by separating them with newline (\n) characters.
Format: `<x>,<y>,<colour>`
`x` and `y` must either be a value in the range 0-7, or `*` to indicate the entire row or column.
`x` and `y` must either be a value from 0 to 7, a `*` to indicate the entire row
or column, or a range such as `3-6`.
`colour` must be one of:
@ -93,6 +94,9 @@ To set the four corners of the display to red, green (#00ff00), yellow and blue
`0,0,red,0,7,#00ff00,7,7,yellow,7,0,0,0,255`
To set a 3-pixel wide column to purple: `4-6,*,purple`
#### Rotate the screen
Format: `R<angle>`

View File

@ -0,0 +1 @@
[{"id":"8b346a30.651a88","type":"rpi-sensehat out","z":"7756eff1.08d0a","name":"","x":530,"y":220,"wires":[]},{"id":"c2e7a15d.237bd","type":"function","z":"7756eff1.08d0a","name":"Simple graphical clock","func":"\n// array to hold \"random\" pixels\nvar ranNums = [];\n\n// create a non-overlapping array of random numbers 0-8\nfunction rerand() {\n var nums = [0,1,2,3,4,5,6,7,8];\n var i = nums.length;\n var j;\n ranNums = [];\n while (i--) {\n j = Math.floor(Math.random() * (i+1));\n ranNums.push(nums[j]);\n nums.splice(j,1);\n }\n}\n\n// Get the hours and minutes and split into tens and units\nvar d = new Date();\nvar s = d.getSeconds();\nvar su = s%4;\nif (su === 0) {\n var h = d.getHours();\n var m = d.getMinutes();\n var hu = h%10;\n h = parseInt(h/10);\n var mu = m%10;\n m = parseInt(m/10);\n \n // Do the tens of hours (red)\n rerand();\n node.send({payload:\"1-3,1-3,0,0,0\"});\n for (var i=0; i<h; i++) {\n node.send({payload:(1+ranNums[i]%3)+\",\"+(1+parseInt(ranNums[i]/3))+\",255,0,0\"});\n }\n \n // Do the units of hours (green)\n rerand();\n node.send({payload:\"4-6,1-3,0,0,0\"});\n for (var i=0; i<hu; i++) {\n node.send({payload:(4+ranNums[i]%3)+\",\"+(1+parseInt(ranNums[i]/3))+\",0,255,0\"});\n }\n \n // Do the tens of minutes (yellow)\n rerand();\n node.send({payload:\"1-3,4-6,0,0,0\"});\n for (var i=0; i<m; i++) {\n node.send({payload:(1+ranNums[i]%3)+\",\"+(4+parseInt(ranNums[i]/3))+\",255,255,0\"});\n }\n \n // Do the unit of minutes (blue)\n rerand();\n node.send({payload:\"4-6,4-6,0,0,0\"});\n for (var i=0; i<mu; i++) {\n node.send({payload:(4+ranNums[i]%3)+\",\"+(4+parseInt(ranNums[i]/3))+\",0,0,255\"});\n }\n \n}\nnode.send({payload:\"0,0,\"+(su===0?\"purple\":\"off\")+\",7,0,\"+(su===1?\"purple\":\"off\")+\",7,7,\"+(su===2?\"purple\":\"off\")+\",0,7,\"+(su===3?\"purple\":\"off\")})\n\n\n// nothing left to do\nreturn null;","outputs":1,"noerr":0,"x":320,"y":220,"wires":[["8b346a30.651a88"]]},{"id":"fc3808df.6cbcf8","type":"comment","z":"7756eff1.08d0a","name":"Simple graphical clock for Sense Hat","info":"Generates a graphical clock, showing hours and minutes. \n\nHours are shown by the red and green pixels, minutes\nare the yellow and blue pixels.\n\nUses the local time of the Pi - you may need to set your local\ntimezone using\n\n sudo dpkg-reconfigure tzdata\n","x":210,"y":180,"wires":[]},{"id":"f33ea1a7.27216","type":"inject","z":"7756eff1.08d0a","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":true,"x":120,"y":220,"wires":[["c2e7a15d.237bd"]]}]

View File

@ -1 +1 @@
[{"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"]]}]
[{"id":"104a54e7.6656cb","type":"rpi-sensehat out","z":"7756eff1.08d0a","name":"","x":470,"y":300,"wires":[]},{"id":"a3786fbb.b3ff9","type":"rpi-sensehat in","z":"7756eff1.08d0a","name":"","motion":true,"env":false,"stick":false,"x":120,"y":300,"wires":[["931798d.9640d68"]]},{"id":"931798d.9640d68","type":"function","z":"7756eff1.08d0a","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":280,"y":300,"wires":[["104a54e7.6656cb"]]},{"id":"434680a8.c2e54","type":"comment","z":"7756eff1.08d0a","name":"Simple compass for Sense Hat","info":"Generates a compass, showing north (red) and south (white).\n\nBased on the python example provided with the Sense Hat: https://github.com/RPi-Distro/python-sense-hat/tree/master/examples\n\nThe accuracy of the compass will depend on callibrating the Sense Hat\ninternal compass. Search the Sense Hat forums for a guide on how to\ndo this.\n","x":190,"y":260,"wires":[]}]

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-pi-sense-hat",
"version" : "0.0.9",
"version" : "0.0.10",
"description" : "A Node-RED node to interact with a Raspberry Pi Sense HAT",
"repository" : {
"type":"git",

View File

@ -92,8 +92,8 @@ be sent in a single message by separating them with newline (\n) characters.<p>
<p><b>Set the colour of individual pixels</b></p>
<p>Format: <code>&lt;x&gt;,&lt;y&gt;,&lt;colour&gt;</code>
<p><code>x</code> and <code>y</code> must either be a value in the range 0-7, or
<code>*</code> to indicate the entire row or column.</p>
<p><code>x</code> and <code>y</code> must either be a value from 0 to 7, a
<code>*</code> to indicate the entire row or column, or a range such as <code>3-6</code>.</p>
<p><code>colour</code> must be one of:
<ul>
<li>the well-known <a href="https://en.wikipedia.org/wiki/Web_colors" target="_new">HTML colour names</a>
@ -109,6 +109,8 @@ be sent in a single message by separating them with newline (\n) characters.<p>
<p><code>*,*,red</code></p>
<p>To set the four corners of the display to red, green (#00ff00), yellow and blue (0,0,255):</p>
<p><code>0,0,red,0,7,#00ff00,7,7,yellow,7,0,0,0,255</code></p>
<p>To set a 3-pixel wide column to purple:</p>
<p><code>4-6,*,purple</code></p>
<p><b>Rotate the screen</b></p>
<p>Format: <code>R&lt;angle&gt;</code></p>

View File

@ -286,8 +286,7 @@ module.exports = function(RED) {
var lines = msg.payload.split("\n");
lines.forEach(function(line) {
command = null;
col = colours.getRGB(line);
if ( /^(([0-7]|\*),([0-7]|\*),(\d{1,3},\d{1,3},\d{1,3}|#[a-f0-9]{3,6}|[a-z]+))(,([0-7]|\*),([0-7]|\*),(\d{1,3},\d{1,3},\d{1,3}|#[a-f0-9]{3,6}|[a-z]+))*$/i.test(line)) {
if ( /^(([0-7]|\*|[0-7]-[0-7]),([0-7]|\*|[0-7]-[0-7]),(\d{1,3},\d{1,3},\d{1,3}|#[a-f0-9]{3,6}|[a-z]+))(,([0-7]|\*|[0-7]-[0-7]),([0-7]|\*|[0-7]-[0-7]),(\d{1,3},\d{1,3},\d{1,3}|#[a-f0-9]{3,6}|[a-z]+))*$/i.test(line)) {
parts = line.split(",");
var expanded = [];
var i=0;
@ -305,22 +304,46 @@ module.exports = function(RED) {
} else {
col += ","+parts[i++]+","+parts[i++];
}
if (x!=='*' && y!=='*') {
expanded.push([x,y,col]);
} else if (x == '*' && y == '*') {
for (j=0;j<8;j++) {
for (var k=0;k<8;k++) {
expanded.push([j,k,col]);
}
if (x === '*') {
x = "0-7";
}
if (y === '*') {
y = "0-7";
}
var x0,x1;
var y0,y1;
if (x.indexOf("-") === -1) {
x0 = x1 = parseInt(x);
} else {
var px = x.split("-");
x0 = parseInt(px[0]);
x1 = parseInt(px[1]);
if (x1<x0) {
j = x1;
x1 = x0;
x0 = j;
}
} else if (x == '*') {
for (j=0;j<8;j++) {
expanded.push([j,y,col]);
}
if (y.indexOf("-") === -1) {
y0 = y1 = parseInt(y);
} else {
var py = y.split("-");
y0 = parseInt(py[0]);
y1 = parseInt(py[1]);
if (y1<y0) {
j = y1;
y1 = y0;
y0 = j;
}
} else if (y == '*') {
for (j=0;j<8;j++) {
expanded.push([x,j,col]);
}
x = x0;
while(x<=x1) {
y = y0;
while(y<=y1) {
expanded.push([x,y,col]);
y++;
}
x++;
}
}
if (expanded.length > 0) {