Created
December 19, 2018 12:30
-
-
Save gyula-ny/b53328b2ae0bb9ef53e42a67ac00e810 to your computer and use it in GitHub Desktop.
binary clock for a rp3 sense-hat-led driven LED-matrix (playing in the ramda REPL)
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 COLORED = [220, 0, 0]; // RGB values for sense-hat-led package "setPixel" func | |
const UNCOLORED = [0, 0, 0]; | |
const VERTICAL_OFFSET = 4; // clock will appear at bottom half of the 8x8 matrix | |
const coloredOrNot = bit => [UNCOLORED, COLORED][bit]; | |
const zeroPad = num => str => str.padStart(num, "0"); | |
const toBinary = num => num.toString(2); | |
const explode = str => str.split(""); | |
const eachToNumber = R.map(Number); | |
const explodeAsBinary = R.pipe(toBinary, zeroPad(4), explode, eachToNumber); // 9 => [1, 0, 0, 1] | |
const explodeTimePiece = R.pipe(String, zeroPad(2), explode, eachToNumber); // "8" => [0, 8] | |
const takeRightNow = () => Reflect.construct(Date, []); | |
const mapIndexed = R.addIndex(R.map); | |
// for "setPixel" we need triplets of position x, y and colour [rgb], e.g. : [3, 4, [220,0,0]] - | |
// given the column this fn returns a mapper func of a 4 bit binary digit into such a triplet: | |
const calcPixelsOfColumn = column => (bit, idx) => [column, VERTICAL_OFFSET + idx, coloredOrNot(bit)]; | |
// mapper of binary time pieces to [posX, posY, [rgb]] triplets: | |
const calcBinaryClock = (num, col) => num === null ? null : explodeAsBinary(num).map(calcPixelsOfColumn(col)); | |
const arrOfTimePiece = method => R.pipe(takeRightNow, R.invoker(0, method), explodeTimePiece); | |
// a loop should start here: | |
const binaryClockSchema = R.concat(arrOfTimePiece("getHours")(), R.concat( | |
[null], R.concat(arrOfTimePiece("getMinutes")(), R.concat( | |
[null], arrOfTimePiece("getSeconds")())))); // => [1,3,null,0,9,null,2,2] at 13:09:22 | |
// Using the 8 columns of the matrix, we leave empty cols in between hrs - mins - secs: | |
const binaryClock = mapIndexed(calcBinaryClock, binaryClockSchema).filter(el => el !== null); | |
console.log(binaryClock); // now we can use setPixel(x, y, [rgb]) for each element. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment