From 55284cde25f49d039b0d18354ea6d1ee27be3f49 Mon Sep 17 00:00:00 2001 From: PS Date: Sat, 10 Oct 2020 16:52:00 -0700 Subject: [PATCH] Created serial_monitor, and debugged uart functionality. It is now working --- build/build_app_msvc_win32_debug.bat | 2 + .../deprecated/foldhaus_panel_node_graph.h | 6 +- src/app/editor/foldhaus_panel.h | 38 ++- .../foldhaus_panel_animation_timeline.h | 2 +- src/app/engine/animation/foldhaus_animation.h | 3 - .../animation/foldhaus_animation_parser.cpp | 10 - src/app/engine/foldhaus_assembly.cpp | 7 - src/app/engine/foldhaus_assembly.h | 9 +- src/app/engine/uart/foldhaus_uart.cpp | 95 ++++++++ src/app/engine/uart/foldhaus_uart.h | 112 ++------- src/app/foldhaus_app.cpp | 5 +- src/app/foldhaus_app.h | 1 + src/app/platform_win32/win32_foldhaus.cpp | 28 +++ .../platform_win32/win32_foldhaus_serial.h | 31 +++ src/serial_monitor/first.cpp | 221 ++++++++++++++++++ 15 files changed, 422 insertions(+), 148 deletions(-) delete mode 100644 src/app/engine/animation/foldhaus_animation_parser.cpp create mode 100644 src/app/engine/uart/foldhaus_uart.cpp create mode 100644 src/serial_monitor/first.cpp diff --git a/build/build_app_msvc_win32_debug.bat b/build/build_app_msvc_win32_debug.bat index 1a4c884..a60ed6c 100644 --- a/build/build_app_msvc_win32_debug.bat +++ b/build/build_app_msvc_win32_debug.bat @@ -27,6 +27,8 @@ del lock.tmp cl %CommonCompilerFlags% %SourceCodePath%\platform_win32\win32_foldhaus.cpp /link %CommonLinkerFlags% user32.lib winmm.lib gdi32.lib opengl32.lib dsound.lib Ws2_32.lib Comdlg32.lib +cl %CommonCompilerFlags% %ProjectDevPath%\src\serial_monitor\first.cpp /Feserial_monitor.exe /link %CommonLinkerFlags% user32.lib winmm.lib gdi32.lib + popd call %MyPath%\_postbuild_win32.bat \ No newline at end of file diff --git a/src/app/deprecated/foldhaus_panel_node_graph.h b/src/app/deprecated/foldhaus_panel_node_graph.h index 6830d2f..fceb052 100644 --- a/src/app/deprecated/foldhaus_panel_node_graph.h +++ b/src/app/deprecated/foldhaus_panel_node_graph.h @@ -111,7 +111,7 @@ OPERATION_STATE_DEF(connect_nodes_operation_state) OPERATION_RENDER_PROC(UpdateConnectNodeOperation) { - panel_and_bounds NodeGraphPanel = GetPanelContainingPoint(Mouse.DownPos, &State->PanelSystem, State->WindowBounds); + panel_with_layout NodeGraphPanel = GetPanelContainingPoint(Mouse.DownPos, &State->PanelSystem, State->WindowBounds); node_graph_state* GraphState = (node_graph_state*)NodeGraphPanel.Panel->PanelStateMemory; GraphState->Layout.InProgressConnectionEnd = Mouse.Pos; @@ -121,7 +121,7 @@ FOLDHAUS_INPUT_COMMAND_PROC(EndConnectNodesOperation) { connect_nodes_operation_state* OpState = GetCurrentOperationState(State->Modes, connect_nodes_operation_state); - panel_and_bounds NodeGraphPanel = GetPanelContainingPoint(Mouse.DownPos, &State->PanelSystem, State->WindowBounds); + panel_with_layout NodeGraphPanel = GetPanelContainingPoint(Mouse.DownPos, &State->PanelSystem, State->WindowBounds); node_graph_state* GraphState = (node_graph_state*)NodeGraphPanel.Panel->PanelStateMemory; GraphState->Layout.ConnectionIsInProgress = false; @@ -157,7 +157,7 @@ BeginConnectNodesOperation(visual_port VisualPort, u32 VisualPortIndex, mouse_st OpState->VisualPort = VisualPort; OpState->VisualPortIndex = VisualPortIndex; - panel_and_bounds NodeGraphPanel = GetPanelContainingPoint(Mouse.DownPos, &State->PanelSystem, State->WindowBounds); + panel_with_layout NodeGraphPanel = GetPanelContainingPoint(Mouse.DownPos, &State->PanelSystem, State->WindowBounds); node_graph_state* GraphState = (node_graph_state*)NodeGraphPanel.Panel->PanelStateMemory; GraphState->Layout.ConnectionIsInProgress = true; diff --git a/src/app/editor/foldhaus_panel.h b/src/app/editor/foldhaus_panel.h index 53415f1..c1a2248 100644 --- a/src/app/editor/foldhaus_panel.h +++ b/src/app/editor/foldhaus_panel.h @@ -24,6 +24,8 @@ typedef struct panel_entry panel_entry; struct panel { + // TODO(pjs): We want this to be a list, so that you can push sub panels on + // and let them return to you, to perform certain tasks, like loading a file s32 PanelDefinitionIndex; panel_split_direction SplitDirection; @@ -154,11 +156,11 @@ FreePanelAtIndex(s32 Index, panel_system* PanelSystem) } internal void -SplitPanelVertically(panel* Parent, r32 Percent, panel_system* PanelSystem) +SplitPanel(panel* Parent, r32 Percent, panel_split_direction SplitDirection, panel_system* PanelSystem) { if (Percent >= 0.0f && Percent <= 1.0f) { - Parent->SplitDirection = PanelSplit_Vertical; + Parent->SplitDirection = SplitDirection; Parent->SplitPercent = Percent; Parent->Left = TakeNewPanelEntry(PanelSystem); @@ -169,20 +171,16 @@ SplitPanelVertically(panel* Parent, r32 Percent, panel_system* PanelSystem) } } +internal void +SplitPanelVertically(panel* Parent, r32 Percent, panel_system* PanelSystem) +{ + SplitPanel(Parent, Percent, PanelSplit_Vertical, PanelSystem); +} + internal void SplitPanelHorizontally(panel* Parent, r32 Percent, panel_system* PanelSystem) { - if (Percent >= 0.0f && Percent <= 1.0f) - { - Parent->SplitDirection = PanelSplit_Horizontal; - Parent->SplitPercent = Percent; - - Parent->Bottom = TakeNewPanelEntry(PanelSystem); - Parent->Bottom->Panel.PanelDefinitionIndex = Parent->PanelDefinitionIndex; - - Parent->Top = TakeNewPanelEntry(PanelSystem); - Parent->Top->Panel.PanelDefinitionIndex = Parent->PanelDefinitionIndex; - } + SplitPanel(Parent, Percent, PanelSplit_Horizontal, PanelSystem); } internal void @@ -290,16 +288,10 @@ GetPanelLayout(panel_system* System, rect2 WindowBounds, gs_memory_arena* Storag return Result; } -struct panel_and_bounds -{ - panel* Panel; - rect2 Bounds; -}; - -internal panel_and_bounds +internal panel_with_layout GetPanelContainingPoint(v2 Point, panel* Panel, rect2 PanelBounds) { - panel_and_bounds Result = {0}; + panel_with_layout Result = {0}; if (Panel->SplitDirection == PanelSplit_NoSplit) { @@ -338,10 +330,10 @@ GetPanelContainingPoint(v2 Point, panel* Panel, rect2 PanelBounds) return Result; } -internal panel_and_bounds +internal panel_with_layout GetPanelContainingPoint(v2 Point, panel_system* PanelSystem, rect2 WindowBounds) { - panel_and_bounds Result = {0}; + panel_with_layout Result = {0}; if (PanelSystem->PanelsUsed > 0) { Result = GetPanelContainingPoint(Point, &PanelSystem->Panels[0].Panel, WindowBounds); diff --git a/src/app/editor/panels/foldhaus_panel_animation_timeline.h b/src/app/editor/panels/foldhaus_panel_animation_timeline.h index 016ff00..6f4f5cf 100644 --- a/src/app/editor/panels/foldhaus_panel_animation_timeline.h +++ b/src/app/editor/panels/foldhaus_panel_animation_timeline.h @@ -265,7 +265,7 @@ FOLDHAUS_INPUT_COMMAND_PROC(AddAnimationBlockCommand) { animation* ActiveAnim = AnimationSystem_GetActiveAnimation(&State->AnimationSystem); - panel_and_bounds ActivePanel = GetPanelContainingPoint(Mouse.Pos, &State->PanelSystem, State->WindowBounds); + panel_with_layout ActivePanel = GetPanelContainingPoint(Mouse.Pos, &State->PanelSystem, State->WindowBounds); frame_range Range = ActiveAnim->PlayableRange; u32 MouseDownFrame = GetFrameFromPointInAnimationPanel(Mouse.Pos, ActivePanel.Bounds, Range); diff --git a/src/app/engine/animation/foldhaus_animation.h b/src/app/engine/animation/foldhaus_animation.h index 840f314..00d4278 100644 --- a/src/app/engine/animation/foldhaus_animation.h +++ b/src/app/engine/animation/foldhaus_animation.h @@ -83,9 +83,6 @@ struct animation_system gs_memory_arena* Storage; animation_array Animations; - frame_range PlayableRange_; - gs_list Blocks_; - // NOTE(Peter): The frame currently being displayed/processed. you // can see which frame you're on by looking at the time slider on the timeline // panel diff --git a/src/app/engine/animation/foldhaus_animation_parser.cpp b/src/app/engine/animation/foldhaus_animation_parser.cpp deleted file mode 100644 index 16233e6..0000000 --- a/src/app/engine/animation/foldhaus_animation_parser.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// -// File: foldhaus_animation_parser.cpp -// Author: Peter Slattery -// Creation Date: 2020-10-09 -// -#ifndef FOLDHAUS_ANIMATION_PARSER_CPP - - -#define FOLDHAUS_ANIMATION_PARSER_CPP -#endif // FOLDHAUS_ANIMATION_PARSER_CPP \ No newline at end of file diff --git a/src/app/engine/foldhaus_assembly.cpp b/src/app/engine/foldhaus_assembly.cpp index 5be7342..fce8922 100644 --- a/src/app/engine/foldhaus_assembly.cpp +++ b/src/app/engine/foldhaus_assembly.cpp @@ -128,13 +128,6 @@ LedSystemFreeBuffer(led_system* System, u32 BufferIndex) *Buffer = {}; } -internal led_buffer* -LedSystemGetBuffer(led_system* System, u32 Index) -{ - led_buffer* Result = &System->Buffers[Index]; - return Result; -} - internal void LedBufferSetLed(led_buffer* Buffer, u32 Led, v4 Position) { diff --git a/src/app/engine/foldhaus_assembly.h b/src/app/engine/foldhaus_assembly.h index 55185e3..aa422f3 100644 --- a/src/app/engine/foldhaus_assembly.h +++ b/src/app/engine/foldhaus_assembly.h @@ -112,7 +112,14 @@ struct assembly_array assembly* Values; }; -internal led_buffer* LedSystemGetBuffer(led_system* System, u32 Index); + +internal led_buffer* +LedSystemGetBuffer(led_system* System, u32 Index) +{ + led_buffer* Result = &System->Buffers[Index]; + return Result; +} + #define FOLDHAUS_ASSEMBLY_H #endif // FOLDHAUS_ASSEMBLY_H \ No newline at end of file diff --git a/src/app/engine/uart/foldhaus_uart.cpp b/src/app/engine/uart/foldhaus_uart.cpp new file mode 100644 index 0000000..1e7f635 --- /dev/null +++ b/src/app/engine/uart/foldhaus_uart.cpp @@ -0,0 +1,95 @@ +// +// File: foldhaus_uart.cpp +// Author: Peter Slattery +// Creation Date: 2020-10-10 +// +#ifndef FOLDHAUS_UART_CPP + + +internal void +UART_SetChannelBuffer_Create(gs_memory_cursor* WriteCursor, uart_channel ChannelSettings, v2_strip Strip, led_buffer LedBuffer) +{ + // NOTE(pjs): This is just here because the information is duplicated and I want to be sure + // to catch the error where they are different + Assert(ChannelSettings.PixelsCount == Strip.LedCount); + + uart_header* Header = PushStructOnCursor(WriteCursor, uart_header); + UART_FillHeader(Header, Strip.UARTAddr.Channel, UART_SET_CHANNEL_WS2812); + + uart_channel* Channel = PushStructOnCursor(WriteCursor, uart_channel); + *Channel = ChannelSettings; + + for (u32 i = 0; i < Channel->PixelsCount; i++) + { + u32 LedIndex = Strip.LedLUT[i]; + pixel Color = LedBuffer.Colors[LedIndex]; + + u8* OutputPixel = PushArrayOnCursor(WriteCursor, u8, 3); + + // TODO(pjs): Use the Output mask + OutputPixel[0] = Color.R; + OutputPixel[1] = Color.G; + OutputPixel[2] = Color.B; + + if (Channel->ElementsCount == 4) + { + // TODO(pjs): Calculate white from the RGB components? + // Generally we just need a good way to handle the white channel, + // both in the renderer and in output + + //OutputPixel[Channel->WhiteIndex] = Color.W; + } + } + + uart_footer* Footer = PushStructOnCursor(WriteCursor, uart_footer); + UART_FillFooter(Footer, (u8*)Header); +} + +internal void +UART_DrawAll_Create(gs_memory_cursor* WriteCursor) +{ + uart_header* Header = PushStructOnCursor(WriteCursor, uart_header); + UART_FillHeader(Header, 1, UART_DRAW_ALL); + + uart_footer* Footer = PushStructOnCursor(WriteCursor, uart_footer); + UART_FillFooter(Footer, (u8*)Header); +} + +internal void +UART_BuildOutputData(addressed_data_buffer_list* Output, assembly_array Assemblies, led_system* LedSystem) +{ + uart_channel ChannelSettings = {0}; + ChannelSettings.ElementsCount = 3; + ChannelSettings.ColorPackingOrder = 36; + + // NOTE(pjs): This is the minimum size of every UART message. SetChannelBuffer messages will + // be bigger than this, but their size is based on the number of pixels in each channel + u32 MessageBaseSize = sizeof(uart_header) + sizeof(uart_channel) + sizeof(uart_footer); + + for (u32 AssemblyIdx = 0; AssemblyIdx < Assemblies.Count; AssemblyIdx++) + { + assembly Assembly = Assemblies.Values[AssemblyIdx]; + led_buffer* LedBuffer = LedSystemGetBuffer(LedSystem, Assembly.LedBufferIndex); + + u32 TotalBufferSize = MessageBaseSize * Assembly.StripCount; // SetChannelBuffer messages + TotalBufferSize += MessageBaseSize; // DrawAll message + TotalBufferSize += ChannelSettings.ElementsCount * Assembly.LedCountTotal; // pixels * channels per pixel + + addressed_data_buffer* Buffer = AddressedDataBufferList_Push(Output, TotalBufferSize); + AddressedDataBuffer_SetCOMPort(Buffer, Assembly.UARTComPort); + gs_memory_cursor WriteCursor = CreateMemoryCursor(Buffer->Data); + + for (u32 StripIdx = 0; StripIdx < Assembly.StripCount; StripIdx++) + { + v2_strip StripAt = Assembly.Strips[StripIdx]; + ChannelSettings.PixelsCount = StripAt.LedCount; + UART_SetChannelBuffer_Create(&WriteCursor, ChannelSettings, StripAt, *LedBuffer); + } + + UART_DrawAll_Create(&WriteCursor); + } +} + + +#define FOLDHAUS_UART_CPP +#endif // FOLDHAUS_UART_CPP \ No newline at end of file diff --git a/src/app/engine/uart/foldhaus_uart.h b/src/app/engine/uart/foldhaus_uart.h index 7e36e00..f866efc 100644 --- a/src/app/engine/uart/foldhaus_uart.h +++ b/src/app/engine/uart/foldhaus_uart.h @@ -16,7 +16,7 @@ enum uart_record_type struct uart_header { - u8 MagicNumber[4]; + s8 MagicNumber[4]; u8 Channel; u8 RecordType; }; @@ -24,12 +24,7 @@ struct uart_header struct uart_channel { u8 ElementsCount; - - u8 RedIndex; - u8 GreenIndex; - u8 BlueIndex; - u8 WhiteIndex; - + u8 ColorPackingOrder; u16 PixelsCount; }; @@ -57,113 +52,34 @@ UART_FillHeader(uart_header* Header, u8 Channel, u8 RecordType) Header->RecordType = RecordType; } -internal void -UART_FillFooter(uart_footer* Footer, u8* BufferStart) +internal u32 +UART_CalculateCRC(u8* BufferStart, u8* BufferEnd) { // Calculate the CRC u32 CRC = 0xFFFFFFFF; - u32 BytesCount = (u8*)Footer - BufferStart; + u32 BytesCount = (u8*)BufferEnd - BufferStart; for (u32 i = 0; i < BytesCount; i++) { u8 At = BufferStart[i]; - +#if 0 // Cameron's Version CRC = UART_CRCTable[(CRC ^ At) & 0x0F] ^ (CRC >> 4); CRC = UART_CRCTable[(CRC ^ (At >> 4)) & 0x0F] ^ (CRC >> 4); - -#if 0 - // The Libraries Version - CRC = (UART_CRCTable[(CRC ^ At) & 0xFF] ^ (CRC >> 8)) & 0xFFFFFFFF; +#else + // https://github.com/simap/pixelblaze_output_expander/blob/master/firmware/Core/Src/uart.c + u32 TableIndex = (CRC ^ At) & 0xFF; + CRC = (UART_CRCTable[TableIndex] ^ (CRC >> 8)) & 0xFFFFFFFF; #endif } - Footer->CRC = CRC; + CRC = CRC ^ 0xFFFFFFFF; + return CRC; } internal void -UART_SetChannelBuffer_Create(gs_memory_cursor* WriteCursor, uart_channel ChannelSettings, v2_strip Strip, led_buffer LedBuffer) +UART_FillFooter(uart_footer* Footer, u8* BufferStart) { - // NOTE(pjs): This is just here because the information is duplicated and I want to be sure - // to catch the error where they are different - Assert(ChannelSettings.PixelsCount == Strip.LedCount); - - uart_header* Header = PushStructOnCursor(WriteCursor, uart_header); - UART_FillHeader(Header, Strip.UARTAddr.Channel, UART_SET_CHANNEL_WS2812); - - uart_channel* Channel = PushStructOnCursor(WriteCursor, uart_channel); - *Channel = ChannelSettings; - - for (u32 i = 0; i < Channel->PixelsCount; i++) - { - u32 LedIndex = Strip.LedLUT[i]; - pixel Color = LedBuffer.Colors[LedIndex]; - - u8* OutputPixel = PushArrayOnCursor(WriteCursor, u8, 3); - - OutputPixel[Channel->RedIndex] = Color.R; - OutputPixel[Channel->GreenIndex] = Color.G; - OutputPixel[Channel->BlueIndex] = Color.B; - - if (OutputPixel[Channel->ElementsCount == 4]) - { - // TODO(pjs): Calculate white from the RGB components? - // Generally we just need a good way to handle the white channel, - // both in the renderer and in output - - //OutputPixel[Channel->WhiteIndex] = Color.W; - } - } - - uart_footer* Footer = PushStructOnCursor(WriteCursor, uart_footer); - UART_FillFooter(Footer, (u8*)Header); -} - -internal void -UART_DrawAll_Create(gs_memory_cursor* WriteCursor) -{ - uart_header* Header = PushStructOnCursor(WriteCursor, uart_header); - UART_FillHeader(Header, 1, UART_DRAW_ALL); - - uart_footer* Footer = PushStructOnCursor(WriteCursor, uart_footer); - UART_FillFooter(Footer, (u8*)Header); -} - -internal void -UART_BuildOutputData(addressed_data_buffer_list* Output, assembly_array Assemblies, led_system* LedSystem) -{ - uart_channel ChannelSettings = {0}; - ChannelSettings.ElementsCount = 3; - ChannelSettings.RedIndex = 1; - ChannelSettings.GreenIndex = 2; - ChannelSettings.BlueIndex = 3; - ChannelSettings.WhiteIndex = 0; - - // NOTE(pjs): This is the minimum size of every UART message. SetChannelBuffer messages will - // be bigger than this, but their size is based on the number of pixels in each channel - u32 MessageBaseSize = sizeof(uart_header) + sizeof(uart_channel) + sizeof(uart_footer); - - for (u32 AssemblyIdx = 0; AssemblyIdx < Assemblies.Count; AssemblyIdx++) - { - assembly Assembly = Assemblies.Values[AssemblyIdx]; - led_buffer* LedBuffer = LedSystemGetBuffer(LedSystem, Assembly.LedBufferIndex); - - u32 TotalBufferSize = MessageBaseSize * Assembly.StripCount; // SetChannelBuffer messages - TotalBufferSize += MessageBaseSize; // DrawAll message - TotalBufferSize += ChannelSettings.ElementsCount * Assembly.LedCountTotal; // pixels * channels per pixel - - addressed_data_buffer* Buffer = AddressedDataBufferList_Push(Output, TotalBufferSize); - AddressedDataBuffer_SetCOMPort(Buffer, Assembly.UARTComPort); - gs_memory_cursor WriteCursor = CreateMemoryCursor(Buffer->Data); - - for (u32 StripIdx = 0; StripIdx < Assembly.StripCount; StripIdx++) - { - v2_strip StripAt = Assembly.Strips[StripIdx]; - ChannelSettings.PixelsCount = StripAt.LedCount; - UART_SetChannelBuffer_Create(&WriteCursor, ChannelSettings, StripAt, *LedBuffer); - } - - UART_DrawAll_Create(&WriteCursor); - } + Footer->CRC = UART_CalculateCRC(BufferStart, (u8*)Footer); } #define FOLDHAUS_UART_H diff --git a/src/app/foldhaus_app.cpp b/src/app/foldhaus_app.cpp index 7dd6042..5644aa3 100644 --- a/src/app/foldhaus_app.cpp +++ b/src/app/foldhaus_app.cpp @@ -153,9 +153,10 @@ INITIALIZE_APPLICATION(InitializeApplication) Animation_AddLayer(&Anim, MakeString("Color Layer"), BlendMode_Multiply, &State->AnimationSystem); Animation_AddLayer(&Anim, MakeString("Sparkles"), BlendMode_Add, &State->AnimationSystem); - Animation_AddBlock(&Anim, 22, 123, 2, 1); + Animation_AddBlock(&Anim, 22, 123, 2, 0); AnimationArray_Push(&State->AnimationSystem.Animations, Anim); + } // End Animation Playground @@ -179,7 +180,7 @@ HandleInput (app_state* State, rect2 WindowBounds, input_queue InputQueue, mouse } else { - panel_and_bounds PanelWithMouseOverIt = GetPanelContainingPoint(Mouse.Pos, &State->PanelSystem, WindowBounds); + panel_with_layout PanelWithMouseOverIt = GetPanelContainingPoint(Mouse.Pos, &State->PanelSystem, WindowBounds); if (!PanelWithMouseOverIt.Panel) { return; } State->HotPanel = PanelWithMouseOverIt.Panel; diff --git a/src/app/foldhaus_app.h b/src/app/foldhaus_app.h index 1675b20..8c653da 100644 --- a/src/app/foldhaus_app.h +++ b/src/app/foldhaus_app.h @@ -22,6 +22,7 @@ #include "engine/sacn/foldhaus_sacn.h" #include "engine/uart/foldhaus_uart.h" +#include "engine/uart/foldhaus_uart.cpp" typedef struct app_state app_state; diff --git a/src/app/platform_win32/win32_foldhaus.cpp b/src/app/platform_win32/win32_foldhaus.cpp index d70938d..d11e851 100644 --- a/src/app/platform_win32/win32_foldhaus.cpp +++ b/src/app/platform_win32/win32_foldhaus.cpp @@ -426,6 +426,34 @@ WinMain ( { gs_thread_context ThreadContext = Win32CreateThreadContext(); +#if 0 + u32 LedCount = 48; + u32 MessageBaseSize = sizeof(uart_header) + sizeof(uart_channel) + sizeof(uart_footer); + MessageBaseSize += sizeof(u8) * 3 * LedCount; + gs_data MessageBuffer = PushSizeToData(ThreadContext.Transient); + + gs_memory_cursor WriteCursor = CreateMemoryCursor(MessageBuffer); + + uart_header* Header = PushStructOnCursor(WriteCursor, uart_header); + UART_FillHeader(Header, Strip.UARTAddr.Channel, UART_SET_CHANNEL_WS2812); + uart_channel* Channel = PushStructOnCursor(WriteCursor, uart_channel); + *Channel = ChannelSettings; + + for (u32 i = 0; i < LedCount; i++) + { + u8* OutputPixel = PushArrayOnCursor(WriteCursor, u8, 3); + OutputPixel[Channel->RedIndex] = (u8)(i); + OutputPixel[Channel->GreenIndex] = 0; + OutputPixel[Channel->BlueIndex] = 0; + } + + uart_footer* Footer = PushStructOnCursor(WriteCursor, uart_footer); + UART_FillFooter(Footer, (u8*)Header); + +#endif + + + MainWindow = Win32CreateWindow (HInstance, "Foldhaus", 1440, 768, HandleWindowEvents); Win32UpdateWindowDimension(&MainWindow); diff --git a/src/app/platform_win32/win32_foldhaus_serial.h b/src/app/platform_win32/win32_foldhaus_serial.h index fbf389f..0368547 100644 --- a/src/app/platform_win32/win32_foldhaus_serial.h +++ b/src/app/platform_win32/win32_foldhaus_serial.h @@ -143,6 +143,37 @@ Win32SerialPort_Write(HANDLE PortHandle, gs_data Buffer) return Success; } +bool +Win32SerialPort_SetRead(HANDLE PortHandle) +{ + bool Status = SetCommMask(PortHandle, EV_RXCHAR); + return Status; +} + +u32 +Win32SerialPort_ReadMessageWhenReady(HANDLE PortHandle, gs_data Data) +{ + u32 ReadSize = 0; + + DWORD EventMask = 0; + bool Status = WaitCommEvent(PortHandle, &EventMask, NULL); + if (Status) + { + DWORD NoBytesRead = 0; + do + { + u8 Byte = 0; + Status = ReadFile(PortHandle, &Byte, sizeof(char), &NoBytesRead, NULL); + Data.Memory[ReadSize] = Byte; + ReadSize++; + } + while (NoBytesRead > 0 && ReadSize < Data.Size); + } + //Read data and store in a buffer + + return ReadSize; +} + ///////////////////////// // Win32SerialArray diff --git a/src/serial_monitor/first.cpp b/src/serial_monitor/first.cpp new file mode 100644 index 0000000..1f0c6d6 --- /dev/null +++ b/src/serial_monitor/first.cpp @@ -0,0 +1,221 @@ +// +// File: first.cpp +// Author: Peter Slattery +// Creation Date: 2020-10-10 +// +#ifndef FIRST_CPP + + +#include "../gs_libs/gs_types.h" +#include "../gs_libs/gs_types.cpp" + +#define DEBUG_TRACK_FUNCTION + +#include +#include + +//#include "../app/foldhaus_platform.h" +//#include "../gs_libs/gs_win32.cpp" +#include "../app/platform_win32/win32_foldhaus_utils.h" +#include "../app/platform_win32/win32_foldhaus_memory.h" +#include "../app/platform_win32/win32_foldhaus_fileio.h" +#include "../app/platform_win32/win32_foldhaus_serial.h" +#include "../app/platform_win32/win32_foldhaus_work_queue.h" + +#include "../app/engine/uart/foldhaus_uart.h" + +u8* +FindNextHeader(gs_data Data, u8* StartAt) +{ + u8* At = StartAt; + while (!(At[0] == 'U' && + At[1] == 'P' && + At[2] == 'X' && + At[3] == 'L') && + (u32)(At - Data.Memory) < Data.Size) + { + At++; + } + return At; +} + +void +CreateMessage(gs_data* Data, u8 Count) +{ + gs_memory_cursor WriteCursor = CreateMemoryCursor(*Data); + + u32 Channels[] = { + 0, 1, 2, 3, 4, 5, 6, 7, + 16, 17, 18, 19, 20, 21, 22, 23, + //40, 41, 42, 43, 44, 45, 46, 47, + }; + + u8* FirstHeaderAddr = 0; + + for (u32 j = 0; j < sizeof(Channels) / sizeof(u32); j++) + { + u32 ChannelIndex = Channels[j]; + uart_header* Header = PushStructOnCursor(&WriteCursor, uart_header); + UART_FillHeader(Header, ChannelIndex, UART_SET_CHANNEL_WS2812); + + if (FirstHeaderAddr == 0) + { + FirstHeaderAddr = (u8*)Header; + } + + uart_channel* Channel = PushStructOnCursor(&WriteCursor, uart_channel); + Channel->ElementsCount = 3; + Channel->ColorPackingOrder = 36; // 10010000 + Channel->PixelsCount = 300; + + for (u32 i = 0; i < Channel->PixelsCount; i++) + { + u8* Pixel = PushArrayOnCursor(&WriteCursor, u8, 3); + Pixel[0] = Count; + Pixel[1] = 0; + Pixel[2] = 255 - Count; + } + + uart_footer* Footer = PushStructOnCursor(&WriteCursor, uart_footer); + Footer->CRC = UART_CalculateCRC((u8*)Header, (u8*)(Footer)); + } + + uart_header* DrawAllHeader = PushStructOnCursor(&WriteCursor, uart_header); + UART_FillHeader(DrawAllHeader, 255, UART_DRAW_ALL); + uart_footer* DrawAllFooter = + PushStructOnCursor(&WriteCursor, uart_footer); + DrawAllFooter->CRC = UART_CalculateCRC((u8*)DrawAllHeader, (u8*)(DrawAllFooter)); + + Data->Size = ((u8*)DrawAllFooter - (u8*)FirstHeaderAddr) + sizeof(uart_footer); +} + +int main(int ArgCount, char** Args) +{ + gs_thread_context Ctx = Win32CreateThreadContext(); + + HANDLE SerialHandle = Win32SerialPort_Open("\\\\.\\COM9"); + Win32SerialPort_SetState(SerialHandle, 2000000, 8, 0, 1); + + gs_const_string OutFileName = ConstString("./serial_dump.data"); + + + if (false) + { + Win32SerialPort_SetRead(SerialHandle); + + gs_data Data = PushSizeToData(Ctx.Transient, KB(32)); + + Win32SerialPort_SetRead(SerialHandle); + u32 ReadSize = Win32SerialPort_ReadMessageWhenReady(SerialHandle, Data); + + u8* SetChannelHeaderAddr = 0; + uart_header* SetChannelHeader = 0; + uart_header* DrawAllHeader = 0; + u8* ScanAt = Data.Memory; + do + { + ScanAt = FindNextHeader(Data, ScanAt); + uart_header* Header = (uart_header*)ScanAt; + + if (Header->RecordType == UART_SET_CHANNEL_WS2812) + { + printf("Set Channel:\n"); + printf(" Channel: %d\n", Header->Channel); + printf(" Pixels: %d\n", ((uart_channel*)(Header + 1))->PixelsCount); + if (!SetChannelHeader) + { + SetChannelHeaderAddr = (u8*)Header; + SetChannelHeader = Header; + } + } + + if (Header->RecordType == UART_DRAW_ALL) + { + printf("Draw All:\n"); + printf(" Channel: %d\n", Header->Channel); + if (!DrawAllHeader) + { + DrawAllHeader= Header; + } + } + + ScanAt += sizeof(uart_header); + }while(((u32)(ScanAt - Data.Memory + sizeof(uart_header)) < Data.Size)); + + uart_channel* Channel = (uart_channel*)(SetChannelHeader + 1); + + u8* DataStart = (u8*)(Channel + 1); + + uart_footer* Footer = (uart_footer*)(DataStart + (Channel->ElementsCount * Channel->PixelsCount)); + + u32 TestCRC = UART_CalculateCRC((u8*)SetChannelHeader, (u8*)(Footer)); + + uart_footer* DrawAllFooter = (uart_footer*)(DrawAllHeader + 1); + u32 DrawwAllCRC = UART_CalculateCRC((u8*)DrawAllHeader, (u8*)(DrawAllFooter)); + + HANDLE FileHandle = CreateFileA(OutFileName.Str, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (FileHandle != INVALID_HANDLE_VALUE) + { + DWORD BytesWritten = 0; + if (!WriteFile(FileHandle, Data.Memory, Data.Size, &BytesWritten, NULL)) + { + InvalidCodePath; + } + } + CloseHandle(FileHandle); + Win32SerialPort_Close(SerialHandle); + + } + else if (true) + { + gs_data Data = PushSizeToData(Ctx.Transient, KB(32)); + + u8 Count = 0; + while(true) + { + CreateMessage(&Data, ++Count); + Win32SerialPort_Write(SerialHandle, Data); + Sleep(100); + } + } + else if (false) + { + gs_data Data = PushSizeToData(Ctx.Transient, KB(32)); + gs_file File = Win32ReadEntireFile(Ctx.FileHandler, OutFileName, Data); + + gs_data Messages = {0}; + u8* ScanAt = Data.Memory; + ScanAt = FindNextHeader(Data, ScanAt); + uart_header* FirstHeader = (uart_header*)ScanAt; + ScanAt += sizeof(uart_header); + + uart_header* LastHeader = 0; + do + { + ScanAt = FindNextHeader(Data, ScanAt); + uart_header* Header = (uart_header*)ScanAt; + if (Header->RecordType == UART_DRAW_ALL) + { + LastHeader = Header; + } + ScanAt += sizeof(uart_header); + }while((u32)(ScanAt - Data.Memory) < Data.Size); + + u8* OnePastLastByte = ((u8*)(LastHeader + 1)) + sizeof(uart_footer); + + Messages.Memory = (u8*)FirstHeader; + Messages.Size = OnePastLastByte - Messages.Memory; + + while (true) + { + Win32SerialPort_Write(SerialHandle, Messages); + Sleep(100); + } + } + + return 0; +} + + +#define FIRST_CPP +#endif // FIRST_CPP \ No newline at end of file