Fixed motor time of day functionality

This commit is contained in:
PS 2021-04-12 16:00:09 -10:00
parent e4d9a389c3
commit 40f3a9f817
3 changed files with 36 additions and 85 deletions

View File

@ -8,7 +8,7 @@ call %MyPath%\setup_cl.bat
SET CommonCompilerFlags=-nologo -DDEBUG=1 -DPLATFORM_WINDOWS -FC -WX -W4 -Z7 -Oi -GR- -EHsc -EHa- -MTd -fp:fast -fp:except- -IC:\programs-dev\gs_libs\src
SET CommonCompilerFlags=-wd4127 -wd4702 -wd4101 -wd4505 -wd4100 -wd4189 -wd4244 -wd4201 -wd4996 -I%CommonLibs% -O2 %CommonCompilerFlags%
SET CommonCompilerFlags=-wd4127 -wd4702 -wd4101 -wd4505 -wd4100 -wd4189 -wd4244 -wd4201 -wd4996 -I%CommonLibs% -Od %CommonCompilerFlags%
SET CommonLinkerFlags= -opt:ref -incremental:no

View File

@ -486,9 +486,6 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context)
gs_string DebugStr = PushString(State->Transient, 256);
bool SendMotorCommand = false;
blumen_packet MotorCommand = {};
while (MessageQueue_CanRead(BLState->IncomingMsgQueue))
{
gs_data PacketData = MessageQueue_Read(&BLState->IncomingMsgQueue);
@ -624,101 +621,42 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context)
if (MessageQueue_CanWrite(BLState->OutgoingMsgQueue) &&
!BLState->IgnoreTimeOfDay_MotorState)
{
bool SendMotorCommand = false;
u64 NanosSinceLastSend = Context->SystemTime_Current.NanosSinceEpoch - BLState->LastSendTime.NanosSinceEpoch;
r32 SecondsSinceLastSend = (r64)NanosSinceLastSend * NanosToSeconds;
bool ShouldSendCurrentState = SecondsSinceLastSend >= MotorResendStatePeriod;
bl_motor_state_value NewMotorState = MotorState_Closed;
bool SendOpen = false;
for (u32 i = 0; i < MotorOpenTimesCount; i++)
{
time_range Range = MotorOpenTimes[i];
bool CurrTimeInRange = SystemTimeIsInTimeRange(Context->SystemTime_Current, Range);
bool LastSendTimeInRange = SystemTimeIsInTimeRange(BLState->LastSendTime, Range);
bool LastTimeInRange = SystemTimeIsInTimeRange(Context->SystemTime_Last, Range);
#if 0
bool SendOpen = CurrTimeInRange && !LastSendTimeInRange;
bool SendClose = !CurrTimeInRange && LastSendTimeInRange;
//SendOpen = SecondsSinceLastSend > 2;
if (SendOpen)
{
SendMotorCommand = true;
BLState->LastSendTime = Context->SystemTime_Current;
OutputDebugString("Motors: Open\n");
blumen_packet Packet = {};
Packet.Type = PacketType_MotorState;
Packet.MotorPacket.FlowerPositions[0] = 2;
Packet.MotorPacket.FlowerPositions[1] = 2;
Packet.MotorPacket.FlowerPositions[2] = 2;
MotorCommand = Packet;
break;
}
else if (SendClose)
{
SendMotorCommand = true;
BLState->LastSendTime = Context->SystemTime_Current;
OutputDebugString("Motors: Close\n");
blumen_packet Packet = {};
Packet.Type = PacketType_MotorState;
Packet.MotorPacket.FlowerPositions[0] = 1;
Packet.MotorPacket.FlowerPositions[1] = 1;
Packet.MotorPacket.FlowerPositions[2] = 1;
MotorCommand = Packet;
break;
}
#else
if (CurrTimeInRange) {
SendOpen = true;
// if the current state isn't what we want, we want to
// send immediately, rather than wait for the periodic time out
if (BLState->LastSendState != MotorState_Open)
NewMotorState = MotorState_Open;
}
}
if (NewMotorState != BLState->LastSendState)
{
ShouldSendCurrentState = true;
}
break;
}
#endif
}
if (ShouldSendCurrentState)
{
if (SendOpen)
{
BLState->LastSendTime = Context->SystemTime_Current;
BLState->LastSendState = MotorState_Open;
OutputDebugString("Motors: Open\n");
BLState->LastSendState = NewMotorState;
blumen_packet Packet = {};
Packet.Type = PacketType_MotorState;
Packet.MotorPacket.FlowerPositions[0] = 2;
Packet.MotorPacket.FlowerPositions[1] = 2;
Packet.MotorPacket.FlowerPositions[2] = 2;
MotorCommand = Packet;
}
else
{
BLState->LastSendTime = Context->SystemTime_Current;
BLState->LastSendState = MotorState_Closed;
OutputDebugString("Motors: Close\n");
Packet.MotorPacket.FlowerPositions[0] = NewMotorState;
Packet.MotorPacket.FlowerPositions[1] = NewMotorState;
Packet.MotorPacket.FlowerPositions[2] = NewMotorState;
blumen_packet Packet = {};
Packet.Type = PacketType_MotorState;
Packet.MotorPacket.FlowerPositions[0] = 1;
Packet.MotorPacket.FlowerPositions[1] = 1;
Packet.MotorPacket.FlowerPositions[2] = 1;
MotorCommand = Packet;
}
gs_data Msg = StructToData(&MotorCommand, blumen_packet);
gs_data Msg = StructToData(&Packet, blumen_packet);
MessageQueue_Write(&BLState->OutgoingMsgQueue, Msg);
DEBUG_SentMotorCommand(MotorCommand.MotorPacket, Context->ThreadContext);
DEBUG_SentMotorCommand(Packet.MotorPacket, Context->ThreadContext);
}
}

View File

@ -137,10 +137,23 @@ typedef struct time_range
internal bool
SystemTimeIsInTimeRange(system_time SysTime, time_range Range)
{
bool Result = (SysTime.Hour >= Range.StartHour &&
SysTime.Minute >= Range.StartMinute &&
SysTime.Hour <= Range.EndHour &&
SysTime.Minute <= Range.EndMinute);
bool Result = false;
if (SysTime.Hour >= Range.StartHour &&
SysTime.Hour <= Range.EndHour)
{
if (SysTime.Hour == Range.StartHour)
{
Result = (SysTime.Minute >= Range.StartMinute);
}
else if (SysTime.Hour == Range.EndHour)
{
Result = (SysTime.Minute <= Range.EndMinute);
}
else
{
Result = true;
}
}
return Result;
}