forked from Moddable-OpenSource/moddable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodLpm013m126a.c
525 lines (450 loc) · 13.8 KB
/
modLpm013m126a.c
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* Copyright (c) 2016-2023 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "lpm013m126a.h"
#include "xsmc.h"
#include "xsHost.h"
#include "modGPIO.h"
#include "modSPI.h"
#include "commodettoBitmap.h"
#include "commodettoPixelsOut.h"
#include "mc.xs.h"
#include "mc.defines.h"
#define LPMSIZE (176)
#ifndef MODDEF_LPM013M126A_CS_PORT
#define MODDEF_LPM013M126A_CS_PORT NULL
#endif
#ifndef MODDEF_LPM013M126A_HZ
#define MODDEF_LPM013M126A_HZ (2500000)
#endif
#ifndef MODDEF_LPM013M126A_WIDTH
#define MODDEF_LPM013M126A_WIDTH LPMSIZE
#endif
#ifndef MODDEF_LPM013M126A_HEIGHT
#define MODDEF_LPM013M126A_HEIGHT LPMSIZE
#endif
#ifndef MODDEF_LPM013M126A_DITHER
#define MODDEF_LPM013M126A_DITHER (0)
#endif
#if kCommodettoBitmapFormat != kCommodettoBitmapRGB332
#error rgb332 pixels required
#endif
#if (MODDEF_LPM013M126A_WIDTH != LPMSIZE) || (MODDEF_LPM013M126A_HEIGHT != LPMSIZE)
#error invalid dimensions
#endif
/*
LPM013M126A ESP8266
DISPLAY MCU
---------- ----------
SCLK GPIO 14
SI GPIO 13
SCS GPIO 4
DISP 3V3
GND GND
VIN 3V3
*/
#define redMask 0xE0 // 0b11100000
#define greenMask 0x1C // 0b00011100
#define blueMask 0x3 // 0b00000011
#define redThresh 4
#define greenThresh 4
#define blueThresh 2
#ifdef MODDEF_LPM013M126A_CS_PIN
#define SCREEN_CS_DEACTIVE modGPIOWrite(&lpm->cs, 0)
#define SCREEN_CS_ACTIVE modGPIOWrite(&lpm->cs, 1)
#define SCREEN_CS_INIT modGPIOInit(&lpm->cs, MODDEF_LPM013M126A_CS_PORT, MODDEF_LPM013M126A_CS_PIN, kModGPIOOutput); \
SCREEN_CS_DEACTIVE
#else
#define SCREEN_CS_DEACTIVE
#define SCREEN_CS_ACTIVE
#define SCREEN_CS_INIT
#endif
#if !MODDEF_LPM013M126A_DITHER
// deliberately not setting ICACHE_XS6RO_ATTR to allow fast access on ESP8266
static uint8_t gRGB332to111[256] = {
0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07
};
#endif
//Host data record.
struct lpm013m126aRecord {
PixelsOutDispatch dispatch;
modSPIConfigurationRecord spiConfig;
modGPIOConfigurationRecord cs;
uint16_t onRow; //store what scanline row we are on
uint16_t bufferSize;
uint8_t *pixelBuffer;
uint8_t updateCycle; //for toggling COM bit to avoid DC Bias
#if MODDEF_LPM013M126A_DITHER
uint8_t ditherPhase;
int8_t ditherA[MODDEF_LPM013M126A_WIDTH + 4];
int8_t ditherB[MODDEF_LPM013M126A_WIDTH + 4];
#endif
};
typedef struct lpm013m126aRecord lpm013m126aRecord;
typedef lpm013m126aRecord *lpm013m126a;
static void lpm013m126aChipSelect(uint8_t active, modSPIConfiguration config);
static void lpm_clear(lpm013m126a lpm);
static void lpm013m126aBegin(void *refcon, CommodettoCoordinate x, CommodettoCoordinate y, CommodettoDimension w, CommodettoDimension h);
static void lpm013m126aEnd(void *refcon);
static void lpm013m126aSend(PocoPixel *pixels, int byteLength, void *refcon);
static void lpm013m126aAdaptInvalid(void *refcon, CommodettoRectangle r);
static const PixelsOutDispatchRecord gPixelsOutDispatch ICACHE_RODATA_ATTR = {
lpm013m126aBegin,
lpm013m126aEnd,
lpm013m126aEnd,
lpm013m126aSend,
lpm013m126aAdaptInvalid
};
void xs_LPM013M126A(xsMachine *the){
lpm013m126a lpm;
lpm = c_calloc(1, sizeof(lpm013m126aRecord));
if (!lpm)
xsUnknownError("out of memory");
xsmcSetHostData(xsThis, lpm);
lpm->dispatch = (PixelsOutDispatch)&gPixelsOutDispatch;
lpm->onRow = 0;
lpm->bufferSize = 0;
SCREEN_CS_INIT;
modSPIConfig(lpm->spiConfig, MODDEF_LPM013M126A_HZ, MODDEF_LPM013M126A_SPI_PORT,
MODDEF_LPM013M126A_CS_PORT, MODDEF_LPM013M126A_CS_PIN, lpm013m126aChipSelect);
modSPIInit(&lpm->spiConfig);
lpm_clear(lpm);
}
void xs_lpm013m126a_get_c_dispatch(xsMachine *the) {
xsResult = xsThis;
}
void xs_lpm013m126a_begin(xsMachine *the){
lpm013m126a lpm = xsmcGetHostData(xsThis);
uint16_t xMin = xsmcToInteger(xsArg(0));
uint16_t yMin = xsmcToInteger(xsArg(1));
uint16_t xMax = xMin + xsmcToInteger(xsArg(2));
uint16_t yMax = yMin + xsmcToInteger(xsArg(3));
if ((0 != xMin) || (MODDEF_LPM013M126A_WIDTH != xMax))
xsUnknownError("partial updates unsupported");
lpm013m126aBegin(lpm, xMin, yMin, xMax - xMin, yMax - yMin);
}
void lpm013m126aBegin(void *refcon, CommodettoCoordinate x, CommodettoCoordinate y, CommodettoDimension w, CommodettoDimension h)
{
lpm013m126a lpm = refcon;
lpm->onRow = y + 1;
#if MODDEF_LPM013M126A_DITHER
c_memset(lpm->ditherA, 0, sizeof(lpm->ditherA));
c_memset(lpm->ditherB, 0, sizeof(lpm->ditherB));
lpm->ditherPhase = 0;
#endif
}
void xs_lpm013m126a_send(xsMachine *the){
lpm013m126a lpm = xsmcGetHostData(xsThis);
int argc = xsmcArgc;
const uint8_t *data;
xsUnsignedValue count;
xsmcGetBufferReadable(xsArg(0), (void **)&data, &count);
if (argc > 1) {
xsIntegerValue offset = xsmcToInteger(xsArg(1));
if ((xsUnsignedValue)offset >= count)
xsUnknownError("bad offset");
data += offset;
count -= offset;
if (argc > 2) {
xsIntegerValue c = xsmcToInteger(xsArg(2));
if (c > count)
xsUnknownError("bad count");
count = c;
}
}
lpm013m126aSend((PocoPixel *)data, count, lpm);
}
#define PIXEL_BYTES_PER_LINE (((MODDEF_LPM013M126A_WIDTH * 3) + 7) >> 3)
void lpm013m126aSend(PocoPixel *data, int count, void *refcon)
{
lpm013m126a lpm = refcon;
int lines;
int size;
uint8_t *out;
if (count < 0) count = -count;
modSPIActivateConfiguration(NULL);
lines = count / MODDEF_LPM013M126A_WIDTH;
size = 2 + ((PIXEL_BYTES_PER_LINE + 2) * lines);
if (size > lpm->bufferSize){
if (lpm->pixelBuffer)
c_free(lpm->pixelBuffer);
lpm->pixelBuffer = c_malloc(size);
if (!lpm->pixelBuffer)
return;
lpm->bufferSize = size;
}
lpm->updateCycle = !(lpm->updateCycle);
out = lpm->pixelBuffer;
*out++ = (lpm->updateCycle ? DATA_UPDATE_3BIT_COMH : DATA_UPDATE_3BIT_COML);
*out++ = lpm->onRow;
//Prader Pixel Packing
uint32_t fourPixels;
uint8_t outByte;
/*
* Dithering: Filter Lite
* X (1/2)
* (1/4) (1/4)
*/
#if MODDEF_LPM013M126A_DITHER
int8_t *thisLineErrors, *nextLineErrors;
uint8_t thisPixel;
uint8_t toAdd;
//@@ assumes 8 lines....
for (int lineIndex = 0; lineIndex < 8; lineIndex++) { // 8 lines at a time
if (lpm->ditherPhase) {
c_memset(lpm->ditherB, 0, sizeof(lpm->ditherB));
thisLineErrors = lpm->ditherA + 2;
nextLineErrors = lpm->ditherB + 2;
lpm->ditherPhase = 0;
}
else {
c_memset(lpm->ditherA, 0, sizeof(lpm->ditherA));
thisLineErrors = lpm->ditherB + 2;
nextLineErrors = lpm->ditherA + 2;
lpm->ditherPhase = 1;
}
for (int i = 0; i < MODDEF_LPM013M126A_WIDTH; i += 8) { // 8 pixels per loop iteration
fourPixels = *(uint32_t *)data;
data += 4;
outByte = 0;
toAdd = 128;
// RGBRGBRG, BRGB...
for (int j=0; j<4; j++) {
// R
thisPixel = ((fourPixels & redMask) + (thisLineErrors[0] & redMask)) >> 5;
if (thisPixel >= redThresh) {
outByte |= toAdd;
thisPixel -= 7;
}
thisLineErrors[ 1] += (thisPixel >> 1) << 5;
nextLineErrors[-1] += (thisPixel >> 2) << 5;
nextLineErrors[ 0] += (thisPixel >> 2) << 5;
toAdd = toAdd >> 1;
// G
thisPixel = ((fourPixels & greenMask) + (thisLineErrors[0] & greenMask)) >> 2;
if (thisPixel >= greenThresh) {
outByte |= toAdd;
thisPixel -= 7;
}
thisLineErrors[ 1] += (thisPixel >> 1) << 2;
nextLineErrors[-1] += (thisPixel >> 2) << 2;
nextLineErrors[ 0] += (thisPixel >> 2) << 2;
toAdd = toAdd >> 1;
if (toAdd == 0) { // end of RGBRGBRG
*out++ = outByte;
outByte = 0;
toAdd = 128;
}
// B
thisPixel = (fourPixels & blueMask) + (thisLineErrors[0] & blueMask);
if (thisPixel >= blueThresh) {
outByte |= toAdd;
thisPixel -= 3;
}
thisLineErrors[ 1] += (thisPixel >> 1); // no need to edit nextLineErrors since blue only starts with 2 bits
toAdd = toAdd >> 1;
fourPixels = fourPixels >> 8;
thisLineErrors++;
nextLineErrors++;
}
fourPixels = *(uint32_t *)data;
data += 4;
// ...RGBR, GBRGBRGB
for (int j=0; j<4; j++) {
// R
thisPixel = ((fourPixels & redMask) + (thisLineErrors[0] & redMask)) >> 5;
if (thisPixel >= redThresh) {
outByte |= toAdd;
thisPixel -= 7;
}
thisLineErrors[ 1] += (thisPixel >> 1) << 5;
nextLineErrors[-1] += (thisPixel >> 2) << 5;
nextLineErrors[ 0] += (thisPixel >> 2) << 5;
toAdd = toAdd >> 1;
if (toAdd == 0) { // end of BRGBRGBR
*out++ = outByte;
outByte = 0;
toAdd = 128;
}
// G
thisPixel = ((fourPixels & greenMask) + (thisLineErrors[0] & greenMask)) >> 2;
if (thisPixel >= greenThresh) {
outByte |= toAdd;
thisPixel -= 7;
}
thisLineErrors[ 1] += (thisPixel >> 1) << 2;
nextLineErrors[-1] += (thisPixel >> 2) << 2;
nextLineErrors[ 0] += (thisPixel >> 2) << 2;
toAdd = toAdd >> 1;
// B
thisPixel = (fourPixels & blueMask) + (thisLineErrors[0] & blueMask);
if (thisPixel >= blueThresh) {
outByte |= toAdd;
thisPixel -= 3;
}
thisLineErrors[ 1] += (thisPixel >> 1);
toAdd = toAdd >> 1;
fourPixels = fourPixels >> 8;
thisLineErrors++;
nextLineErrors++;
}
*out++ = outByte;
}
*out++ = 0;
lpm->onRow++;
*out++ = (uint8_t)lpm->onRow;
}
#else
while (lines--) {
for (int i = MODDEF_LPM013M126A_WIDTH / 8; i > 0; i -= 1, out += 3, data += 8) { // 8 pixels per loop iteration
uint8_t t;
// RGBRGBRG
t = *(gRGB332to111 + data[0]);
outByte = t << 5;
t = *(gRGB332to111 + data[1]);
outByte |= t << 2;
t = *(gRGB332to111 + data[2]);
outByte |= t >> 1;
out[0] = outByte;
// BRGBRGBR
outByte = t << 7;
t = *(gRGB332to111 + data[3]);
outByte |= t << 4;
t = *(gRGB332to111 + data[4]);
outByte |= t << 1;
t = *(gRGB332to111 + data[5]);
outByte |= t >> 2;
out[1] = outByte;
// GBRGBRGB
outByte = t << 6;
t = *(gRGB332to111 + data[6]);
outByte |= t << 3;
t = *(gRGB332to111 + data[7]);
outByte |= t;
out[2] = outByte;
}
*out++ = 0;
lpm->onRow++;
*out++ = (uint8_t)lpm->onRow;
}
#endif
modSPITx(&lpm->spiConfig, lpm->pixelBuffer, size);
}
void xs_lpm013m126a_end(xsMachine *the){
lpm013m126a lpm = xsmcGetHostData(xsThis);
lpm013m126aEnd(lpm);
}
void lpm013m126aEnd(void *refcon)
{
modSPIActivateConfiguration(NULL);
}
void xs_LPM013M126A_destructor(void *data){
if (data){
lpm013m126a lpm = data;
if ( lpm->pixelBuffer )
c_free(lpm->pixelBuffer);
modGPIOUninit(&lpm->cs);
c_free(data);
}
}
void lpm013m126aHold(lpm013m126a lpm){
lpm->updateCycle = !(lpm->updateCycle);
uint8_t mode[2] = {0,0};
if (lpm->updateCycle){
mode[0] = NO_UPDATE_COMH;
}else{
mode[0] = NO_UPDATE_COML;
}
modSPITx(&lpm->spiConfig, mode, 2);
modSPIActivateConfiguration(NULL);
}
static void lpm_clear(lpm013m126a lpm){
uint8_t mode[2] = {0, 0};
lpm->updateCycle = !(lpm->updateCycle);
if (lpm->updateCycle){
mode[0] = ALL_CLEAR_COMH;
}else{
mode[0] = ALL_CLEAR_COML;
}
modSPITx(&lpm->spiConfig, mode, 2);
modSPIActivateConfiguration(NULL);
}
void xs_lpm013m126a_adaptInvalid(xsMachine *the)
{
lpm013m126a lpm = xsmcGetHostData(xsThis);
CommodettoRectangle invalid = xsmcGetHostChunk(xsArg(0));
lpm013m126aAdaptInvalid(lpm, invalid);
}
void lpm013m126aAdaptInvalid(void *refcon, CommodettoRectangle r)
{
lpm013m126a lpm = refcon;
r->x = 0;
r->w = MODDEF_LPM013M126A_WIDTH;
}
void lpm013m126aChipSelect(uint8_t active, modSPIConfiguration config)
{
lpm013m126a lpm = (lpm013m126a)(((char *)config) - offsetof(lpm013m126aRecord, spiConfig));
if (active)
SCREEN_CS_ACTIVE;
else
SCREEN_CS_DEACTIVE;
}
void xs_lpm013m126a_get_pixelFormat(xsMachine *the)
{
xsmcSetInteger(xsResult, kCommodettoBitmapFormat);
}
void xs_lpm013m126a_get_width(xsMachine *the)
{
xsmcSetInteger(xsResult, MODDEF_LPM013M126A_WIDTH);
}
void xs_lpm013m126a_get_height(xsMachine *the)
{
xsmcSetInteger(xsResult, MODDEF_LPM013M126A_HEIGHT);
}