Clock improvements: source selectable, RTC or GPS

This commit is contained in:
Thomas Hooge 2024-12-30 19:55:12 +01:00
parent 3412da8e18
commit eb3a0d5fc0
6 changed files with 267 additions and 82 deletions

View File

@ -421,9 +421,21 @@ void displayHeader(CommonData &commonData, GwApi::BoatValue *date, GwApi::BoatVa
heartbeat = !heartbeat; heartbeat = !heartbeat;
// Date and time // Date and time
getdisplay().setTextColor(commonData.fgcolor); String fmttype = commonData.config->getString(commonData.config->dateFormat);
String timesource = commonData.config->getString(commonData.config->timeSource);
int tz = commonData.config->getInt(commonData.config->timeZone);
getdisplay().setTextColor(textcolor);
getdisplay().setFont(&Ubuntu_Bold8pt7b); getdisplay().setFont(&Ubuntu_Bold8pt7b);
getdisplay().setCursor(230, 15); getdisplay().setCursor(230, 15);
if (timesource == "RTC") {
time_t tv = mktime(&commonData.data.rtcTime) + tz * 3600;
struct tm *local_tm = localtime(&tv);
getdisplay().print(formatTime('m', local_tm->tm_hour, local_tm->tm_min, 0));
getdisplay().print(" ");
getdisplay().print(formatDate(fmttype, local_tm->tm_year + 1900, local_tm->tm_mon + 1, local_tm->tm_mday));
getdisplay().print(" ");
}
else { // timesource == "GPS"
// Show date and time if date present // Show date and time if date present
if(date->valid == true){ if(date->valid == true){
String acttime = formatValue(time, commonData).svalue; String acttime = formatValue(time, commonData).svalue;
@ -433,12 +445,6 @@ void displayHeader(CommonData &commonData, GwApi::BoatValue *date, GwApi::BoatVa
getdisplay().print(" "); getdisplay().print(" ");
getdisplay().print(actdate); getdisplay().print(actdate);
getdisplay().print(" "); getdisplay().print(" ");
if(commonData.config->getInt(commonData.config->timeZone) == 0){
getdisplay().print("UTC");
}
else{
getdisplay().print("LOT");
}
} }
else{ else{
if(commonData.config->getBool(commonData.config->useSimuData) == true){ if(commonData.config->getBool(commonData.config->useSimuData) == true){
@ -448,6 +454,13 @@ void displayHeader(CommonData &commonData, GwApi::BoatValue *date, GwApi::BoatVa
getdisplay().print("No GPS data"); getdisplay().print("No GPS data");
} }
} }
} // timesource == "GPS"
if (tz == 0) {
getdisplay().print("UTC");
}
else {
getdisplay().print("LOT");
}
} }
} }

View File

