From ac19ab97ba148e11274eabefdfefc7323deba61a Mon Sep 17 00:00:00 2001 From: Peter Slattery Date: Sat, 6 Feb 2021 15:10:14 -0800 Subject: [PATCH] saturday at foldspace --- src/app/blumen_lumen.cpp | 68 +++++++++++-------- src/app/blumen_lumen.h | 56 +++++++++++++++ .../platform_win32/win32_foldhaus_socket.h | 61 ++++++++++++++--- 3 files changed, 148 insertions(+), 37 deletions(-) diff --git a/src/app/blumen_lumen.cpp b/src/app/blumen_lumen.cpp index 3ea2102..d2e5a5c 100644 --- a/src/app/blumen_lumen.cpp +++ b/src/app/blumen_lumen.cpp @@ -23,7 +23,7 @@ BlumenLumen_MicListenJob(gs_thread_context* Ctx, u8* UserData) while (true) { -#if 0 +#if 1 // TODO(pjs): Removing this block for now - nothing is wrong with it except that SocketPeek is still blocking for some reason if (SocketPeek(Data->SocketManager, Data->ListenSocket)) { @@ -117,7 +117,7 @@ BlumenLumen_CustomInit(app_state* State, context Context) BLState->MicListenJobData.OutgoingMsgQueue = &BLState->OutgoingMsgQueue; BLState->MicListenJobData.ListenSocket = CreateSocket(Context.SocketManager, "127.0.0.1", "20185"); - //BLState->MicListenThread = CreateThread(Context.ThreadManager, BlumenLumen_MicListenJob, (u8*)&BLState->MicListenJobData); + BLState->MicListenThread = CreateThread(Context.ThreadManager, BlumenLumen_MicListenJob, (u8*)&BLState->MicListenJobData); gs_const_string SculpturePath = ConstString("data/test_blumen.fold"); LoadAssembly(&State->Assemblies, &State->LedSystem, State->Transient, Context, SculpturePath, State->GlobalLog); @@ -172,13 +172,13 @@ BlumenLumen_CustomInit(app_state* State, context Context) return Result; } -u8 temp = 0; - internal void BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) { blumen_lumen_state* BLState = (blumen_lumen_state*)UserData.Memory; + MotorTimeElapsed += Context->DeltaTime; + gs_string BlueString = MakeString("blue"); gs_string GreenString = MakeString("green"); gs_string ILoveYouString = MakeString("i_love_you"); @@ -208,33 +208,47 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) } } - if ((BLState->OutgoingMsgQueue.WriteHead >= BLState->OutgoingMsgQueue.ReadHead) || - (BLState->OutgoingMsgQueue.WriteHead < BLState->OutgoingMsgQueue.ReadHead)) + if (MotorTimeElapsed > OpenClosePeriod) { - u32 WriteIndex = BLState->OutgoingMsgQueue.WriteHead; - - gs_data* Msg = BLState->OutgoingMsgQueue.Buffers + WriteIndex; - if (Msg->Size == 0) + // NOTE(pjs): + MotorTimeElapsed = 0; + u8 Position = LastPosition; + if (LastPosition == 2) { - *Msg = PushSizeToData(&State->Permanent, sizeof(motor_packet)); + LastPosition = ClosedValue; } - motor_packet* Packet = (motor_packet*)Msg->Memory; - Packet->FlowerPositions[0] = temp; - Packet->FlowerPositions[1] = temp + 1; - Packet->FlowerPositions[2] = temp + 2; - temp++; - - // NOTE(pjs): We increment the write head AFTER we've written so that - // the network thread doesn't think the buffer is ready to send before - // the data is set. We want to avoid the case of: - // 1. Main Thread increments write head to 1 - // 2. Network Thread thinks theres a new message to send at 0 - // 3. Network Thread sends the message at 0 - // 4. Main Thread sets the message at 0 - BLState->OutgoingMsgQueue.WriteHead += 1; - if (BLState->OutgoingMsgQueue.WriteHead >= BLUMEN_MESSAGE_QUEUE_COUNT) + else { - BLState->OutgoingMsgQueue.WriteHead = 0; + LastPosition = OpenValue; + } + + if ((BLState->OutgoingMsgQueue.WriteHead >= BLState->OutgoingMsgQueue.ReadHead) || + (BLState->OutgoingMsgQueue.WriteHead < BLState->OutgoingMsgQueue.ReadHead)) + { + u32 WriteIndex = BLState->OutgoingMsgQueue.WriteHead; + + gs_data* Msg = BLState->OutgoingMsgQueue.Buffers + WriteIndex; + if (Msg->Size == 0) + { + *Msg = PushSizeToData(&State->Permanent, sizeof(motor_packet)); + } + motor_packet* Packet = (motor_packet*)Msg->Memory; + Packet->FlowerPositions[0] = Position; + Packet->FlowerPositions[1] = Position; + Packet->FlowerPositions[2] = Position; + + // NOTE(pjs): We increment the write head AFTER we've written so that + // the network thread doesn't think the buffer is ready to send before + // the data is set. We want to avoid the case of: + // 1. Main Thread increments write head to 1 + // 2. Network Thread thinks theres a new message to send at 0 + // 3. Network Thread sends the message at 0 + // 4. Main Thread sets the message at 0 + BLState->OutgoingMsgQueue.WriteHead += 1; + if (BLState->OutgoingMsgQueue.WriteHead >= BLUMEN_MESSAGE_QUEUE_COUNT) + { + BLState->OutgoingMsgQueue.WriteHead = 0; + } } } } diff --git a/src/app/blumen_lumen.h b/src/app/blumen_lumen.h index d5ce452..9e1b166 100644 --- a/src/app/blumen_lumen.h +++ b/src/app/blumen_lumen.h @@ -53,6 +53,62 @@ struct blumen_lumen_state platform_thread_handle MicListenThread; mic_listen_job_data MicListenJobData; }; + + + + + + + + + + + + + + + + + + +// If you change anything, exit lumenarium if its running +// then in this application hit f1 to compile then +// go to remedybg (the debugger) and hit f5 + + +// don't touch this +u8 LastPosition = 1; + +u8 ClosedValue = 1; +u8 OpenValue = 2; + + +r64 MotorTimeElapsed = 0; +r64 OpenClosePeriod = 15.0f; + + + + + + + + + + + + + + + + + + + + + + + + #define BLUMEN_LUMEN_H diff --git a/src/app/platform_win32/win32_foldhaus_socket.h b/src/app/platform_win32/win32_foldhaus_socket.h index dcbc1e8..bd5b5cd 100644 --- a/src/app/platform_win32/win32_foldhaus_socket.h +++ b/src/app/platform_win32/win32_foldhaus_socket.h @@ -154,19 +154,53 @@ Win32CreateSocket(platform_socket* Socket, char* Address, char* DefaultPort) InvalidCodePath; } + // If iMode == 0, blocking is enabled + // if iMode != 0, non-blocking mode is enabled + u_long iMode = 1; + Error = ioctlsocket(SocketHandle, FIONBIO, &iMode); + if (Error != NO_ERROR) + { + InvalidCodePath; + } + Error = connect(SocketHandle, InfoAt->ai_addr, (int)InfoAt->ai_addrlen); if (Error == SOCKET_ERROR) { - closesocket(SocketHandle); - continue; - } - else - { - Socket->PlatformHandle = (u8*)Win32Alloc(sizeof(SOCKET), 0); - *(SOCKET*)Socket->PlatformHandle = SocketHandle; - Result = true; - break; + u32 Status = WSAGetLastError(); + if (Status == WSAEWOULDBLOCK) + { +#if 0 + TIMEVAL Timeout = { 0, 500 }; + fd_set SocketSet = {}; + FD_ZERO(&SocketSet); + FD_SET(SocketHandle, &SocketSet); + Assert(FD_ISSET(SocketHandle, &SocketSet)); + Status = select(0, &SocketSet, 0, 0, {}); + if (Status == SOCKET_ERROR) + { + + } + else if (Status == 0) + { + } + else + { + + } +#endif + } + else + { + closesocket(SocketHandle); + continue; + } } + + + Socket->PlatformHandle = (u8*)Win32Alloc(sizeof(SOCKET), 0); + *(SOCKET*)Socket->PlatformHandle = SocketHandle; + Result = true; + break; } } else @@ -212,7 +246,14 @@ Win32SocketPeek(platform_socket* Socket) { // TODO(pjs): Error handling s32 Error = WSAGetLastError(); - InvalidCodePath; + if (Error == WSAEWOULDBLOCK) + { + // NOTE(pjs): + } + else + { + InvalidCodePath; + } } return Result; }