forked from ttrftech/NanoVNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathili9341.c
755 lines (685 loc) · 17.7 KB
/
ili9341.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
* Copyright (c) 2014-2015, TAKAHASHI Tomohiro (TTRFTECH) edy555@gmail.com
* All rights reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* The software 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ch.h"
#include "hal.h"
#include "nanovna.h"
#define RESET_ASSERT palClearPad(GPIOA, 15)
#define RESET_NEGATE palSetPad(GPIOA, 15)
#define CS_LOW palClearPad(GPIOB, 6)
#define CS_HIGH palSetPad(GPIOB, 6)
#define DC_CMD palClearPad(GPIOB, 7)
#define DC_DATA palSetPad(GPIOB, 7)
uint16_t spi_buffer[SPI_BUFFER_SIZE];
void
ssp_wait(void)
{
while (SPI1->SR & SPI_SR_BSY)
;
}
void
ssp_wait_slot(void)
{
while ((SPI1->SR & 0x1800) == 0x1800)
;
}
void
ssp_senddata(uint8_t x)
{
*(uint8_t*)(&SPI1->DR) = x;
while (SPI1->SR & SPI_SR_BSY)
;
}
uint8_t
ssp_sendrecvdata(uint8_t x)
{
while (!(SPI1->SR & SPI_SR_TXE));
// clear OVR
while (SPI1->SR & SPI_SR_RXNE) (void)SPI1->DR;
*(uint8_t*)(&SPI1->DR) = x;
while (!(SPI1->SR & SPI_SR_RXNE));
return SPI1->DR;
}
void
ssp_senddata16(uint16_t x)
{
ssp_wait_slot();
SPI1->DR = x;
//while (SPI1->SR & SPI_SR_BSY)
// ;
}
static inline void
ssp_senddata16RGB(uint16_t x)
{
uint8_t r = (x & 0xF800) >> 8;
uint8_t g = (x & 0x07E0) >> 3;
uint8_t b = (x & 0x001F) << 3;
ssp_senddata(r);
ssp_senddata(g);
ssp_senddata(b);
}
void
ssp_databit8(void)
{
SPI1->CR2 = (SPI1->CR2 & 0xf0ff) | 0x0700;
//LPC_SSP1->CR0 = (LPC_SSP1->CR0 & 0xf0) | SSP_DATABIT_8;
}
void
ssp_databit16(void)
{
SPI1->CR2 = (SPI1->CR2 & 0xf0ff) | 0x0f00;
//LPC_SSP1->CR0 = (LPC_SSP1->CR0 & 0xf0) | SSP_DATABIT_16;
}
const stm32_dma_stream_t *dmatx;
uint32_t txdmamode;
static void spi_lld_serve_tx_interrupt(SPIDriver *spip, uint32_t flags) {
(void)spip;
(void)flags;
}
void
spi_init(void)
{
rccEnableSPI1(FALSE);
dmatx = STM32_DMA_STREAM(STM32_SPI_SPI1_TX_DMA_STREAM);
txdmamode = STM32_DMA_CR_CHSEL(SPI1_TX_DMA_CHANNEL) |
STM32_DMA_CR_PL(STM32_SPI_SPI1_DMA_PRIORITY) |
STM32_DMA_CR_DIR_M2P |
STM32_DMA_CR_DMEIE |
STM32_DMA_CR_TEIE |
STM32_DMA_CR_PSIZE_HWORD |
STM32_DMA_CR_MSIZE_HWORD;
dmaStreamAllocate(dmatx,
STM32_SPI_SPI1_IRQ_PRIORITY,
(stm32_dmaisr_t)spi_lld_serve_tx_interrupt,
NULL);
dmaStreamSetPeripheral(dmatx, &SPI1->DR);
SPI1->CR1 = 0;
// SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI; // | SPI_CR1_BR_0; // | SPI_CR1_BR_0 ;// | SPI_CR1_BR_1; fPCLK/4
//SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0; // | SPI_CR1_BR_0 ;// | SPI_CR1_BR_1; fPCLK/4
SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI ;
SPI1->CR2 = 0x0700 | SPI_CR2_TXDMAEN | SPI_CR2_FRXTH;
SPI1->CR1 |= SPI_CR1_SPE;
}
void
send_command(uint8_t cmd, int len, const uint8_t *data)
{
CS_LOW;
DC_CMD;
ssp_databit8();
ssp_senddata(cmd);
DC_DATA;
#ifdef ILI9488
if ((cmd==0x2C) && (len==2)) {
// write pixel, convert to RGB888
int color = (data[1]<<8)+data[0];
uint8_t r = (color & 0xF800) >> 8;
uint8_t g = (color & 0x07E0) >> 3;
uint8_t b = (color & 0x001F)<<3;
ssp_senddata(r);
ssp_senddata(g);
ssp_senddata(b);
} else {
while (len-- > 0) {
ssp_senddata(*data++);
}
}
#else
while (len-- > 0) {
ssp_senddata(*data++);
}
#endif
//CS_HIGH;
}
void
send_command16(uint8_t cmd, int data)
{
CS_LOW;
DC_CMD;
ssp_databit8();
ssp_senddata(cmd);
DC_DATA;
ssp_databit16();
ssp_senddata16(data);
CS_HIGH;
}
const uint8_t ili9341_init_seq[] = {
// cmd, len, data...,
// Power control B
0xCF, 3, 0x00, 0x83, 0x30,
// Power on sequence control
0xED, 4, 0x64, 0x03, 0x12, 0x81,
//0xED, 4, 0x55, 0x01, 0x23, 0x01,
// Driver timing control A
0xE8, 3, 0x85, 0x01, 0x79,
//0xE8, 3, 0x84, 0x11, 0x7a,
// Power control A
0xCB, 5, 0x39, 0x2C, 0x00, 0x34, 0x02,
// Pump ratio control
0xF7, 1, 0x20,
// Driver timing control B
0xEA, 2, 0x00, 0x00,
// POWER_CONTROL_1
0xC0, 1, 0x26,
// POWER_CONTROL_2
0xC1, 1, 0x11,
// VCOM_CONTROL_1
0xC5, 2, 0x35, 0x3E,
// VCOM_CONTROL_2
0xC7, 1, 0xBE,
// MEMORY_ACCESS_CONTROL
//0x36, 1, 0x48, // portlait
0x36, 1, 0x28, // landscape, BGR565
// COLMOD_PIXEL_FORMAT_SET : 16 bit pixel
0x3A, 1, 0x55,
// Frame Rate
0xB1, 2, 0x00, 0x1B,
// Gamma Function Disable
0xF2, 1, 0x08,
// gamma set for curve 01/2/04/08
0x26, 1, 0x01,
// positive gamma correction
0xE0, 15, 0x1F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0x87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00,
// negativ gamma correction
0xE1, 15, 0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x1F,
// Column Address Set
0x2A, 4, 0x00, 0x00, 0x01, 0x3f, // width 320
// Page Address Set
0x2B, 4, 0x00, 0x00, 0x00, 0xef, // height 240
// entry mode
0xB7, 1, 0x06,
// display function control
0xB6, 4, 0x0A, 0x82, 0x27, 0x00,
// control display
//0x53, 1, 0x0c,
// diaplay brightness
//0x51, 1, 0xff,
// sleep out
0x11, 0,
0 // sentinel
};
const uint8_t ili9488_init_seq[] = {
//Interface Mode Control
0xB0,1,0x00,
//Frame Rate
0xB1, 1, 0xA,
//Display Inversion Control , 2 Dot
0xB4, 1, 0x02,
//RGB/MCU Interface Control
0xB6, 3, 0x02, 0x02, 0x3B,
//EntryMode
0xB7, 1, 0xC6,
//Power Control 1
0xC0, 2, 0x17, 0x15,
//Power Control 2
0xC1, 1, 0x41,
//Power Control 3
// 0xC5, 3, 0x00, 0x4D, 0x90,
//VCOM Control
0xC5, 3, 0x00, 0x12, 0x80,
//Memory Access
// ILI9488_MADCTL, 1, MADCTL_MX | MADCTL_BGR
0x36, 1, 0x28, // landscape, BGR
//0x36, 1, 0x20, // landscape, RGB
//16bpp DPI and DBI and
//Interface Pixel Format
#if defined(ILI9488)
0x3A, 1, 0x66,
#else
0x3A, 1, 0x55,
#endif
//default gamma
// 0xC0, 2, 0x18, 0x16,
// 0xBE, 2, 0x00, 0x04,
//P-Gamma
0xE0, 15, 0x00, 0x03, 0x09, 0x08,
0x16, 0x0A, 0x3F, 0x78,
0x4C, 0x09, 0x0A, 0x08,
0x16, 0x1A, 0x0F,
//N-Gamma
0xE1, 15, 0x00, 0X16, 0X19, 0x03,
0x0F, 0x05, 0x32, 0x45,
0x46, 0x04, 0x0E, 0x0D,
0x35, 0x37, 0x0F,
//Set Image Func
0xE9, 1, 0x00,
//Set Brightness to Max
0x51, 1, 0xFF,
//Adjust Control
0xF7, 4, 0xA9, 0x51, 0x2C, 0x82,
//set default rotation to 0
//Exit Sleep
0x11, 0x00,
// sentinel
0
};
void
ili9341_init(void)
{
spi_init();
DC_DATA;
RESET_ASSERT;
chThdSleepMilliseconds(10);
RESET_NEGATE;
send_command(0x01, 0, NULL); // SW reset
chThdSleepMilliseconds(5);
send_command(0x28, 0, NULL); // display off
const uint8_t *p;
#if defined(ILI9488) || defined(ILI9486) || defined(ST7796S)
for (p = ili9488_init_seq; *p; ) {
#else
for (p = ili9341_init_seq; *p; ) {
#endif
send_command(p[0], p[1], &p[2]);
p += 2 + p[1];
chThdSleepMilliseconds(5);
}
chThdSleepMilliseconds(100);
send_command(0x29, 0, NULL); // display on
}
void ili9341_pixel(int x, int y, int color)
{
uint8_t xx[4] = { x >> 8, x, (x+1) >> 8, (x+1) };
uint8_t yy[4] = { y >> 8, y, (y+1) >> 8, (y+1) };
uint8_t cc[2] = { color >> 8, color };
send_command(0x2A, 4, xx);
send_command(0x2B, 4, yy);
send_command(0x2C, 2, cc);
//send_command16(0x2C, color);
}
#if defined(ILI9488)
void ili9341_fill(int x, int y, int w, int h, int color)
{
if (((x+w)>LCD_WIDTH) || ((y+h)>LCD_HEIGHT)) {
return;
}
uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
int len = w * h;
uint8_t r = (color & 0xF800) >> 8;
uint8_t g = (color & 0x07E0) >> 3;
uint8_t b = (color & 0x001F) << 3;
uint16_t rg = r<<8 | g;
uint16_t br = b<<8 | r;
uint16_t gb = g<<8 | b;
send_command(0x2A, 4, xx);
send_command(0x2B, 4, yy);
send_command(0x2C, 0, NULL);
while (len > 0) {
ssp_senddata16(rg);
ssp_senddata16(br);
ssp_senddata16(gb);
len -= 2;
}
}
#else
void ili9341_fill(int x, int y, int w, int h, int color)
{
uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
int len = w * h;
send_command(0x2A, 4, xx);
send_command(0x2B, 4, yy);
send_command(0x2C, 0, NULL);
while (len-- > 0)
ssp_senddata16(color);
}
#endif
#if 0
void ili9341_bulk(int x, int y, int w, int h)
{
uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
uint16_t *buf = spi_buffer;
int len = w * h;
send_command(0x2A, 4, xx);
send_command(0x2B, 4, yy);
send_command(0x2C, 0, NULL);
while (len-- > 0)
ssp_senddata16(*buf++);
}
#else
void ili9341_bulk(int x, int y, int w, int h)
{
uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
int len = w * h;
send_command(0x2A, 4, xx);
send_command(0x2B, 4, yy);
send_command(0x2C, 0, NULL);
#ifdef ILI9488
uint16_t *buf=spi_buffer;
ssp_databit8();
while (len-- > 0)
ssp_senddata16RGB(*buf++);
ssp_databit16();
#else
dmaStreamSetMemory0(dmatx, spi_buffer);
dmaStreamSetTransactionSize(dmatx, len);
dmaStreamSetMode(dmatx, txdmamode | STM32_DMA_CR_MINC);
dmaStreamEnable(dmatx);
dmaWaitCompletion(dmatx);
#endif
}
#endif
void
ili9341_read_memory_raw(uint8_t cmd, int len, uint16_t* out)
{
uint8_t r, g, b;
send_command(cmd, 0, NULL);
ssp_databit8();
// consume old data
while (!(SPI1->SR & SPI_SR_TXE));
// clear OVR
while (SPI1->SR & SPI_SR_RXNE) r = SPI1->DR;
// require 8bit dummy clock
r = ssp_sendrecvdata(0);
while (len-- > 0) {
// read data is always 18bit
r = ssp_sendrecvdata(0);
g = ssp_sendrecvdata(0);
#ifdef ILI9488
b = ssp_sendrecvdata(0);
*out++ = (r << 11) | (g << 5) | b;
#elif defined(ST7796S)
// Control interface color format, ‘101’ = 16bit/pixel
*out++ = (r << 8) | g;
#else
*out++ = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
#endif
}
CS_HIGH;
}
void
ili9341_read_memory(int x, int y, int w, int h, int len, uint16_t *out)
{
uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
send_command(0x2A, 4, xx);
send_command(0x2B, 4, yy);
ili9341_read_memory_raw(0x2E, len, out);
}
void
ili9341_read_memory_continue(int len, uint16_t* out)
{
ili9341_read_memory_raw(0x3E, len, out);
}
#if !defined(ST7796S)
void
ili9341_drawchar_5x7(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg)
{
#if defined(ILI9488)
ili9341_drawchar_size(ch, x, y, fg, bg, 1);
#else
uint16_t *buf = spi_buffer;
uint16_t bits;
int c, r;
for(c = 0; c < 7; c++) {
bits = x5x7_bits[(ch * 7) + c];
for (r = 0; r < 5; r++) {
*buf++ = (0x8000 & bits) ? fg : bg;
bits <<= 1;
}
}
ili9341_bulk(x, y, 5, 7);
#endif
}
void
ili9341_drawstring_5x7(const char *str, int x, int y, uint16_t fg, uint16_t bg)
{
while (*str) {
ili9341_drawchar_5x7(*str, x, y, fg, bg);
x += X_SPACE;
str++;
}
}
void
ili9341_drawstring_5x7_inv(const char *str, int x, int y, uint16_t fg, uint16_t bg, bool invert)
{
if (invert)
ili9341_drawstring_5x7(str, x, y, bg, fg);
else
ili9341_drawstring_5x7(str, x, y, fg, bg);
}
void
ili9341_drawchar_size(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg, uint8_t size)
{
uint16_t *buf = spi_buffer;
uint16_t bits;
int c, r;
for(c = 0; c < 7*size; c++) {
bits = x5x7_bits[(ch * 7) + (c / size)];
for (r = 0; r < 5*size; r++) {
*buf++ = (0x8000 & bits) ? fg : bg;
if (r % size == (size-1)) {
bits <<= 1;
}
}
}
ili9341_bulk(x, y, 5*size, 7*size);
}
void
ili9341_drawstring_size(const char *str, int x, int y, uint16_t fg, uint16_t bg, uint8_t size)
{
while (*str) {
ili9341_drawchar_size(*str, x, y, fg, bg, size);
x += 5 * size;
str++;
}
}
#else
void
ili9341_drawchar_7x13(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg)
{
uint16_t *buf = spi_buffer;
uint16_t bits;
int c, r;
for(c = 0; c < 13; c++) {
bits = x7x13b_bits[(ch * 13) + c];
for (r = 0; r < 7; r++) {
*buf++ = (0x8000 & bits) ? fg : bg;
bits <<= 1;
}
}
ili9341_bulk(x, y, 7, 13);
}
void
ili9341_drawstring_7x13(const char *str, int x, int y, uint16_t fg, uint16_t bg)
{
while (*str) {
ili9341_drawchar_7x13(*str, x, y, fg, bg);
x += 7;
str++;
}
}
void
ili9341_drawstring_7x13_inv(const char *str, int x, int y, uint16_t fg, uint16_t bg, bool invert)
{
if (invert)
ili9341_drawstring_7x13(str, x, y, bg, fg);
else
ili9341_drawstring_7x13(str, x, y, fg, bg);
}
void
ili9341_drawchar_size(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg, uint8_t size)
{
uint16_t *buf = spi_buffer;
uint16_t bits;
int c, r;
for(c = 0; c < 13*size; c++) {
bits = x7x13b_bits[(ch * 13) + (c / size)];
for (r = 0; r < 7*size; r++) {
*buf++ = (0x8000 & bits) ? fg : bg;
if (r % size == (size-1)) {
bits <<= 1;
}
}
}
ili9341_bulk(x, y, 7*size, 13*size);
}
void
ili9341_drawstring_size(const char *str, int x, int y, uint16_t fg, uint16_t bg, uint8_t size)
{
while (*str) {
ili9341_drawchar_size(*str, x, y, fg, bg, size);
x += 7 * size;
str++;
}
}
#endif
#define SWAP(x,y) do { int z=x; x = y; y = z; } while(0)
void
ili9341_line(int x0, int y0, int x1, int y1, uint16_t fg)
{
if (x0 > x1) {
SWAP(x0, x1);
SWAP(y0, y1);
}
while (x0 <= x1) {
int dx = x1 - x0 + 1;
int dy = y1 - y0;
if (dy >= 0) {
dy++;
if (dy > dx) {
dy /= dx; dx = 1;
} else {
dx /= dy; dy = 1;
}
} else {
dy--;
if (-dy > dx) {
dy /= dx; dx = 1;
} else {
dx /= -dy; dy = -1;
}
}
if (dy > 0)
ili9341_fill(x0, y0, dx, dy, fg);
else
ili9341_fill(x0, y0+dy, dx, -dy, fg);
x0 += dx;
y0 += dy;
}
}
const font_t NF20x22 = { 20, 22, 1, 3*22, (const uint8_t *)numfont20x22 };
void
ili9341_drawfont(uint8_t ch, const font_t *font, int x, int y, uint16_t fg, uint16_t bg)
{
uint16_t *buf = spi_buffer;
const uint8_t *bitmap = &font->bitmap[font->slide * ch];
int c, r;
for (c = 0; c < font->height; c++) {
uint8_t bits = *bitmap++;
uint8_t m = 0x80;
for (r = 0; r < font->width; r++) {
*buf++ = (bits & m) ? fg : bg;
m >>= 1;
if (m == 0) {
bits = *bitmap++;
m = 0x80;
}
}
}
ili9341_bulk(x, y, font->width, font->height);
}
#if 1
static long rands = 654321;
uint16_t my_rand () {
rands = (54321123457 * rands) % 31459;
return rands;
}
#if 1
const uint16_t colormap[] = {
RGB565(255,0,0), RGB565(0,255,0), RGB565(0,0,255),
RGB565(255,255,0), RGB565(0,255,255), RGB565(255,0,255)
};
void
ili9341_test(int mode)
{
int x, y;
int i;
uint16_t x0, x1, y0, y1;
int c;
// int r;
switch (mode) {
default:
#if 1
//ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0);
for (y = 0; y < LCD_HEIGHT; y++) {
ili9341_fill(0, y, LCD_WIDTH, 1, RGB565(y, (y + 120) % 256, LCD_HEIGHT-y));
}
break;
case 1:
ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0);
for (y = 0; y < LCD_HEIGHT; y++) {
for (x = 0; x < LCD_WIDTH; x++) {
ili9341_pixel(x, y, (y<<8)|x);
}
}
break;
case 2:
//send_command16(0x55, 0xff00);
for (y = 0; y < LCD_HEIGHT; y++) {
for (x = 0; x < LCD_WIDTH; x++) {
ili9341_pixel(x, y, my_rand()%65536);
}
}
break;
#endif
#if 1
case 3:
for (i = 0; i < 10; i++)
ili9341_drawfont(i, &NF20x22, i*20, 120, colormap[i%6], 0x0000);
break;
#endif
#if 0
case 4:
draw_grid(10, 8, 29, 29, 15, 0, 0xffff, 0);
break;
#endif
#if 1
case 4:
ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0);
x0 = (uint16_t) my_rand() % LCD_WIDTH;
y0 = (uint16_t) my_rand() % LCD_HEIGHT-4;
if ((x0>=LCD_WIDTH) || (y0>=LCD_HEIGHT)) {
return;
}
//r = (int) my_rand() % 100;
for (i=0; i<100; i++) {
x1 = (uint16_t) my_rand() % LCD_WIDTH;
y1 = (uint16_t) my_rand() % LCD_HEIGHT-4;
if ((x1>=LCD_WIDTH) || (y1>=LCD_HEIGHT)) {
return;
}
//c = RGB565(255,255,255);
//c = colormap[i % 6];
c = (uint16_t) my_rand() % 65536;
ili9341_line(x0, y0, x1, y1, c);
ili9341_line(x0, y0+1, x1, y1+1, c);
//ili9341_line(x0, y0+2, x1, y1+2, c);
//ili9341_line(x0, y0+3, x1, y1+3, c);
x0 = x1;
y0 = y1;
}
break;
#endif
}
}
#endif
#endif