@ -8,6 +8,35 @@
// simulation data // simulation data
// hold values by missing data // hold values by missing data
String formatDate(String fmttype, uint16_t year, uint8_t month, uint8_t day) {
char buffer[12];
if (fmttype == "GB") {
snprintf(buffer, 12, "%02d/%02d/%04d", day , month, year);
}
else if (fmttype == "US") {
snprintf(buffer, 12, "%02d/%02d/%04d", month, day, year);
}
else if (fmttype == "ISO") {
snprintf(buffer, 12, "%04d-%02d-%02d", year, month, day);
}
else {
snprintf(buffer, 12, "%02d.%02d.%04d", day, month, year);
}
return String(buffer);
}
String formatTime(char fmttype, uint8_t hour, uint8_t minute, uint8_t second) {
// fmttype: s: with seconds, m: only minutes
char buffer[10];
if (fmttype == 'm') {
snprintf(buffer, 10, "%02d:%02d", hour , minute);
}
else {
snprintf(buffer, 10, "%02d:%02d:%02d", hour, minute, second);
}
return String(buffer);
}
FormatedData formatValue(GwApi::BoatValue *value, CommonData &commondata){ FormatedData formatValue(GwApi::BoatValue *value, CommonData &commondata){
GwLog *logger = commondata.logger; GwLog *logger = commondata.logger;
FormatedData result; FormatedData result;

View File

@ -428,38 +428,40 @@ void sensorTask(void *param){
loopCounter++; loopCounter++;
} }
// If GPS not ready or installed then send RTC time on bus all 500ms // Get current RTC date and time all 500ms
if (millis() > starttime12 + 500) { if (millis() > starttime12 + 500) {
starttime12 = millis(); starttime12 = millis();
if((rtcOn == "DS1388" && RTC_ready == true && GPS_ready == false) || (rtcOn == "DS1388" && RTC_ready == true && GPS_ready == true && hdop->valid == false)){ if (rtcOn == "DS1388" && RTC_ready) {
sensors.rtcTime.tm_year = ds1388.now().year() - 1900; // Save values in SensorData
sensors.rtcTime.tm_mon = ds1388.now().month() - 1;
sensors.rtcTime.tm_mday = ds1388.now().day();
sensors.rtcTime.tm_hour = ds1388.now().hour();
sensors.rtcTime.tm_min = ds1388.now().minute();
sensors.rtcTime.tm_sec = ds1388.now().second();
sensors.rtcTime.tm_isdst = 0; // Not considering daylight saving time
// If GPS not ready or installed then send RTC time on bus
if ((GPS_ready == false) || (GPS_ready == true && hdop->valid == false)) {
// Convert RTC time to Unix system time // Convert RTC time to Unix system time
// https://de.wikipedia.org/wiki/Unixzeit // https://de.wikipedia.org/wiki/Unixzeit
const short daysOfYear[12] = {0,31,59,90,120,151,181,212,243,273,304,334}; const short daysOfYear[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
long unixtime = ds1388.now().get(); // unused: long unixtime = ds1388.now().get();
uint16_t year = ds1388.now().year(); uint16_t switchYear = ((sensors.rtcTime.tm_year-1)-1968)/4 - ((sensors.rtcTime.tm_year-1)-1900)/100 + ((sensors.rtcTime.tm_year-1)-1600)/400;
uint8_t month = ds1388.now().month(); long daysAt1970 = (sensors.rtcTime.tm_year-1970)*365 + switchYear + daysOfYear[sensors.rtcTime.tm_mon-1] + sensors.rtcTime.tm_mday-1;
uint8_t hour = ds1388.now().hour();
uint8_t minute = ds1388.now().minute();
uint8_t second = ds1388.now().second();
uint8_t day = ds1388.now().day();
uint16_t switchYear = ((year-1)-1968)/4 - ((year-1)-1900)/100 + ((year-1)-1600)/400;
long daysAt1970 = (year-1970)*365 + switchYear + daysOfYear[month-1] + day-1;
// If switch year then add one day // If switch year then add one day
if ( (month>2) && (year%4==0 && (year%100!=0 || year%400==0)) ){ if ((sensors.rtcTime.tm_mon > 2) && (sensors.rtcTime.tm_year % 4 == 0
&& (sensors.rtcTime.tm_year % 100 != 0 || sensors.rtcTime.tm_year % 400 == 0))) {
daysAt1970 += 1; daysAt1970 += 1;
} }
double sysTime = (hour * 3600) + (minute * 60) + second; // N2K sysTime ist double in n2klib
if(!isnan(daysAt1970) && !isnan(sysTime)){ double sysTime = (sensors.rtcTime.tm_hour * 3600) + (sensors.rtcTime.tm_min * 60) + sensors.rtcTime.tm_sec;
sensors.rtcYear = year; // Save values in SensorData // WHY? isnan should always fail here
sensors.rtcMonth = month; //if(!isnan(daysAt1970) && !isnan(sysTime)){
sensors.rtcDay = day;
sensors.rtcHour = hour;
sensors.rtcMinute = minute;
sensors.rtcSecond = second;
// api->getLogger()->logDebug(GwLog::LOG,"RTC time: %04d/%02d/%02d %02d:%02d:%02d",year, month, day, hour, minute, second); // api->getLogger()->logDebug(GwLog::LOG,"RTC time: %04d/%02d/%02d %02d:%02d:%02d",year, month, day, hour, minute, second);
// api->getLogger()->logDebug(GwLog::LOG,"Send PGN126992: %10d %10d",daysAt1970, (uint16_t)sysTime); // api->getLogger()->logDebug(GwLog::LOG,"Send PGN126992: %10d %10d",daysAt1970, (uint16_t)sysTime);
SetN2kPGN126992(N2kMsg,0,daysAt1970,sysTime,N2ktimes_LocalCrystalClock); SetN2kPGN126992(N2kMsg,0,daysAt1970,sysTime,N2ktimes_LocalCrystalClock);
api->sendN2kMessage(N2kMsg); api->sendN2kMessage(N2kMsg);
// }
} }
} }
} }

View File

@ -3,10 +3,25 @@
#include "Pagedata.h" #include "Pagedata.h"
#include "OBP60Extensions.h" #include "OBP60Extensions.h"
/*
* TODO mode: race timer: keys
* - prepare: set countdown to 5min
* reset: abort current countdown and start over with 5min preparation
* - 5min: key press
* - 4min: key press to sync
* - 1min: buzzer signal
* - start: buzzer signal for start
*
*/
class PageClock : public Page class PageClock : public Page
{ {
bool simulation = false; bool simulation = false;
int simtime; int simtime;
bool keylock = false;
char source = 'R'; // time source (R)TC | (G)PS
char mode = 'A'; // display mode (A)nalog | (D)igital | race (T)imer
char tz = 'L'; // time zone (L)ocal | (U)TC
public: public:
PageClock(CommonData &common){ PageClock(CommonData &common){
@ -18,9 +33,38 @@ class PageClock : public Page
// Key functions // Key functions
virtual int handleKey(int key){ virtual int handleKey(int key){
// Code for keylock // Time source
if(key == 11){ if (key == 1) {
commonData->keylock = !commonData->keylock; if (source == 'G') {
source = 'R';
} else {
source = 'G';
}
return 0;
}
if (key == 2) {
if (mode == 'A') {
mode = 'D';
} else if (mode == 'D') {
mode = 'T';
} else {
mode = 'A';
}
return 0;
}
// Time zone: Local / UTC
if (key == 5) {
if (tz == 'L') {
tz = 'U';
} else {
tz = 'L';
}
return 0;
}
// Keylock function
if(key == 11){ // Code for keylock
keylock = !keylock; // Toggle keylock
return 0; // Commit the key return 0; // Commit the key
} }
return key; return key;
@ -47,6 +91,8 @@ class PageClock : public Page
// Get config data // Get config data
String lengthformat = config->getString(config->lengthFormat); String lengthformat = config->getString(config->lengthFormat);
String dateformat = config->getString(config->dateFormat);
bool simulation = config->getBool(config->useSimuData);
bool holdvalues = config->getBool(config->holdvalues); bool holdvalues = config->getBool(config->holdvalues);
String flashLED = config->getString(config->flashLED); String flashLED = config->getString(config->flashLED);
String backlightMode = config->getString(config->backlight); String backlightMode = config->getString(config->backlight);
@ -115,11 +161,28 @@ class PageClock : public Page
getdisplay().setTextColor(commonData->fgcolor); getdisplay().setTextColor(commonData->fgcolor);
time_t tv = mktime(&commonData.data.rtcTime) + timezone * 3600;
struct tm *local_tm = localtime(&tv);
// Show values GPS date // Show values GPS date
getdisplay().setFont(&Ubuntu_Bold8pt7b); getdisplay().setFont(&Ubuntu_Bold8pt7b);
getdisplay().setCursor(10, 65); getdisplay().setCursor(10, 65);
if(holdvalues == false) getdisplay().print(svalue2); // Value if (holdvalues == false) {
else getdisplay().print(svalue2old); if (source == 'G') {
// GPS value
getdisplay().print(svalue2);
} else {
// RTC value
if (tz == 'L') {
getdisplay().print(formatDate(dateformat, local_tm->tm_year + 1900, local_tm->tm_mon + 1, local_tm->tm_mday));
}
else {
getdisplay().print(formatDate(dateformat, commonData.data.rtcTime.tm_year + 1900, commonData.data.rtcTime.tm_mon + 1, commonData.data.rtcTime.tm_mday));
}
}
} else {
getdisplay().print(svalue2old);
}
getdisplay().setFont(&Ubuntu_Bold12pt7b); getdisplay().setFont(&Ubuntu_Bold12pt7b);
getdisplay().setCursor(10, 95); getdisplay().setCursor(10, 95);
getdisplay().print("Date"); // Name getdisplay().print("Date"); // Name
@ -130,8 +193,22 @@ class PageClock : public Page
// Show values GPS time // Show values GPS time
getdisplay().setFont(&Ubuntu_Bold8pt7b); getdisplay().setFont(&Ubuntu_Bold8pt7b);
getdisplay().setCursor(10, 250); getdisplay().setCursor(10, 250);
if(holdvalues == false) getdisplay().print(svalue1); // Value if (holdvalues == false) {
else getdisplay().print(svalue1old); if (source == 'G') {
getdisplay().print(svalue1); // Value
}
else {
if (tz == 'L') {
getdisplay().print(formatTime('s', local_tm->tm_hour, local_tm->tm_min, local_tm->tm_sec));
}
else {
getdisplay().print(formatTime('s', commonData.data.rtcTime.tm_hour, commonData.data.rtcTime.tm_min, commonData.data.rtcTime.tm_sec));
}
}
}
else {
getdisplay().print(svalue1old);
}
getdisplay().setFont(&Ubuntu_Bold12pt7b); getdisplay().setFont(&Ubuntu_Bold12pt7b);
getdisplay().setCursor(10, 220); getdisplay().setCursor(10, 220);
getdisplay().print("Time"); // Name getdisplay().print("Time"); // Name
@ -246,22 +323,51 @@ class PageClock : public Page
getdisplay().setFont(&Ubuntu_Bold12pt7b); getdisplay().setFont(&Ubuntu_Bold12pt7b);
getdisplay().setCursor(175, 110); getdisplay().setCursor(175, 110);
if(holdvalues == false){ if(holdvalues == false){
if (tz == 'L') {
getdisplay().print(unit2); // Unit getdisplay().print(unit2); // Unit
} else {
getdisplay().print("UTC");
}
} }
else{ else{
getdisplay().print(unit2old); // Unit getdisplay().print(unit2old); // Unit
} }
getdisplay().setFont(&Ubuntu_Bold8pt7b);
getdisplay().setCursor(185, 190);
if (source == 'G') {
getdisplay().print("GPS");
} else {
getdisplay().print("RTC");
}
// Clock values // Clock values
double hour = 0; double hour = 0;
double minute = 0; double minute = 0;
value1 = value1 + int(timezone*3600); if (source == 'R') {
if (tz == 'L') {
time_t tv = mktime(&commonData.data.rtcTime) + timezone * 3600;
struct tm *local_tm = localtime(&tv);
minute = local_tm->tm_min;
hour = local_tm->tm_hour;
} else {
minute = commonData.data.rtcTime.tm_min;
hour = commonData.data.rtcTime.tm_hour;
}
hour += minute / 60;
} else {
if (tz == 'L') {
value1 += int(timezone*3600);
}
if (value1 > 86400) {value1 = value1 - 86400;} if (value1 > 86400) {value1 = value1 - 86400;}
if (value1 < 0) {value1 = value1 + 86400;} if (value1 < 0) {value1 = value1 + 86400;}
hour = (value1 / 3600.0); hour = (value1 / 3600.0);
if(hour > 12) hour = hour - 12.0;
// minute = (hour - int(hour)) * 3600.0 / 60.0; // Analog minute pointer smooth moving // minute = (hour - int(hour)) * 3600.0 / 60.0; // Analog minute pointer smooth moving
minute = int((hour - int(hour)) * 3600.0 / 60.0); // Jumping minute pointer from minute to minute minute = int((hour - int(hour)) * 3600.0 / 60.0); // Jumping minute pointer from minute to minute
}
if (hour > 12) {
hour -= 12.0;
}
LOG_DEBUG(GwLog::DEBUG,"... PageClock, value1: %f hour: %f minute:%f", value1, hour, minute); LOG_DEBUG(GwLog::DEBUG,"... PageClock, value1: %f hour: %f minute:%f", value1, hour, minute);
// Draw hour pointer // Draw hour pointer
@ -320,6 +426,28 @@ class PageClock : public Page
getdisplay().fillCircle(200, 150, startwidth + 6, commonData->bgcolor); getdisplay().fillCircle(200, 150, startwidth + 6, commonData->bgcolor);
getdisplay().fillCircle(200, 150, startwidth + 4, commonData->fgcolor); getdisplay().fillCircle(200, 150, startwidth + 4, commonData->fgcolor);
//*******************************************************************************************
// Key Layout
getdisplay().setFont(&Ubuntu_Bold8pt7b);
if(keylock == false){
getdisplay().setCursor(10, 290);
getdisplay().print("[SRC]");
getdisplay().setCursor(60, 290);
getdisplay().print("[MODE]");
getdisplay().setCursor(293, 290);
getdisplay().print("[TZ]");
getdisplay().setCursor(130, 290);
getdisplay().print("[ <<<< " + String(commonData.data.actpage) + "/" + String(commonData.data.maxpage) + " >>>> ]");
if(String(backlightMode) == "Control by Key"){ // Key for illumination
getdisplay().setCursor(343, 290);
getdisplay().print("[ILUM]");
}
}
else{
getdisplay().setCursor(130, 290);
getdisplay().print(" [ Keylock active ]");
}
// Update display // Update display
getdisplay().nextPage(); // Partial update (fast) getdisplay().nextPage(); // Partial update (fast)

View File

@ -45,12 +45,7 @@ typedef struct{
double onewireTemp[8] = {0,0,0,0,0,0,0,0}; double onewireTemp[8] = {0,0,0,0,0,0,0,0};
double rotationAngle = 0; // Rotation angle in radiant double rotationAngle = 0; // Rotation angle in radiant
bool validRotAngle = false; // Valid flag magnet present for rotation sensor bool validRotAngle = false; // Valid flag magnet present for rotation sensor
int rtcYear = 0; // UTC time struct tm rtcTime; // UTC time from internal RTC
int rtcMonth = 0;
int rtcDay = 0;
int rtcHour = 0;
int rtcMinute = 0;
int rtcSecond = 0;
int sunsetHour = 0; int sunsetHour = 0;
int sunsetMinute = 0; int sunsetMinute = 0;
int sunriseHour = 0; int sunriseHour = 0;
@ -169,13 +164,16 @@ class PageStruct{
PageDescription *description=NULL; PageDescription *description=NULL;
}; };
// Structure for formated boat values // Standard format functions without overhead
String formatDate(String fmttype, uint16_t year, uint8_t month, uint8_t day);
String formatTime(char fmttype, uint8_t hour, uint8_t minute, uint8_t second);
// Structure for formatted boat values
typedef struct{ typedef struct{
double value; double value;
String svalue; String svalue;
String unit; String unit;
} FormatedData; } FormatedData;
// Formatter for boat values
// Formater for boat values
FormatedData formatValue(GwApi::BoatValue *value, CommonData &commondata); FormatedData formatValue(GwApi::BoatValue *value, CommonData &commondata);

View File

@ -690,6 +690,21 @@
"obp60":"true" "obp60":"true"
} }
}, },
{
"name": "timeSource",
"label": "Status Time Source",
"type": "list",
"default": "GPS",
"description": "Data source for date and time display in status line [RTC|GPS]",
"list": [
{"l":"Internal real time clock (RTC)","v":"RTC"},
{"l":"External time via bus (GPS)","v":"GPS"}
],
"category": "OBP60 Display",
"capabilities": {
"obp60":"true"
}
},
{ {
"name": "refresh", "name": "refresh",
"label": "Refresh", "label": "Refresh",