From b636e9ef3db03bfc6c69e1b63dd32ee3743c47f2 Mon Sep 17 00:00:00 2001 From: PS Date: Thu, 8 Apr 2021 15:35:55 -1000 Subject: [PATCH] Debug utilities for internal motor state tracking, and fixed a bug where closing the motors wouldn't turn the upper leds back on --- src/app/ss_blumen_lumen/blumen_lumen.cpp | 51 +++++++++++++++++------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/app/ss_blumen_lumen/blumen_lumen.cpp b/src/app/ss_blumen_lumen/blumen_lumen.cpp index 8995226..be87d78 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen.cpp +++ b/src/app/ss_blumen_lumen/blumen_lumen.cpp @@ -392,6 +392,25 @@ BlumenLumen_CustomInit(app_state* State, context Context) return Result; } +internal void +BlumenLumen_UpdateMotorState(blumen_lumen_state* BLState, motor_status_packet Motor, context Context) +{ + motor_packet CurrPos = Motor.Pos; + motor_packet LastPos = BLState->LastKnownMotorState; + DEBUG_ReceivedMotorPositions(LastPos, Motor.Pos, Context.ThreadContext); + + for (u32 i = 0; i < BL_FLOWER_COUNT; i++) + { + if (LastPos.FlowerPositions[i] != CurrPos.FlowerPositions[i]) + { + BLState->LastTimeMotorStateChanged[i] = Context.SystemTime_Current.NanosSinceEpoch; + } + } + + BLState->LastKnownMotorState = Motor.Pos; + BLState->ShouldUpdateLog = true; +} + internal void BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) { @@ -444,20 +463,7 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) Motor.Temperature = (T[0] << 8 | T[1] << 0); - motor_packet CurrPos = Motor.Pos; - motor_packet LastPos = BLState->LastKnownMotorState; - DEBUG_ReceivedMotorPositions(LastPos, Motor.Pos, Context->ThreadContext); - - for (u32 i = 0; i < BL_FLOWER_COUNT; i++) - { - if (LastPos.FlowerPositions[i] != CurrPos.FlowerPositions[i]) - { - BLState->LastTimeMotorStateChanged[i] = Context->SystemTime_Current.NanosSinceEpoch; - } - } - - BLState->LastKnownMotorState = Motor.Pos; - BLState->ShouldUpdateLog = true; + BlumenLumen_UpdateMotorState(BLState, Motor, *Context); }break; case PacketType_Temperature: @@ -628,6 +634,11 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) BLState->ShouldDimUpperLeds[i] = true; } } + else if (MotorPos == MotorState_Closed || + MotorPos == MotorState_HalfOpen) + { + BLState->ShouldDimUpperLeds[i] = false; + } } // NOTE(PS): If the flowers are mostly open or full open @@ -794,11 +805,23 @@ US_CUSTOM_DEBUG_UI(BlumenLumen_DebugUI) ui_Label(I, MakeString("Set Internal Motor State:")); if (ui_Button(I, MakeString("Closed"))) { + motor_status_packet Motor = {}; + Motor.Pos.FlowerPositions[0] = MotorState_Closed; + Motor.Pos.FlowerPositions[1] = MotorState_Closed; + Motor.Pos.FlowerPositions[2] = MotorState_Closed; + Motor.Temperature = 16; + BlumenLumen_UpdateMotorState(BLState, Motor, Context); } if (ui_Button(I, MakeString("Open"))) { + motor_status_packet Motor = {}; + Motor.Pos.FlowerPositions[0] = MotorState_Open; + Motor.Pos.FlowerPositions[1] = MotorState_Open; + Motor.Pos.FlowerPositions[2] = MotorState_Open; + Motor.Temperature = 16; + BlumenLumen_UpdateMotorState(BLState, Motor, Context); } } }