Skip to content

Commit bf5f172

Browse files
committed
Simplified UTC offset calculation, removed floats, using suggestion from @henrygab
1 parent f5288e1 commit bf5f172

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

esp8266-fastled-webserver/Fields.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ const String UtcOffsetIndexFieldType = "UtcOffsetIndex";
2929
uint8_t power = 1;
3030
uint8_t brightness = brightnessMap[brightnessIndex];
3131

32-
33-
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
34-
{
35-
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
36-
}
37-
3832
//String setPower(String value) {
3933
// power = value.toInt();
4034
// if(power < 0) power = 0;
@@ -164,10 +158,18 @@ String setUtcOffsetIndex(uint8_t value)
164158
{
165159
utcOffsetIndex = value;
166160
if (utcOffsetIndex > 104) utcOffsetIndex = 104;
167-
float utcOffsetInHours = mapfloat(utcOffsetIndex, 0, 104, -12, 14);
168-
utcOffsetInSeconds = utcOffsetInHours * 60 * 60;
161+
162+
const int32_t UTC_OFFSET_MINIMUM_MINUTES = ((int32_t)-12) * 60; // corresponds to index 0
163+
const int32_t UTC_OFFSET_INCREMENT_MINUTES = 15; // each higher index increments by this amount
164+
165+
// minutes above the minimum
166+
int32_t tmp = utcOffsetIndex * UTC_OFFSET_INCREMENT_MINUTES;
167+
// add that to the minimum value
168+
tmp = UTC_OFFSET_MINIMUM_MINUTES + tmp;
169+
// convert to seconds
170+
utcOffsetInSeconds = tmp * 60;
171+
169172
Serial.print(F("utcOffsetIndex: ")); Serial.println(utcOffsetIndex);
170-
Serial.print(F("utcOffsetInHours: ")); Serial.println(utcOffsetInHours);
171173
Serial.print(F("utcOffsetInSeconds: ")); Serial.println(utcOffsetInSeconds);
172174
timeClient.setTimeOffset(utcOffsetInSeconds);
173175
writeAndCommitSettings();

0 commit comments

Comments
 (0)