Last active
August 30, 2016 09:05
-
-
Save knolleary/da4fc4348e44dcf81744eb4e042e8635 to your computer and use it in GitHub Desktop.
Connecting a Sense HAT to Watson IoT using Node-RED - https://developer.ibm.com/recipes/tutorials/connecting-a-sense-hat-to-watson-iot-using-node-red/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"id":"479b30f5.0d2bb","type":"function","z":"7756eff1.08d0a","name":"Marble Maze","func":"// A Function to generate a new goal position\nfunction updateGoal() {\n do {\n goalX = Math.floor(Math.random()*7)+1;\n goalY = Math.floor(Math.random()*7)+1;\n } while(checkWall(goalX,goalY));\n \n flow.set('goalX',goalX);\n flow.set('goalY',goalY); \n}\n\n// Generate 'walls' - 10 randomly placed blocks\nfunction generateWalls() {\n walls = [];\n for (var i=0;i<10;i++) {\n var wx = 0;\n var wy = 0;\n do {\n wx = Math.floor(Math.random()*8);\n wy = Math.floor(Math.random()*8);\n } while(wx === x && wy === y);\n walls.push({x:wx,y:wy});\n }\n flow.set('walls',walls);\n}\n\n// Check whether a given position collides with any of the walls\nfunction checkWall(x,y) {\n for (var i=0;i<walls.length;i++) {\n if (walls[i].x === x && walls[i].y === y) {\n return true;\n }\n }\n return false;\n}\n\n// Retrieve the current player positions from local context\nvar x = context.get('x');\nvar y = context.get('y');\n\n// Retrieve the current goal position from flow context\nvar goalX = flow.get('goalX');\nvar goalY = flow.get('goalY');\n\n// Retrieve the current walls from flow context\nvar walls = flow.get('walls');\n\n// Generate new walls if needed\nif (!walls) {\n generateWalls();\n}\n\n// Generate a new goal if needed\nif (isNaN(goalX) || isNaN(goalY)) {\n updateGoal();\n}\n\n\nvar moved = false;\nvar ox = x;\nvar oy = y;\n\n// Initialise the player position\nif (isNaN(x) || isNaN(y)) {\n ox = 3;\n oy = 3;\n x = 3;\n y = 3;\n moved = true;\n} else {\n // Retrieve the current roll & pitch from the message\n // arriving from the SenseHAT\n var roll = msg.payload.orientation.roll;\n var pitch = msg.payload.orientation.pitch;\n\n // Move the player based on tilt, ensuring it stays\n // within the bounds of the screen\n var sensitivity = 7;\n if (roll > sensitivity && roll < 90) {\n y += 1;\n moved = true;\n if (y > 6) { y = 7; }\n } else if (roll < 360-sensitivity && roll > 270) {\n y -= 1;\n moved = true;\n if (y < 1) { y = 0; }\n }\n if (pitch > sensitivity && pitch < 90) {\n x -= 1;\n moved = true;\n if (x < 1) { x = 0; }\n } else if (pitch < 360-sensitivity && pitch > 270) {\n x += 1;\n moved = true;\n if (x > 6) { x = 7; }\n }\n // If the resultant position hits a wall, go back to\n // where it started\n if (checkWall(x,y)) {\n x = ox;\n y = oy;\n }\n}\n\n// Store the new player position\ncontext.set('x',x);\ncontext.set('y',y);\n\nif (moved) {\n // If the player is on the goal, generate a new set of\n // walls and goal position\n if (x === goalX && y === goalY) {\n generateWalls();\n updateGoal();\n // Blank the display\n msg.payload = \"*,*,off,\";\n } else {\n // Blank the old position of the player\n msg.payload = ox+\",\"+oy+\",off,\";\n }\n // Draw each of the walls\n for (var i =0;i<walls.length;i++) {\n msg.payload += walls[i].x+\",\"+walls[i].y+\",green,\";\n }\n // Draw the goal and player position\n msg.payload += goalX+\",\"+goalY+\",blue,\"+x+\",\"+y+\",red\";\n\n // Pass the message on to the SenseHAT Out node\n return msg;\n}\n\n// Nothing has moved so no need to update screen - return nothing\nreturn null;","outputs":"1","noerr":0,"x":877,"y":226,"wires":[[]]}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"id":"68ae11d8.30a5b","type":"function","z":"7756eff1.08d0a","name":"Marble Maze","func":"// A Function to generate a new goal position\nfunction updateGoal() {\n do {\n goalX = Math.floor(Math.random()*7)+1;\n goalY = Math.floor(Math.random()*7)+1;\n } while(checkWall(goalX,goalY));\n \n flow.set('goalX',goalX);\n flow.set('goalY',goalY); \n}\n\n// Generate 'walls' - 10 randomly placed blocks\nfunction generateWalls() {\n walls = [];\n for (var i=0;i<10;i++) {\n var wx = 0;\n var wy = 0;\n do {\n wx = Math.floor(Math.random()*8);\n wy = Math.floor(Math.random()*8);\n } while(wx === x && wy === y);\n walls.push({x:wx,y:wy});\n }\n flow.set('walls',walls);\n}\n\n// Check whether a given position collides with any of the walls\nfunction checkWall(x,y) {\n for (var i=0;i<walls.length;i++) {\n if (walls[i].x === x && walls[i].y === y) {\n return true;\n }\n }\n return false;\n}\n\n// Retrieve the current player positions from local context\nvar x = context.get('x');\nvar y = context.get('y');\n\n// Retrieve the current goal position from flow context\nvar goalX = flow.get('goalX');\nvar goalY = flow.get('goalY');\n\n// Retrieve the current walls from flow context\nvar walls = flow.get('walls');\n\n// Generate new walls if needed\nif (!walls) {\n generateWalls();\n}\n\n// Generate a new goal if needed\nif (isNaN(goalX) || isNaN(goalY)) {\n updateGoal();\n}\n\n\nvar moved = false;\nvar ox = x;\nvar oy = y;\n\n// Initialise the player position\nif (isNaN(x) || isNaN(y)) {\n ox = 3;\n oy = 3;\n x = 3;\n y = 3;\n moved = true;\n} else {\n // Retrieve the current roll & pitch from the message\n // arriving from the SenseHAT\n var roll = msg.payload.orientation.roll;\n var pitch = msg.payload.orientation.pitch;\n\n // Move the player based on tilt, ensuring it stays\n // within the bounds of the screen\n var sensitivity = 7;\n if (roll > sensitivity && roll < 90) {\n y += 1;\n moved = true;\n if (y > 6) { y = 7; }\n } else if (roll < 360-sensitivity && roll > 270) {\n y -= 1;\n moved = true;\n if (y < 1) { y = 0; }\n }\n if (pitch > sensitivity && pitch < 90) {\n x -= 1;\n moved = true;\n if (x < 1) { x = 0; }\n } else if (pitch < 360-sensitivity && pitch > 270) {\n x += 1;\n moved = true;\n if (x > 6) { x = 7; }\n }\n // If the resultant position hits a wall, go back to\n // where it started\n if (checkWall(x,y)) {\n x = ox;\n y = oy;\n }\n}\n\n// Store the new player position\ncontext.set('x',x);\ncontext.set('y',y);\n\nif (moved) {\n var eventMsg = null;\n \n // If the player is on the goal, generate a new set of\n // walls and goal position\n if (x === goalX && y === goalY) {\n generateWalls();\n updateGoal();\n // Blank the display\n msg.payload = \"*,*,off,\";\n eventMsg = {\n payload: {\n x: goalX,\n y: goalY\n }\n };\n } else {\n // Blank the old position of the player\n msg.payload = ox+\",\"+oy+\",off,\";\n }\n // Draw each of the walls\n for (var i =0;i<walls.length;i++) {\n msg.payload += walls[i].x+\",\"+walls[i].y+\",green,\";\n }\n // Draw the goal and player position\n msg.payload += goalX+\",\"+goalY+\",blue,\"+x+\",\"+y+\",red\";\n\n // Pass the message on to the SenseHAT Out node\n return [msg,eventMsg];\n}\n\n// Nothing has moved so no need to update screen - return nothing\nreturn null;","outputs":"2","noerr":0,"x":290,"y":60,"wires":[["4255b5b7.9dfa3c"],["5d660aa0.db3394"]]}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"id":"be1a48ed.8739a8","type":"function","z":"8df0e45e.720f18","name":"Generate Maze","func":"var x = msg.payload.d.x;\nvar y = msg.payload.d.y;\nvar goalX;\nvar goalY;\n\ndo {\n goalX = Math.floor(Math.random()*7)+1;\n goalY = Math.floor(Math.random()*7)+1;\n} while(goalX === x && goalY === y);\n\nvar walls = [];\nfor (var i=0;i<10;i++) {\n var wx = 0;\n var wy = 0;\n do {\n wx = Math.floor(Math.random()*8);\n wy = Math.floor(Math.random()*8);\n } while((wx === x && wy === y)||(goalX === wx && goalY === wy));\n walls.push({x:wx,y:wy});\n}\n\n\nmsg.payload = JSON.stringify({\n goal: {\n x: goalX,\n y: goalY\n },\n walls: walls\n})\nreturn msg;","outputs":1,"noerr":0,"x":536,"y":360,"wires":[["24e34620.c8902a"]]}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"id":"87f42900.93a7d8","type":"change","z":"7756eff1.08d0a","name":"","rules":[{"t":"set","p":"goalX","pt":"flow","to":"payload.goal.x","tot":"msg"},{"t":"set","p":"goalY","pt":"flow","to":"payload.goal.y","tot":"msg"},{"t":"set","p":"walls","pt":"flow","to":"payload.walls","tot":"msg"},{"t":"set","p":"refresh","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":120,"wires":[["68ae11d8.30a5b"]]}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"id":"68ae11d8.30a5b","type":"function","z":"7756eff1.08d0a","name":"Marble Maze","func":"// Check whether a given position collides with any of the walls\nfunction checkWall(x,y) {\n for (var i=0;i<walls.length;i++) {\n if (walls[i].x === x && walls[i].y === y) {\n return true;\n }\n }\n return false;\n}\n\nif (msg.topic === \"joystick\" && msg.payload.key === \"ENTER\" && msg.payload.state === 1) {\n // Reset everything if needed\n msg.refresh = true;\n flow.set('goalX',null);\n flow.set('goalY',null);\n flow.set('walls',[]);\n \n}\n\n// Retrieve the current player positions from local context\nvar x = context.get('x');\nvar y = context.get('y');\n\n// Retrieve the current goal position from flow context\nvar goalX = flow.get('goalX');\nvar goalY = flow.get('goalY');\n\n// Retrieve the current walls from flow context\nvar walls = flow.get('walls')||[];\n\n// Generate a new goal if needed\nif (goalX===null || goalY===null) {\n goalX = Math.floor(Math.random()*7)+1;\n goalY = Math.floor(Math.random()*7)+1;\n flow.set('goalX',goalX);\n flow.set('goalY',goalY);\n msg.refresh = true;\n}\n\n\nvar moved = false;\nvar ox = x;\nvar oy = y;\n\n// Initialise the player position\nif (isNaN(x) || isNaN(y)) {\n ox = 3;\n oy = 3;\n x = 3;\n y = 3;\n moved = true;\n} else {\n if (msg.payload.orientation) {\n // Retrieve the current roll & pitch from the message\n // arriving from the SenseHAT\n var roll = msg.payload.orientation.roll;\n var pitch = msg.payload.orientation.pitch;\n \n // Move the player based on tilt, ensuring it stays\n // within the bounds of the screen\n var sensitivity = 7;\n if (roll > sensitivity && roll < 90) {\n y += 1;\n moved = true;\n if (y > 6) { y = 7; }\n } else if (roll < 360-sensitivity && roll > 270) {\n y -= 1;\n moved = true;\n if (y < 1) { y = 0; }\n }\n if (pitch > sensitivity && pitch < 90) {\n x -= 1;\n moved = true;\n if (x < 1) { x = 0; }\n } else if (pitch < 360-sensitivity && pitch > 270) {\n x += 1;\n moved = true;\n if (x > 6) { x = 7; }\n }\n // If the resultant position hits a wall, go back to\n // where it started\n if (checkWall(x,y)) {\n x = ox;\n y = oy;\n }\n }\n}\n\n// Store the new player position\ncontext.set('x',x);\ncontext.set('y',y);\n\nif (moved || msg.refresh) {\n var eventMsg = null;\n \n // If the player is on the goal, generate a new set of\n // walls and goal position\n if (x === goalX && y === goalY) {\n walls = [];\n flow.set('walls',walls);\n // Blank the display\n eventMsg = {\n payload: {\n x: goalX,\n y: goalY\n }\n };\n goalX = -1;\n goalY = -1;\n flow.set('goalX',goalX);\n flow.set('goalY',goalY);\n \n }\n msg.payload = \"*,*,off,\";\n // Draw each of the walls\n for (var i =0;i<walls.length;i++) {\n msg.payload += walls[i].x+\",\"+walls[i].y+\",green,\";\n }\n if (goalX !== -1 && goalY !== -1) {\n msg.payload += goalX+\",\"+goalY+\",blue,\";\n }\n // Draw the goal and player position\n msg.payload += x+\",\"+y+\",red\";\n\n // Pass the message on to the SenseHAT Out node\n return [msg,eventMsg];\n}\n\n// Nothing has moved so no need to update screen - return nothing\nreturn null;","outputs":"2","noerr":0,"x":470,"y":60,"wires":[["4255b5b7.9dfa3c"],["5d660aa0.db3394"]]}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment