Skip to content

Commit

Permalink
feat: initial checkin
Browse files Browse the repository at this point in the history
Current state of affairs will retrieve a framebuffer, if one exists. Not
yet a promised version as i experience ongoing probs with my limited
brain capacity.
  • Loading branch information
jhinrichsen committed Jan 3, 2016
1 parent c76cfa8 commit 4ee15d4
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Find our matrix in all available framebuffers

"use strict";

const fsp = require('fs-promise'),
glob = require('glob-promise'),
path = require('path'),
Stream = require('streamjs'),

// the framebuffer's name file
namefile = framebuffer => path.join(framebuffer, 'name'),

// Linux file spec for framebuffers
prospects = glob('/sys/class/graphics/fb*'),

// predicte: has a name file
// Promise hasNamefile = dir => fsp.statSync(namefile(dir)).then(stat => stat.isFile()),
// exists() is deprecated in favour of stat(), but stat throws an Error, which i cannot
// handle taking my limited Javascript into consideration
// hasNamefile = dir => fsp.statSync(namefile(dir)),
hasNamefile = dir => fsp.existsSync(namefile(dir)),

// Predicate: framebuffer is a Sense HAT LED matrix
isSenseHatMatrix = dir => {
// Promise return fsp.readFileSync(namefile(dir)).then(buf => 'RPi-Sense FB\n' === b.toString());
const s = fsp.readFileSync(namefile(dir)).toString().trim();
// Ignore any trailing EOL
const p = 'RPi-Sense FB' === s;
return p;
},

// Returns an Optional, which is also called a Maybe in other languages
fb = prospects.then(a => {
let r = Stream(a)
.filter(hasNamefile)
.filter(isSenseHatMatrix)
.findFirst();
return r;
}),

rc = fb.then(a => {
if (a.isPresent()) {
const led = a.get();
console.log(`Found framebuffer ${led}`);
return led;
} else {
console.log('Cannot find a Raspberry Pi Sense HAT matrix LED!' +
'Are we running on a Pi?');
return null;
}
});

// EOF

0 comments on commit 4ee15d4

Please sign in to comment.