@thingssdk/ht16k33
Version:
ES6 compatible display driver for Adafruit HT16K33 matrices, 7 segment and 14 segment displays.
130 lines (122 loc) • 4.98 kB
JavaScript
import { createMatrix } from './main';
function fixBitmap(bitmap) {
let newBitmap = bitmap
.filter(value => value !== undefined)
.map(num => num);
for (var i = newBitmap.length; i < 4; i++) {
newBitmap.unshift([0, 0]);
}
return newBitmap;
}
function charToFourteenSegment(char) {
const CHARS = {
" ": [0b00000000, 0b00000000],
"!": [0b00000110, 0b01000000],
"\"": [0b00100000, 0b00000010],
"#": [0b11001110, 0b00010010],
"$": [0b11101101, 0b00010010],
"%": [0b00100100, 0b00001100],
"&": [0b01011101, 0b00100011],
"'": [0b00000000, 0b00000100],
"(": [0b00000000, 0b00100100],
")": [0b00000000, 0b00001001],
"*": [0b11000000, 0b00111111],
"+": [0b11000000, 0b00010010],
",": [0b00000000, 0b00001000],
"-": [0b11000000, 0b00000000],
"/": [0b00000000, 0b00001100],
"0": [0b00111111, 0b00001100],
"1": [0b00000110, 0b00000000],
"2": [0b11011011, 0b00000000],
"3": [0b10001111, 0b00000000],
"4": [0b11100110, 0b00000000],
"5": [0b01101001, 0b00100000],
"6": [0b11111101, 0b00000000],
"7": [0b00000111, 0b00000000],
"8": [0b11111111, 0b00000000],
"9": [0b11101111, 0b00000000],
":": [0b00000000, 0b00010010],
";": [0b00000000, 0b00001010],
"<": [0b01000000, 0b00100100],
"=": [0b11001000, 0b00000000],
">": [0b10000000, 0b00001001],
"?": [0b10100011, 0b01100000],
"@": [0b10111011, 0b00000010],
"A": [0b11110111, 0b00000000],
"B": [0b10001111, 0b00010010],
"C": [0b00111001, 0b00000000],
"D": [0b00001111, 0b00010010],
"E": [0b11111001, 0b00000000],
"F": [0b01110001, 0b00000000],
"G": [0b10111101, 0b00000000],
"H": [0b11110110, 0b00000000],
"I": [0b00000000, 0b00010010],
"J": [0b00011110, 0b00000000],
"K": [0b01110000, 0b00100100],
"L": [0b00111000, 0b00000000],
"M": [0b00110110, 0b00000101],
"N": [0b00110110, 0b00100001],
"O": [0b00111111, 0b00000000],
"P": [0b11110011, 0b00000000],
"Q": [0b00111111, 0b00100000],
"R": [0b11110011, 0b00100000],
"S": [0b11101101, 0b00000000],
"T": [0b00000001, 0b00010010],
"U": [0b00111110, 0b00000000],
"V": [0b00110000, 0b00001100],
"W": [0b00110110, 0b00101000],
"X": [0b00000000, 0b00101101],
"Y": [0b00000000, 0b00010101],
"Z": [0b00001001, 0b00001100],
"[": [0b00111001, 0b00000000],
"\\": [0b00000000, 0b00100001],
"]": [0b00001111, 0b00000000],
"^": [0b00000011, 0b00001100],
"_": [0b00001000, 0b00000000],
"`": [0b00000000, 0b00000001],
"a": [0b01011000, 0b00010000],
"b": [0b01111000, 0b00100000],
"c": [0b11011000, 0b00000000],
"d": [0b10001110, 0b00001000],
"e": [0b01011000, 0b00001000],
"f": [0b01110001, 0b00000000],
"g": [0b10001110, 0b00000100],
"h": [0b01110000, 0b00010000],
"i": [0b00000000, 0b00010000],
"j": [0b00001110, 0b00000000],
"k": [0b00000000, 0b00110110],
"l": [0b00110000, 0b00000000],
"m": [0b11010100, 0b00010000],
"n": [0b01010000, 0b00010000],
"o": [0b11011100, 0b00000000],
"p": [0b01110000, 0b00000001],
"q": [0b10000110, 0b00000100],
"r": [0b01010000, 0b00000000],
"s": [0b10001000, 0b00100000],
"t": [0b01111000, 0b00000000],
"u": [0b00011100, 0b00000000],
"v": [0b00000100, 0b00100000],
"w": [0b00010100, 0b00101000],
"x": [0b11000000, 0b00101000],
"y": [0b00001100, 0b00100000],
"z": [0b01001000, 0b00001000],
"{": [0b01001001, 0b00001001],
"|": [0b00000000, 0b00010010],
"}": [0b10001001, 0b00100100],
"~": [0b00100000, 0b00000101]
};
const FILL = [0b11111111, 0b00111111];
const charArray = CHARS[char];
return charArray && charArray.map(value => value);
}
function format(numberOrStringOrArray) {
const stringRep = Array.isArray(numberOrStringOrArray) ? numberOrStringOrArray.join('') : numberOrStringOrArray.toString();
const chars = stringRep.split('');
const dotIndicies = chars.map((value, i) => value === '.' ? i : -1).filter(value => value != -1);
const bitmap = chars.map(charToFourteenSegment);
dotIndicies.forEach(value => bitmap[value - 1][1] |= 1 << 6);
return fixBitmap(bitmap);
}
export default function connect(setup, write, address, brightness) {
return createMatrix(setup, format, write, address, brightness);
}