forked from g4klx/MMDVMHost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextion.cpp
210 lines (164 loc) · 4.36 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
/*
* Copyright (C) 2016 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>
CNextion::CNextion(const char* callsign, unsigned int dmrid, const std::string& port, unsigned int brightness) :
m_callsign(callsign),
m_dmrid(dmrid),
m_serial(port, SERIAL_9600),
m_brightness(brightness),
m_mode(MODE_IDLE)
{
assert(callsign != 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");
return false;
}
sendCommand("bkcmd=0");
char command[20U];
::sprintf(command, "dim=%u", m_brightness);
sendCommand(command);
setIdle();
return true;
}
void CNextion::setIdle()
{
sendCommand("page MMDVM");
char command[30];
::sprintf(command, "t0.txt=\"%-6s / %u\"", m_callsign, m_dmrid);
sendCommand(command);
sendCommand("t1.txt=\"MMDVM IDLE\"");
m_mode = MODE_IDLE;
}
void CNextion::setError(const char* text)
{
assert(text != NULL);
sendCommand("page MMDVM");
char command[20];
::sprintf(command, "t0.txt=\"%s\"", text);
sendCommand(command);
sendCommand("t1.txt=\"ERROR\"");
m_mode = MODE_ERROR;
}
void CNextion::setLockout()
{
sendCommand("page MMDVM");
sendCommand("t0.txt=\"LOCKOUT\"");
m_mode = MODE_LOCKOUT;
}
void CNextion::writeDStar(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, "t0.txt=\"%s %.8s/%4.4s\"", type, my1, my2);
sendCommand(text);
if (strcmp(reflector, " ") == 0) {
::sprintf(text, "t1.txt=\"%.8s\"", your);
} else {
::sprintf(text, "t1.txt=\"%.8s <- %-8s\"", your, reflector);
}
sendCommand(text);
m_mode = MODE_DSTAR;
}
void CNextion::clearDStar()
{
sendCommand("t0.txt=\"Listening\"");
sendCommand("t1.txt=\"\"");
}
void CNextion::writeDMR(unsigned int slotNo, const char* src, bool group, const char* dst, const char* type)
{
assert(src != NULL);
assert(dst != NULL);
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\"");
}
if (slotNo == 1U) {
char text[30U];
::sprintf(text, "t0.txt=\"1 %s %s\"", type, src);
sendCommand(text);
::sprintf(text, "t1.txt=\"%s%s\"", group ? "TG" : "", dst);
sendCommand(text);
} else {
char text[30U];
::sprintf(text, "t2.txt=\"2 %s %s\"", type, src);
sendCommand(text);
::sprintf(text, "t3.txt=\"%s%s\"", group ? "TG" : "", dst);
sendCommand(text);
}
m_mode = MODE_DMR;
}
void CNextion::clearDMR(unsigned int slotNo)
{
if (slotNo == 1U) {
sendCommand("t0.txt=\"1 Listening\"");
sendCommand("t1.txt=\"\"");
} else {
sendCommand("t2.txt=\"2 Listening\"");
sendCommand("t3.txt=\"\"");
}
}
void CNextion::writeFusion(const char* source, const char* dest)
{
assert(source != NULL);
assert(dest != NULL);
if (m_mode != MODE_YSF)
sendCommand("page YSF");
char text[30U];
::sprintf(text, "t0.txt=\"%.10s\"", source);
sendCommand(text);
::sprintf(text, "t1.txt=\"%.10s\"", dest);
sendCommand(text);
m_mode = MODE_YSF;
}
void CNextion::clearFusion()
{
sendCommand("t0.txt=\"Listening\"");
sendCommand("t1.txt=\"\"");
}
void CNextion::close()
{
m_serial.close();
}
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);
}