From a5e774c8d173ed3f0ff5cbfd5b5629d24604fd9a Mon Sep 17 00:00:00 2001 From: Peter Slattery Date: Fri, 22 Nov 2019 23:33:48 -0800 Subject: [PATCH] created a mapping from leds to universes in assemly structures that doesn't rely on SACN --- src/foldhaus_app.cpp | 55 ++++++++++++++++++++------------------- src/foldhaus_assembly.cpp | 30 +++++++++++++++------ src/foldhaus_assembly.h | 15 ++++++++--- 3 files changed, 61 insertions(+), 39 deletions(-) diff --git a/src/foldhaus_app.cpp b/src/foldhaus_app.cpp index d83a233..e36da86 100644 --- a/src/foldhaus_app.cpp +++ b/src/foldhaus_app.cpp @@ -149,11 +149,15 @@ LoadAssembly (app_state* State, context Context, char* Path) r32 Scale = 100; Assert(State->AssembliesCount < ASSEMBLY_LIST_LENGTH); + s32 AssemblyMemorySize = GetAssemblyMemorySizeFromDefinition(AssemblyDefinition, FileName); + u8* AssemblyMemory = Context.PlatformAlloc(AssemblyMemorySize); + assembly NewAssembly = ConstructAssemblyFromDefinition(AssemblyDefinition, FileName, v3{0, 0, 0}, Scale, - Context); + AssemblyMemory, + AssemblyMemorySize); State->AssemblyList[State->AssembliesCount++] = NewAssembly; State->TotalLEDsCount += NewAssembly.LEDCount; @@ -376,36 +380,33 @@ CreateDMXBuffers(assembly Assembly, s32 BufferHeaderSize, memory_arena* Arena) s32 BufferSize = BufferHeaderSize + 512; - s32 Universe = 1; - s32 ChannelsUsed = 0; - for (s32 l = 0; l < Assembly.LEDCount; l++) + for (s32 Range = 0; Range < Assembly.LEDUniverseMapCount; Range++) { - if(ChannelsUsed == 0) - { - dmx_buffer_list* NewBuffer = PushStruct(Arena, dmx_buffer_list); - NewBuffer->Buffer.Universe = Universe; - NewBuffer->Buffer.Base = PushArray(Arena, u8, BufferSize); - NewBuffer->Buffer.TotalSize = BufferSize; - NewBuffer->Buffer.HeaderSize = BufferHeaderSize; - - // Append - if (!Result) { - Result = NewBuffer; - Head = Result; - } - Head->Next = NewBuffer; - Head = NewBuffer; + leds_in_universe_range LEDUniverseRange = Assembly.LEDUniverseMap[Range]; + + dmx_buffer_list* NewBuffer = PushStruct(Arena, dmx_buffer_list); + NewBuffer->Buffer.Universe = LEDUniverseRange.Universe; + NewBuffer->Buffer.Base = PushArray(Arena, u8, BufferSize); + NewBuffer->Buffer.TotalSize = BufferSize; + NewBuffer->Buffer.HeaderSize = BufferHeaderSize; + + // Append + if (!Result) { + Result = NewBuffer; + Head = Result; } + Head->Next = NewBuffer; + Head = NewBuffer; - led LED = Assembly.LEDs[l]; - pixel Color = Assembly.Colors[LED.Index]; - *((pixel*)(Head->Buffer.Base + ChannelsUsed)) = Color; - ChannelsUsed += 3; - - if (ChannelsUsed + 3 >= 512) + for (s32 LEDIdx = LEDUniverseRange.RangeStart; + LEDIdx < LEDUniverseRange.RangeOnePastLast; + LEDIdx++) { - Universe++; - ChannelsUsed= 0; + led LED = Assembly.LEDs[LEDIdx]; + pixel Color = Assembly.Colors[LED.Index]; + + s32 DestinationStartChannel = LEDIdx * 3; + *((pixel*)(Head->Buffer.Base + DestinationStartChannel)) = Color; } } diff --git a/src/foldhaus_assembly.cpp b/src/foldhaus_assembly.cpp index 84ca649..418b169 100644 --- a/src/foldhaus_assembly.cpp +++ b/src/foldhaus_assembly.cpp @@ -1,31 +1,45 @@ +internal s32 +GetAssemblyMemorySizeFromDefinition(assembly_definition Definition, string Name) +{ + s32 Result = (sizeof(led) + sizeof(pixel)) * Definition.TotalLEDCount; + Result += sizeof(leds_in_universe_range) * Definition.LEDStripCount; + Result += Name.Length; + return Result; +} + internal assembly ConstructAssemblyFromDefinition (assembly_definition Definition, string AssemblyName, v3 RootPosition, r32 Scale, - context Context) + u8* MemoryBase, + s32 MemorySize) { assembly Assembly = {}; + Assembly.Arena = CreateMemoryArena(MemoryBase, MemorySize); - s32 MemorySize = AssemblySize(Definition.TotalLEDCount, AssemblyName.Length); - Assembly.Memory = AllocateNonGrowableArenaWithSpace(Context.PlatformAlloc, MemorySize); - - Assembly.Name = MakeString(PushArray(&Assembly.Memory, char, AssemblyName.Length), - AssemblyName.Length); + Assembly.Name = MakeString(PushArray(&Assembly.Arena, char, AssemblyName.Length), AssemblyName.Length); CopyStringTo(AssemblyName, &Assembly.Name); // NOTE(Peter): Setting this to zero so we can check at the end of the loop that creates leds // and make sure we created to correct number. By the time this function returns it should be // the case that: (Assembly.LEDCount == Definition.TotalLEDCount) Assembly.LEDCount = 0; - Assembly.Colors = PushArray(&Assembly.Memory, pixel, Definition.TotalLEDCount); - Assembly.LEDs = PushArray(&Assembly.Memory, led, Definition.TotalLEDCount); + Assembly.Colors = PushArray(&Assembly.Arena, pixel, Definition.TotalLEDCount); + Assembly.LEDs = PushArray(&Assembly.Arena, led, Definition.TotalLEDCount); + Assembly.LEDUniverseMapCount = Definition.LEDStripCount; + Assembly.LEDUniverseMap = PushArray(&Assembly.Arena, leds_in_universe_range, Definition.LEDStripCount); // Add LEDs for (s32 StripIdx = 0; StripIdx < Definition.LEDStripCount; StripIdx++) { led_strip_definition StripDef = Definition.LEDStrips[StripIdx]; + leds_in_universe_range* LEDUniverseRange = Assembly.LEDUniverseMap + StripIdx; + LEDUniverseRange->Universe = StripDef.StartUniverse; + LEDUniverseRange->RangeStart = Assembly.LEDCount; + LEDUniverseRange->RangeOnePastLast = Assembly.LEDCount + StripDef.LEDsPerStrip; + // NOTE(Peter): this should be a switch on the type, but we only have one for // now. The assert is to remind you to create more cases when necessary Assert(StripDef.InterpolationType == StripInterpolate_Points); diff --git a/src/foldhaus_assembly.h b/src/foldhaus_assembly.h index 3a38c83..ea945f7 100644 --- a/src/foldhaus_assembly.h +++ b/src/foldhaus_assembly.h @@ -15,12 +15,16 @@ union pixel u8 Channels[3]; }; -#define AssemblySize(leds, name_length) ((sizeof(led) + sizeof(pixel)) * (leds)) + name_length; +struct leds_in_universe_range +{ + s32 RangeStart; + s32 RangeOnePastLast; + s32 Universe; +}; + struct assembly { - memory_arena Memory; - s32 MemorySize; - u8* MemoryBase; + static_memory_arena Arena; string Name; string FilePath; @@ -28,4 +32,7 @@ struct assembly s32 LEDCount; pixel* Colors; led* LEDs; + + s32 LEDUniverseMapCount; + leds_in_universe_range* LEDUniverseMap; };