-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathILI9341.cpp
More file actions
1106 lines (965 loc) · 27 KB
/
ILI9341.cpp
File metadata and controls
1106 lines (965 loc) · 27 KB
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ILI9341.h"
#include <fcntl.h> // Needed for SPI port
#include <sys/ioctl.h> // Needed for SPI port
#include <linux/spi/spidev.h> // Needed for SPI port
#include <unistd.h> // Needed for SPI port
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <signal.h>
#include <pthread.h>
#include <wiringPi.h>
#include <errno.h>
#include "Logger.h"
#include "Externs.h"
#include "Buzzer.h"
#define SCR_HEIGHT 240
#define SCR_WIDTH 320
#define SCR_BPP 16
#define IMG_BPP 24
#define BURST_BS 2048 // Max 4096 (a C SPI limitation it seems)
#define BITS_PER_WORD 8
#define TOUCH_IRQ_PIN 1
#define DC_PIN 25
#define LED_PIN 22
#define RESET_PIN 24
#define BUZZ_PIN 0
// XPT2046 Touch Screen Controller
// 1 x x x 0 0 0 0
// Control Channel 12-Bit Differenctial Power-Down
// B Select Mode Reference Between
// Bits Mode Conversions
#define TOUCH_X 0xD0; // CSB: 1 0 1
#define TOUCH_Y 0x90; // CSB: 0 0 1
#define TOUCH_Z1 0xB0; // CSB: 0 1 1
#define TOUCH_Z2 0xC0; // csb: 1 0 0
#define CO2_BLINKS 20
#define HT_BLINKS 6
#define BLACK_DYE 0
#define WHITE_DYE 1
#define RED_DYE 2
#define BLUE_DYE 3
#define NO_DYE 4
#define FONT_BIG 0
#define FONT_MED 1
#define FONT_SML 2
#define B(op) (unsigned char)(op)
// 16 bit pixel to 24 bit pixel convertions
#define RED(rg, gb) B(rg & 0xF8)
#define GREEN(rg, gb) B(B(rg << 5) | B(B(gb >> 3) & 0xF8)) // Clamp least G bit, to equalise it with R&B
#define BLUE(rg, gb) B(gb << 3)
#define EXTRACT_TOUCH_DATA() ((int)rtx[2] >> 3 | (int)rtx[1] << 5)
// Sends command to LCD with additional arguments Bs
#define COMMAND(cmd, ...) do { \
unsigned char dat[] = { __VA_ARGS__ }; \
spiCommand(cmd); \
spiData(dat, sizeof dat); \
} while(0)
// Wait 120 ms after going IN sleep before going OUT of sleep and vice versa
#define ENTER_SLEEP() \
spiCommand(0x10 /*Sleep IN*/); \
delay(5);
#define EXIT_SLEEP() \
spiCommand(0x11 /*Sleep OUT*/); \
delay(5);
struct Pixel
{
unsigned char rg;
unsigned char gb;
unsigned char a;
};
int spi0_fs; // Descriptor for the SPI0 device filestream
int spi1_fs; // Descriptor for the SPI1 device filestream
unsigned char* frame_buffer;
struct spi_ioc_transfer spi0; // Screen SPI
struct spi_ioc_transfer spi1; // Touch SPI
const unsigned int spi0_speed = 75000000;
const unsigned int spi1_speed = 2000000;
// Theoretically scaling factor should be like this:
//const float touch_x_factor = 4096.0f/SCR_WIDTH;
//const float touch_y_factor = 4096.0f/SCR_HEIGHT;
// But in reality it's more like this (calibrated):
const float touch_x_factor = 3450.0f/SCR_HEIGHT;
const float touch_y_factor = 3416.0f/SCR_WIDTH;
const int touch_x_offset = 400;
const int touch_y_offset = 280;
#ifndef MODE_16_BIT
const int lcd_size = SCR_WIDTH * SCR_HEIGHT * IMG_BPP/8; // 230400
#else
const int lcd_size = SCR_WIDTH * SCR_HEIGHT * 2; // 153600
#endif
volatile bool box_should_be_red;
volatile bool readings_are_updating;
volatile bool poweroff_pending;
// Image resources
// Data always starts from 5th element, first 4 reserved for width/height
// Big numbers
unsigned char* big_num_w[10];
unsigned char* big_num_b[10];
// Medium numbers
unsigned char* med_num_w[10];
unsigned char* med_num_b[10];
// Small numbers
unsigned char* sml_num_w[10];
unsigned char* sml_num_b[10];
// Backgrounds
unsigned char* bg_main;
unsigned char* bg_co2;
unsigned char *bg_humd_low, *bg_humd_high;
unsigned char *bg_temp_low, *bg_temp_high;
// Misc
unsigned char* x_button;
unsigned char *dot_b, *dot_w;
unsigned char *minus_b, *minus_w;
int spiOpenPort(int spi_device);
int spiCommand(unsigned char cmd);
int spiData(const unsigned char* dat, int s);
void drawScrBuffer();
void hardwareReset();
void onTouchInput();
unsigned char* loadPGM(const char* path, int dye, bool alpha);
unsigned char* loadPAM(const char* path);
unsigned char* createBox(const unsigned char* orig_img);
void multiplex24(const unsigned char* img, int x, int y);
void multiplex32(const unsigned char* img, int x, int y);
void multiplexInt(int num, const unsigned char *const *font, int f_size, int x, int y);
void multiplexTempFloat(float num, const unsigned char *const *font, const unsigned char *const *smol_font,
const unsigned char* dot, const unsigned char* minus, int x, int y);
void loadNums(const char* suffix, int dye, unsigned char** dest);
void freeNums(unsigned char** targ);
void eraseX();
// External functions
void initLCD()
{
wiringPiSetupGpio();
pinMode(DC_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(TOUCH_IRQ_PIN, INPUT);
pinMode(RESET_PIN, OUTPUT);
pullUpDnControl(DC_PIN, PUD_OFF);
pullUpDnControl(LED_PIN, PUD_OFF);
pullUpDnControl(TOUCH_IRQ_PIN, PUD_OFF);
pullUpDnControl(RESET_PIN, PUD_OFF);
wiringPiISR(TOUCH_IRQ_PIN, INT_EDGE_FALLING, &onTouchInput); // Setup Interrupt Service Routine for touch-screen
spiOpenPort(0);
spi0.delay_usecs = 0;
spi0.speed_hz = spi1_speed;
spi0.bits_per_word = BITS_PER_WORD;
spiOpenPort(1);
spi1.delay_usecs = 0;
spi1.speed_hz = spi1_speed;
spi1.bits_per_word = BITS_PER_WORD;
spi1.cs_change = 0; // If 1, touch chip select stays low after transfer and IRQ gets locked by screen data!
spi1.len = 3;
// Load resourses
frame_buffer = (unsigned char*)malloc(lcd_size);
loadNums("b", WHITE_DYE, big_num_w);
loadNums("b", BLACK_DYE, big_num_b);
loadNums("m", WHITE_DYE, med_num_w);
loadNums("m", BLACK_DYE, med_num_b);
loadNums("s", WHITE_DYE, sml_num_w);
loadNums("s", BLACK_DYE, sml_num_b);
bg_main = loadPGM("./img/bg.pgm", NO_DYE, false);
bg_co2 = loadPGM("./img/co2bg.pgm", RED_DYE, false);
bg_humd_low = loadPGM("./img/rhbg.pgm", RED_DYE, false);
bg_humd_high = loadPGM("./img/rhbg.pgm", BLUE_DYE, false);
bg_temp_low = loadPGM("./img/tbg.pgm", BLUE_DYE, false);
bg_temp_high = loadPGM("./img/tbg.pgm", RED_DYE, false);
x_button = loadPAM("./img/x.pam");
dot_b = loadPGM("./img/d.pgm", BLACK_DYE, true);
dot_w = loadPGM("./img/d.pgm", WHITE_DYE, true);
minus_b = loadPGM("./img/m.pgm", BLACK_DYE, true);
minus_w = loadPGM("./img/m.pgm", WHITE_DYE, true);
initBuzz(BUZZ_PIN);
// Enable reset pin to make possible screen operation
digitalWrite(RESET_PIN, 1);
delay(120);
// Change screen orientation to landscape mode. This command MUST be called BEFORE Soft. Reset, otherwise screen bugs out
COMMAND(0x36, /*Memory Access Control*/ 0xE0 /*Row/Column Exchange + Column + Row Address Order*/);
spiCommand(0x01 /*Software reset*/);
delay(5); // Wait some time for the supply voltages and clock circuits to stabilise
#ifdef MODE_16_BIT
COMMAND(0x3A, /*Pixel Format Set*/ 0x55 /*16 bit mode 65 536 colors*/);
#endif
COMMAND(0xE0, /*Positive Gamma Correction*/ 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00);
COMMAND(0xE1, /*Negative Gamma Correction*/ 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F);
EXIT_SLEEP();
spiCommand(0x29 /*Display ON*/);
delay(130); // This delay is only cosmetic, to skip few white frames at display turning ON
digitalWrite(LED_PIN, 1); // Turn backlight ON
lcd_is_on = true;
spi0.speed_hz = spi0_speed;
}
void deinitLCD()
{
digitalWrite(LED_PIN, 0); // Turn backlight OFF
spiCommand(0x28 /*Display OFF*/);
ENTER_SLEEP();
lcd_is_on = false;
free(frame_buffer);
freeNums(big_num_w);
freeNums(big_num_b);
freeNums(med_num_w);
freeNums(med_num_b);
freeNums(sml_num_w);
freeNums(sml_num_b);
free(bg_main);
free(bg_co2);
free(bg_humd_low);
free(bg_humd_high);
free(bg_temp_low);
free(bg_temp_high);
free(x_button);
free(dot_b);
free(dot_w);
free(minus_b);
free(minus_w);
close(spi0_fs);
close(spi1_fs);
}
void updateReadings(int ppm, float humd, float temp)
{
// Critical Section Beg
pthread_mutex_lock(&warning_levels_lock);
int co2wh = co2_warning;
int humdwl = humd_warning_low;
int humdwh = humd_warning_high;
int tempwl = temp_warning_low;
int tempwh = temp_warning_high;
pthread_mutex_unlock(&warning_levels_lock);
// Critical Section End
readings_are_updating = true;
memcpy((void*)frame_buffer, (const void*)(bg_main+4), lcd_size); // 4 first Bs are Width/Height
unsigned char** co2_num = big_num_w;
unsigned char** humd_num = med_num_w;
unsigned char** sml_humd_num = sml_num_w;
unsigned char** temp_num = med_num_w;
unsigned char** sml_temp_num = sml_num_w;
unsigned char* temp_minus = minus_w;
unsigned char* temp_dot = dot_w;
static int co2_blinks = CO2_BLINKS; // 12 seconds
static int humd_blinks = HT_BLINKS; // 6 seconds
static int temp_blinks = HT_BLINKS; // 6 seconds
static bool co2_sound_warned;
// Check reading exceeding max safe levels
if(ppm > co2wh)
{
if(co2_blinks <= 0 || co2_blinks%2 == 0)
{
multiplex24(bg_co2, 0, 0);
co2_num = big_num_b;
box_should_be_red = true;
}
if(co2_blinks > 0)
{
--co2_blinks;
}
}
else
{
co2_blinks = CO2_BLINKS;
box_should_be_red = false;
co2_sound_warned = false;
}
if((int)humd > humdwh)
{
if(humd_blinks <= 0 || humd_blinks%2 == 0)
{
multiplex24(bg_humd_high, 0, 122);
humd_num = med_num_b;
sml_humd_num = sml_num_b;
}
if(humd_blinks > 0)
{
--humd_blinks;
}
}
else if((int)humd < humdwl)
{
if(humd_blinks <= 0 || humd_blinks%2 == 0)
{
multiplex24(bg_humd_low, 0, 122);
humd_num = med_num_b;
sml_humd_num = sml_num_b;
}
if(humd_blinks > 0)
{
--humd_blinks;
}
}
else
{
humd_blinks = HT_BLINKS;
}
if(temp > (float)tempwh)
{
if(temp_blinks <= 0 || temp_blinks%2 == 0)
{
multiplex24(bg_temp_high, 162, 122);
temp_num = med_num_b;
sml_temp_num = sml_num_b;
temp_minus = minus_b;
temp_dot = dot_b;
}
if(temp_blinks > 0)
{
--temp_blinks;
}
}
else if(temp < (float)tempwl)
{
if(temp_blinks <= 0 || temp_blinks%2 == 0)
{
multiplex24(bg_temp_low, 162, 122);
temp_num = med_num_b;
sml_temp_num = sml_num_b;
temp_minus = minus_b;
temp_dot = dot_b;
}
if(temp_blinks > 0)
{
--temp_blinks;
}
}
else
{
temp_blinks = HT_BLINKS;
}
if(ppm < 0)
{
ppm = 0;
}
if(temp <= -100.0f || temp >= 100.0f)
{
temp_num = sml_temp_num;
}
int humd_x = 65, humd_f_size = FONT_MED;
if((int)humd >= 100)
{
humd = 100.0f;
humd_num = sml_humd_num;
humd_f_size = FONT_SML;
humd_x = 79;
}
else if((int)humd <= 0)
{
humd = 0.0f;
}
multiplexInt(ppm, (const unsigned char**)co2_num, FONT_BIG, 195, 99);
multiplexInt((int)humd, (const unsigned char**)humd_num, humd_f_size, humd_x, 214);
multiplexTempFloat(temp, (const unsigned char**)temp_num,
(const unsigned char**)sml_temp_num, temp_dot, temp_minus, 285, 214);
if(poweroff_pending)
{
multiplex32(x_button, 282, 0);
}
drawScrBuffer();
if(co2_warning_song != SNG_NONE && !co2_sound_warned && co2_blinks <= 0)
{
// Critical Section Beg
pthread_mutex_lock(&co2_warning_song_lock);
buzzPlay(co2_warning_song);
pthread_mutex_unlock(&co2_warning_song_lock);
// Critical Section End
co2_sound_warned = true;
}
readings_are_updating = false;
}
void onLCD()
{
lcd_is_on = true;
EXIT_SLEEP();
delay(120); // Must wait at least 120 ms until next ENTER_SLEEP command
digitalWrite(LED_PIN, 1);
}
void offLCD()
{
lcd_is_on = false;
digitalWrite(LED_PIN, 0);
ENTER_SLEEP();
}
// Internal functions
int spiOpenPort(int spi_device)
{
int *spi_fs;
static int tries = 0;
tries = 0;
// SPI_MODE_0 (0,0) CPOL = 0, CPHA = 0
// Clock idle low, data is clocked in on rising edge, output data (change) on falling edge
unsigned char spi_mode = SPI_MODE_0;
unsigned char spi_bits_per_word = BITS_PER_WORD;
unsigned int max_spd = spi0_speed;
if (spi_device)
{
spi_fs = &spi1_fs;
}
else
{
spi_fs = &spi0_fs;
}
jmp_retry:
if (spi_device)
{
*spi_fs = open("/dev/spidev0.1", O_RDWR);
}
else
{
*spi_fs = open("/dev/spidev0.0", O_RDWR);
}
if (*spi_fs < 0)
{
char msg[80];
sprintf(msg, "ILI9341: Error - Could not open SPI%d device [try %d]", spi_device, tries);
logError(msg, errno);
delay(1000);
++tries;
goto jmp_retry;
//return -1;
}
int res = 0;
res += ioctl(*spi_fs, SPI_IOC_WR_MODE, &spi_mode);
res += ioctl(*spi_fs, SPI_IOC_RD_MODE, &spi_mode);
res += ioctl(*spi_fs, SPI_IOC_WR_BITS_PER_WORD, &spi_bits_per_word);
res += ioctl(*spi_fs, SPI_IOC_RD_BITS_PER_WORD, &spi_bits_per_word);
res += ioctl(*spi_fs, SPI_IOC_WR_MAX_SPEED_HZ, &max_spd);
res += ioctl(*spi_fs, SPI_IOC_RD_MAX_SPEED_HZ, &max_spd);
if(res < 0)
{
logError("ILI9341: Something went wrong with SPI%d initialisation\n", errno);
return -1;
}
return 0;
}
int spiCommand(unsigned char cmd)
{
digitalWrite(DC_PIN, 0);
spi0.cs_change = 0;
spi0.len = 1;
spi0.tx_buf = (unsigned long)&cmd;
spi0.rx_buf = 0;
return ioctl(spi0_fs, SPI_IOC_MESSAGE(1), &spi0);
}
int spiData(const unsigned char* dat, int s)
{
digitalWrite(DC_PIN, 1);
spi0.cs_change = 0;
spi0.len = s;
spi0.tx_buf = (unsigned long)dat;
spi0.rx_buf = 0;
return ioctl(spi0_fs, SPI_IOC_MESSAGE(1), &spi0);
}
void drawScrBuffer()
{
spiCommand(0x2C /*Memory Write*/);
spi0.cs_change = 1;
spi0.len = BURST_BS;
spi0.rx_buf = 0;
digitalWrite(DC_PIN, 1); // Switch to data mode
for(int i = 0; i < lcd_size; i+=BURST_BS)
{
spi0.tx_buf = (unsigned long)&frame_buffer[i];
ioctl(spi0_fs, SPI_IOC_MESSAGE(1), &spi0);
}
spiCommand(0x00 /*NOP - No Operation*/);
}
void hardwareReset()
{
digitalWrite(RESET_PIN, 1);
delay(120);
digitalWrite(RESET_PIN, 0);
delay(120);
digitalWrite(RESET_PIN, 1);
delay(120);
}
// Screen was pressed WARNING: this runs in WiringPi ISR, thread sefety must be enforced!
// BUT! Considering that this ISR will be invoked only very rarely, I replaced costly hardware locks with
// simple custom software boolean locks and just blocked LCD updates while screen is pressed.
void onTouchInput()
{
// Response:
// 0111 1111 1111 1000 => Only 12 bits are data, skip 1 most sig. bit and 3 least sig. bits
if(digitalRead(TOUCH_IRQ_PIN))
{
return;
}
while(readings_are_updating)
{
delay(10);
}
update_allowed = false; // Block any LCD updates while touch screen input is processed
unsigned char rtx[3] = {0};
int x = 0, y = 0;
#ifdef CALC_TOUCH_PRESSURE
int x_raw = 0, z1 = 0, z2 = 0;;
float prs = 0.0f; // Pressure
#endif
spi1.tx_buf = (unsigned long)rtx;
spi1.rx_buf = (unsigned long)rtx;
bool x_button_drawn = false;
while(!digitalRead(TOUCH_IRQ_PIN))
{
rtx[1] = rtx[2] = 0;
rtx[0] = TOUCH_X;
ioctl(spi1_fs, SPI_IOC_MESSAGE(1), &spi1);
#ifdef CALC_TOUCH_PRESSURE
x_raw = EXTRACT_TOUCH_DATA();
y = (x_raw - touch_x_offset)/touch_x_factor; // Touch Scr. X = Screen Y
#else
y = (EXTRACT_TOUCH_DATA() - touch_x_offset)/touch_x_factor;
#endif
rtx[1] = rtx[2] = 0;
rtx[0] = TOUCH_Y;
ioctl(spi1_fs, SPI_IOC_MESSAGE(1), &spi1);
x = (EXTRACT_TOUCH_DATA() - touch_y_offset)/touch_y_factor; // Touch Scr. Y = Screen X
#ifdef CALC_TOUCH_PRESSURE
rtx[1] = rtx[2] = 0;
rtx[0] = TOUCH_Z1;
ioctl(spi1_fs, SPI_IOC_MESSAGE(1), &spi1);
z1 = EXTRACT_TOUCH_DATA();
rtx[1] = rtx[2] = 0;
rtx[0] = TOUCH_Z2;
ioctl(spi1_fs, SPI_IOC_MESSAGE(1), &spi1);
z2 = EXTRACT_TOUCH_DATA();
prs = 1000.0f * (float)x_raw/4096.0f * ((float)z2/z1 - 1.0f);
#endif
if(lcd_is_on && x >= 282 && y <= 37 && !x_button_drawn)
{
x_button_drawn = true;
multiplex32(x_button, 282, 0);
drawScrBuffer();
}
else if(lcd_is_on && x < 282 && y > 37 && x_button_drawn)
{
x_button_drawn = false;
eraseX();
}
delay(100); // 10 Hz touch screen sampling rate
}
if(lcd_is_on && x >= 282 && y <= 37) // Top right corner, X-icon
{
if(poweroff_pending)
{
poweroff_pending = false;
allow_poweroff = true;
raise(SIGINT);
}
else
{
poweroff_pending = true;
multiplex32(x_button, 282, 0);
drawScrBuffer();
}
}
else
{
if(poweroff_pending)
{
poweroff_pending = false;
eraseX();
update_allowed = true;
delay(120);
return;
}
if(lcd_is_on)
{
lcd_is_on = false;
digitalWrite(LED_PIN, 0);
eraseX();
ENTER_SLEEP();
delay(120); // Must wait at least 120 ms until next EXIT_SLEEP command
}
else
{
lcd_is_on = true;
EXIT_SLEEP();
delay(120); // Must wait at least 120 ms until next ENTER_SLEEP command
digitalWrite(LED_PIN, 1);
}
}
delay(80); // Wait some time to not mess up 2 LCD redraw commands together
update_allowed = true;
}
unsigned char* loadPGM(const char* path, int dye, bool alpha)
{
FILE* img = fopen(path, "rb");
char wa[4] = {0}, ha[4] = {0}; // Max width/height -> 9999
fseek(img, 3, SEEK_SET);
char c;
for(int i = 0; i < 5; ++i) // i < 5 not 4, to catch 0x20 for w with 4 digits
{
fread((void*)&c, 1, 1, img);
if(c == 0x20)
{
int* tmp = (int*)wa;
*tmp <<= (4 - i) * 8; // This will effectively shift all arr. elements to its end
break;
}
wa[i] = B(c & 0x0F); // For example: 0x37 -> 0x07 aka char to number
}
for(int i = 0; i < 5; ++i)
{
fread((void*)&c, 1, 1, img);
if(c == 0x0A)
{
int* tmp = (int*)ha;
*tmp <<= (4 - i) * 8; // Direction of shift must be reversed cos memory holds/reads ints in little endian...
break; // ...while char array holds decimal numbers in big endian mode!
}
ha[i] = B(c & 0x0F);
}
int w = wa[0] * 1000 + wa[1] * 100 + wa[2] * 10 + wa[3];
int h = ha[0] * 1000 + ha[1] * 100 + ha[2] * 10 + ha[3];
c = 0;
while(c != 0x0A)
{
fread((void*)&c, 1, 1, img);
}
const int mp = alpha ? 3 : 2; // Multiplier
// Screen memory accepts pixels in 2 B form -> 5 red 6 green 5 blue bits
int size = w * h * mp + 4; // First 4 Bs reserved for width/height, so increse size by 4
unsigned char* dst_img = (unsigned char*)malloc(size);
unsigned short* wp = (unsigned short*)dst_img;
unsigned short* hp = (unsigned short*)(dst_img+2);
*wp = (unsigned short)w;
*hp = (unsigned short)h;
unsigned char gp; // Grey Pixel
for(int i = 4; i < size; i+=mp) // Data starts from 5th element
{
fread((void*)&gp, 1, 1, img);
switch(dye)
{
case BLACK_DYE:
if(!alpha)
{
unsigned char inverse = B(0xFF - gp);
dst_img[i] = B(B(inverse & 0xF8) | B(inverse >> 5));
dst_img[i+1] = B(B(B(inverse & 0xFC) << 3) | B(inverse >> 3));
}
else
{
dst_img[i] = 0x00;
dst_img[i+1] = 0x00;
dst_img[i+2] = gp;
}
break;
case RED_DYE:
if(!alpha)
{
dst_img[i] = 0x00;
dst_img[i+1] = B(gp >> 3);
}
else
{
dst_img[i] = 0x00;
dst_img[i+1] = 0x1F;
dst_img[i+2] = gp;
}
break;
case BLUE_DYE:
if(!alpha)
{
dst_img[i] = B(gp & 0xF8);
dst_img[i+1] = 0x00;
}
else
{
dst_img[i] = 0xF1;
dst_img[i+1] = 0x00;
dst_img[i+2] = gp;
}
break;
case WHITE_DYE:
case NO_DYE:
default:
if(!alpha)
{
dst_img[i] = B(B(gp & 0xF8) | B(gp >> 5));
dst_img[i+1] = B(B(B(gp & 0xFC) << 3) | B(gp >> 3));
}
else
{
dst_img[i] = 0xFF;
dst_img[i+1] = 0xFF;
dst_img[i+2] = gp;
}
break;
}
}
fclose(img);
return dst_img;
}
unsigned char* loadPAM(const char* path)
{
FILE* img = fopen(path, "rb");
char wa[4] = {0}, ha[4] = {0};
fseek(img, 0x09, SEEK_SET);
char c;
for(int i = 0; i < 5; ++i)
{
fread((void*)&c, 1, 1, img);
if(c == 0x0A)
{
int* tmp = (int*)wa;
*tmp <<= (4 - i) * 8;
break;
}
wa[i] = B(c & 0x0F);
}
fseek(img, 0x13, SEEK_SET);
for(int i = 0; i < 5; ++i)
{
fread((void*)&c, 1, 1, img);
if(c == 0x0A)
{
int* tmp = (int*)ha;
*tmp <<= (4 - i) * 8;
break;
}
ha[i] = B(c & 0x0F);
}
int w = wa[0] * 1000 + wa[1] * 100 + wa[2] * 10 + wa[3];
int h = ha[0] * 1000 + ha[1] * 100 + ha[2] * 10 + ha[3];
c = 0;
int lf_count = 0;
while(lf_count < 4)
{
fread((void*)&c, 1, 1, img);
lf_count += c == 0x0A ? 1 : 0;
}
int size = w * h * 3 + 4;
unsigned char* dst_img = (unsigned char*)malloc(size);
unsigned short* wp = (unsigned short*)dst_img;
unsigned short* hp = (unsigned short*)(dst_img+2);
*wp = (unsigned short)w;
*hp = (unsigned short)h;
unsigned char pix[4];
for(int i = 4; i < size; i+=3)
{
fread((void*)pix, 1, 4, img);
dst_img[i] = B(B(pix[2] & 0xF8) | B(pix[1] >> 5));
dst_img[i+1] = B(B(B(pix[1] & 0xFC) << 3) | B(pix[0] >> 3));
dst_img[i+2] = pix[3]; // Alpha Channel
}
fclose(img);
return dst_img;
}
unsigned char* createBox(const unsigned char* orig_img)
{
int w = *(unsigned short*)orig_img;
int h = *(unsigned short*)(orig_img+2);
int s = w * h * 2 + 4;
unsigned char* black_box = (unsigned char*)malloc(s);
memset((void*)black_box, 0, s);
unsigned short* wp = (unsigned short*)black_box;
unsigned short* hp = (unsigned short*)(black_box+2);
*wp = (unsigned short)w;
*hp = (unsigned short)h;
if(box_should_be_red)
{
for(int i = 5; i < s; i+=2)
{
black_box[i] = 0x1F;
}
}
return black_box;
}
// Replaces pixels of screen buffer at position x/y with img pixels
void multiplex24(const unsigned char* img, int x, int y)
{
if(x < 0 || y < 0)
{
return;
}
const int fbw = SCR_WIDTH;
const int fbh = SCR_HEIGHT;
const int w = *(unsigned short*)img;
const int h = *(unsigned short*)(img+2);
// Data starts from 5th element, first 4 reserved for width/height
unsigned short* im = (unsigned short*)(img+4);
unsigned short* fb = (unsigned short*)frame_buffer;
for(int i = y, ii = 0; i < fbh && ii < h; ++i, ++ii)
{
for(int j = x, jj = 0; j < fbw && jj < w; ++j, ++jj)
{
fb[j + i*fbw] = im[jj + ii*w];
}
}
}
void multiplex32(const unsigned char* img, int x, int y)
{
if(x < 0 || y < 0)
{
return;
}
const int fbw = SCR_WIDTH;
const int fbh = SCR_HEIGHT;
const int w = *(unsigned short*)img;
const int h = *(unsigned short*)(img+2);
// Data starts from 5th element, first 4 reserved for width/height
Pixel* im = (Pixel*)(img+4);
unsigned short* fb = (unsigned short*)frame_buffer;
for(int i = y, ii = 0; i < fbh && ii < h; ++i, ++ii)
{
for(int j = x, jj = 0; j < fbw && jj < w; ++j, ++jj)
{
unsigned short* fb_p = fb + j + i*fbw; // Frame Buffer Pixel
Pixel* im_p = im + jj + ii*w; // Image Pixel
unsigned char* fb_pb = (unsigned char*)fb_p; // Frame Buffer Pixel Bs
if(im_p->a == 0xFF) // This is solid pixel, just replace it
{
*fb_p = *(unsigned short*)im_p;
continue;
}
else if(!im_p->a) // This is empty pixel, skip it
{
continue;
}
// Alpha Blending Routine
float scale = im_p->a/255.0f;
unsigned char r = B(B(RED(fb_pb[0], fb_pb[1]) * (1.0f - scale)) + B(RED(im_p->rg, im_p->gb) * scale));
unsigned char g = B(B(GREEN(fb_pb[0], fb_pb[1]) * (1.0f - scale)) + B(GREEN(im_p->rg, im_p->gb) * scale));
unsigned char b = B(B(BLUE(fb_pb[0], fb_pb[1]) * (1.0f - scale)) + B(BLUE(im_p->rg, im_p->gb) * scale));
fb_pb[0] = B(B(r & 0xF8) | B(g >> 5));
fb_pb[1] = B(B(B(g & 0xFC) << 3) | B(b >> 3));
}
}
}
void multiplexInt(int num, const unsigned char *const *font, int f_size, int x, int y)
{
int w_max; // Width Max
switch(f_size)
{
case FONT_SML:
w_max = 24;
break;
case FONT_MED:
w_max = 44;
break;
case FONT_BIG:
default:
w_max = 55;
break;
}
char snum[42];
sprintf(snum, "%d", num);
int size = strlen(snum) - 1;
for(int i = size; i >= 0; --i)
{
int w, h;
h = *(unsigned short*)(font[snum[i]&0x0F]+2);
if(i < size)
{
w = *(unsigned short*)font[snum[i]&0x0F];
x -= w + 4 + (w_max-w)/2 + (w_max-w)%2;
}
multiplex32(font[snum[i]&0x0F], x, y-h);
if(i < size)
{
x -= (w_max-w)/2;
}
}
}
void multiplexTempFloat(float num, const unsigned char *const *font, const unsigned char *const *smol_font,
const unsigned char* dot, const unsigned char* minus, int x, int y)
{
int w_max = num >= 100.0f ? 24 : 44;
char snum[42];