-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
188 lines (159 loc) · 6.57 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
let width = 16;
let height = 2;
let panelWidth = 5;
let activeSegments = [];
let updateCanvasSize = (w, h) => {
document.getElementById("copyAlert").style.opacity = 0;
width = w;
height = h;
panelWidth = w > 16 ? 4 : 5;
generateSegments();
if (!codeStyle.checked) {
document.getElementById("setup").innerText = `#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // RS, E, D4, D5, D6, D7
void setup() {
lcd.begin(${width}, ${height});
image();
}
void loop() {}
`
}
}
let updateColors = (color) => {
document.documentElement.style.setProperty('--panelBg', 'var(--panelBg' + color + ')');
document.documentElement.style.setProperty('--activePixel', 'var(--activePixel' + color + ')');
document.documentElement.style.setProperty('--inactivePixel', 'var(--inactivePixel' + color + ')');
}
let copyCode = () => {
let code = document.getElementById("bitmap");
code.style.whiteSpace = `pre-line`;
document.getElementById("dummy").value = code.innerText.trim().replace(/\n\n/gm, '\n');
document.querySelector("#dummy").select();
document.execCommand("copy");
code.style.whiteSpace = `nowrap`;
document.getElementById("copyAlert").style.opacity = 0.8;
}
let replaceAt = (string, index, replace) => {
return string.substring(0, index) + replace + string.substring(index + 1);
}
let setCursorAndWrite = (i, j) => {
document.getElementById("createChar").innerText = "";
document.getElementById("setCursor").innerText = "";
let bytes = document.getElementById("bytes").innerText;
let images = bytes.match(/(image\d\d)/gm);
// createChar
for (let i = 0; i < images.length; i++) {
document.getElementById("createChar").innerText += `lcd.createChar(${i}, ${images[i]});
`
}
// setCursorAndWrite
for (i in images) {
let segmentIndex = images[i].match(/\d\d/)[0] - 1;
let column = segmentIndex < width ? segmentIndex : segmentIndex - Math.floor(segmentIndex / width) * width;
let row = Math.floor(segmentIndex / width);
document.getElementById("setCursor").innerText += `lcd.setCursor(${column}, ${row});
lcd.write(byte(${i}));
`
}
}
let generateSegmentCode = (i, j, pixel) => {
if (document.getElementById(`imageByteText${i}`) === null) {
let emptySegmentCode;
if (i < (10 - 1)) {
emptySegmentCode = `byte image0${i + 1}[8] = {B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000};
`
} else {
emptySegmentCode = `byte image${i + 1}[8] = {B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000};
`
}
const text = document.createElement("span");
text.setAttribute("id", `imageByteText${i}`);
text.innerText = emptySegmentCode;
let bytes = document.getElementById("bytes");
bytes.appendChild(text);
}
let workingLine = document.getElementById(`imageByteText${i}`);
workingLine = workingLine.innerText.substr(19, 62);
workingLine = workingLine.split(', ').join('');
workingLine = workingLine.split('B').join('');
if (workingLine[j] == '0') {
workingLine = replaceAt(workingLine, j, '1');
pixel.style.backgroundColor = "var(--activePixel)";
}
else if (workingLine[j] == '1') {
workingLine = replaceAt(workingLine, j, '0');
pixel.style.backgroundColor = "var(--inactivePixel)";
}
workingLine = workingLine.match(/.{1,5}/g).join(', B');
workingLine = 'B' + workingLine;
let oldLine = document.getElementById(`imageByteText${i}`).innerText;
document.getElementById(`imageByteText${i}`).innerText = oldLine.replace(oldLine.substr(19, 62), workingLine);
checkForEmptySegments(i);
}
let checkForEmptySegments = (i) => {
emptyString = document.getElementById(`imageByteText${i}`).innerText.includes(`B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000`);
if (emptyString === true) {
document.getElementById(`imageByteText${i}`).remove();
activeSegments = activeSegments.filter(item => item !== i);
}
}
let generateSegments = () => {
let canvas = document.getElementById("canvas");
let bytes = document.getElementById("bytes");
canvas.style.padding = `${panelWidth / 10}vw`;
document.getElementById("createChar").innerHTML = "";
document.getElementById("setCursor").innerHTML = "";
canvas.innerHTML = "";
bytes.innerHTML = "";
document.getElementById("canvas").style.width = `${width * panelWidth + width * (panelWidth / 5)}vw`;
for (let i = 0; i < width * height; i++) {
const segment = document.createElement("div");
segment.setAttribute("id", "panel");
segment.style.width = `${panelWidth}vw`;
segment.style.height = `${8 * panelWidth / 5}vw`;
segment.style.margin = `${panelWidth / 10}vw`;
for (let j = 0; j < 5 * 8; j++) {
const pixel = document.createElement("pixel");
pixel.setAttribute("id", "pixel");
pixel.style.width = `${panelWidth / 5}vw`;
pixel.style.height = `${panelWidth / 5}vw`;
pixel.onclick = () => {
document.getElementById("copyAlert").style.opacity = 0;
if (activeSegments.includes(i) || activeSegments.length < 8) {
activeSegments.includes(i) ? null : activeSegments.push(i);
generateSegmentCode(i, j, pixel);
setCursorAndWrite(i, j);
checkForEmptySegments(i);
activeSegments.includes(i) ? null : activeSegments.push(i);
}
}
segment.appendChild(pixel);
}
canvas.appendChild(segment);
}
}
let updateSetupCode = () => {
let codeStyleCheckBox = document.querySelector('input[id="codeStyle"]');
codeStyleCheckBox.addEventListener('change', () => {
document.getElementById("copyAlert").style.opacity = 0;
if (codeStyleCheckBox.checked) {
document.getElementById("setup").innerText = ``;
}
else {
document.getElementById("setup").innerText = `#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // RS, E, D4, D5, D6, D7
void setup() {
lcd.begin(${width}, ${height});
image();
}
void loop() {}
`
}
});
}
window.onload = () => {
updateCanvasSize(width, height);
updateSetupCode();
document.getElementById("bitmap").style.width = `calc(${(width * 5 + width) / 2}vw - 20px)`;
document.getElementById("bitmap").style.height = `${document.getElementById("canvas").offsetHeight - 130}px`;
};