From c1ac7173db0c150527974d2553e2adff71bbaa0d Mon Sep 17 00:00:00 2001 From: PS Date: Thu, 22 Apr 2021 12:44:55 -1000 Subject: [PATCH] Updating time / led brightness logging and fixed a time range checking issue --- src/app/ss_blumen_lumen/blumen_lumen.cpp | 49 ++++++++++++++----- src/app/ss_blumen_lumen/blumen_lumen.h | 62 +++++++++++++++++++----- 2 files changed, 87 insertions(+), 24 deletions(-) diff --git a/src/app/ss_blumen_lumen/blumen_lumen.cpp b/src/app/ss_blumen_lumen/blumen_lumen.cpp index 9bde8d9..6e0995c 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen.cpp +++ b/src/app/ss_blumen_lumen/blumen_lumen.cpp @@ -314,6 +314,24 @@ BlumenLumen_UpdateLog(app_state* State, blumen_lumen_state* BLState, context Con AppendPrintF(&FileStr, "Pattern Brightness: %f\n", BLState->BrightnessPercent); + time_range RangeIn = {}; + if (SystemTimeIsInTimeRangeList(Context.SystemTime_Current, + LedOnTimes, + LedOnTimesCount, + &RangeIn)) + { + AppendPrintF(&FileStr, "\tIn Leds-On Time Range: ( %d:%d - %d:%d)\n", + RangeIn.StartHour, RangeIn.StartMinute, + RangeIn.EndHour, RangeIn.EndMinute); + } + else + { + AppendPrintF(&FileStr, "\tIn Leds-On Time Range: None\n"); + } + + AppendPrintF(&FileStr, "\tTemp Dimming: %s\n", + Blumen_TempShouldDimLeds(BLState) ? "On" : "Off"); + AppendPrintF(&FileStr, "Last Temp Received: %d\n", BLState->LastTemperatureReceived); gs_data LogMem = StringToData(FileStr); @@ -528,8 +546,9 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) case PacketType_Temperature: { temp_packet Temp = Packet.TempPacket; + BLState->LastTemperatureReceived = Temp.Temperature; - if (Temp.Temperature > MinHighTemperature) + if (Blumen_TempShouldDimLeds(BLState)) { BLState->BrightnessPercent = HighTemperatureBrightnessPercent; } @@ -537,7 +556,6 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) { BLState->BrightnessPercent = FullBrightnessPercent; } - BLState->LastTemperatureReceived = Temp.Temperature; DEBUG_ReceivedTemperature(Temp, Context->ThreadContext); BLState->ShouldUpdateLog = true; @@ -711,19 +729,24 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) { bool TimelineShouldAdvance = false; r32 OverrideBrightness = 0.0f; - for (u32 i = 0; i < LedOnTimesCount; i++) + + time_range RangeIn = {}; + if (SystemTimeIsInTimeRangeList(Context->SystemTime_Current, + LedOnTimes, + LedOnTimesCount, + &RangeIn)) { - time_range Range = LedOnTimes[i]; - bool CurrTimeInRange = SystemTimeIsInTimeRange(Context->SystemTime_Current, Range); - if (CurrTimeInRange) - { - // If we're in one of the specified time ranges, - // play animations and set brightness - OverrideBrightness = BLState->BrightnessPercent; - TimelineShouldAdvance = State->AnimationSystem.TimelineShouldAdvance; - break; - } + // If we're in one of the specified time ranges, + // play animations and set brightness + // + // The values of BrightnessPercent and TimelineShouldAdvance + // are set according to less strict rules above in this update + // function. All we are doing is saying "If we are in a valid + // time range, keep those values". + OverrideBrightness = BLState->BrightnessPercent; + TimelineShouldAdvance = State->AnimationSystem.TimelineShouldAdvance; } + State->AnimationSystem.TimelineShouldAdvance = TimelineShouldAdvance; BLState->BrightnessPercent = OverrideBrightness; } diff --git a/src/app/ss_blumen_lumen/blumen_lumen.h b/src/app/ss_blumen_lumen/blumen_lumen.h index c5822a8..6c5ab47 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen.h +++ b/src/app/ss_blumen_lumen/blumen_lumen.h @@ -134,24 +134,57 @@ typedef struct time_range s32 EndMinute; } time_range; +internal bool +SystemTimeIsBeforeTime(system_time SysTime, s32 Hour, s32 Minute) +{ + bool Result = false; + if (SysTime.Hour == Hour) { + Result = SysTime.Minute < Minute; + } else { + Result = SysTime.Hour < Hour; + } + return Result; +} + +internal bool +SystemTimeIsAfterTime(system_time SysTime, s32 Hour, s32 Minute) +{ + bool Result = false; + if (SysTime.Hour == Hour) { + Result = SysTime.Minute > Minute; + } else { + Result = SysTime.Hour > Hour; + } + return Result; +} + internal bool SystemTimeIsInTimeRange(system_time SysTime, time_range Range) { bool Result = false; - if (SysTime.Hour >= Range.StartHour && - SysTime.Hour <= Range.EndHour) + + bool IsAfterStartTime = SystemTimeIsAfterTime(SysTime, Range.StartHour, Range.StartMinute); + bool IsBeforeEndTime = SystemTimeIsBeforeTime(SysTime, Range.EndHour, Range.EndMinute); + Result = IsAfterStartTime && IsBeforeEndTime; + + return Result; +} + +internal bool +SystemTimeIsInTimeRangeList(system_time SysTime, time_range* Ranges, u32 RangesCount, time_range* RangeOut = 0) +{ + bool Result = false; + for (u32 i = 0; i < RangesCount; i++) { - if (SysTime.Hour == Range.StartHour) - { - Result = (SysTime.Minute >= Range.StartMinute); - } - else if (SysTime.Hour == Range.EndHour) - { - Result = (SysTime.Minute <= Range.EndMinute); - } - else + time_range Range = Ranges[i]; + bool CurrTimeInRange = SystemTimeIsInTimeRange(SysTime, Range); + if (CurrTimeInRange) { Result = true; + if (RangeOut != 0) { + *RangeOut = Range; + } + break; } } return Result; @@ -233,6 +266,13 @@ struct blumen_lumen_state phrase_hue DebugHue; }; +internal bool +Blumen_TempShouldDimLeds(blumen_lumen_state* BLState) +{ + bool Result = BLState->LastTemperatureReceived > MinHighTemperature; + return Result; +} + #include "message_queue.cpp" internal void