Created serial_monitor, and debugged uart functionality. It is now working

This commit is contained in:
PS 2020-10-10 16:52:00 -07:00
parent 1940483620
commit 55284cde25
15 changed files with 422 additions and 148 deletions

View File

@ -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% %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 popd
call %MyPath%\_postbuild_win32.bat call %MyPath%\_postbuild_win32.bat

View File

@ -111,7 +111,7 @@ OPERATION_STATE_DEF(connect_nodes_operation_state)
OPERATION_RENDER_PROC(UpdateConnectNodeOperation) 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; node_graph_state* GraphState = (node_graph_state*)NodeGraphPanel.Panel->PanelStateMemory;
GraphState->Layout.InProgressConnectionEnd = Mouse.Pos; 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); 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; node_graph_state* GraphState = (node_graph_state*)NodeGraphPanel.Panel->PanelStateMemory;
GraphState->Layout.ConnectionIsInProgress = false; GraphState->Layout.ConnectionIsInProgress = false;
@ -157,7 +157,7 @@ BeginConnectNodesOperation(visual_port VisualPort, u32 VisualPortIndex, mouse_st
OpState->VisualPort = VisualPort; OpState->VisualPort = VisualPort;
OpState->VisualPortIndex = VisualPortIndex; 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; node_graph_state* GraphState = (node_graph_state*)NodeGraphPanel.Panel->PanelStateMemory;
GraphState->Layout.ConnectionIsInProgress = true; GraphState->Layout.ConnectionIsInProgress = true;

View File

@ -24,6 +24,8 @@ typedef struct panel_entry panel_entry;
struct panel 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; s32 PanelDefinitionIndex;
panel_split_direction SplitDirection; panel_split_direction SplitDirection;
@ -154,11 +156,11 @@ FreePanelAtIndex(s32 Index, panel_system* PanelSystem)
} }
internal void 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) if (Percent >= 0.0f && Percent <= 1.0f)
{ {
Parent->SplitDirection = PanelSplit_Vertical; Parent->SplitDirection = SplitDirection;
Parent->SplitPercent = Percent; Parent->SplitPercent = Percent;
Parent->Left = TakeNewPanelEntry(PanelSystem); 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 internal void
SplitPanelHorizontally(panel* Parent, r32 Percent, panel_system* PanelSystem) SplitPanelHorizontally(panel* Parent, r32 Percent, panel_system* PanelSystem)
{ {
if (Percent >= 0.0f && Percent <= 1.0f) SplitPanel(Parent, Percent, PanelSplit_Horizontal, PanelSystem);
{
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;
}
} }
internal void internal void
@ -290,16 +288,10 @@ GetPanelLayout(panel_system* System, rect2 WindowBounds, gs_memory_arena* Storag
return Result; return Result;
} }
struct panel_and_bounds internal panel_with_layout
{
panel* Panel;
rect2 Bounds;
};
internal panel_and_bounds
GetPanelContainingPoint(v2 Point, panel* Panel, rect2 PanelBounds) GetPanelContainingPoint(v2 Point, panel* Panel, rect2 PanelBounds)
{ {
panel_and_bounds Result = {0}; panel_with_layout Result = {0};
if (Panel->SplitDirection == PanelSplit_NoSplit) if (Panel->SplitDirection == PanelSplit_NoSplit)
{ {
@ -338,10 +330,10 @@ GetPanelContainingPoint(v2 Point, panel* Panel, rect2 PanelBounds)
return Result; return Result;
} }
internal panel_and_bounds internal panel_with_layout
GetPanelContainingPoint(v2 Point, panel_system* PanelSystem, rect2 WindowBounds) GetPanelContainingPoint(v2 Point, panel_system* PanelSystem, rect2 WindowBounds)
{ {
panel_and_bounds Result = {0}; panel_with_layout Result = {0};
if (PanelSystem->PanelsUsed > 0) if (PanelSystem->PanelsUsed > 0)
{ {
Result = GetPanelContainingPoint(Point, &PanelSystem->Panels[0].Panel, WindowBounds); Result = GetPanelContainingPoint(Point, &PanelSystem->Panels[0].Panel, WindowBounds);

View File

@ -265,7 +265,7 @@ FOLDHAUS_INPUT_COMMAND_PROC(AddAnimationBlockCommand)
{ {
animation* ActiveAnim = AnimationSystem_GetActiveAnimation(&State->AnimationSystem); 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; frame_range Range = ActiveAnim->PlayableRange;
u32 MouseDownFrame = GetFrameFromPointInAnimationPanel(Mouse.Pos, ActivePanel.Bounds, Range); u32 MouseDownFrame = GetFrameFromPointInAnimationPanel(Mouse.Pos, ActivePanel.Bounds, Range);

View File

@ -83,9 +83,6 @@ struct animation_system
gs_memory_arena* Storage; gs_memory_arena* Storage;
animation_array Animations; animation_array Animations;
frame_range PlayableRange_;
gs_list<animation_block> Blocks_;
// NOTE(Peter): The frame currently being displayed/processed. you // 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 // can see which frame you're on by looking at the time slider on the timeline
// panel // panel

View File

@ -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

View File

@ -128,13 +128,6 @@ LedSystemFreeBuffer(led_system* System, u32 BufferIndex)
*Buffer = {}; *Buffer = {};
} }
internal led_buffer*
LedSystemGetBuffer(led_system* System, u32 Index)
{
led_buffer* Result = &System->Buffers[Index];
return Result;
}
internal void internal void
LedBufferSetLed(led_buffer* Buffer, u32 Led, v4 Position) LedBufferSetLed(led_buffer* Buffer, u32 Led, v4 Position)
{ {

View File

@ -112,7 +112,14 @@ struct assembly_array
assembly* Values; 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 #define FOLDHAUS_ASSEMBLY_H
#endif // FOLDHAUS_ASSEMBLY_H #endif // FOLDHAUS_ASSEMBLY_H

View File

@ -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

View File

@ -16,7 +16,7 @@ enum uart_record_type
struct uart_header struct uart_header
{ {
u8 MagicNumber[4]; s8 MagicNumber[4];
u8 Channel; u8 Channel;
u8 RecordType; u8 RecordType;
}; };
@ -24,12 +24,7 @@ struct uart_header
struct uart_channel struct uart_channel
{ {
u8 ElementsCount; u8 ElementsCount;
u8 ColorPackingOrder;
u8 RedIndex;
u8 GreenIndex;
u8 BlueIndex;
u8 WhiteIndex;
u16 PixelsCount; u16 PixelsCount;
}; };
@ -57,113 +52,34 @@ UART_FillHeader(uart_header* Header, u8 Channel, u8 RecordType)
Header->RecordType = RecordType; Header->RecordType = RecordType;
} }
internal void internal u32
UART_FillFooter(uart_footer* Footer, u8* BufferStart) UART_CalculateCRC(u8* BufferStart, u8* BufferEnd)
{ {
// Calculate the CRC // Calculate the CRC
u32 CRC = 0xFFFFFFFF; u32 CRC = 0xFFFFFFFF;
u32 BytesCount = (u8*)Footer - BufferStart; u32 BytesCount = (u8*)BufferEnd - BufferStart;
for (u32 i = 0; i < BytesCount; i++) for (u32 i = 0; i < BytesCount; i++)
{ {
u8 At = BufferStart[i]; u8 At = BufferStart[i];
#if 0
// Cameron's Version // Cameron's Version
CRC = UART_CRCTable[(CRC ^ At) & 0x0F] ^ (CRC >> 4); CRC = UART_CRCTable[(CRC ^ At) & 0x0F] ^ (CRC >> 4);
CRC = UART_CRCTable[(CRC ^ (At >> 4)) & 0x0F] ^ (CRC >> 4); CRC = UART_CRCTable[(CRC ^ (At >> 4)) & 0x0F] ^ (CRC >> 4);
#else
#if 0 // https://github.com/simap/pixelblaze_output_expander/blob/master/firmware/Core/Src/uart.c
// The Libraries Version u32 TableIndex = (CRC ^ At) & 0xFF;
CRC = (UART_CRCTable[(CRC ^ At) & 0xFF] ^ (CRC >> 8)) & 0xFFFFFFFF; CRC = (UART_CRCTable[TableIndex] ^ (CRC >> 8)) & 0xFFFFFFFF;
#endif #endif
} }
Footer->CRC = CRC; CRC = CRC ^ 0xFFFFFFFF;
return CRC;
} }
internal void 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 Footer->CRC = UART_CalculateCRC(BufferStart, (u8*)Footer);
// 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);
}
} }
#define FOLDHAUS_UART_H #define FOLDHAUS_UART_H

View File

@ -153,9 +153,10 @@ INITIALIZE_APPLICATION(InitializeApplication)
Animation_AddLayer(&Anim, MakeString("Color Layer"), BlendMode_Multiply, &State->AnimationSystem); Animation_AddLayer(&Anim, MakeString("Color Layer"), BlendMode_Multiply, &State->AnimationSystem);
Animation_AddLayer(&Anim, MakeString("Sparkles"), BlendMode_Add, &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); AnimationArray_Push(&State->AnimationSystem.Animations, Anim);
} // End Animation Playground } // End Animation Playground
@ -179,7 +180,7 @@ HandleInput (app_state* State, rect2 WindowBounds, input_queue InputQueue, mouse
} }
else 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; } if (!PanelWithMouseOverIt.Panel) { return; }
State->HotPanel = PanelWithMouseOverIt.Panel; State->HotPanel = PanelWithMouseOverIt.Panel;

View File

@ -22,6 +22,7 @@
#include "engine/sacn/foldhaus_sacn.h" #include "engine/sacn/foldhaus_sacn.h"
#include "engine/uart/foldhaus_uart.h" #include "engine/uart/foldhaus_uart.h"
#include "engine/uart/foldhaus_uart.cpp"
typedef struct app_state app_state; typedef struct app_state app_state;

View File

@ -426,6 +426,34 @@ WinMain (
{ {
gs_thread_context ThreadContext = Win32CreateThreadContext(); 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); MainWindow = Win32CreateWindow (HInstance, "Foldhaus", 1440, 768, HandleWindowEvents);
Win32UpdateWindowDimension(&MainWindow); Win32UpdateWindowDimension(&MainWindow);

View File

@ -143,6 +143,37 @@ Win32SerialPort_Write(HANDLE PortHandle, gs_data Buffer)
return Success; 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 // Win32SerialArray

View File

@ -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 <windows.h>
#include <stdio.h>
//#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