-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathTFTSurenoo.cpp
544 lines (428 loc) · 13.9 KB
/
TFTSurenoo.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
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
* Copyright (C) 2019 by SASANO Takayoshi JG1UAA
* Copyright (C) 2015,2016,2018,2019,2020 by Jonathan Naylor G4KLX
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "TFTSurenoo.h"
#include "Thread.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
/*
* UART-TFT LCD Driver for Surenoo JC22-V05 (128x160)
* other Surenoo UART-LCD will be work, but display area is still 160x128
* (tested with JC028-V03 240x320 module)
*/
struct layoutdef {
int x_width;
int y_width;
int rotation;
int mode_font_size;
int status_font_size;
int status_margin;
};
#define ROTATION_PORTRAIT 0
#define ROTATION_LANDSCAPE 1
#define FONT_SMALL 16 // 8x16
#define FONT_MEDIUM 24 // 12x24
#define FONT_LARGE 32 // 16x32
static const struct layoutdef Layout[] = {
{160, 128, ROTATION_LANDSCAPE, FONT_MEDIUM, FONT_SMALL, 32},
{128, 160, ROTATION_PORTRAIT, FONT_MEDIUM, FONT_SMALL, 32},
{320, 240, ROTATION_LANDSCAPE, FONT_LARGE, FONT_MEDIUM, 48},
{240, 320, ROTATION_PORTRAIT, FONT_LARGE, FONT_MEDIUM, 48},
};
#define X_WIDTH Layout[m_screenLayout].x_width
#define Y_WIDTH Layout[m_screenLayout].y_width
#define ROTATION Layout[m_screenLayout].rotation
enum LcdColour {
COLOUR_BLACK, COLOUR_RED, COLOUR_GREEN, COLOUR_BLUE,
COLOUR_YELLOW, COLOUR_CYAN, COLOUR_MAGENTA, COLOUR_GREY,
COLOUR_DARK_GREY, COLOUR_DARK_RED, COLOUR_DARK_GREEN, COLOUR_DARK_BLUE,
COLOUR_DARK_YELLOW, COLOUR_DARK_CYAN, COLOUR_DARK_MAGENTA, COLOUR_WHITE
};
#define INFO_COLOUR COLOUR_CYAN
#define EXT_COLOUR COLOUR_DARK_GREEN
#define BG_COLOUR COLOUR_BLACK
#define ERROR_COLOUR COLOUR_DARK_RED
#define MODE_COLOUR COLOUR_YELLOW
// MODE_FONT_SIZE should be equal or larger than STATUS_FONT_SIZE
#define MODE_FONT_SIZE Layout[m_screenLayout].mode_font_size
#define STATUS_FONT_SIZE Layout[m_screenLayout].status_font_size
#define STATUS_MARGIN Layout[m_screenLayout].status_margin
#define MODE_CHARS (X_WIDTH / (MODE_FONT_SIZE / 2))
#define STATUS_CHARS (X_WIDTH / (STATUS_FONT_SIZE / 2))
#define STATUS_LINES ((Y_WIDTH - STATUS_MARGIN) / STATUS_FONT_SIZE)
#define statusLineOffset(x) ((STATUS_CHARS + 1) * ((x) + 1))
#define statusLineNo(x) (x)
#define INFO_LINES statusLineNo(2)
// This module sometimes ignores display command (too busy?),
// so supress display refresh
#define REFRESH_PERIOD 600 // msec
#define STR_CRLF "\x0D\x0A"
#define STR_DMR "DMR"
#define STR_DSTAR "D-STAR"
#define STR_MMDVM "MMDVM"
#define STR_NXDN "NXDN"
#define STR_M17 "M17"
#define STR_P25 "P25"
#define STR_YSF "SystemFusion"
CTFTSurenoo::CTFTSurenoo(const std::string& callsign, unsigned int dmrid, ISerialPort* serial, unsigned int brightness, bool duplex, unsigned int screenLayout) :
CDisplay(),
m_callsign(callsign),
m_dmrid(dmrid),
m_serial(serial),
m_brightness(brightness),
m_mode(MODE_IDLE),
m_duplex(duplex),
//m_duplex(true), // uncomment to force duplex display for testing!
m_refresh(false),
m_refreshTimer(1000U, 0U, REFRESH_PERIOD),
m_lineBuf(NULL),
m_screenLayout(screenLayout)
{
assert(serial != NULL);
assert(brightness >= 0U && brightness <= 255U);
}
CTFTSurenoo::~CTFTSurenoo()
{
}
bool CTFTSurenoo::open()
{
bool ret = m_serial->open();
if (!ret) {
LogError("Cannot open the port for the TFT Serial");
delete m_serial;
return false;
}
m_lineBuf = new char[statusLineOffset(STATUS_LINES)];
if (m_lineBuf == NULL) {
LogError("Cannot allocate line buffer");
m_serial->close();
delete m_serial;
return false;
}
lcdReset();
clearScreen(BG_COLOUR);
setIdle();
m_refreshTimer.start();
return true;
}
void CTFTSurenoo::setIdleInt()
{
setModeLine(STR_MMDVM);
::snprintf(m_temp, sizeof(m_temp), "%s / %u", m_callsign.c_str(), m_dmrid);
setStatusLine(statusLineNo(0), m_temp);
setStatusLine(statusLineNo(1), "IDLE");
m_mode = MODE_IDLE;
}
void CTFTSurenoo::setErrorInt(const char* text)
{
assert(text != NULL);
setModeLine(STR_MMDVM);
setStatusLine(statusLineNo(0), text);
setStatusLine(statusLineNo(1), "ERROR");
m_mode = MODE_ERROR;
}
void CTFTSurenoo::setLockoutInt()
{
setModeLine(STR_MMDVM);
setStatusLine(statusLineNo(1), "LOCKOUT");
m_mode = MODE_LOCKOUT;
}
void CTFTSurenoo::setQuitInt()
{
// m_refreshTimer has stopped, need delay here
CThread::sleep(REFRESH_PERIOD);
setModeLine(STR_MMDVM);
setStatusLine(statusLineNo(1), "STOPPED");
refreshDisplay();
m_mode = MODE_QUIT;
}
void CTFTSurenoo::setFMInt()
{
setModeLine(STR_MMDVM);
setStatusLine(statusLineNo(1), "FM");
m_mode = MODE_FM;
}
void CTFTSurenoo::writeDStarInt(const char* my1, const char* my2, const char* your, const char* type, const char* reflector)
{
assert(my1 != NULL);
assert(my2 != NULL);
assert(your != NULL);
assert(type != NULL);
assert(reflector != NULL);
setModeLine(STR_MMDVM);
::snprintf(m_temp, sizeof(m_temp), "%s %s/%s", type, my1, my2);
setStatusLine(statusLineNo(0), m_temp);
::snprintf(m_temp, sizeof(m_temp), "%s", your);
setStatusLine(statusLineNo(1), m_temp);
if (::strcmp(reflector, " ") != 0)
::snprintf(m_temp, sizeof(m_temp), "via %s", reflector);
else
::strcpy(m_temp, "");
setStatusLine(statusLineNo(2), m_temp);
m_mode = MODE_DSTAR;
}
void CTFTSurenoo::clearDStarInt()
{
setStatusLine(statusLineNo(0), "Listening");
for (int i = 1; i < STATUS_LINES; i++)
setStatusLine(statusLineNo(i), "");
}
void CTFTSurenoo::writeDMRInt(unsigned int slotNo, const std::string& src, bool group, const std::string& dst, const char* type)
{
assert(type != NULL);
if (m_mode != MODE_DMR) {
setModeLine(STR_DMR);
if (m_duplex) {
setStatusLine(statusLineNo(0), "Listening");
setStatusLine(statusLineNo(1), "TS1");
setStatusLine(statusLineNo(2), "Listening");
setStatusLine(statusLineNo(3), "TS2");
}
}
int pos = m_duplex ? (slotNo - 1) : 0;
::snprintf(m_temp, sizeof(m_temp), "%s %s", type, src.c_str());
setStatusLine(statusLineNo(pos * 2), m_temp);
::snprintf(m_temp, sizeof(m_temp), "TS%d %s%s", slotNo, group ? "TG" : "", dst.c_str());
setStatusLine(statusLineNo(pos * 2 + 1), m_temp);
m_mode = MODE_DMR;
}
int CTFTSurenoo::writeDMRIntEx(unsigned int slotNo, const CUserDBentry& src, bool group, const std::string& dst, const char* type)
{
assert(type != NULL);
// duplex mode is not supported
if (m_duplex)
return -1;
setModeLine(STR_DMR);
setStatusLine(statusLineNo(2), (src.get(keyFIRST_NAME) + " " + src.get(keyLAST_NAME)).c_str());
setStatusLine(statusLineNo(3), src.get(keyCITY).c_str());
setStatusLine(statusLineNo(4), src.get(keySTATE).c_str());
setStatusLine(statusLineNo(5), src.get(keyCOUNTRY).c_str());
m_mode = MODE_DMR;
return 1;
}
void CTFTSurenoo::clearDMRInt(unsigned int slotNo)
{
int pos = m_duplex ? (slotNo - 1) : 0;
setStatusLine(statusLineNo(pos * 2), "Listening");
if (m_duplex) {
::snprintf(m_temp, sizeof(m_temp), "TS%d", slotNo);
setStatusLine(statusLineNo(pos * 2 + 1), m_temp);
} else {
for (int i = 1; i < STATUS_LINES; i++)
setStatusLine(statusLineNo(i), "");
}
}
void CTFTSurenoo::writeFusionInt(const char* source, const char* dest, unsigned char dgid, const char* type, const char* origin)
{
assert(source != NULL);
assert(dest != NULL);
assert(type != NULL);
assert(origin != NULL);
setModeLine(STR_YSF);
::snprintf(m_temp, sizeof(m_temp), "%s %s", type, source);
setStatusLine(statusLineNo(0), m_temp);
::snprintf(m_temp, sizeof(m_temp), "DG-ID %u", dgid);
setStatusLine(statusLineNo(1), m_temp);
if (::strcmp(origin, " ") != 0)
::snprintf(m_temp, sizeof(m_temp), "at %s", origin);
else
::strcpy(m_temp, "");
setStatusLine(statusLineNo(2), m_temp);
m_mode = MODE_YSF;
}
void CTFTSurenoo::clearFusionInt()
{
clearDStarInt();
}
void CTFTSurenoo::writeP25Int(const char* source, bool group, unsigned int dest, const char* type)
{
assert(source != NULL);
assert(type != NULL);
setModeLine(STR_P25);
::snprintf(m_temp, sizeof(m_temp), "%s %s", type, source);
setStatusLine(statusLineNo(0), m_temp);
::snprintf(m_temp, sizeof(m_temp), "%s%u", group ? "TG" : "", dest);
setStatusLine(statusLineNo(1), m_temp);
m_mode = MODE_P25;
}
void CTFTSurenoo::clearP25Int()
{
clearDStarInt();
}
void CTFTSurenoo::writeNXDNInt(const char* source, bool group, unsigned int dest, const char* type)
{
assert(source != NULL);
assert(type != NULL);
if (m_mode != MODE_NXDN)
setModeLine(STR_NXDN);
::snprintf(m_temp, sizeof(m_temp), "%s %s", type, source);
setStatusLine(statusLineNo(0), m_temp);
::snprintf(m_temp, sizeof(m_temp), "%s%u", group ? "TG" : "", dest);
setStatusLine(statusLineNo(1), m_temp);
m_mode = MODE_NXDN;
}
int CTFTSurenoo::writeNXDNIntEx(const CUserDBentry& source, bool group, unsigned int dest, const char* type)
{
assert(type != NULL);
setModeLine(STR_NXDN);
setStatusLine(statusLineNo(2), (source.get(keyFIRST_NAME) + " " + source.get(keyLAST_NAME)).c_str());
setStatusLine(statusLineNo(3), source.get(keyCITY).c_str());
setStatusLine(statusLineNo(4), source.get(keySTATE).c_str());
setStatusLine(statusLineNo(5), source.get(keyCOUNTRY).c_str());
m_mode = MODE_NXDN;
return 1;
}
void CTFTSurenoo::clearNXDNInt()
{
clearDStarInt();
}
void CTFTSurenoo::writeM17Int(const char* source, const char* dest, const char* type)
{
assert(source != NULL);
assert(dest != NULL);
assert(type != NULL);
if (m_mode != MODE_M17)
setModeLine(STR_M17);
::snprintf(m_temp, sizeof(m_temp), "%s %s", type, source);
setStatusLine(statusLineNo(0), m_temp);
::snprintf(m_temp, sizeof(m_temp), "%s", dest);
setStatusLine(statusLineNo(1), m_temp);
m_mode = MODE_M17;
}
void CTFTSurenoo::clearM17Int()
{
clearDStarInt();
}
void CTFTSurenoo::writePOCSAGInt(uint32_t ric, const std::string& message)
{
setStatusLine(statusLineNo(1), "POCSAG TX");
m_mode = MODE_POCSAG;
}
void CTFTSurenoo::clearPOCSAGInt()
{
setStatusLine(statusLineNo(1), "IDLE");
}
void CTFTSurenoo::writeCWInt()
{
setStatusLine(statusLineNo(1), "CW TX");
m_mode = MODE_CW;
}
void CTFTSurenoo::clearCWInt()
{
setStatusLine(statusLineNo(1), "IDLE");
}
void CTFTSurenoo::close()
{
delete[] m_lineBuf;
m_serial->close();
delete m_serial;
}
void CTFTSurenoo::clockInt(unsigned int ms)
{
m_refreshTimer.clock(ms); // renew timer status
if (m_refreshTimer.isRunning() && m_refreshTimer.hasExpired()) {
refreshDisplay();
m_refreshTimer.start(); // reset timer, wait for next period
}
}
void CTFTSurenoo::setLineBuffer(char *buf, const char *text, int maxchar)
{
int i;
for (i = 0; i < maxchar && text[i] != '\0'; i++)
buf[i] = text[i];
buf[i] = '\0';
m_refresh = true;
}
void CTFTSurenoo::setModeLine(const char *text)
{
setLineBuffer(m_lineBuf, text, MODE_CHARS);
// clear all status line
for (int i = 0; i < STATUS_LINES; i++) setStatusLine(i, "");
}
void CTFTSurenoo::setStatusLine(unsigned int line, const char *text)
{
setLineBuffer(m_lineBuf + statusLineOffset(line), text, STATUS_CHARS);
}
void CTFTSurenoo::refreshDisplay(void)
{
if (!m_refresh) return;
// send CR+LF to avoid first command is not processed
::snprintf(m_temp, sizeof(m_temp), STR_CRLF);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
// config display
setRotation(ROTATION);
setBrightness(m_brightness);
setBackground(BG_COLOUR);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
CThread::sleep(5);
// clear display
::snprintf(m_temp, sizeof(m_temp), "BOXF(%d,%d,%d,%d,%d);",
0, 0, X_WIDTH - 1, Y_WIDTH - 1, BG_COLOUR);
m_serial->write((unsigned char*)m_temp, ::strlen(m_temp));
// mode line
::snprintf(m_temp, sizeof(m_temp), "DCV%d(%d,%d,'%s',%d);",
MODE_FONT_SIZE, 0, 0, m_lineBuf, MODE_COLOUR);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
// status line
for (int i = 0; i < STATUS_LINES; i++) {
char *p = m_lineBuf + statusLineOffset(i);
if (!::strlen(p)) continue;
::snprintf(m_temp, sizeof(m_temp), "DCV%d(%d,%d,'%s',%d);",
STATUS_FONT_SIZE, 0,
STATUS_MARGIN + STATUS_FONT_SIZE * i, p,
(!m_duplex && i >= INFO_LINES) ? EXT_COLOUR : INFO_COLOUR);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
}
// sending CR+LF finishes commands
::snprintf(m_temp, sizeof(m_temp), STR_CRLF);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
m_refresh = false;
}
void CTFTSurenoo::lcdReset(void)
{
::snprintf(m_temp, sizeof(m_temp), "RESET;" STR_CRLF);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
CThread::sleep(250); // document says 230ms
}
void CTFTSurenoo::clearScreen(unsigned char colour)
{
assert(colour >= 0U && colour <= 63U);
::snprintf(m_temp, sizeof(m_temp), "CLR(%d);" STR_CRLF, colour);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
CThread::sleep(100); // at least 60ms (@240x320 panel)
}
void CTFTSurenoo::setBackground(unsigned char colour)
{
assert(colour >= 0U && colour <= 63U);
::snprintf(m_temp, sizeof(m_temp), "SBC(%d);", colour);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
}
void CTFTSurenoo::setRotation(unsigned char rotation)
{
assert(rotation >= 0U && rotation <= 1U);
::snprintf(m_temp, sizeof(m_temp), "DIR(%d);", rotation);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
}
void CTFTSurenoo::setBrightness(unsigned char brightness)
{
assert(brightness >= 0U && brightness <= 255U);
::snprintf(m_temp, sizeof(m_temp), "BL(%d);", brightness);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
}