MMDVMHost-Private/TFTSerial.cpp

341 lines
7.5 KiB
C++
Raw Normal View History

2016-01-14 18:45:04 +00:00
/*
* Copyright (C) 2015,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 "TFTSerial.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
2016-01-14 22:58:45 +00:00
#include <cstring>
const unsigned char ROTATION_PORTRAIT_LEFT = 0U;
const unsigned char ROTATION_LANDSCAPE_UD = 1U;
const unsigned char ROTATION_PORTRAIT_RIGHT = 2U;
const unsigned char ROTATION_LANDSCAPE = 3U;
const unsigned char FONT_SMALL = 0U;
const unsigned char FONT_MEDIUM = 1U;
const unsigned char FONT_LARGE = 2U;
// x = 0 to 159, y = 0 to 127 - Landscape
// x = 0 to 127, y = 0 to 159 - Portrait
CTFTSerial::CTFTSerial(const std::string& port, unsigned int brightness) :
m_serial(port, SERIAL_9600),
m_brightness(brightness)
2016-01-14 18:45:04 +00:00
{
assert(brightness >= 0U && brightness <= 100U);
2016-01-14 18:45:04 +00:00
}
CTFTSerial::~CTFTSerial()
{
}
bool CTFTSerial::open()
{
bool ret = m_serial.open();
if (!ret) {
LogError("Cannot open the port for the TFT Serial");
return false;
}
setRotation(ROTATION_LANDSCAPE);
setFontSize(FONT_MEDIUM);
setBrightness(m_brightness);
2016-01-14 18:45:04 +00:00
// Set background white
setBackground(7U);
2016-01-14 18:45:04 +00:00
// Set foreground black
setForeground(0U);
2016-01-14 18:45:04 +00:00
setIdle();
return true;
}
void CTFTSerial::setIdle()
{
// Clear the screen
2016-02-16 07:48:07 +00:00
clearScreen();
2016-01-14 18:45:04 +00:00
// Draw MMDVM logo
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 0U, "MMDVM_sm.bmp");
2016-01-14 18:45:04 +00:00
// Draw all mode insignias
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 30U, "ALL_sm.bmp");
2016-01-14 18:45:04 +00:00
}
void CTFTSerial::setDStar()
{
// Clear the screen
2016-02-16 07:48:07 +00:00
clearScreen();
2016-01-14 18:45:04 +00:00
// Draw MMDVM logo
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 0U, "MMDVM_sm.bmp");
2016-01-14 18:45:04 +00:00
// Draw D-Star insignia
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 30U, "DStar_sm.bmp");
2016-01-14 18:45:04 +00:00
2016-02-18 18:31:44 +00:00
gotoPosText(0U, 8U);
displayText("Listening");
2016-01-14 18:45:04 +00:00
}
void CTFTSerial::writeDStar(const char* my1, const char* my2, const char* your)
2016-01-14 18:45:04 +00:00
{
assert(my1 != NULL);
assert(my2 != NULL);
assert(your != NULL);
char text[30U];
::sprintf(text, "%8.8s/%4.4s -> %8.8s", my1, my2, your);
2016-01-14 18:45:04 +00:00
2016-02-18 18:31:44 +00:00
gotoPosText(0U, 8U);
displayText(text);
2016-01-14 18:45:04 +00:00
}
void CTFTSerial::clearDStar()
{
// Draw MMDVM logo
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 0U, "MMDVM_sm.bmp");
2016-01-14 18:45:04 +00:00
// Draw D-Star insignia
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 30U, "DStar_sm.bmp");
2016-01-14 18:45:04 +00:00
2016-02-18 18:31:44 +00:00
gotoPosText(0U, 8U);
displayText("Listening");
2016-01-14 18:45:04 +00:00
}
void CTFTSerial::setDMR()
{
// Clear the screen
2016-02-16 07:48:07 +00:00
clearScreen();
2016-01-14 18:45:04 +00:00
// Draw MMDVM logo
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 0U, "MMDVM_sm.bmp");
2016-01-14 18:45:04 +00:00
// Draw DMR insignia
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 30U, "DMR_sm.bmp");
2016-01-14 18:45:04 +00:00
2016-02-18 18:31:44 +00:00
gotoPosText(0U, 8U);
displayText("1: Listening");
gotoPosText(0U, 9U);
displayText("2: Listening");
2016-01-14 18:45:04 +00:00
}
void CTFTSerial::writeDMR(unsigned int slotNo, unsigned int srcId, bool group, unsigned int dstId, const char* type)
2016-01-14 18:45:04 +00:00
{
assert(type != NULL);
2016-02-18 18:31:44 +00:00
if (slotNo == 1U) {
char text[30U];
2016-02-18 18:31:44 +00:00
::sprintf(text, "1: %s %u -> %s%u", type, srcId, group ? "TG" : "", dstId);
gotoPosText(0U, 8U);
displayText(text);
} else {
char text[30U];
2016-02-18 18:31:44 +00:00
::sprintf(text, "2: %s %u -> %s%u", type, srcId, group ? "TG" : "", dstId);
gotoPosText(0U, 9U);
displayText(text);
}
2016-01-14 18:45:04 +00:00
}
void CTFTSerial::clearDMR(unsigned int slotNo)
{
2016-02-18 18:31:44 +00:00
if (slotNo == 1U) {
gotoPosText(0U, 8U);
displayText("1: Listening");
} else {
gotoPosText(0U, 9U);
displayText("2: Listening");
}
2016-01-14 18:45:04 +00:00
}
void CTFTSerial::setFusion()
{
// Clear the screen
2016-02-16 07:48:07 +00:00
clearScreen();
2016-01-14 18:45:04 +00:00
// Draw MMDVM logo
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 0U, "MMDVM_sm.bmp");
2016-01-14 18:45:04 +00:00
// Draw the System Fusion insignia
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 30U, "YSF_sm.bmp");
2016-01-14 18:45:04 +00:00
2016-02-18 18:31:44 +00:00
gotoPosText(0U, 8U);
displayText("Listening");
2016-01-14 18:45:04 +00:00
}
void CTFTSerial::writeFusion(const char* source, const char* dest)
2016-01-14 18:45:04 +00:00
{
assert(source != NULL);
assert(dest != NULL);
char text[30U];
2016-03-01 06:40:08 +00:00
::sprintf(text, "%10.10s -> %10.10s", source, dest);
2016-01-14 18:45:04 +00:00
2016-02-18 18:31:44 +00:00
gotoPosText(0U, 8U);
displayText(text);
2016-01-14 18:45:04 +00:00
}
void CTFTSerial::clearFusion()
{
// Draw MMDVM logo
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 0U, "MMDVM_sm.bmp");
2016-01-14 18:45:04 +00:00
// Draw the System Fusion insignia
2016-02-18 18:31:44 +00:00
displayBitmap(0U, 30U, "YSF_sm.bmp");
2016-01-14 18:45:04 +00:00
2016-02-18 18:31:44 +00:00
gotoPosText(0U, 8U);
displayText("Listening");
2016-01-14 18:45:04 +00:00
}
void CTFTSerial::close()
{
m_serial.close();
}
2016-02-16 07:48:07 +00:00
void CTFTSerial::clearScreen()
{
m_serial.write((unsigned char*)"\x1B\x00\xFF", 3U);
}
void CTFTSerial::setForeground(unsigned char colour)
{
assert(colour >= 0U && colour <= 7U);
m_serial.write((unsigned char*)"\x1B\x01", 2U);
m_serial.write(&colour, 1U);
m_serial.write((unsigned char*)"\xFF", 1U);
}
void CTFTSerial::setBackground(unsigned char colour)
{
assert(colour >= 0U && colour <= 7U);
m_serial.write((unsigned char*)"\x1B\x02", 2U);
m_serial.write(&colour, 1U);
m_serial.write((unsigned char*)"\xFF", 1U);
}
void CTFTSerial::setRotation(unsigned char rotation)
{
assert(rotation >= 0U && rotation <= 3U);
m_serial.write((unsigned char*)"\x1B\x03", 2U);
m_serial.write(&rotation, 1U);
m_serial.write((unsigned char*)"\xFF", 1U);
}
void CTFTSerial::setFontSize(unsigned char size)
{
assert(size >= 0U && size <= 2U);
m_serial.write((unsigned char*)"\x1B\x04", 2U);
m_serial.write(&size, 1U);
m_serial.write((unsigned char*)"\xFF", 1U);
}
2016-02-18 18:31:44 +00:00
void CTFTSerial::gotoBegOfLine()
2016-02-16 07:48:07 +00:00
{
2016-02-18 18:31:44 +00:00
m_serial.write((unsigned char*)"\x1B\x05\xFF", 3U);
}
2016-02-18 18:31:44 +00:00
void CTFTSerial::gotoPosText(unsigned char x, unsigned char y)
{
2016-02-16 07:48:07 +00:00
m_serial.write((unsigned char*)"\x1B\x06", 2U);
m_serial.write(&x, 1U);
m_serial.write(&y, 1U);
m_serial.write((unsigned char*)"\xFF", 1U);
2016-02-18 18:31:44 +00:00
}
void CTFTSerial::gotoPosPixel(unsigned char x, unsigned char y)
{
m_serial.write((unsigned char*)"\x1B\x07", 2U);
m_serial.write(&x, 1U);
m_serial.write(&y, 1U);
m_serial.write((unsigned char*)"\xFF", 1U);
2016-02-16 07:48:07 +00:00
}
void CTFTSerial::drawLine(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)
2016-02-16 07:48:07 +00:00
{
m_serial.write((unsigned char*)"\x1B\x08", 2U);
m_serial.write(&x1, 1U);
m_serial.write(&y1, 1U);
m_serial.write(&x2, 1U);
m_serial.write(&y2, 1U);
m_serial.write((unsigned char*)"\xFF", 1U);
}
void CTFTSerial::drawBox(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, bool filled)
{
if (filled)
m_serial.write((unsigned char*)"\x1B\x0A", 2U);
else
m_serial.write((unsigned char*)"\x1B\x09", 2U);
m_serial.write(&x1, 1U);
m_serial.write(&y1, 1U);
m_serial.write(&x2, 1U);
m_serial.write(&y2, 1U);
m_serial.write((unsigned char*)"\xFF", 1U);
}
void CTFTSerial::drawCircle(unsigned char x, unsigned char y, unsigned char radius, bool filled)
{
if (filled)
m_serial.write((unsigned char*)"\x1B\x0C", 2U);
else
m_serial.write((unsigned char*)"\x1B\x0B", 2U);
m_serial.write(&x, 1U);
m_serial.write(&y, 1U);
m_serial.write(&radius, 1U);
m_serial.write((unsigned char*)"\xFF", 1U);
}
2016-02-18 18:31:44 +00:00
void CTFTSerial::displayBitmap(unsigned char x, unsigned char y, const char* filename)
{
assert(filename != NULL);
2016-02-16 07:48:07 +00:00
m_serial.write((unsigned char*)"\x1B\x0D", 2U);
m_serial.write(&x, 1U);
m_serial.write(&y, 1U);
m_serial.write((unsigned char*)filename, ::strlen(filename));
m_serial.write((unsigned char*)"\xFF", 1U);
}
void CTFTSerial::setBrightness(unsigned char brightness)
{
assert(brightness >= 0U && brightness <= 100U);
m_serial.write((unsigned char*)"\x1B\x0E", 2U);
m_serial.write(&brightness, 1U);
m_serial.write((unsigned char*)"\xFF", 1U);
}
2016-02-18 18:31:44 +00:00
void CTFTSerial::displayText(const char* text)
{
assert(text != NULL);
m_serial.write((unsigned char*)text, ::strlen(text));
}