MMDVMHost-Private/Nextion.cpp

218 lines
4.7 KiB
C++
Raw Normal View History

/*
* 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 std::string& callsign, unsigned int dmrid, const std::string& port, unsigned int brightness) :
CDisplay(),
m_callsign(callsign),
m_dmrid(dmrid),
m_serial(port, SERIAL_9600),
m_brightness(brightness),
m_mode(MODE_IDLE)
{
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::setIdleInt()
{
sendCommand("page MMDVM");
char command[30];
2016-04-20 21:23:51 +02:00
::sprintf(command, "t0.txt=\"%-6s / %u\"", m_callsign.c_str(), m_dmrid);
sendCommand(command);
sendCommand("t1.txt=\"MMDVM IDLE\"");
m_mode = MODE_IDLE;
}
void CNextion::setErrorInt(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::setLockoutInt()
{
sendCommand("page MMDVM");
sendCommand("t0.txt=\"LOCKOUT\"");
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);
2016-04-16 22:20:18 +02:00
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);
::sprintf(text, "t1.txt=\"%.8s\"", your);
sendCommand(text);
if (::strcmp(reflector, " ") != 0) {
::sprintf(text, "t2.txt=\"via %.8s\"", reflector);
sendCommand(text);
2016-04-16 22:20:18 +02:00
}
m_mode = MODE_DSTAR;
}
void CNextion::clearDStarInt()
{
sendCommand("t0.txt=\"Listening\"");
sendCommand("t1.txt=\"\"");
sendCommand("t2.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\"");
}
if (slotNo == 1U) {
char text[30U];
::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 {
char text[30U];
::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_mode = MODE_DMR;
}
void CNextion::clearDMRInt(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::writeFusionInt(const char* source, const char* dest, const char* type, const char* origin)
{
assert(source != NULL);
assert(dest != NULL);
2016-05-16 22:57:32 +02:00
assert(type != NULL);
assert(origin != NULL);
if (m_mode != MODE_YSF)
sendCommand("page YSF");
char text[30U];
2016-05-16 22:57:32 +02:00
::sprintf(text, "t0.txt=\"%s %.10s\"", type, source);
sendCommand(text);
::sprintf(text, "t1.txt=\"%.10s\"", dest);
sendCommand(text);
if (::strcmp(origin, " ") != 0) {
2016-05-23 19:04:17 +02:00
::sprintf(text, "t2.txt=\"at %.10s\"", origin);
sendCommand(text);
}
m_mode = MODE_YSF;
}
void CNextion::clearFusionInt()
{
sendCommand("t0.txt=\"Listening\"");
sendCommand("t1.txt=\"\"");
sendCommand("t2.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);
}