From da53f06197db99bf7952830b1aef43fedca814f3 Mon Sep 17 00:00:00 2001 From: Tony Corbett G0WFV Date: Fri, 24 Jun 2016 16:08:28 +0100 Subject: [PATCH] Time/date format by system locale (HD44780 & Nextion) --- Conf.cpp | 16 ---------------- Conf.h | 4 ---- HD44780.cpp | 31 +++++++++++-------------------- HD44780.h | 3 +-- MMDVM.ini | 2 -- MMDVMHost.cpp | 6 ++---- Nextion.cpp | 19 +++---------------- Nextion.h | 3 +-- 8 files changed, 18 insertions(+), 66 deletions(-) diff --git a/Conf.cpp b/Conf.cpp index 1dd916c..4e877f9 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -136,12 +136,10 @@ m_hd44780PWMBright(), m_hd44780PWMDim(), m_hd44780DisplayClock(false), m_hd44780UTC(false), -m_hd44780DateFormat("British"), m_nextionPort("/dev/ttyAMA0"), m_nextionBrightness(50U), m_nextionDisplayClock(false), m_nextionUTC(false), -m_nextionDateFormat("British"), m_nextionIdleBrightness(20U), m_oledType(3), m_oledBrightness(0), @@ -452,8 +450,6 @@ bool CConf::read() m_hd44780DisplayClock = ::atoi(value) == 1; else if (::strcmp(key, "UTC") == 0) m_hd44780UTC = ::atoi(value) == 1; - else if (::strcmp(key, "DateFormat") == 0) - m_hd44780DateFormat = value; else if (::strcmp(key, "Pins") == 0) { char* p = ::strtok(value, ",\r\n"); while (p != NULL) { @@ -471,8 +467,6 @@ bool CConf::read() m_nextionDisplayClock = ::atoi(value) == 1; else if (::strcmp(key, "UTC") == 0) m_nextionUTC = ::atoi(value) == 1; - else if (::strcmp(key, "DateFormat") == 0) - m_nextionDateFormat = value; else if (::strcmp(key, "IdleBrightness") == 0) m_nextionIdleBrightness = (unsigned int)::atoi(value); } else if (section == SECTION_OLED) { @@ -925,11 +919,6 @@ bool CConf::getHD44780UTC() const return m_hd44780UTC; } -std::string CConf::getHD44780DateFormat() const -{ - return m_hd44780DateFormat; -} - std::string CConf::getNextionPort() const { return m_nextionPort; @@ -950,11 +939,6 @@ bool CConf::getNextionUTC() const return m_nextionUTC; } -std::string CConf::getNextionDateFormat() const -{ - return m_nextionDateFormat; -} - unsigned int CConf::getNextionIdleBrightness() const { return m_nextionIdleBrightness; diff --git a/Conf.h b/Conf.h index 5160328..16eb6fd 100644 --- a/Conf.h +++ b/Conf.h @@ -143,14 +143,12 @@ public: unsigned int getHD44780PWMDim() const; bool getHD44780DisplayClock() const; bool getHD44780UTC() const; - std::string getHD44780DateFormat() const; // The Nextion section std::string getNextionPort() const; unsigned int getNextionBrightness() const; bool getNextionDisplayClock() const; bool getNextionUTC() const; - std::string getNextionDateFormat() const; unsigned int getNextionIdleBrightness() const; // The OLED section @@ -260,13 +258,11 @@ private: unsigned int m_hd44780PWMDim; bool m_hd44780DisplayClock; bool m_hd44780UTC; - std::string m_hd44780DateFormat; std::string m_nextionPort; unsigned int m_nextionBrightness; bool m_nextionDisplayClock; bool m_nextionUTC; - std::string m_nextionDateFormat; unsigned int m_nextionIdleBrightness; unsigned char m_oledType; diff --git a/HD44780.cpp b/HD44780.cpp index 7721ea7..ac397e6 100644 --- a/HD44780.cpp +++ b/HD44780.cpp @@ -36,7 +36,7 @@ char m_buffer2[128U]; char m_buffer3[128U]; char m_buffer4[128U]; -CHD44780::CHD44780(unsigned int rows, unsigned int cols, const std::string& callsign, unsigned int dmrid, const std::vector& pins, bool pwm, unsigned int pwmPin, unsigned int pwmBright, unsigned int pwmDim, bool displayClock, bool utc, bool duplex, const std::string& dateformat) : +CHD44780::CHD44780(unsigned int rows, unsigned int cols, const std::string& callsign, unsigned int dmrid, const std::vector& pins, bool pwm, unsigned int pwmPin, unsigned int pwmBright, unsigned int pwmDim, bool displayClock, bool utc, bool duplex) : CDisplay(), m_rows(rows), m_cols(cols), @@ -56,7 +56,6 @@ m_displayClock(displayClock), m_utc(utc), m_duplex(duplex), //m_duplex(true), // uncomment to force duplex display for testing! -m_dateformat(dateformat), m_fd(-1), m_dmr(false), m_clockDisplayTimer(1000U, 0U, 75U), // Update the clock display every 75ms @@ -719,29 +718,21 @@ void CHD44780::clockInt(unsigned int ms) Time = localtime(¤tTime); } - int Day = Time->tm_mday; - int Month = Time->tm_mon + 1; - int Year = Time->tm_year + 1900; - int Hour = Time->tm_hour; - int Min = Time->tm_min; - int Sec = Time->tm_sec; - + setlocale(LC_ALL,""); + strftime(m_buffer1, 128, "%X", Time); // Time + strftime(m_buffer2, 128, "%x", Time); // Date + if (m_cols == 16U && m_rows == 2U) { - ::lcdPosition(m_fd, m_cols - 8, 1); + ::lcdPosition(m_fd, m_cols - 10, 1); + ::lcdPrintf(m_fd, "%s%.*s", strlen(m_buffer1) > 8 ? "" : " ", 10, m_buffer1); } else { - ::lcdPosition(m_fd, (m_cols - 8) / 2, m_rows == 2 ? 1 : 2); + ::lcdPosition(m_fd, (m_cols - strlen(m_buffer1) > 8 ? 10 : 8) / 2, m_rows == 2 ? 1 : 2); + ::lcdPrintf(m_fd, "%.*s", strlen(m_buffer1) > 8 ? 10 : 8, m_buffer1); } - ::lcdPrintf(m_fd, "%02d:%02d:%02d", Hour, Min, Sec); if (m_cols != 16U && m_rows != 2U) { - ::lcdPosition(m_fd, (m_cols - 8) / 2, m_rows == 2 ? 0 : 1); - if (strcmp(m_dateformat.c_str(), "British") == 0) { - ::lcdPrintf(m_fd, "%02d/%02d/%2d", Day, Month, Year%100); - } else if (strcmp(m_dateformat.c_str(), "German") == 0) { - ::lcdPrintf(m_fd, "%02d.%02d.%2d", Day, Month, Year%100); - } else if (strcmp(m_dateformat.c_str(), "American") == 0) { - ::lcdPrintf(m_fd, "%02d/%02d/%2d", Month, Day, Year%100); - } + ::lcdPosition(m_fd, (m_cols - strlen(m_buffer2)) / 2, m_rows == 2 ? 0 : 1); + ::lcdPrintf(m_fd, "%s", m_buffer2); } m_clockDisplayTimer.start(); } diff --git a/HD44780.h b/HD44780.h index ae999ae..7cd3805 100644 --- a/HD44780.h +++ b/HD44780.h @@ -53,7 +53,7 @@ enum ADAFRUIT_COLOUR { class CHD44780 : public CDisplay { public: - CHD44780(unsigned int rows, unsigned int cols, const std::string& callsign, unsigned int dmrid, const std::vector& pins, bool pwm, unsigned int pwmPin, unsigned int pwmBright, unsigned int pwmDim, bool displayClock, bool utc, bool duplex, const std::string& dateformat); + CHD44780(unsigned int rows, unsigned int cols, const std::string& callsign, unsigned int dmrid, const std::vector& pins, bool pwm, unsigned int pwmPin, unsigned int pwmBright, unsigned int pwmDim, bool displayClock, bool utc, bool duplex); virtual ~CHD44780(); virtual bool open(); @@ -94,7 +94,6 @@ private: bool m_displayClock; bool m_utc; bool m_duplex; - std::string m_dateformat; int m_fd; bool m_dmr; CTimer m_clockDisplayTimer; diff --git a/MMDVM.ini b/MMDVM.ini index a4b0031..518ee35 100644 --- a/MMDVM.ini +++ b/MMDVM.ini @@ -120,14 +120,12 @@ PWMBright=100 PWMDim=16 DisplayClock=1 UTC=0 -DateFormat=British [Nextion] Port=/dev/ttyAMA0 Brightness=50 DisplayClock=1 UTC=0 -DateFormat=British IdleBrightness=20 [OLED] diff --git a/MMDVMHost.cpp b/MMDVMHost.cpp index c0fd30d..1d2d114 100644 --- a/MMDVMHost.cpp +++ b/MMDVMHost.cpp @@ -860,7 +860,6 @@ void CMMDVMHost::createDisplay() unsigned int brightness = m_conf.getNextionBrightness(); bool displayClock = m_conf.getNextionDisplayClock(); bool utc = m_conf.getNextionUTC(); - std::string dateformat = m_conf.getNextionDateFormat(); unsigned int idleBrightness = m_conf.getNextionIdleBrightness(); LogInfo(" Port: %s", port.c_str()); @@ -870,7 +869,7 @@ void CMMDVMHost::createDisplay() LogInfo(" Display UTC: %s", utc ? "yes" : "no"); LogInfo(" Idle Brightness: %u", idleBrightness); - m_display = new CNextion(m_callsign, dmrid, port, brightness, displayClock, utc, dateformat, idleBrightness); + m_display = new CNextion(m_callsign, dmrid, port, brightness, displayClock, utc, idleBrightness); #if defined(HD44780) } else if (type == "HD44780") { unsigned int rows = m_conf.getHD44780Rows(); @@ -882,7 +881,6 @@ void CMMDVMHost::createDisplay() unsigned int pwmDim = m_conf.getHD44780PWMDim(); bool displayClock = m_conf.getHD44780DisplayClock(); bool utc = m_conf.getHD44780UTC(); - std::string dateformat = m_conf.getHD44780DateFormat(); if (pins.size() == 6U) { LogInfo(" Rows: %u", rows); @@ -900,7 +898,7 @@ void CMMDVMHost::createDisplay() if (displayClock) LogInfo(" Display UTC: %s", utc ? "yes" : "no"); - m_display = new CHD44780(rows, columns, m_callsign, dmrid, pins, pwm, pwmPin, pwmBright, pwmDim, displayClock, utc, m_duplex, dateformat); + m_display = new CHD44780(rows, columns, m_callsign, dmrid, pins, pwm, pwmPin, pwmBright, pwmDim, displayClock, utc, m_duplex); } #endif #if defined(OLED) diff --git a/Nextion.cpp b/Nextion.cpp index 676b2a0..4d74c38 100644 --- a/Nextion.cpp +++ b/Nextion.cpp @@ -24,7 +24,7 @@ #include #include -CNextion::CNextion(const std::string& callsign, unsigned int dmrid, const std::string& port, unsigned int brightness, bool displayClock, bool utc, const std::string& dateformat, unsigned int idleBrightness) : +CNextion::CNextion(const std::string& callsign, unsigned int dmrid, const std::string& port, unsigned int brightness, bool displayClock, bool utc, unsigned int idleBrightness) : CDisplay(), m_callsign(callsign), m_dmrid(dmrid), @@ -33,7 +33,6 @@ m_brightness(brightness), m_mode(MODE_IDLE), m_displayClock(displayClock), m_utc(utc), -m_dateformat(dateformat), m_idleBrightness(idleBrightness), m_clockDisplayTimer(1000U, 0U, 400U) { @@ -247,21 +246,9 @@ void CNextion::clockInt(unsigned int ms) else Time = ::localtime(¤tTime); - int Day = Time->tm_mday; - int Month = Time->tm_mon + 1; - int Year = Time->tm_year + 1900; - int Hour = Time->tm_hour; - int Min = Time->tm_min; - int Sec = Time->tm_sec; - + setlocale(LC_ALL,""); char text[50U]; - if (strcmp(m_dateformat.c_str(), "British") == 0) { - ::sprintf(text, "t2.txt=\"%02d:%02d:%02d %02d/%02d/%2d\"", Hour, Min, Sec, Day, Month, Year % 100); - } else if (strcmp(m_dateformat.c_str(), "German") == 0) { - ::sprintf(text, "t2.txt=\"%02d:%02d:%02d %02d.%02d.%2d\"", Hour, Min, Sec, Day, Month, Year % 100); - } else if (strcmp(m_dateformat.c_str(), "American") == 0) { - ::sprintf(text, "t2.txt=\"%02d:%02d:%02d %02d/%02d/%2d\"", Hour, Min, Sec, Month, Day, Year % 100); - } + strftime(text, 50, "t2.txt=\"%x %X\"", Time); sendCommand(text); m_clockDisplayTimer.start(); // restart the clock display timer diff --git a/Nextion.h b/Nextion.h index f7de717..38fa55f 100644 --- a/Nextion.h +++ b/Nextion.h @@ -29,7 +29,7 @@ class CNextion : public CDisplay { public: - CNextion(const std::string& callsign, unsigned int dmrid, const std::string& port, unsigned int brightness, bool displayClock, bool utc, const std::string& dateformat, unsigned int idleBrightness); + CNextion(const std::string& callsign, unsigned int dmrid, const std::string& port, unsigned int brightness, bool displayClock, bool utc, unsigned int idleBrightness); virtual ~CNextion(); virtual bool open(); @@ -60,7 +60,6 @@ private: unsigned char m_mode; bool m_displayClock; bool m_utc; - std::string m_dateformat; unsigned int m_idleBrightness; CTimer m_clockDisplayTimer;