Skip to content

Commit

Permalink
Simplified UTC offset calculation, removed floats, using suggestion f…
Browse files Browse the repository at this point in the history
…rom @henrygab
  • Loading branch information
jasoncoon committed Nov 25, 2021
1 parent f5288e1 commit bf5f172
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions esp8266-fastled-webserver/Fields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ const String UtcOffsetIndexFieldType = "UtcOffsetIndex";
uint8_t power = 1;
uint8_t brightness = brightnessMap[brightnessIndex];


float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

//String setPower(String value) {
// power = value.toInt();
// if(power < 0) power = 0;
Expand Down Expand Up @@ -164,10 +158,18 @@ String setUtcOffsetIndex(uint8_t value)
{
utcOffsetIndex = value;
if (utcOffsetIndex > 104) utcOffsetIndex = 104;
float utcOffsetInHours = mapfloat(utcOffsetIndex, 0, 104, -12, 14);
utcOffsetInSeconds = utcOffsetInHours * 60 * 60;

const int32_t UTC_OFFSET_MINIMUM_MINUTES = ((int32_t)-12) * 60; // corresponds to index 0
const int32_t UTC_OFFSET_INCREMENT_MINUTES = 15; // each higher index increments by this amount

// minutes above the minimum
int32_t tmp = utcOffsetIndex * UTC_OFFSET_INCREMENT_MINUTES;
// add that to the minimum value
tmp = UTC_OFFSET_MINIMUM_MINUTES + tmp;
// convert to seconds
utcOffsetInSeconds = tmp * 60;

Serial.print(F("utcOffsetIndex: ")); Serial.println(utcOffsetIndex);
Serial.print(F("utcOffsetInHours: ")); Serial.println(utcOffsetInHours);
Serial.print(F("utcOffsetInSeconds: ")); Serial.println(utcOffsetInSeconds);
timeClient.setTimeOffset(utcOffsetInSeconds);
writeAndCommitSettings();
Expand Down

0 comments on commit bf5f172

Please sign in to comment.