-
Notifications
You must be signed in to change notification settings - Fork 4
/
LCD_UC1609.cpp
531 lines (472 loc) · 16.3 KB
/
LCD_UC1609.cpp
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
526
527
528
529
530
531
/*
version 1.0.0 June 2020
"Revised BSD License",
Copyright (c) 2020, Lukas Vyhnalek aka KiLLA
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "LCD_UC1609.h"
////////////////////////////////////////////////////////////////
// Send a byte to the display
void LCD_UC1609::uc1609_data (uint8_t d) {
(void)SPI.transfer(d);
}
// Send a command to the display
void LCD_UC1609::uc1609_command (uint8_t command, uint8_t value) {
#ifdef __AVR__
PORTB &= ~(1 << dc);
uc1609_data(command | value);
PORTB |= (1 << dc);
#else
digitalWrite(dc, LOW);
uc1609_data(command | value);
digitalWrite(dc, HIGH);
#endif
}
LCD_UC1609 :: LCD_UC1609(int8_t _dc, int8_t _rst, int8_t _cs, int8_t _SRAM_cs) : Adafruit_GFX(LCD_WIDTH, LCD_HEIGHT) {
// check for define and set pin numbers
#ifdef __AVR__
dc = _dc % 8;
rst = _rst % 8;
cs = _cs % 8;
SRAM_cs = _SRAM_cs % 8;
#else
dc = _dc;
rst = _rst;
cs = _cs;
SRAM_cs = _SRAM_cs;
#endif
// check for define and set buffer size
#ifndef USE_ADVANCED_BUFFERING
bufferWidth = LCD_WIDTH;
bufferHeight = LCD_HEIGHT;
#endif
}
// https://www.arduino.cc/en/reference/SPI
void LCD_UC1609::begin () {
#ifdef __AVR__
DDRB = 1 << dc | 1 << cs | 1 << rst ; //
if (SRAM_cs != -1) {
DDRD = 1 << SRAM_cs;
}
#else
pinMode(dc, OUTPUT);
pinMode(cs, OUTPUT);
pinMode(rst, OUTPUT);
if (SRAM_cs != -1) {
pinMode (SRAM_cs, OUTPUT);
}
#endif
if (true) {
SPI.begin();
#ifdef SPI_HAS_TRANSACTION
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0)); // FAST
//SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0)); // slow
#else
SPI.setClockDivider (1); // FAST
//SPI.setClockDivider (8); // slow
#endif
}
/* esp 8266 TESTING only!!! and maybe esp32... 22/5/2020 TODO: TESTING!
// https://github.com/esp8266/Arduino/issues/2624
SPI.setFrequency(32000000L);
//SPI.setFrequency(24000000L);
//SPI.setFrequency(16000000L);
SPI.setBitOrder(MSBFIRST);
*/
// DISPLAY POWER ON SEQUENCE
initDisplay();
// INIT SRAM
initSRAM(); // check for chip select pin is done in the init, prepared for hotswap
}
void LCD_UC1609::initDisplay() {
// POWER ON SEQUENCE
#ifdef __AVR__
PORTB |= (1 << dc);
#else
digitalWrite(dc, HIGH);
#endif
LCD_deselect();
hardwareReset();
LCD_select();
//uc1609_command(UC160X_SYS_RESET, 0); // not required to do sys reset
//delay(100);
uc1609_command(UC160X_SET_MR_TC, 0); // set the multiplex ratio and temperature compensation
uc1609_command(UC160X_SET_AC, B00000010); // set RAM address control
uc1609_command(UC160X_SET_BR, 3); // set bias ratio to default
uc1609_command(UC160X_SET_POWER_CONTROL, 0); // set power control
delay(100);
uc1609_command(UC160X_SET_GN_PM, 0); // set gain and potentiometer - double byte command
uc1609_command(UC160X_SET_GN_PM, UC1608_DEFAULT_GN_PM); // set gain and potentiometer
uc1609_command(UC160X_SET_DISPLAY_ENABLE, 1); // display enable
uc1609_command(UC160X_SET_MAP_CONTROL, LCD_ROTATION_NORMAL); // set map control bit 3: MY, bit 2: MX, bit 1 unset, bit 0: MSF
LCD_deselect();
}
void LCD_UC1609::LCD_select () {
#ifdef __AVR__
PORTB &= ~(1 << cs);
#else
digitalWrite(cs, LOW);
#endif
}
void LCD_UC1609::LCD_deselect () {
#ifdef __AVR__
PORTB |= (1 << cs);
#else
digitalWrite(cs, HIGH);
#endif
}
void LCD_UC1609::hardwareReset () {
#ifdef __AVR__
PORTB &= ~(1 << rst);
delay(150);
PORTB |= (1 << rst);
#else
digitalWrite(rst, LOW);
delay(150);
digitalWrite(rst, HIGH);
#endif
}
void LCD_UC1609::displayEnable (uint8_t b) {
LCD_select();
uc1609_command(UC160X_SET_DISPLAY_ENABLE, b);
LCD_deselect();
}
void LCD_UC1609::rotation(uint8_t b) {
LCD_select();
b = (b == 0) ? LCD_ROTATION_NORMAL : LCD_ROTATION_FLIP;
uc1609_command(UC160X_SET_MAP_CONTROL, b);
LCD_deselect();
}
void LCD_UC1609::invertDisplay (uint8_t b) {
LCD_select();
uc1609_command(UC1609_SET_INVERSE_DISPLAY, b);
LCD_deselect();
}
void LCD_UC1609::allPixelsOn (uint8_t i) {
LCD_select();
uc1609_command(UC1609_SET_ALL_PIXEL_ON, i);
LCD_deselect();
}
void LCD_UC1609::drawPixel(int16_t x, int16_t y, uint16_t color) {
#ifdef USE_ADVANCED_BUFFERING
// do offscreen check
if ((x < 0) || (x >= this->selectedBuffer->width) || (y < 0) || (y >= this->selectedBuffer->height)) {
return;
}
uint16_t tc = (this->selectedBuffer->width * (y >> 3)) + x; //
switch (this->selectedBuffer->type) {
case TYPE_INTERNAL_V:
switch (color)
{
case WHITE: this->selectedBuffer->bitmap[tc] |= (1 << (y & 7)); break;
case BLACK: this->selectedBuffer->bitmap[tc] &= ~(1 << (y & 7)); break;
case INVERSE: this->selectedBuffer->bitmap[tc] ^= (1 << (y & 7)); break;
}
break;
case TYPE_SRAM_V:
switch (color)
{
uint8_t data;
case WHITE: data = SRAM_read_byte(this->selectedBuffer->address + tc); data |= (1 << (y & 7)); SRAM_write_byte(this->selectedBuffer->address + tc, data); break;
case BLACK: data = SRAM_read_byte(this->selectedBuffer->address + tc); data &= ~(1 << (y & 7)); SRAM_write_byte(this->selectedBuffer->address + tc, data); break;
case INVERSE: data = SRAM_read_byte(this->selectedBuffer->address + tc); data ^= (1 << (y & 7)); SRAM_write_byte(this->selectedBuffer->address + tc, data); break;
}
}
#else
// do offscreen check
if ((x < 0) || (x >= this->bufferWidth) || (y < 0) || (y >= this->bufferHeight)) {
return;
}
uint16_t tc = (this->bufferWidth * (y >> 3)) + x; //
switch (color)
{
case WHITE: this->buffer[tc] |= (1 << (y & 7)); break;
case BLACK: this->buffer[tc] &= ~(1 << (y & 7)); break;
case INVERSE: this->buffer[tc] ^= (1 << (y & 7)); break;
}
#endif
}
void LCD_UC1609::display() {
// compatibility!
//uint16_t x = 0; uint16_t y = 0; uint16_t w = 192; uint16_t h = 64; // FAILSAFE - JUST FOR TESTING, the buffer must have same dimensions
#ifdef USE_ADVANCED_BUFFERING
switch (this->selectedBuffer->type) {
case TYPE_INTERNAL_V:
displayBuffer( this->selectedBuffer->x, this->selectedBuffer->y, this->selectedBuffer->width, this->selectedBuffer->height, (uint8_t*) this->selectedBuffer->bitmap); break;
case TYPE_SRAM_V:
displaySRAMBuffer( this->selectedBuffer->x, this->selectedBuffer->y, this->selectedBuffer->width, this->selectedBuffer->height, this->selectedBuffer->address); break;
}
#else
uint16_t x = 0; uint16_t y = 0; uint16_t w = this->bufferWidth; uint16_t h = this->bufferHeight; // For simple buffer, x, y are zero
displayBuffer( x, y, w, h, (uint8_t*) this->buffer);
#endif
}
void LCD_UC1609::clearDisplay() {
// CLEARS CURRENTLY SELECTED BUFFER! compatibility!
#ifdef USE_ADVANCED_BUFFERING
switch (this->selectedBuffer->type) {
case TYPE_INTERNAL_V:
memset( this->selectedBuffer->bitmap, 0x00, (this->selectedBuffer->width * (this->selectedBuffer->height >> 3)) ); break;
case TYPE_SRAM_V:
SRAM_fill(this->selectedBuffer->address, 0x00, (this->selectedBuffer->width * (this->selectedBuffer->height >> 3))); break;
}
#else
memset( this->buffer, 0x00, (this->bufferWidth * (this->bufferHeight >> 3)) ); // avoid sizeof to keep dynamic!
#endif
}
void LCD_UC1609::clearDisplay(uint8_t pixels) {
// NEW FUNCTION to overwrite the display memory with value pixels -> 0 for clear, 0xFF all pixels on, 0x55 -> strips, 0xaa -> strips INVERTED
LCD_select();
uint16_t bytesize = LCD_WIDTH * (LCD_HEIGHT >> 3); // width * height
for (uint16_t tx = 0; tx < bytesize; tx++) {
(void)SPI.transfer( pixels );
}
LCD_deselect();
}
void LCD_UC1609::clearDisplay(uint8_t pixels, uint8_t mdelay) {
// NEW FUNCTION to overwrite the display memory, with delayMicroseconds between writes
LCD_select();
uint16_t bytesize = LCD_WIDTH * (LCD_HEIGHT >> 3); // width * height
for (uint16_t tx = 0; tx < bytesize; tx++) {
(void)SPI.transfer( pixels );
delayMicroseconds(mdelay);
}
LCD_deselect();
}
void LCD_UC1609::displayBuffer(int16_t x, int16_t y, uint16_t w, uint16_t h, uint8_t* data) {
LCD_select();
uint8_t tx, ty; // temp x, y
uint16_t tc = 0; // mem offset
uint8_t col = (x < 0) ? 0 : x;
uint8_t page = (y < 0) ? 0 : y >> 3;
for (ty = 0; ty < h; ty = ty + 8) {
if (y + ty < 0 || y + ty >= LCD_HEIGHT) {
continue;
}
uc1609_command(UC1609_SET_COL_LSB, (col & B00001111)); // column low nibble
uc1609_command(UC1609_SET_COL_MSB, (col & B11110000) >> 4); // column high nibble
uc1609_command(UC1609_SET_PAGE, page++); // page adr
for (tx = 0; tx < w; tx++) {
if (x + tx < 0 || x + tx >= LCD_WIDTH) {
continue;
}
tc = (w * (ty >> 3)) + tx; // get offset to read from vertically addressed bitmap
(void)SPI.transfer( data[tc++] ); //
}
}
LCD_deselect();
}
void LCD_UC1609::displayBitmap(int16_t x, int16_t y, uint16_t w, uint16_t h, const uint8_t* data) {
LCD_select();
uint8_t tx, ty; // temp x, y
uint16_t tc = 0; // mem offset
uint8_t col = (x < 0) ? 0 : x;
uint8_t page = (y < 0) ? 0 : y >> 3;
for (ty = 0; ty < h; ty = ty + 8) {
if (y + ty < 0 || y + ty >= LCD_HEIGHT) {
continue;
}
uc1609_command(UC1609_SET_COL_LSB, (col & B00001111)); // column low nibble
uc1609_command(UC1609_SET_COL_MSB, (col & B11110000) >> 4); // column high nibble
uc1609_command(UC1609_SET_PAGE, page++); // page adr
for (tx = 0; tx < w; tx++) {
if (x + tx < 0 || x + tx >= LCD_WIDTH) {
continue;
}
tc = (w * (ty >> 3)) + tx; // get offset to read from vertically addressed bitmap
(void)SPI.transfer( pgm_read_byte(&data[tc]) ); // send byte
}
}
LCD_deselect();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SRAM
void LCD_UC1609::displaySRAMBuffer(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t address) {
uint8_t xtraBuffer[w]; // internal memory buffer large as bitmap width to speed up transfer -> caching
uint8_t tx, ty; // temp x, y
uint16_t tc = 0; // mem offset
uint8_t col = (x < 0) ? 0 : x;
uint8_t page = (y < 0) ? 0 : y >> 3;
for (ty = 0; ty < h; ty = ty + 8) {
if (y + ty < 0 || y + ty >= LCD_HEIGHT) {
continue;
}
tc = 0;
SRAM_read(address + (w * (ty >> 3)), (uint8_t*)&xtraBuffer, w);
LCD_select();
uc1609_command(UC1609_SET_COL_LSB, (col & B00001111)); // column low nibble
uc1609_command(UC1609_SET_COL_MSB, (col & B11110000) >> 4); // column high nibble
uc1609_command(UC1609_SET_PAGE, page++); // page adr
for (tx = 0; tx < w; tx++) {
if (x + tx < 0 || x + tx >= LCD_WIDTH) {
continue;
}
//tc = (w * (ty >> 3)) + tx; // get offset to read from vertically addressed bitmap not required here until we write outside of screen
(void)SPI.transfer( xtraBuffer[tc++] ); // send byte
}
LCD_deselect();
}
}
// SRAM init - general init for all devices
void LCD_UC1609::initSRAM(){
if (SRAM_cs != -1) {
SRAM_deselect(); // for intitial high to low transition
#ifdef USE_PSRAM64_INIT
initPSRAM();
#endif
SRAM_WRMR(SRAM_SEQUENTIAL_MODE); // set sequential mode, common for all types
}
}
// PSRAM init - special init for PSRAM64H
void LCD_UC1609::initPSRAM(){
SRAM_select();
SPI.transfer(SRAM_PSRAM64_RESET_ENABLE);
SRAM_deselect();
__asm__ __volatile__ ("nop; nop; nop; nop; nop; nop; nop; nop;"); // wait 8 cycles, datasheet does not specify exact length
SRAM_select();
SPI.transfer(SRAM_PSRAM64_DEVICE_RESET);
SRAM_deselect();
}
// SRAM chip select
void LCD_UC1609::SRAM_select() {
// SPI.beginTransaction(SRAM_settings); // futureproof
// SPI.setModule(2); // stm
#ifdef __AVR__
PORTD &= ~(1 << SRAM_cs);
#else
digitalWrite(SRAM_cs, LOW);
#endif
}
// SRAM chip deselect
void LCD_UC1609::SRAM_deselect() {
// SPI.setModule(1); // stm
#ifdef __AVR__
PORTD |= (1 << SRAM_cs);
#else
digitalWrite(SRAM_cs, HIGH);
#endif
}
// SRAM read mode register
uint8_t LCD_UC1609::SRAM_RDMR() {
byte rc;
SRAM_select();
SPI.transfer(SRAM_CMD_RDMR);
rc = SPI.transfer(0xFF);
SRAM_deselect();
return rc;
}
// SRAM write mode register
void LCD_UC1609::SRAM_WRMR(uint8_t mode) {
SRAM_select();
SPI.transfer(SRAM_CMD_WRMR);
SPI.transfer(mode);
SRAM_deselect();
}
// SRAM read - reads data to buffer
#ifdef USE_SRAM_24BIT_ADDRESS
void LCD_UC1609::SRAM_read(uint32_t addr, uint8_t* data, uint16_t n) {
SRAM_select();
SPI.transfer(SRAM_CMD_READ);
SPI.transfer((uint8_t)(addr >> 16) & 0xFF); // A23-A16
#else
void LCD_UC1609::SRAM_read(uint16_t addr, uint8_t* data, uint16_t n) {
SRAM_select();
SPI.transfer(SRAM_CMD_READ);
#endif
SPI.transfer((uint8_t)(addr >> 8) & 0xFF); // A15-A08
SPI.transfer((uint8_t)(addr) & 0xFF); // A07-A00
for (uint16_t i = 0; i < n; i++ ) {
data[i] = SPI.transfer(0xFF);
}
SRAM_deselect();
}
// SRAM write - writes n data to address
#ifdef USE_SRAM_24BIT_ADDRESS
void LCD_UC1609::SRAM_write(uint32_t addr, uint8_t* data, uint16_t n) {
SRAM_select();
SPI.transfer(SRAM_CMD_WRITE);
SPI.transfer((uint8_t)(addr >> 16) & 0xFF); // A23-A16
#else
void LCD_UC1609::SRAM_write(uint16_t addr, uint8_t* data, uint16_t n) {
SRAM_select();
SPI.transfer(SRAM_CMD_WRITE);
#endif
SPI.transfer((uint8_t)(addr >> 8) & 0xFF); // A15-A08
SPI.transfer((uint8_t)(addr) & 0xFF); // A07-A00
for (uint16_t i = 0; i < n; i++) {
SPI.transfer(data[i]);
}
SRAM_deselect();
}
// SRAM fill - similar to memset
#ifdef USE_SRAM_24BIT_ADDRESS
void LCD_UC1609::SRAM_fill(uint32_t addr, uint8_t data, uint16_t n) {
SRAM_select();
SPI.transfer(SRAM_CMD_WRITE);
SPI.transfer((uint8_t)(addr >> 16) & 0xFF); // A23-A16
#else
void LCD_UC1609::SRAM_fill(uint16_t addr, uint8_t data, uint16_t n) {
SRAM_select();
SPI.transfer(SRAM_CMD_WRITE);
#endif
SPI.transfer((uint8_t)(addr >> 8) & 0xFF); // A15-A08
SPI.transfer((uint8_t)(addr) & 0xFF); // A07-A00
for (uint16_t i = 0; i < n; i++) {
SPI.transfer(data);
}
SRAM_deselect();
}
// SRAM read byte - similar to PGM_read_byte - reads a byte and returns it
#ifdef USE_SRAM_24BIT_ADDRESS
uint8_t LCD_UC1609::SRAM_read_byte(uint32_t addr) {
SRAM_select();
SPI.transfer(SRAM_CMD_READ);
SPI.transfer((uint8_t)(addr >> 16) & 0xFF); // A23-A16
#else
uint8_t LCD_UC1609::SRAM_read_byte(uint16_t addr) {
SRAM_select();
SPI.transfer(SRAM_CMD_READ);
#endif
SPI.transfer((uint8_t)(addr >> 8) & 0xFF); // A15-A08
SPI.transfer((uint8_t)(addr) & 0xFF); // A07-A00
uint8_t data;
data = SPI.transfer(0x00);
SRAM_deselect();
return data;
}
// SRAM write - writes a byte to address
#ifdef USE_SRAM_24BIT_ADDRESS
void LCD_UC1609::SRAM_write_byte(uint32_t addr, uint8_t data) {
SRAM_select();
SPI.transfer(SRAM_CMD_WRITE);
SPI.transfer((uint8_t)(addr >> 16) & 0xFF); // A23-A16
#else
void LCD_UC1609::SRAM_write_byte(uint16_t addr, uint8_t data) {
SRAM_select();
SPI.transfer(SRAM_CMD_WRITE);
#endif
SPI.transfer((uint8_t)(addr >> 8) & 0xFF); // A15-A08
SPI.transfer((uint8_t)(addr) & 0xFF); // A07-A00
SPI.transfer(data);
SRAM_deselect();
}