Skip to content

Commit

Permalink
feat: Add getPixel() to read an RGB value
Browse files Browse the repository at this point in the history
  • Loading branch information
jhinrichsen committed Jan 3, 2016
1 parent 421a5cb commit d6b3bf2
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ const fsp = require('fs-promise'),
return r;
}),

// Decodes 16 bit RGB565 into list [R,G,B]
rgb = n => {
let r = (n & 0xF800) >> 11,
g = (n & 0x7E0) >> 5,
b = (n & 0x1F),
rc = [ r << 3, g << 2, b << 3 ];
return rc;
},

// Returns a list of [R,G,B] representing the pixel specified by x and y
// on the LED matrix. Top left = 0,0 Bottom right = 7,7
getPixel = (fb, x, y) => {
if (x < 0 || x > 7) throw new Error(`x=${x} violates 0 <= x <= 7`);
if (y < 0 || y > 7) throw new Error(`y=${y} violates 0 <= y <= 7`);
// TODO support rotation

// Two bytes per pixel in fb memory, 16 bit RGB565
const fd = fsp.openSync(fb, 'r');
// fread() supports no sync'd version, so read in all 8 x 8 x 2 bytes in one shot
const buf = fsp.readFileSync(fd);
const n = buf.readUInt16BE(y * 8 + x);
return rgb(n);
},

rc = fb.then(a => {
if (a.isPresent()) {
const led = devname(a.get());
Expand All @@ -51,6 +75,10 @@ const fsp = require('fs-promise'),
'Are we running on a Pi?');
return null;
}
}),

rrc = rc.then(fb => {
console.log(`Pixel (0,0) = ${getPixel(fb, 0, 0)}`);
});

// EOF

0 comments on commit d6b3bf2

Please sign in to comment.