-
Notifications
You must be signed in to change notification settings - Fork 1
/
oled-bitmapper.html
290 lines (251 loc) · 10.7 KB
/
oled-bitmapper.html
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>OLED BitMap Generator</title>
<meta charset="utf-8">
<!--
Bitmap array generator for SSD1306 and possibly other microcontroller displays.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
-->
<script>
var bitmapArray = [];
var bitmapRows = 8;
var bitmapCols = 8;
var bytesPerRow = 1; // Set whenever Columns dropdown is changed. 1 for 8 cols wide, 2 for 16 cols wide, etc.
function clearDisplays() {
document.getElementById('magnified').getContext('2d').fillStyle = 'black';
document.getElementById('magnified').getContext('2d').fillRect(0, 0, 128, 128);
document.getElementById('preview32').getContext('2d').fillStyle = 'black';
document.getElementById('preview32').getContext('2d').fillRect(0, 0, 128, 32);
document.getElementById('preview64').getContext('2d').fillStyle = 'black';
document.getElementById('preview64').getContext('2d').fillRect(0, 0, 128, 64);
}
function clearArray() {
bitmapArray = [];
document.getElementById('array-declaration').value = '';
displayArray();
}
function setDimensions(rows, cols) {
bitmapRows = rows;
bitmapCols = cols;
bytesPerRow = Math.floor(cols / 8);
console.log(`Setting dimensions of: rows = ${rows}, cols = ${cols}, and bytes per row = ${bytesPerRow}`);
console.log(`Allocating ${Math.ceil(rows * bytesPerRow / 16)} rows in textarea`);
document.getElementById('array-declaration').rows = Math.ceil(rows * bytesPerRow / 16);
}
function generateGrid() {
document.getElementById('grid-input').innerHTML = '';
for (let row = 0; row < bitmapRows; row++) {
if (row % 8 == 0 && row != 0) {
document.getElementById('grid-input').innerHTML += '<br>\n';
}
for (let col = 0; col < bitmapCols; col++) {
if (col % 8 == 0) { // Each element of the array holds 8 columns of pixels.
bitmapArray.push(0);
document.getElementById('grid-input').innerHTML += ' '; // Create a visual break. Useful for 16px and larger.
}
document.getElementById('grid-input').innerHTML += `<input id="r${row}c${col}" onclick="updateArray(${row}, ${col}, this.checked); updateCanvas(${row}, ${col}, this.checked);" type="checkbox">`;
}
document.getElementById('grid-input').innerHTML += '<br>\n';
}
}
function displayArray() {
document.getElementById('array-declaration').value = '';
for (let i = 0; i < bitmapArray.length; i++) {
document.getElementById('array-declaration').value += bitmapArray[i];
if (i < bitmapArray.length - 1) { // No comma at end of array declaration.
document.getElementById('array-declaration').value += ',';
}
if ((i + 1) % 16 == 0 && i != 0) { // Newline every 16 bytes to help readability.
document.getElementById('array-declaration').value += '\n';
}
}
}
function updateArray(row, col, value) {
let byteNum = Math.floor(col / 8);
let bitNum = 8 - col % 8 - 1;
let arrayElement = row * bytesPerRow + byteNum;
console.log(`Setting grid pixel (${row}, ${col}) to ${value}`);
console.log(`Grid row: ${row}, Byte Number: ${byteNum}, Bit Number: ${bitNum}, Array Element: ${arrayElement}`);
if (value) {
bitmapArray[arrayElement] += Math.pow(2, bitNum);
}
else {
bitmapArray[arrayElement] -= Math.pow(2, bitNum);
}
displayArray();
}
function updateCanvas(row, col, value) {
if (value) {
document.getElementById("magnified").getContext("2d").fillStyle = 'aqua';
document.getElementById("preview32").getContext("2d").fillStyle = 'aqua';
document.getElementById("preview64").getContext("2d").fillStyle = 'aqua';
}
else {
document.getElementById("magnified").getContext("2d").fillStyle = 'black';
document.getElementById("preview32").getContext("2d").fillStyle = 'black';
document.getElementById("preview64").getContext("2d").fillStyle = 'black';
}
console.log(`Preview canvas pixel (${row}, ${col}) set to ${value}`);
document.getElementById("magnified").getContext("2d").fillRect(col * 4, row * 4, 4, 4);
document.getElementById("preview32").getContext("2d").fillRect(col, row, 1, 1);
document.getElementById("preview64").getContext("2d").fillRect(col, row, 1, 1);
}
function updateGrid(row, col, value) {
console.log(`Grid checkbox with id='r${row}c${col}' set to ${value}`)
document.getElementById(`r${row}c${col}`).checked = value;
}
function decodeArray(arrayCSV) {
generateGrid();
bitmapArray = JSON.parse(`[${arrayCSV}]`);
console.log('Array declaration changed. New value is:')
console.log(JSON.stringify(bitmapArray));
let col = 0;
for (let byte = 0; byte < bitmapArray.length; byte++) {
console.log(`Processing bitmapArray[${byte}] = ${bitmapArray[byte]}`)
let row = Math.floor(byte / bytesPerRow);
for (let bitMask = 128; bitMask >= 1; bitMask /= 2) { // Loops through 128, 64, 32, 16, 8, 4, 2, 1.
console.log(`Applying bit mask ${bitMask} to isolate row: ${row}, col: ${col}`);
if (bitmapArray[byte] & bitMask) {
updateGrid(row, col, true);
updateCanvas(row, col, true);
}
else {
updateGrid(row, col, false);
updateCanvas(row, col, false);
}
col++;
if (col >= 8 * bytesPerRow) {
col = 0;
}
}
}
let padBytes = bitmapRows * bytesPerRow - bitmapArray.length; // Be sure to fill the entire Row x Col grid so editing won't encounter undefineds.
console.log(`Input stopped after ${bitmapArray.length} bytes, leaving ${padBytes} array elements to pad with zeros.`);
for (let byte = 0; byte < padBytes; byte++) {
bitmapArray.push(0);
}
}
function changeDeclarationStyle() {
if (document.getElementById('c').checked) {
document.getElementById('array-opener').innerHTML = 'unsigned char bitmap = {'
document.getElementById('array-closer').innerHTML = '};'
}
else if (document.getElementById('micropython').checked) {
document.getElementById('array-opener').innerHTML = 'bitmap = bytearray(['
document.getElementById('array-closer').innerHTML = '])'
}
}
</script>
<style>
body {
background-color: whitesmoke;
color: black;
font-family: sans-serif;
max-width: 1024px;
margin-left: auto;
margin-right: auto;
}
#array-declaration {
border: 1px dotted gray;
margin-left: 2em;
}
aside {
float: right;
margin-left: 1em;
width: 150px;
}
canvas {
border: 5px solid green;
}
fieldset {
margin-top: 1em;
padding: 1em;
}
main {
width: calc(100% - 200px);
}
.right-justified {
text-align: right;
}
</style>
</head>
<body onload="clearDisplays(); generateGrid();">
<header>
<h1>OLED Bitmap Generator</h1>
</header>
<!-- Having aside before main makes it easier to float to the top right. -->
<aside>
<p>4x Magnification</p>
<canvas id="magnified" width="128" height="128"></canvas>
<p>128 x 32 Display</p>
<canvas id="preview32" width="128" height="32"></canvas>
<p>128 x 64 Display</p>
<canvas id="preview64" width="128" height="64"></canvas>
</aside>
<main>
<form>
<div class="right-justified">
<button type="button"
onclick="clearArray(); clearDisplays(); setDimensions(Number(document.getElementById('rows').value), Number(document.getElementById('cols').value)); generateGrid();">Clear</button>
</div>
<fieldset>
<legend>Bitmap Dimensions</legend>
<label for="rows">Rows</label>
<select id="rows"
onchange="clearArray(); clearDisplays(); setDimensions(Number(document.getElementById('rows').value), Number(document.getElementById('cols').value)); generateGrid();">
<option>4</option>
<option selected>8</option>
<option>12</option>
<option>16</option>
<option>24</option>
<option>32</option>
</select>
<label for="cols">Columns</label>
<select id="cols"
onchange="clearArray(); clearDisplays(); setDimensions(Number(document.getElementById('rows').value), Number(document.getElementById('cols').value)); generateGrid();">
<option selected>8</option>
<option>16</option>
<option>24</option>
<option>32</option>
</select>
</fieldset>
<fieldset>
<legend>Array Declaration</legend>
<label id="array-opener" for="array-declaration">unsigned char bitmap = {</label>
<br>
<textarea id="array-declaration" onchange="decodeArray(this.value);" onclick="this.select();" rows="2"
cols="80"></textarea><br>
<span id="array-closer">};</span>
<br><br>
Declaration Style:
<input type="radio" id="c" name="language" onchange="changeDeclarationStyle()" value="C" checked><label for="c">C / C++</label>
<input type="radio" id="micropython" name="language" onchange="changeDeclarationStyle()" value="MicroPython"><label for="micropython">MicroPython (using framebuf.MONO_HLSB format.)</label>
</fieldset>
<fieldset>
<legend>Direct Edit</legend>
<span id="grid-input"></span>
</fieldset>
</form>
</main>
</body>
</html>