From 1ba65d784cabfdd6d81f69f4da4063b310cd53b7 Mon Sep 17 00:00:00 2001 From: Geronius Date: Sat, 2 Apr 2022 00:27:56 +0200 Subject: [PATCH] sunset/sunrise + backlight --- lib/obp60task/OBP60Extensions.cpp | 49 +++++++------------------------ lib/obp60task/OBP60Extensions.h | 2 +- lib/obp60task/OBP60Formater.cpp | 6 +++- lib/obp60task/PageClock.cpp | 18 +++++------- lib/obp60task/Pagedata.h | 1 + lib/obp60task/obp60task.cpp | 14 ++++----- lib/obp60task/platformio.ini | 2 +- 7 files changed, 33 insertions(+), 59 deletions(-) diff --git a/lib/obp60task/OBP60Extensions.cpp b/lib/obp60task/OBP60Extensions.cpp index c33a6d5..ca81bc3 100644 --- a/lib/obp60task/OBP60Extensions.cpp +++ b/lib/obp60task/OBP60Extensions.cpp @@ -233,13 +233,14 @@ SensorData calcSunsetSunrise(double time, double date, double latitude, double l int intminSR = 0; int inthrSS = 0; int intminSS = 0; + bool sunDown = true; - // Calculate local time - time_t t = (date * secPerYear) + (time + int(timezone * secPerHour)); -// api->getLogger()->logDebug(GwLog::DEBUG,"... PageClock: Lat %f, Lon %f, at: %d, next SR: %d (%s), next SS: %d (%s)", latitude, longitude, t, sunR, sSunR, sunS, sSunS); +// api->getLogger()->logDebug(GwLog::DEBUG,"... calcSun: Lat %f, Lon %f, at: %d, next SR: %d (%s), next SS: %d (%s)", latitude, longitude, t, sunR, sSunR, sunS, sSunS); - if (!isnan(time) && !isnan(date) && !isnan(latitude) && !isnan(longitude) && !isnan(timezone)) { + if (!isnan(time) && !isnan(date) && !isnan(latitude) && !isnan(longitude) && !isnan(timezone)) { + // Calculate local time + time_t t = (date * secPerYear) + (time + int(timezone * secPerHour)); sr.calculate(latitude, longitude, t); // LAT, LON, EPOCH // Sunrise if (sr.hasRise) { @@ -253,48 +254,20 @@ SensorData calcSunsetSunrise(double time, double date, double latitude, double l inthrSS = int (sunS / secPerHour); intminSS = int((sunS - inthrSS * secPerHour)/60); } + // Sun control (return value by sun on sky = false, sun down = true) + if ((t >= (sr.riseTime + int(timezone * secPerHour))) && (t <= (sr.setTime + int(timezone * secPerHour)))) + sunDown = false; + else sunDown = true; } // Return values returnset.sunsetHour = inthrSS; returnset.sunsetMinute = intminSS; returnset.sunriseHour = inthrSR; returnset.sunriseMinute = intminSR; + returnset.sunDown = sunDown; -// api->getLogger()->logDebug(GwLog::DEBUG,"... PageClock: at t: %d, hasRise: %d, next SR: %d '%s', hasSet: %d, next SS: %d '%s'\n", t, sr.hasRise, sr.riseTime, sSunR, sr.hasSet, sr.setTime, sSunS); +// api->getLogger()->logDebug(GwLog::DEBUG,"... calcSun: at t: %d, hasRise: %d, next SR: %d '%s', hasSet: %d, next SS: %d '%s'\n", t, sr.hasRise, sr.riseTime, sSunR, sr.hasSet, sr.setTime, sSunS); return returnset; } -// Sun control (return valu by sun on sky = false, sun down = true) -bool sunControl(double time, double date, double latitude, double longitude, double timezone){ - SunRise sr; - int secPerHour = 3600; - int secPerYear = 86400; - sr.hasRise = false; - sr.hasSet = false; - time_t sunR = 0; - time_t sunS = 0; - - // Calculate local time - time_t t = (date * secPerYear) + (time + int(timezone * secPerHour)); - - if (!isnan(time) && !isnan(date) && !isnan(latitude) && !isnan(longitude) && !isnan(timezone)) { - sr.calculate(latitude, longitude, t); // LAT, LON, EPOCH - // Sunrise - if (sr.hasRise) { - sunR = (sr.riseTime + int(timezone * secPerHour) + 30); // add 30 seconds: round to minutes - } - // Sunset - if (sr.hasSet) { - sunS = (sr.setTime + int(timezone * secPerHour) + 30); // add 30 seconds: round to minutes - } - } - // Return values (sun on sky = false, sun down = true) - if(t > sunR && t < sunS){ - return false; - } - else{ - return true; - } -} - #endif \ No newline at end of file diff --git a/lib/obp60task/OBP60Extensions.h b/lib/obp60task/OBP60Extensions.h index 525ca28..aee8763 100644 --- a/lib/obp60task/OBP60Extensions.h +++ b/lib/obp60task/OBP60Extensions.h @@ -43,6 +43,6 @@ void displayTrendLow(int16_t x, int16_t y, uint16_t size, uint16_t color); void displayHeader(CommonData &commonData, GwApi::BoatValue *date, GwApi::BoatValue *time); // Draw display header SensorData calcSunsetSunrise(double time, double date, double latitude, double longitude, double timezone); // Calulate sunset and sunrise -bool sunControl(double time, double date, double latitude, double longitude, double timezone); // Control bit for sun +//bool sunControl(double time, double date, double latitude, double longitude, double timezone); // Control bit for sun #endif \ No newline at end of file diff --git a/lib/obp60task/OBP60Formater.cpp b/lib/obp60task/OBP60Formater.cpp index c944fa6..060261f 100644 --- a/lib/obp60task/OBP60Formater.cpp +++ b/lib/obp60task/OBP60Formater.cpp @@ -14,7 +14,9 @@ FormatedData formatValue(GwApi::BoatValue *value, CommonData &commondata){ static int dayoffset = 0; // Load configuration values - int timeZone = commondata.config->getInt(commondata.config->timeZone); // [UTC -12...+14] + + String stimeZone = commondata.config->getString(commondata.config->timeZone); // [UTC -14.00...+12.00] + double timeZone = stimeZone.toDouble(); String lengthFormat = commondata.config->getString(commondata.config->lengthFormat); // [m|ft] String distanceFormat = commondata.config->getString(commondata.config->distanceFormat); // [m|km|nm] String speedFormat = commondata.config->getString(commondata.config->speedFormat); // [m/s|km/h|kn] @@ -28,6 +30,8 @@ FormatedData formatValue(GwApi::BoatValue *value, CommonData &commondata){ result.svalue = "---"; return result; } + + LOG_DEBUG(GwLog::DEBUG,"formatValue init: getFormat: %s date->value: %f time->value: %f", value->getFormat(), commondata.date->value, commondata.time->value); static const int bsize = 30; char buffer[bsize+1]; buffer[0]=0; diff --git a/lib/obp60task/PageClock.cpp b/lib/obp60task/PageClock.cpp index 6ab6f2d..e83524d 100644 --- a/lib/obp60task/PageClock.cpp +++ b/lib/obp60task/PageClock.cpp @@ -147,13 +147,11 @@ public: } // Show values sunrise - String sunrise = ""; + String sunrise = "---"; if(valid1 == true && valid2 == true){ - sunrise = String(commonData.data.sunriseHour) + ":" + String(commonData.data.sunriseMinute); - } - else{ - sunrise = "---"; + sunrise = String(commonData.data.sunriseHour) + ":" + String(commonData.data.sunriseMinute + 100).substring(1); } + display.setTextColor(textcolor); display.setFont(&Ubuntu_Bold8pt7b); display.setCursor(335, 65); @@ -166,13 +164,13 @@ public: display.fillRect(340, 149, 80, 3, pixelcolor); // Show values sunset - String sunset = ""; + String sunset = "---"; if(valid1 == true && valid2 == true){ - sunset= String(commonData.data.sunsetHour) + ":" + String(commonData.data.sunsetMinute); - } - else{ - sunset = "---"; + sunset = String(commonData.data.sunsetHour) + ":" + String(commonData.data.sunsetMinute + 100).substring(1); } + + LOG_DEBUG(GwLog::LOG,"sunrise: .%s., sunset: .%s.", String(commonData.data.sunriseMinute + 100), String(commonData.data.sunsetMinute + 100)); + display.setTextColor(textcolor); display.setFont(&Ubuntu_Bold8pt7b); display.setCursor(335, 250); diff --git a/lib/obp60task/Pagedata.h b/lib/obp60task/Pagedata.h index 892f725..9a6e26d 100644 --- a/lib/obp60task/Pagedata.h +++ b/lib/obp60task/Pagedata.h @@ -47,6 +47,7 @@ typedef struct{ int sunsetMinute = 0; int sunriseHour = 0; int sunriseMinute = 0; + bool sunDown = true; } SensorData; typedef struct{ diff --git a/lib/obp60task/obp60task.cpp b/lib/obp60task/obp60task.cpp index 4352f09..7e4e998 100644 --- a/lib/obp60task/obp60task.cpp +++ b/lib/obp60task/obp60task.cpp @@ -391,13 +391,13 @@ void OBP60Task(GwApi *api){ setPortPin(OBP_FLASH_LED, true); } - // Back light with sun control + // Back light with sun control: turn on if no valid data for safety reasons if(String(backlight) == "Control by Sun"){ - if(time->valid == true && date->valid == true && lat->valid == true && lon->valid == true){ - setPortPin(OBP_BACKLIGHT_LED, sunControl(time->value, date->value, lat->value, lon->value, tz.toDouble())); + if(time->valid == false || date->valid == false || lat->valid == false || lon->valid == false){ + setPortPin(OBP_BACKLIGHT_LED, true); } } - + // Check the keyboard message int keyboardMessage=0; while (xQueueReceive(allParameters.queue,&keyboardMessage,0)){ @@ -454,10 +454,8 @@ void OBP60Task(GwApi *api){ if(time->valid == true && date->valid == true && lat->valid == true && lon->valid == true){ commonData.data = calcSunsetSunrise(time->value , date->value, lat->value, lon->value, tz.toDouble()); // Backlight with sun control - if(String(backlight) == "Control by Sun"){ - if(time->valid == true && date->valid == true && lat->valid == true && lon->valid == true){ - setPortPin(OBP_BACKLIGHT_LED, sunControl(time->value, date->value, lat->value, lon->value, tz.toDouble())); - } + if(String(backlight) == "Control by Sun"){ + setPortPin(OBP_BACKLIGHT_LED, commonData.data.sunDown); } } } diff --git a/lib/obp60task/platformio.ini b/lib/obp60task/platformio.ini index 6be9046..96afbe6 100644 --- a/lib/obp60task/platformio.ini +++ b/lib/obp60task/platformio.ini @@ -22,6 +22,6 @@ lib_deps = build_flags= -D BOARD_NODEMCU32S_OBP60 ${env.build_flags} -upload_port = COM3 +upload_port = COM6 upload_protocol = esptool monitor_speed = 115200 \ No newline at end of file