From a4daf91a30f7cb94ccd7886d49848885f77e8819 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 28 Mar 2016 22:53:28 +0100 Subject: [PATCH] SenseHat: Add clock example and support for x0-x1 ranges --- hardware/sensehat/README.md | 6 ++- hardware/sensehat/examples/Clock.json | 1 + hardware/sensehat/examples/Compass.json | 2 +- hardware/sensehat/package.json | 2 +- hardware/sensehat/sensehat.html | 6 ++- hardware/sensehat/sensehat.js | 53 ++++++++++++++++++------- 6 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 hardware/sensehat/examples/Clock.json diff --git a/hardware/sensehat/README.md b/hardware/sensehat/README.md index 4df8f549..f286ad8b 100644 --- a/hardware/sensehat/README.md +++ b/hardware/sensehat/README.md @@ -77,7 +77,8 @@ be sent in a single message by separating them with newline (\n) characters. Format: `,,` -`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` diff --git a/hardware/sensehat/examples/Clock.json b/hardware/sensehat/examples/Clock.json new file mode 100644 index 00000000..54525109 --- /dev/null +++ b/hardware/sensehat/examples/Clock.json @@ -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

Set the colour of individual pixels

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:

  • the well-known HTML colour names @@ -109,6 +109,8 @@ be sent in a single message by separating them with newline (\n) characters.

    *,*,red

    To set the four corners of the display to red, green (#00ff00), yellow and blue (0,0,255):

    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>

    diff --git a/hardware/sensehat/sensehat.js b/hardware/sensehat/sensehat.js index d437ddb4..4ad3cf60 100644 --- a/hardware/sensehat/sensehat.js +++ b/hardware/sensehat/sensehat.js @@ -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 0) {