src/main.cpp

changeset 1
3021fc79cc3b
child 2
d0826e4a1ff7
equal deleted inserted replaced
0:f3377957d8c0 1:3021fc79cc3b
1 #include "stdio.h"
2 #include "mbed.h"
3 #include "string"
4 #include "Arial12x12.h"
5 #include "Arial28x28.h"
6 //#include "Arial24x23.h"
7 //#include "Terminal6x8.h"
8 //#include "Arial43x48_numb.h"
9
10 #include "BufferedSerial.h"
11
12 #include "SPI8.h"
13 #include "ILI9341.h"
14
15 Serial pc(USBTX, USBRX);
16 BufferedSerial hp(PA_0, PA_1, 32); // serial4
17
18 ILI9341 tft(SPI_8, 24000000, PA_7, PA_6, PA_5, PA_4, PB_6, PC_7, "myLCD"); // Spi 8bit, 24MHz, mosi, miso, sclk, cs, reset, dc
19 //ILI9341 tft(SPI_8, 24000000, PA_7, PA_6, PA_5, PA_4, PB_6, PB_5, "myLCD");
20
21 #define MAX_BUFF 13
22 uint8_t curchar;
23 uint8_t cmd;
24 uint8_t nchars;
25 char buffer[MAX_BUFF+1];
26
27 typedef struct _DSP
28 {
29 uint8_t cmd;
30 uint16_t color;
31 uint16_t bgcolor;
32 uint8_t y0;
33 uint8_t fmt; // 0=>ascii, 1=>hex, 2=>bits
34 uint8_t maxsize;
35 const unsigned char* font;
36 char buffer[MAX_BUFF+1];
37 } DSP;
38
39 static DSP table[] =
40 {
41 { 0x00, Yellow, Red, 5, 0, MAX_BUFF, Arial28x28},
42 { 0x0C, Red, Blue, 40, 0, 3, Arial28x28},
43 { 0x01, Blue, Yellow, 75, 1, MAX_BUFF, Arial12x12},
44 { 0x02, Blue, Yellow, 110, 1, MAX_BUFF, Arial12x12},
45 { 0x0A, Blue, Yellow, 145, 2, 4, Arial12x12},
46 { 0xFF, Red, Black, 210, 0, MAX_BUFF, Arial12x12},
47 };
48 #define DEBUG
49 #ifdef DEBUG
50 #define DBGPIN PC_0
51
52 DigitalOut dbgpin(DBGPIN);
53 inline void DebugPulse(uint8_t count=1)
54 {
55 while (count--)
56 {
57 dbgpin = 1;
58 dbgpin = 0;
59 }
60 }
61 #else
62 inline void DebugPulse(uint8_t count=1)
63 {}
64 #endif
65
66 void setup() {
67 // init the LCD
68
69 tft.set_orientation(3);
70 pc.baud (115200);
71 pc.printf("\n\nSystem Core Clock = %.3f MHZ\r\n",
72 (float)SystemCoreClock/1000000);
73
74 hp.baud(187500);
75
76 // myLCD.set_font((unsigned char*) Terminal6x8);
77 // myLCD.claim(stdout); // send stdout to the LCD display
78 // myLCD.claim(stderr); // send stderr to the LCD display
79 tft.background(Black); // set background to black
80 tft.cls();
81
82 cmd = 0xFF;
83 curchar = 0;
84 nchars = 0;
85 }
86
87 void go_msg() {
88 tft.set_font((unsigned char*) Arial12x12);
89 tft.locate(0, 210);
90 }
91
92 void show(uint8_t cmd, char *txt, uint8_t nchar=0) {
93 uint8_t i, len;
94 uint16_t bgcolor, fgcolor;
95 char *oldv;
96
97 len = MAX_BUFF;
98
99 for (i=0; i<sizeof(table)/sizeof(table[0]); ++i) {
100 if (table[i].cmd == cmd) {
101 bgcolor = table[i].bgcolor;
102 fgcolor = table[i].color;
103 tft.background(bgcolor);
104 tft.foreground(fgcolor);
105 tft.locate(0, table[i].y0);
106 tft.set_font((unsigned char*) table[i].font);
107 break;
108 }
109 }
110
111 switch (table[i].fmt) {
112 case 0: //ascii
113 tft.printf(txt);
114 for (uint8_t j=nchar; j<table[i].maxsize; j++)
115 tft.printf(" ");
116 break;
117 case 1: // hex
118 for (uint8_t j=0; j<nchar; j++)
119 tft.printf("%02X ", txt[j]);
120 for (uint8_t j=nchar; j<table[i].maxsize; j++)
121 tft.printf(" ");
122 break;
123 case 2: // binary
124 for (uint8_t j=0; j<max(nchar, table[i].maxsize) ; j++) {
125 //tft.setCursor(x0+w*i, y0+10);
126 for (uint8_t k=0; k<8; k++) {
127 if (txt[j] & (1 << (7-k)))
128 tft.foreground(fgcolor);
129 else
130 tft.foreground(bgcolor);
131 tft.printf("%d", (8-k));
132 }
133 }
134 break;
135 }
136 }
137
138
139 void loop() { // run over and over
140 if (hp.readable()) {
141 uint8_t val = hp.getc();
142 DebugPulse();
143 // 0xFF: idle
144 // 0xFE: frame finished
145 // 0x66: Start of transmission received
146 if (cmd == 0xFF) {
147 if ((val == 0x66) || (val == 0x99)) {
148 cmd = val;
149 } else if ((val == 0x00) || (val == 0x55)){
150 // probably an ACK for a keypad related event
151 } else {
152 // display "junk" byte
153 //DebugPulse();
154 go_msg();
155 tft.printf("%02X:> %02x", cmd, val);
156 }
157
158 } else if (cmd == 0xFE ) {
159 if ((val == 0x66) || (val == 0x99)) {
160 cmd = val;
161 } else if (val == 0x55) {
162 cmd = 0xFF;
163 } else if (val == 0x00){
164 // probably an ACK for a keypad related event
165 } else {
166 // display "junk" byte
167 //DebugPulse();
168 go_msg();
169 tft.printf("%02X=> %02x", cmd, val);
170 }
171
172 } else if (cmd == 0x66) { // waiting for the display command
173 if ((val == 0x0C) || (val == 0x00) || val == 0x01 || val == 0x02 || val == 0x0A) {
174 cmd = val;
175 nchars = 0;
176 } else {
177 cmd = 0xFE;
178 // display unknown cmd byte
179 //DebugPulse();
180 go_msg();
181 tft.printf("%02X-> %02x", cmd, val);
182 }
183
184 } else if (cmd == 0x99) { // waiting for a 0x00, it's the DP that sent a keypress event
185 if (val != 0x00) {
186 go_msg();
187 tft.printf("%02X kp %02X", cmd, val);
188 }
189 cmd =0xFF;
190
191 } else if (nchars == 0) { // waiting for the number of chars to display
192 if (val>MAX_BUFF) {
193 // display warning
194 //dsp();
195 //tft << cmd << " got len " << val;
196 //DebugPulse();
197 go_msg();
198 tft.printf("%02X len %d", cmd, val);
199 cmd = 0xFE; // weird
200 } else {
201 nchars = val;
202 curchar = 0;
203 }
204
205 } else { // a character to display
206 buffer[curchar] = val;
207 curchar++;
208 if (curchar == nchars) {
209 buffer[curchar] = 0x00;
210 show(cmd, buffer, nchars);
211 nchars = 0;
212 curchar = 0;
213 cmd = 0xFE;
214 }
215 }
216 }
217 }
218
219 int main()
220 {
221 setup();
222 while(1)
223 loop();
224 }

mercurial