forked from g4klx/MMDVMHost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nextion.cpp
581 lines (480 loc) · 12.4 KB
/
Nextion.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
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
/*
* Copyright (C) 2016,2017 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 "Nextion.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
#include <ctime>
#include <clocale>
const unsigned int DSTAR_RSSI_COUNT = 3U; // 3 * 420ms = 1260ms
const unsigned int DSTAR_BER_COUNT = 63U; // 63 * 20ms = 1260ms
const unsigned int DMR_RSSI_COUNT = 4U; // 4 * 360ms = 1440ms
const unsigned int DMR_BER_COUNT = 24U; // 24 * 60ms = 1440ms
const unsigned int YSF_RSSI_COUNT = 13U; // 13 * 100ms = 1300ms
const unsigned int YSF_BER_COUNT = 13U; // 13 * 100ms = 1300ms
const unsigned int P25_RSSI_COUNT = 7U; // 7 * 180ms = 1260ms
const unsigned int P25_BER_COUNT = 7U; // 7 * 180ms = 1260ms
CNextion::CNextion(const std::string& callsign, unsigned int dmrid, ISerialPort* serial, unsigned int brightness, bool displayClock, bool utc, unsigned int idleBrightness) :
CDisplay(),
m_callsign(callsign),
m_dmrid(dmrid),
m_serial(serial),
m_brightness(brightness),
m_mode(MODE_IDLE),
m_displayClock(displayClock),
m_utc(utc),
m_idleBrightness(idleBrightness),
m_clockDisplayTimer(1000U, 0U, 400U),
m_rssiAccum1(0U),
m_rssiAccum2(0U),
m_berAccum1(0.0F),
m_berAccum2(0.0F),
m_rssiCount1(0U),
m_rssiCount2(0U),
m_berCount1(0U),
m_berCount2(0U)
{
assert(serial != NULL);
assert(brightness >= 0U && brightness <= 100U);
}
CNextion::~CNextion()
{
}
bool CNextion::open()
{
bool ret = m_serial->open();
if (!ret) {
LogError("Cannot open the port for the Nextion display");
delete m_serial;
return false;
}
sendCommand("bkcmd=0");
setIdle();
return true;
}
void CNextion::setIdleInt()
{
sendCommand("page MMDVM");
char command[30];
::sprintf(command, "dim=%u", m_idleBrightness);
sendCommand(command);
::sprintf(command, "t0.txt=\"%-6s / %u\"", m_callsign.c_str(), m_dmrid);
sendCommand(command);
sendCommand("t1.txt=\"MMDVM IDLE\"");
m_clockDisplayTimer.start();
m_mode = MODE_IDLE;
}
void CNextion::setErrorInt(const char* text)
{
assert(text != NULL);
sendCommand("page MMDVM");
char command[20];
::sprintf(command, "dim=%u", m_brightness);
sendCommand(command);
::sprintf(command, "t0.txt=\"%s\"", text);
sendCommand(command);
sendCommand("t1.txt=\"ERROR\"");
m_clockDisplayTimer.stop();
m_mode = MODE_ERROR;
}
void CNextion::setLockoutInt()
{
sendCommand("page MMDVM");
char command[20];
::sprintf(command, "dim=%u", m_brightness);
sendCommand(command);
sendCommand("t0.txt=\"LOCKOUT\"");
m_clockDisplayTimer.stop();
m_mode = MODE_LOCKOUT;
}
void CNextion::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);
if (m_mode != MODE_DSTAR)
sendCommand("page DStar");
char text[30U];
::sprintf(text, "dim=%u", m_brightness);
sendCommand(text);
::sprintf(text, "t0.txt=\"%s %.8s/%4.4s\"", type, my1, my2);
sendCommand(text);
::sprintf(text, "t1.txt=\"%.8s\"", your);
sendCommand(text);
if (::strcmp(reflector, " ") != 0) {
::sprintf(text, "t2.txt=\"via %.8s\"", reflector);
sendCommand(text);
}
m_clockDisplayTimer.stop();
m_mode = MODE_DSTAR;
m_rssiAccum1 = 0U;
m_berAccum1 = 0.0F;
m_rssiCount1 = 0U;
m_berCount1 = 0U;
}
void CNextion::writeDStarRSSIInt(unsigned char rssi)
{
if (m_rssiCount1 == 0U) {
char text[20U];
::sprintf(text, "t3.txt=\"-%udBm\"", rssi);
sendCommand(text);
m_rssiCount1 = 1U;
return;
}
m_rssiAccum1 += rssi;
m_rssiCount1++;
if (m_rssiCount1 == DSTAR_RSSI_COUNT) {
char text[20U];
::sprintf(text, "t3.txt=\"-%udBm\"", m_rssiAccum1 / DSTAR_RSSI_COUNT);
sendCommand(text);
m_rssiAccum1 = 0U;
m_rssiCount1 = 1U;
}
}
void CNextion::writeDStarBERInt(float ber)
{
if (m_berCount1 == 0U) {
char text[20U];
::sprintf(text, "t4.txt=\"%.1f%%\"", ber);
sendCommand(text);
m_berCount1 = 1U;
return;
}
m_berAccum1 += ber;
m_berCount1++;
if (m_berCount1 == DSTAR_BER_COUNT) {
char text[20U];
::sprintf(text, "t4.txt=\"%.1f%%\"", m_berAccum1 / float(DSTAR_BER_COUNT));
sendCommand(text);
m_berAccum1 = 0.0F;
m_berCount1 = 1U;
}
}
void CNextion::clearDStarInt()
{
sendCommand("t0.txt=\"Listening\"");
sendCommand("t1.txt=\"\"");
sendCommand("t2.txt=\"\"");
sendCommand("t3.txt=\"\"");
sendCommand("t4.txt=\"\"");
}
void CNextion::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) {
sendCommand("page DMR");
if (slotNo == 1U)
sendCommand("t2.txt=\"2 Listening\"");
else
sendCommand("t0.txt=\"1 Listening\"");
}
char text[30U];
::sprintf(text, "dim=%u", m_brightness);
sendCommand(text);
if (slotNo == 1U) {
::sprintf(text, "t0.txt=\"1 %s %s\"", type, src.c_str());
sendCommand(text);
::sprintf(text, "t1.txt=\"%s%s\"", group ? "TG" : "", dst.c_str());
sendCommand(text);
} else {
::sprintf(text, "t2.txt=\"2 %s %s\"", type, src.c_str());
sendCommand(text);
::sprintf(text, "t3.txt=\"%s%s\"", group ? "TG" : "", dst.c_str());
sendCommand(text);
}
m_clockDisplayTimer.stop();
m_mode = MODE_DMR;
m_rssiAccum1 = 0U;
m_rssiAccum2 = 0U;
m_berAccum1 = 0.0F;
m_berAccum2 = 0.0F;
m_rssiCount1 = 0U;
m_rssiCount2 = 0U;
m_berCount1 = 0U;
m_berCount2 = 0U;
}
void CNextion::writeDMRRSSIInt(unsigned int slotNo, unsigned char rssi)
{
if (slotNo == 1U) {
if (m_rssiCount1 == 0U) {
char text[20U];
::sprintf(text, "t4.txt=\"-%udBm\"", rssi);
sendCommand(text);
m_rssiCount1 = 1U;
return;
}
m_rssiAccum1 += rssi;
m_rssiCount1++;
if (m_rssiCount1 == DMR_RSSI_COUNT) {
char text[20U];
::sprintf(text, "t4.txt=\"-%udBm\"", m_rssiAccum1 / DMR_RSSI_COUNT);
sendCommand(text);
m_rssiAccum1 = 0U;
m_rssiCount1 = 1U;
}
} else {
if (m_rssiCount2 == 0U) {
char text[20U];
::sprintf(text, "t5.txt=\"-%udBm\"", rssi);
sendCommand(text);
m_rssiCount2 = 1U;
return;
}
m_rssiAccum2 += rssi;
m_rssiCount2++;
if (m_rssiCount2 == DMR_RSSI_COUNT) {
char text[20U];
::sprintf(text, "t5.txt=\"-%udBm\"", m_rssiAccum2 / DMR_RSSI_COUNT);
sendCommand(text);
m_rssiAccum2 = 0U;
m_rssiCount2 = 1U;
}
}
}
void CNextion::writeDMRBERInt(unsigned int slotNo, float ber)
{
if (slotNo == 1U) {
if (m_berCount1 == 0U) {
char text[20U];
::sprintf(text, "t6.txt=\"%.1f%%\"", ber);
sendCommand(text);
m_berCount1 = 1U;
return;
}
m_berAccum1 += ber;
m_berCount1++;
if (m_berCount1 == DMR_BER_COUNT) {
char text[20U];
::sprintf(text, "t6.txt=\"%.1f%%\"", m_berAccum1 / DMR_BER_COUNT);
sendCommand(text);
m_berAccum1 = 0U;
m_berCount1 = 1U;
}
} else {
if (m_berCount2 == 0U) {
char text[20U];
::sprintf(text, "t7.txt=\"%.1f%%\"", ber);
sendCommand(text);
m_berCount2 = 1U;
return;
}
m_berAccum2 += ber;
m_berCount2++;
if (m_berCount2 == DMR_BER_COUNT) {
char text[20U];
::sprintf(text, "t7.txt=\"%.1f%%\"", m_berAccum2 / DMR_BER_COUNT);
sendCommand(text);
m_berAccum2 = 0U;
m_berCount2 = 1U;
}
}
}
void CNextion::clearDMRInt(unsigned int slotNo)
{
if (slotNo == 1U) {
sendCommand("t0.txt=\"1 Listening\"");
sendCommand("t1.txt=\"\"");
sendCommand("t4.txt=\"\"");
sendCommand("t6.txt=\"\"");
} else {
sendCommand("t2.txt=\"2 Listening\"");
sendCommand("t3.txt=\"\"");
sendCommand("t5.txt=\"\"");
sendCommand("t7.txt=\"\"");
}
}
void CNextion::writeFusionInt(const char* source, const char* dest, const char* type, const char* origin)
{
assert(source != NULL);
assert(dest != NULL);
assert(type != NULL);
assert(origin != NULL);
if (m_mode != MODE_YSF)
sendCommand("page YSF");
char text[30U];
::sprintf(text, "dim=%u", m_brightness);
sendCommand(text);
::sprintf(text, "t0.txt=\"%s %.10s\"", type, source);
sendCommand(text);
::sprintf(text, "t1.txt=\"%.10s\"", dest);
sendCommand(text);
if (::strcmp(origin, " ") != 0) {
::sprintf(text, "t2.txt=\"at %.10s\"", origin);
sendCommand(text);
}
m_clockDisplayTimer.stop();
m_mode = MODE_YSF;
m_rssiAccum1 = 0U;
m_berAccum1 = 0.0F;
m_rssiCount1 = 0U;
m_berCount1 = 0U;
}
void CNextion::writeFusionRSSIInt(unsigned char rssi)
{
if (m_rssiCount1 == 0U) {
char text[20U];
::sprintf(text, "t3.txt=\"-%udBm\"", rssi);
sendCommand(text);
m_rssiCount1 = 1U;
return;
}
m_rssiAccum1 += rssi;
m_rssiCount1++;
if (m_rssiCount1 == YSF_RSSI_COUNT) {
char text[20U];
::sprintf(text, "t3.txt=\"-%udBm\"", m_rssiAccum1 / YSF_RSSI_COUNT);
sendCommand(text);
m_rssiAccum1 = 0U;
m_rssiCount1 = 1U;
}
}
void CNextion::writeFusionBERInt(float ber)
{
if (m_berCount1 == 0U) {
char text[20U];
::sprintf(text, "t4.txt=\"%.1f%%\"", ber);
sendCommand(text);
m_berCount1 = 1U;
return;
}
m_berAccum1 += ber;
m_berCount1++;
if (m_berCount1 == YSF_BER_COUNT) {
char text[20U];
::sprintf(text, "t4.txt=\"%.1f%%\"", m_berAccum1 / float(YSF_BER_COUNT));
sendCommand(text);
m_berAccum1 = 0.0F;
m_berCount1 = 1U;
}
}
void CNextion::clearFusionInt()
{
sendCommand("t0.txt=\"Listening\"");
sendCommand("t1.txt=\"\"");
sendCommand("t2.txt=\"\"");
sendCommand("t3.txt=\"\"");
sendCommand("t4.txt=\"\"");
}
void CNextion::writeP25Int(const char* source, bool group, unsigned int dest, const char* type)
{
assert(source != NULL);
assert(type != NULL);
if (m_mode != MODE_P25)
sendCommand("page P25");
char text[30U];
::sprintf(text, "dim=%u", m_brightness);
sendCommand(text);
::sprintf(text, "t0.txt=\"%s %.10s\"", type, source);
sendCommand(text);
::sprintf(text, "t1.txt=\"%s%u\"", group ? "TG" : "", dest);
sendCommand(text);
m_clockDisplayTimer.stop();
m_mode = MODE_P25;
m_rssiAccum1 = 0U;
m_berAccum1 = 0.0F;
m_rssiCount1 = 0U;
m_berCount1 = 0U;
}
void CNextion::writeP25RSSIInt(unsigned char rssi)
{
if (m_rssiCount1 == 0U) {
char text[20U];
::sprintf(text, "t2.txt=\"-%udBm\"", rssi);
sendCommand(text);
m_rssiCount1 = 1U;
return;
}
m_rssiAccum1 += rssi;
m_rssiCount1++;
if (m_rssiCount1 == P25_RSSI_COUNT) {
char text[20U];
::sprintf(text, "t2.txt=\"-%udBm\"", m_rssiAccum1 / P25_RSSI_COUNT);
sendCommand(text);
m_rssiAccum1 = 0U;
m_rssiCount1 = 1U;
}
}
void CNextion::writeP25BERInt(float ber)
{
if (m_berCount1 == 0U) {
char text[20U];
::sprintf(text, "t3.txt=\"%.1f%%\"", ber);
sendCommand(text);
m_berCount1 = 1U;
return;
}
m_berAccum1 += ber;
m_berCount1++;
if (m_berCount1 == P25_BER_COUNT) {
char text[20U];
::sprintf(text, "t3.txt=\"%.1f%%\"", m_berAccum1 / float(P25_BER_COUNT));
sendCommand(text);
m_berAccum1 = 0.0F;
m_berCount1 = 1U;
}
}
void CNextion::clearP25Int()
{
sendCommand("t0.txt=\"Listening\"");
sendCommand("t1.txt=\"\"");
sendCommand("t2.txt=\"\"");
sendCommand("t3.txt=\"\"");
}
void CNextion::writeCWInt()
{
sendCommand("t1.txt=\"Sending CW Ident\"");
m_clockDisplayTimer.start();
m_mode = MODE_CW;
}
void CNextion::clearCWInt()
{
sendCommand("t1.txt=\"MMDVM IDLE\"");
}
void CNextion::clockInt(unsigned int ms)
{
// Update the clock display in IDLE mode every 400ms
m_clockDisplayTimer.clock(ms);
if (m_displayClock && (m_mode == MODE_IDLE || m_mode == MODE_CW) && m_clockDisplayTimer.isRunning() && m_clockDisplayTimer.hasExpired()) {
time_t currentTime;
struct tm *Time;
::time(¤tTime); // Get the current time
if (m_utc)
Time = ::gmtime(¤tTime);
else
Time = ::localtime(¤tTime);
setlocale(LC_TIME,"");
char text[50U];
strftime(text, 50, "t2.txt=\"%x %X\"", Time);
sendCommand(text);
m_clockDisplayTimer.start(); // restart the clock display timer
}
}
void CNextion::close()
{
m_serial->close();
delete m_serial;
}
void CNextion::sendCommand(const char* command)
{
assert(command != NULL);
m_serial->write((unsigned char*)command, ::strlen(command));
m_serial->write((unsigned char*)"\xFF\xFF\xFF", 3U);
}