forked from BrandMeister/DisplayServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFTSurenoo.cpp
359 lines (287 loc) · 9.4 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
/*
* 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 "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
#include <unistd.h>
/*
* 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)
*/
// x = 0 to 159, y = 0 to 127 - Landscape
// x = 0 to 127, y = 0 to 159 - Portrait
#define X_WIDTH 160
#define Y_WIDTH 128
#define ROTATION_PORTRAIT 0
#define ROTATION_LANDSCAPE 1
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 FONT_SMALL 16 // 8x16
#define FONT_MEDIUM 24 // 12x24
#define FONT_LARGE 32 // 16x32
#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 FONT_MEDIUM
#define STATUS_FONT_SIZE FONT_SMALL
#define STATUS_MARGIN 32 // pixel
#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_MMDVM "MMDVM"
CTFTSurenoo::CTFTSurenoo(const std::string& callsign, unsigned int dmrid, ISerialPort* serial, unsigned int brightness, bool duplex) :
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_temp()
{
assert(serial != NULL);
}
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::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::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%u %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 class 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%u", slotNo);
setStatusLine(statusLineNo(pos * 2 + 1), m_temp);
} else {
for (int i = 1; i < STATUS_LINES; i++)
setStatusLine(statusLineNo(i), "");
}
}
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_LANDSCAPE);
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)
{
::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)
{
::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)
{
::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)
{
::snprintf(m_temp, sizeof(m_temp), "BL(%d);", brightness);
m_serial->write((unsigned char*)m_temp, (unsigned int)::strlen(m_temp));
}