Skip to content

Commit 1ba65d7

Browse files
committed
sunset/sunrise + backlight
1 parent 40b7daa commit 1ba65d7

File tree

7 files changed

+33
-59
lines changed

7 files changed

+33
-59
lines changed

lib/obp60task/OBP60Extensions.cpp

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,14 @@ SensorData calcSunsetSunrise(double time, double date, double latitude, double l
233233
int intminSR = 0;
234234
int inthrSS = 0;
235235
int intminSS = 0;
236+
bool sunDown = true;
236237

237-
// Calculate local time
238-
time_t t = (date * secPerYear) + (time + int(timezone * secPerHour));
239238

240-
// 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);
239+
// 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);
241240

242-
if (!isnan(time) && !isnan(date) && !isnan(latitude) && !isnan(longitude) && !isnan(timezone)) {
241+
if (!isnan(time) && !isnan(date) && !isnan(latitude) && !isnan(longitude) && !isnan(timezone)) {
242+
// Calculate local time
243+
time_t t = (date * secPerYear) + (time + int(timezone * secPerHour));
243244
sr.calculate(latitude, longitude, t); // LAT, LON, EPOCH
244245
// Sunrise
245246
if (sr.hasRise) {
@@ -253,48 +254,20 @@ SensorData calcSunsetSunrise(double time, double date, double latitude, double l
253254
inthrSS = int (sunS / secPerHour);
254255
intminSS = int((sunS - inthrSS * secPerHour)/60);
255256
}
257+
// Sun control (return value by sun on sky = false, sun down = true)
258+
if ((t >= (sr.riseTime + int(timezone * secPerHour))) && (t <= (sr.setTime + int(timezone * secPerHour))))
259+
sunDown = false;
260+
else sunDown = true;
256261
}
257262
// Return values
258263
returnset.sunsetHour = inthrSS;
259264
returnset.sunsetMinute = intminSS;
260265
returnset.sunriseHour = inthrSR;
261266
returnset.sunriseMinute = intminSR;
267+
returnset.sunDown = sunDown;
262268

263-
// 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);
269+
// 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);
264270
return returnset;
265271
}
266272

267-
// Sun control (return valu by sun on sky = false, sun down = true)
268-
bool sunControl(double time, double date, double latitude, double longitude, double timezone){
269-
SunRise sr;
270-
int secPerHour = 3600;
271-
int secPerYear = 86400;
272-
sr.hasRise = false;
273-
sr.hasSet = false;
274-
time_t sunR = 0;
275-
time_t sunS = 0;
276-
277-
// Calculate local time
278-
time_t t = (date * secPerYear) + (time + int(timezone * secPerHour));
279-
280-
if (!isnan(time) && !isnan(date) && !isnan(latitude) && !isnan(longitude) && !isnan(timezone)) {
281-
sr.calculate(latitude, longitude, t); // LAT, LON, EPOCH
282-
// Sunrise
283-
if (sr.hasRise) {
284-
sunR = (sr.riseTime + int(timezone * secPerHour) + 30); // add 30 seconds: round to minutes
285-
}
286-
// Sunset
287-
if (sr.hasSet) {
288-
sunS = (sr.setTime + int(timezone * secPerHour) + 30); // add 30 seconds: round to minutes
289-
}
290-
}
291-
// Return values (sun on sky = false, sun down = true)
292-
if(t > sunR && t < sunS){
293-
return false;
294-
}
295-
else{
296-
return true;
297-
}
298-
}
299-
300273
#endif

lib/obp60task/OBP60Extensions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ void displayTrendLow(int16_t x, int16_t y, uint16_t size, uint16_t color);
4343
void displayHeader(CommonData &commonData, GwApi::BoatValue *date, GwApi::BoatValue *time); // Draw display header
4444

4545
SensorData calcSunsetSunrise(double time, double date, double latitude, double longitude, double timezone); // Calulate sunset and sunrise
46-
bool sunControl(double time, double date, double latitude, double longitude, double timezone); // Control bit for sun
46+
//bool sunControl(double time, double date, double latitude, double longitude, double timezone); // Control bit for sun
4747

4848
#endif

lib/obp60task/OBP60Formater.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ FormatedData formatValue(GwApi::BoatValue *value, CommonData &commondata){
1414
static int dayoffset = 0;
1515

1616
// Load configuration values
17-
int timeZone = commondata.config->getInt(commondata.config->timeZone); // [UTC -12...+14]
17+
18+
String stimeZone = commondata.config->getString(commondata.config->timeZone); // [UTC -14.00...+12.00]
19+
double timeZone = stimeZone.toDouble();
1820
String lengthFormat = commondata.config->getString(commondata.config->lengthFormat); // [m|ft]
1921
String distanceFormat = commondata.config->getString(commondata.config->distanceFormat); // [m|km|nm]
2022
String speedFormat = commondata.config->getString(commondata.config->speedFormat); // [m/s|km/h|kn]
@@ -28,6 +30,8 @@ FormatedData formatValue(GwApi::BoatValue *value, CommonData &commondata){
2830
result.svalue = "---";
2931
return result;
3032
}
33+
34+
LOG_DEBUG(GwLog::DEBUG,"formatValue init: getFormat: %s date->value: %f time->value: %f", value->getFormat(), commondata.date->value, commondata.time->value);
3135
static const int bsize = 30;
3236
char buffer[bsize+1];
3337
buffer[0]=0;

lib/obp60task/PageClock.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,11 @@ bool keylock = false; // Keylock
147147
}
148148

149149
// Show values sunrise
150-
String sunrise = "";
150+
String sunrise = "---";
151151
if(valid1 == true && valid2 == true){
152-
sunrise = String(commonData.data.sunriseHour) + ":" + String(commonData.data.sunriseMinute);
153-
}
154-
else{
155-
sunrise = "---";
152+
sunrise = String(commonData.data.sunriseHour) + ":" + String(commonData.data.sunriseMinute + 100).substring(1);
156153
}
154+
157155
display.setTextColor(textcolor);
158156
display.setFont(&Ubuntu_Bold8pt7b);
159157
display.setCursor(335, 65);
@@ -166,13 +164,13 @@ bool keylock = false; // Keylock
166164
display.fillRect(340, 149, 80, 3, pixelcolor);
167165

168166
// Show values sunset
169-
String sunset = "";
167+
String sunset = "---";
170168
if(valid1 == true && valid2 == true){
171-
sunset= String(commonData.data.sunsetHour) + ":" + String(commonData.data.sunsetMinute);
172-
}
173-
else{
174-
sunset = "---";
169+
sunset = String(commonData.data.sunsetHour) + ":" + String(commonData.data.sunsetMinute + 100).substring(1);
175170
}
171+
172+
LOG_DEBUG(GwLog::LOG,"sunrise: .%s., sunset: .%s.", String(commonData.data.sunriseMinute + 100), String(commonData.data.sunsetMinute + 100));
173+
176174
display.setTextColor(textcolor);
177175
display.setFont(&Ubuntu_Bold8pt7b);
178176
display.setCursor(335, 250);

lib/obp60task/Pagedata.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct{
4747
int sunsetMinute = 0;
4848
int sunriseHour = 0;
4949
int sunriseMinute = 0;
50+
bool sunDown = true;
5051
} SensorData;
5152

5253
typedef struct{

lib/obp60task/obp60task.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,13 @@ void OBP60Task(GwApi *api){
391391
setPortPin(OBP_FLASH_LED, true);
392392
}
393393

394-
// Back light with sun control
394+
// Back light with sun control: turn on if no valid data for safety reasons
395395
if(String(backlight) == "Control by Sun"){
396-
if(time->valid == true && date->valid == true && lat->valid == true && lon->valid == true){
397-
setPortPin(OBP_BACKLIGHT_LED, sunControl(time->value, date->value, lat->value, lon->value, tz.toDouble()));
396+
if(time->valid == false || date->valid == false || lat->valid == false || lon->valid == false){
397+
setPortPin(OBP_BACKLIGHT_LED, true);
398398
}
399399
}
400-
400+
401401
// Check the keyboard message
402402
int keyboardMessage=0;
403403
while (xQueueReceive(allParameters.queue,&keyboardMessage,0)){
@@ -454,10 +454,8 @@ void OBP60Task(GwApi *api){
454454
if(time->valid == true && date->valid == true && lat->valid == true && lon->valid == true){
455455
commonData.data = calcSunsetSunrise(time->value , date->value, lat->value, lon->value, tz.toDouble());
456456
// Backlight with sun control
457-
if(String(backlight) == "Control by Sun"){
458-
if(time->valid == true && date->valid == true && lat->valid == true && lon->valid == true){
459-
setPortPin(OBP_BACKLIGHT_LED, sunControl(time->value, date->value, lat->value, lon->value, tz.toDouble()));
460-
}
457+
if(String(backlight) == "Control by Sun"){
458+
setPortPin(OBP_BACKLIGHT_LED, commonData.data.sunDown);
461459
}
462460
}
463461
}

lib/obp60task/platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ lib_deps =
2222
build_flags=
2323
-D BOARD_NODEMCU32S_OBP60
2424
${env.build_flags}
25-
upload_port = COM3
25+
upload_port = COM6
2626
upload_protocol = esptool
2727
monitor_speed = 115200

0 commit comments

Comments
 (0)