Created
March 9, 2018 22:16
-
-
Save natcl/b28c6d7ff435c5e1a26aa5f59ab4773a to your computer and use it in GitHub Desktop.
rpi-ws281x-native
This file contains 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
const NUM_LEDS = 64; | |
if (!context.ws281x) { | |
context.ws281x = require('rpi-ws281x-native'); | |
context.finalArray = new Uint32Array(NUM_LEDS); | |
context.ws281x.init(NUM_LEDS, {dmaNum: 10}); | |
} | |
if (context.ws281x) { | |
bufferToArray(msg.payload, NUM_LEDS); | |
context.ws281x.render(context.finalArray); | |
} | |
function bufferToArray(buffer, numLeds){ | |
if (msg.source == 'canvas') { | |
const rowLength = 8; | |
for (let p = 0; p < numLeds; p++) { | |
let rowNum = Math.floor(p/rowLength); | |
// Si impair | |
if (rowNum % 2 == 1) { | |
context.finalArray[(2 * rowNum * rowLength) - p + rowLength - 1] = buffer.readUInt32LE(p*4); | |
} else { | |
context.finalArray[p] = buffer.readUInt32LE(p*4); | |
} | |
} | |
return; | |
} | |
// For each LED | |
for (var p = 0; p < numLeds; p++) { | |
var pixel = Buffer.alloc(4); | |
buffer.copy(pixel, 1, p*3, (p*3)+3); | |
context.finalArray[p] = pixel.readUInt32BE(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment