This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (116 loc) · 3.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
module.exports = (function() {
'use strict';
const omegaOled = require('onion-omega-oled');
const chars = require('./lib/chars');
const charSize = 16;
const byteSize = 8;
const displayWidth = 128;
const displayHeight = 64;
const cols = displayWidth / charSize;
const rows = displayHeight / charSize;
const totalChars = cols * rows;
class OmegaOledText {
constructor() {
this._fontCache = {};
this._renderCache = [];
this._standbyTimeout = 0;
}
/**
* get matrix for both halves of a given character
* @param char
* @returns {*[]}
*/
_getCharMatrices(char) {
if (!char) {
return false;
}
if (this._fontCache[char]) {
return this._fontCache[char];
}
if (!chars[char]) {
return this._getCharMatrices(' ');
}
const result = [
chars[char].slice(0, chars[char].length / 2),
chars[char].slice(chars[char].length / 2)
];
this._fontCache[char] = result;
return result;
}
_standbyFunction() {
return omegaOled.power(false);
}
init() {
return omegaOled.init().then(() => this._renderCache = ' '.repeat(totalChars).split(''));
}
setStandbyTimeout(secondsOfInactivity) {
this._standbyTimeout = secondsOfInactivity || 0;
if (this._standbyId) {
clearTimeout(this._standbyId);
}
if (secondsOfInactivity) {
this._standbyId = setTimeout(this._standbyFunction, secondsOfInactivity * 1000);
}
return Promise.resolve(secondsOfInactivity);
}
addCharacter(character, byteMatrix) {
if (typeof character !== 'string' || character.length !== 1 || typeof byteMatrix !== 'object' || byteMatrix.length !== charSize * (charSize / byteSize)) {
return false;
}
chars[character] = byteMatrix;
return true;
}
writeText(text, reset) {
this.setStandbyTimeout(this._standbyTimeout);
if (typeof reset === 'undefined') {
reset = true;
}
text = String(text).split('\n')
.map((part, i, arr) => i < arr.length - 1 ? part + ' '.repeat(cols - (part.length % cols)) : part)
.join('')
.substr(0, totalChars)
.split('');
const previousChainModeSetting = omegaOled.chainMode();
omegaOled.chainMode(true);
if (!omegaOled.power()) {
omegaOled.power(true);
}
if (reset) {
omegaOled.cursorPixel(0, 0);
let fillCharacters = totalChars - text.length;
let newRenderCache = text.concat(Array.from(Array(fillCharacters < 0 ? 0 : fillCharacters).keys()).map(() => ' '));
text = newRenderCache.map((char, i) => this._renderCache[i] === char ? false : char);
this._renderCache = newRenderCache;
} else {
this._renderCache = [];
}
let matricesArray;
let needsJump = false;
const specialChars = [false];
for (let row = 0; row < Math.ceil(text.length / cols); row++) {
matricesArray = Array.from(Array(cols).keys())
.map(i => text[(row * cols) + i])
.map(char => char || specialChars.indexOf(char) !== -1 ? char : ' ')
.map(char => specialChars.indexOf(char) !== -1 ? char : this._getCharMatrices(char));
for (let part = 0; part < charSize / byteSize; part++) {
matricesArray
.map(matrices => matrices ? matrices[part] : matrices)
.forEach((matrix, i, matrices) => {
if (matrix) {
if (needsJump) {
omegaOled.cursorPixel((row * (charSize / byteSize)) + part, i * charSize);
needsJump = false;
}
return matrix.forEach(byte => omegaOled.writeByte(byte));
}
needsJump = true;
});
}
}
const executionResult = omegaOled.executeChain();
omegaOled.chainMode(previousChainModeSetting);
return executionResult;
}
}
return new OmegaOledText();
})();