From f6baf2290797f8bf61ed27697bb478d55eab7dc6 Mon Sep 17 00:00:00 2001 From: Peter Slattery Date: Sat, 20 Mar 2021 18:06:04 -0700 Subject: [PATCH] Message logging --- .../panels/foldhaus_panel_assembly_debug.h | 2 - src/app/foldhaus_app.cpp | 3 +- src/app/foldhaus_platform.h | 2 +- .../platform_win32/win32_foldhaus_socket.h | 28 +++++ src/app/ss_blumen_lumen/blumen_lumen.cpp | 113 ++++++++++-------- src/app/ss_blumen_lumen/blumen_lumen.h | 21 +++- 6 files changed, 113 insertions(+), 56 deletions(-) diff --git a/src/app/editor/panels/foldhaus_panel_assembly_debug.h b/src/app/editor/panels/foldhaus_panel_assembly_debug.h index 2f4f1a2..ee75876 100644 --- a/src/app/editor/panels/foldhaus_panel_assembly_debug.h +++ b/src/app/editor/panels/foldhaus_panel_assembly_debug.h @@ -107,8 +107,6 @@ AssemblyDebug_Render(panel* Panel, rect2 PanelBounds, render_command_buffer* Ren }break; } - ui_RangeSlider(Interface, MakeString("Test"), .5f, 0, 1); - ui_PopLayout(Interface, MakeString("Assembly Debug Layout")); } diff --git a/src/app/foldhaus_app.cpp b/src/app/foldhaus_app.cpp index 5016e27..e5c1fed 100644 --- a/src/app/foldhaus_app.cpp +++ b/src/app/foldhaus_app.cpp @@ -133,7 +133,8 @@ UPDATE_AND_RENDER(UpdateAndRender) Editor_Render(State, Context, RenderBuffer); } -#if SEND_DATA +#define SEND_DATA +#ifdef SEND_DATA // NOTE(pjs): Building data buffers to be sent out to the sculpture // This array is used on the platform side to actually send the information assembly_array SACNAssemblies = AssemblyArray_Filter(State->Assemblies, AssemblyFilter_OutputsViaSACN, State->Transient); diff --git a/src/app/foldhaus_platform.h b/src/app/foldhaus_platform.h index efe8ff8..8a30d92 100644 --- a/src/app/foldhaus_platform.h +++ b/src/app/foldhaus_platform.h @@ -201,7 +201,7 @@ typedef struct system_time s32 Second; } system_time; -#define STATUS_PACKET_FREQ_SECONDS 5 +#define STATUS_PACKET_FREQ_SECONDS 2 struct context { diff --git a/src/app/platform_win32/win32_foldhaus_socket.h b/src/app/platform_win32/win32_foldhaus_socket.h index 1a51d46..6c83a0a 100644 --- a/src/app/platform_win32/win32_foldhaus_socket.h +++ b/src/app/platform_win32/win32_foldhaus_socket.h @@ -264,6 +264,11 @@ Win32SocketPeek(platform_socket* Socket) { }break; + case WSAECONNABORTED: + { + Win32CloseSocket(Socket); + }break; + InvalidDefaultCase; } } @@ -302,6 +307,14 @@ Win32SocketReceive(platform_socket* Socket, gs_memory_arena* Storage) #endif } + +typedef struct status_packet_foo +{ + u8 NextMotorEventType; + u32 NextEventTime; + char AnimFileName[32]; +} status_packet; + internal s32 Win32SocketSend(platform_socket* Socket, u32 Address, u32 Port, gs_data Data, s32 Flags) { @@ -312,8 +325,11 @@ Win32SocketSend(platform_socket* Socket, u32 Address, u32 Port, gs_data Data, s3 SockAddress.sin_port = HostToNetU16(Port); SockAddress.sin_addr.s_addr = HostToNetU32(Address); + status_packet_foo* Foo = (status_packet_foo*)Data.Memory; + s32 LengthSent = sendto(*Win32Sock, (char*)Data.Memory, Data.Size, Flags, (sockaddr*)&SockAddress, sizeof(sockaddr_in)); + OutputDebugString("Attempting To Send Network Data: "); if (LengthSent == SOCKET_ERROR) { s32 Error = WSAGetLastError(); @@ -330,6 +346,12 @@ Win32SocketSend(platform_socket* Socket, u32 Address, u32 Port, gs_data Data, s3 // TODO(Peter): :ErrorLogging InvalidCodePath; } + + OutputDebugString("Error\n"); + } + else + { + OutputDebugString("Sent\n"); } return LengthSent; @@ -368,9 +390,11 @@ Win32Socket_SendTo(platform_socket_handle SocketHandle, u32 Address, u32 Port, c s32 LengthSent = sendto(Socket->Socket, Buffer, BufferLength, Flags, (sockaddr*)&SockAddress, sizeof(sockaddr_in)); + OutputDebugString("Attempting To Send Network Data: "); if (LengthSent == SOCKET_ERROR) { s32 Error = WSAGetLastError(); + OutputDebugString("Error\n"); if (Error == 10051) { } @@ -380,6 +404,10 @@ Win32Socket_SendTo(platform_socket_handle SocketHandle, u32 Address, u32 Port, c InvalidCodePath; } } + else + { + OutputDebugString("Sent\n"); + } return LengthSent; } diff --git a/src/app/ss_blumen_lumen/blumen_lumen.cpp b/src/app/ss_blumen_lumen/blumen_lumen.cpp index 370341d..b1e29cb 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen.cpp +++ b/src/app/ss_blumen_lumen/blumen_lumen.cpp @@ -37,26 +37,21 @@ MessageQueue_Init(blumen_network_msg_queue* Queue, gs_memory_arena* Arena) } } -internal gs_data* -MessageQueue_GetWrite(blumen_network_msg_queue* Queue) -{ - u32 Index = Queue->WriteHead++; - gs_data* Result = &Queue->Buffers[Index]; - Assert(Result->Size > 0); - - if (Queue->WriteHead >= PACKETS_MAX) - { - Queue->WriteHead = 0; - } - return Result; -} - internal bool MessageQueue_Write(blumen_network_msg_queue* Queue, gs_data Msg) { - gs_data* Dest = MessageQueue_GetWrite(Queue); Assert(Msg.Size <= DEFAULT_QUEUE_ENTRY_SIZE); + + u32 Index = Queue->WriteHead; + gs_data* Dest = Queue->Buffers + Index; CopyMemoryTo(Msg.Memory, Dest->Memory, Msg.Size); + Dest->Size = Msg.Size; + + // NOTE(pjs): We increment write head at the end of writing so that + // a reader thread doesn't pull the message off before we've finished + // filling it out + Queue->WriteHead++; + return true; } internal bool @@ -104,8 +99,6 @@ BlumenLumen_MicListenJob(gs_thread_context* Ctx, u8* UserData) u32 Port = WeathermanPort; s32 Flags = 0; SocketSend(Data->SocketManager, Data->ListenSocket, Address, Port, Msg, Flags); - - OutputDebugString("Sending Motor Packet\n"); } } } @@ -180,7 +173,7 @@ BlumenLumen_CustomInit(app_state* State, context Context) BLState->MicListenJobData.OutgoingMsgQueue = &BLState->OutgoingMsgQueue; BLState->MicListenJobData.ListenSocket = CreateSocket(Context.SocketManager, "127.0.0.1", "20185"); -#if 0 +#if 1 BLState->MicListenThread = CreateThread(Context.ThreadManager, BlumenLumen_MicListenJob, (u8*)&BLState->MicListenJobData); #endif @@ -284,18 +277,30 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) State->AnimationSystem.ActiveFadeGroup.From.Index = 2; } - OutputDebugStringA("Received Pattern Packet\n"); + OutputDebugStringA("\nReceived Pattern Packet\n"); }break; case PacketType_MotorState: { motor_packet Motor = Packet.MotorPacket; + + // NOTE(pjs): Python sends multi-byte integers in little endian + // order. Have to unpack + u8* T = (u8*)&Motor.Temperature; + Motor.Temperature = (T[0] << 8 | + T[1] << 0); + BLState->LastKnownMotorState = Motor; - gs_string Temp = PushStringF(State->Transient, 256, "Received Motor States: %d %d %d\n", + gs_string Temp = PushStringF(State->Transient, 256, "\nReceived Motor States: \n\tPos: %d %d %d\n\tErr: %d %d %d\n\tTemp: %d\n\n", Motor.FlowerPositions[0], Motor.FlowerPositions[1], - Motor.FlowerPositions[2]); + Motor.FlowerPositions[2], + Motor.MotorStatus[0], + Motor.MotorStatus[1], + Motor.MotorStatus[2], + (u32)Motor.Temperature + ); NullTerminate(&Temp); OutputDebugStringA(Temp.Str); @@ -307,14 +312,14 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) if (Temp.Temperature > 21) { - BLState->BrightnessPercent = .5f; + BLState->BrightnessPercent = .25f; } else { BLState->BrightnessPercent = 1.f; } - gs_string TempStr = PushStringF(State->Transient, 256, "Temperature: %d\n", + gs_string TempStr = PushStringF(State->Transient, 256, "\nTemperature: %d\n", Temp.Temperature); NullTerminate(&TempStr); OutputDebugStringA(TempStr.Str); @@ -334,29 +339,37 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) time_range Range = MotorOpenTimes[i]; bool CurrTimeInRange = SystemTimeIsInTimeRange(Context->SystemTime_Current, Range); + bool LastTimeInRange = SystemTimeIsInTimeRange(Context->SystemTime_Last, Range); - if (CurrTimeInRange && !LastTimeInRange) + bool SendOpen = CurrTimeInRange && !LastTimeInRange; + + r64 NanosSinceLastSend = ((r64)Context->SystemTime_Current.NanosSinceEpoch - (r64)BLState->LastSendTime.NanosSinceEpoch); + r64 SecondsSinceLastSend = NanosSinceLastSend / PowR32(10, 8); + + SendOpen = SecondsSinceLastSend > 2; + if (SendOpen) { - OutputDebugString("Open\n"); - gs_data* Msg = MessageQueue_GetWrite(&BLState->OutgoingMsgQueue); - - blumen_packet* Packet = (blumen_packet*)Msg->Memory; - Packet->Type = PacketType_MotorState; - Packet->MotorPacket.FlowerPositions[0] = 2; - Packet->MotorPacket.FlowerPositions[1] = 2; - Packet->MotorPacket.FlowerPositions[2] = 2; + 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; + gs_data Msg = StructToData(&Packet, blumen_packet); + MessageQueue_Write(&BLState->OutgoingMsgQueue, Msg); } else if (!CurrTimeInRange && LastTimeInRange) { - OutputDebugString("Close\n"); - gs_data* Msg = MessageQueue_GetWrite(&BLState->OutgoingMsgQueue); - - blumen_packet* Packet = (blumen_packet*)Msg->Memory; - Packet->Type = PacketType_MotorState; - Packet->MotorPacket.FlowerPositions[0] = 1; - Packet->MotorPacket.FlowerPositions[1] = 1; - Packet->MotorPacket.FlowerPositions[2] = 1; + 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; + gs_data Msg = StructToData(&Packet, blumen_packet); + MessageQueue_Write(&BLState->OutgoingMsgQueue, Msg); } } } @@ -377,23 +390,25 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) // Send Status Packet { system_time LastSendTime = BLState->LastStatusUpdateTime; - s64 NanosSinceLastSend = ((s64)Context->SystemTime_Current.NanosSinceEpoch - (s64)LastSendTime.NanosSinceEpoch); - s64 SecondsSinceLastSend = NanosSinceLastSend * 1000000000; + r64 NanosSinceLastSend = ((r64)Context->SystemTime_Current.NanosSinceEpoch - (r64)LastSendTime.NanosSinceEpoch); + r64 SecondsSinceLastSend = NanosSinceLastSend / PowR32(10, 8); if (SecondsSinceLastSend >= STATUS_PACKET_FREQ_SECONDS) { BLState->LastStatusUpdateTime = Context->SystemTime_Current; - gs_data* Msg = MessageQueue_GetWrite(&BLState->OutgoingMsgQueue); - OutputDebugString("Sending Status\n"); - blumen_packet* Packet = (blumen_packet*)Msg->Memory; - Packet->Type = PacketType_LumenariumStatus; - Packet->StatusPacket.NextMotorEventType = 0; - Packet->StatusPacket.NextEventTime = 0; + blumen_packet Packet = {}; + Packet.Type = PacketType_LumenariumStatus; + Packet.StatusPacket.NextMotorEventType = 0; + Packet.StatusPacket.NextEventTime = 0; animation* ActiveAnim = AnimationSystem_GetActiveAnimation(&State->AnimationSystem); - CopyMemoryTo(ActiveAnim->Name.Str, Packet->StatusPacket.AnimFileName, + CopyMemoryTo(ActiveAnim->Name.Str, Packet.StatusPacket.AnimFileName, Min(ActiveAnim->Name.Length, 32)); + Packet.StatusPacket.AnimFileName[ActiveAnim->Name.Length] = 0; + + gs_data Msg = StructToData(&Packet, blumen_packet); + MessageQueue_Write(&BLState->OutgoingMsgQueue, Msg); } } } diff --git a/src/app/ss_blumen_lumen/blumen_lumen.h b/src/app/ss_blumen_lumen/blumen_lumen.h index 5009dac..b29ea6c 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen.h +++ b/src/app/ss_blumen_lumen/blumen_lumen.h @@ -18,6 +18,14 @@ enum bl_python_packet_type typedef struct motor_packet { u8 FlowerPositions[3]; + /* +u8 Motor1Pos; +u8 Motor2Pos; +u8 Motor3Pos; +*/ + u8 MotorStatus[3]; + u16 Temperature; + } motor_packet; typedef struct microphone_packet @@ -46,13 +54,15 @@ enum motor_event_type typedef struct status_packet { u8 NextMotorEventType; + // u16 Padding; u32 NextEventTime; + char AnimFileName[32]; } status_packet; typedef struct blumen_packet { - bl_python_packet_type Type; + u8 Type; union { motor_packet MotorPacket; @@ -104,9 +114,12 @@ SystemTimeIsInTimeRange(system_time SysTime, time_range Range) } global time_range MotorOpenTimes[] = { - { 14, 28, 14, 29 } + { 17, 56, 17, 56 }, + { 17, 58, 17, 56 }, + { 18, 00, 18, 00 }, + }; -global u32 MotorOpenTimesCount = 1; +global u32 MotorOpenTimesCount = 3; struct blumen_lumen_state { @@ -131,6 +144,8 @@ struct blumen_lumen_state // dim the leds. r32 BrightnessPercent; system_time LastStatusUpdateTime; + + system_time LastSendTime; };