From 874925a5fe12feb311c527939588767e5fc56a9c Mon Sep 17 00:00:00 2001 From: PS Date: Sat, 27 Mar 2021 14:46:17 -0700 Subject: [PATCH] mapping phrases to colors for patterns --- src/app/foldhaus_app.h | 1 + src/app/foldhaus_platform.h | 1 + src/app/patterns/blumen_patterns.h | 3 +- src/app/platform_win32/win32_foldhaus.cpp | 22 +++++- src/app/ss_blumen_lumen/blumen_lumen.cpp | 22 ++++-- src/app/ss_blumen_lumen/blumen_lumen.h | 5 +- src/app/ss_blumen_lumen/phrase_hue_map.h | 83 +++++++++++++++++++++++ 7 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 src/app/ss_blumen_lumen/phrase_hue_map.h diff --git a/src/app/foldhaus_app.h b/src/app/foldhaus_app.h index 6e44c4d..a8eb0f1 100644 --- a/src/app/foldhaus_app.h +++ b/src/app/foldhaus_app.h @@ -42,6 +42,7 @@ typedef struct panel panel; #include "engine/animation/foldhaus_animation_renderer.cpp" #include "engine/user_space.h" +#include "ss_blumen_lumen/phrase_hue_map.h" #include "ss_blumen_lumen/blumen_lumen.h" struct app_state diff --git a/src/app/foldhaus_platform.h b/src/app/foldhaus_platform.h index f464603..dcfafc8 100644 --- a/src/app/foldhaus_platform.h +++ b/src/app/foldhaus_platform.h @@ -27,6 +27,7 @@ Handle_IsValid(handle Handle) } #include "..\gs_libs\gs_string.h" +#include "..\gs_libs\gs_csv.h" #include "foldhaus_debug.h" global debug_services* GlobalDebugServices; diff --git a/src/app/patterns/blumen_patterns.h b/src/app/patterns/blumen_patterns.h index 84d51e5..a1db355 100644 --- a/src/app/patterns/blumen_patterns.h +++ b/src/app/patterns/blumen_patterns.h @@ -1193,8 +1193,7 @@ Pattern_WavyPatchy(led_buffer* Leds, led_buffer_range Range, assembly Assembly, r32 LightHueMin = (ModR32(Time, 10) / 10) * 360; r32 LightHueMax = ModR32((LightHueMin + 45), 360) ; #else - v4 CenterColor = BLState->AssemblyColors[Assembly.AssemblyIndex % 3]; - r32 CenterHue = RGBToHSV(CenterColor).x; + r32 CenterHue = BLState->AssemblyColors[Assembly.AssemblyIndex % 3].Hue0; r32 LightHueMin = ModR32(CenterHue + 30, 360);; r32 LightHueMax = ModR32(CenterHue - 30, 360) ; #endif diff --git a/src/app/platform_win32/win32_foldhaus.cpp b/src/app/platform_win32/win32_foldhaus.cpp index cc1020e..e98567f 100644 --- a/src/app/platform_win32/win32_foldhaus.cpp +++ b/src/app/platform_win32/win32_foldhaus.cpp @@ -524,7 +524,6 @@ SetWorkingDirectory(HINSTANCE HInstance, gs_thread_context ThreadContext) } #include "../../gs_libs/gs_path.h" -#include "../../gs_libs/gs_csv.h" internal void Win32_SendOutputData(gs_thread_context ThreadContext, addressed_data_buffer_list OutputData) @@ -575,6 +574,27 @@ WinMain ( if (!SetWorkingDirectory(HInstance, ThreadContext)) return 1; + + gs_file TestFile = ReadEntireFile(ThreadContext.FileHandler, ConstString("data/flower_codes.tsv")); + gs_const_string TestFileStr = {}; + TestFileStr.Str = (char*)TestFile.Memory; + TestFileStr.Length = TestFile.Size; + gscsv_sheet Sheet = CSV_Parse(TestFileStr, { '\t' }, ThreadContext.Transient); + + gs_string Out = PushString(ThreadContext.Transient, TestFile.Size * 2); + + for (u64 y = 0; y < Sheet.RowCount; y++) + { + for (u64 x = 0; x < Sheet.ColumnCount; x++) + { + gs_const_string Cell = CSVSheet_GetCell(Sheet, x, y); + AppendPrintF(&Out, "%S\t", Cell); + } + AppendPrintF(&Out, "\n"); + } + NullTerminate(&Out); + OutputDebugStringA(Out.Str); + MainWindow = Win32CreateWindow (HInstance, "Foldhaus", 1440, 768, HandleWindowEvents); Win32UpdateWindowDimension(&MainWindow); diff --git a/src/app/ss_blumen_lumen/blumen_lumen.cpp b/src/app/ss_blumen_lumen/blumen_lumen.cpp index 5dc1bde..404a0c0 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen.cpp +++ b/src/app/ss_blumen_lumen/blumen_lumen.cpp @@ -281,6 +281,15 @@ BlumenLumen_CustomInit(app_state* State, context Context) BLState->AssemblyNameToClearCore_Names[2] = HashDJB2ToU32(StringExpand(Flower2->Name)); #endif + { + gs_file ColorPhraseCSVFile = ReadEntireFile(Context.ThreadContext.FileHandler, ConstString("data/flower_codes.tsv")); + gs_const_string ColorPhraseMapStr = ConstString((char*)ColorPhraseCSVFile.Memory, + ColorPhraseCSVFile.Size); + gscsv_sheet ColorPhraseSheet = CSV_Parse(ColorPhraseMapStr, { '\t' }, State->Transient); + + BLState->PhraseHueMap = PhraseHueMap_GenFromCSV(ColorPhraseSheet, &State->Permanent); + } + #if 1 { // Animation PLAYGROUND animation_desc Desc = {}; @@ -323,10 +332,6 @@ BlumenLumen_CustomInit(app_state* State, context Context) #endif State->AnimationSystem.TimelineShouldAdvance = true; - BLState->AssemblyColors[0] = RedV4; - BLState->AssemblyColors[1] = GreenV4; - BLState->AssemblyColors[2] = BlueV4; - return Result; } @@ -360,8 +365,16 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) case PacketType_PatternCommand: { microphone_packet Mic = Packet.MicPacket; + u64 NameHash = HashDJB2ToU32(Mic.AnimationFileName); u32 NameLen = CStringLength(Mic.AnimationFileName); + phrase_hue NewHue = PhraseHueMap_Get(BLState->PhraseHueMap, NameHash); + if (NewHue.PhraseHash != 0) + { + u32 AssemblyIdx = BLState->LastAssemblyColorSet; + BLState->AssemblyColors[AssemblyIdx] = NewHue; + } +#if 0 for (u32 i = 0; i < PhraseToAnimMapCount; i++) { gs_const_string PhraseStr = ConstString(PhraseToAnimMap[i].Phrase); @@ -379,6 +392,7 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) OutputDebugStringA(T.Str); } } +#endif }break; case PacketType_MotorState: diff --git a/src/app/ss_blumen_lumen/blumen_lumen.h b/src/app/ss_blumen_lumen/blumen_lumen.h index b097194..c8fd808 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen.h +++ b/src/app/ss_blumen_lumen/blumen_lumen.h @@ -219,7 +219,8 @@ struct blumen_lumen_state system_time LastSendTime; - v4 AssemblyColors[3]; + phrase_hue AssemblyColors[3]; + u32 LastAssemblyColorSet; // The indices of this array are the index the clear core uses to // represent a motor. @@ -229,6 +230,8 @@ struct blumen_lumen_state u32 AssemblyNameToClearCoreMapCount; u64* AssemblyNameToClearCore_Names; + phrase_hue_map PhraseHueMap; + // Debug motor_packet DEBUG_PendingMotorPacket; }; diff --git a/src/app/ss_blumen_lumen/phrase_hue_map.h b/src/app/ss_blumen_lumen/phrase_hue_map.h new file mode 100644 index 0000000..0d7d66c --- /dev/null +++ b/src/app/ss_blumen_lumen/phrase_hue_map.h @@ -0,0 +1,83 @@ +/* date = March 27th 2021 1:55 pm */ + +#ifndef PHRASE_HUE_MAP_H +#define PHRASE_HUE_MAP_H + +typedef struct phrase_hue_map +{ + u64 Count; + gs_const_string* Phrases; + u64* PhraseHashes; + r32* Hue0; + r32* Hue1; + r32* Hue2; +} phrase_hue_map; + +typedef struct phrase_hue +{ + gs_const_string Phrase; + u64 PhraseHash; + r32 Hue0; + r32 Hue1; + r32 Hue2; +} phrase_hue; + +internal phrase_hue_map +PhraseHueMap_GenFromCSV(gscsv_sheet Sheet, gs_memory_arena* Arena) +{ + phrase_hue_map Result = {}; + Result.Count = Sheet.RowCount - 1; // we don't include the header row + Result.Phrases = PushArray(Arena, gs_const_string, Result.Count); + Result.PhraseHashes = PushArray(Arena, u64, Result.Count); + Result.Hue0 = PushArray(Arena, r32, Result.Count); + Result.Hue1 = PushArray(Arena, r32, Result.Count); + Result.Hue2 = PushArray(Arena, r32, Result.Count); + + for (u32 Row = 1; Row < Sheet.RowCount; Row++) + { + u32 Index = Row - 1; + gs_const_string Phrase = CSVSheet_GetCell(Sheet, + 0, Row); + u64 PhraseHash = HashDJB2ToU32(StringExpand(Phrase)); + gs_const_string Hue0Str = CSVSheet_GetCell(Sheet, + 1, Row); + gs_const_string Hue1Str = CSVSheet_GetCell(Sheet, + 2, Row); + gs_const_string Hue2Str = CSVSheet_GetCell(Sheet, + 3, Row); + gs_const_string Homonyms = CSVSheet_GetCell(Sheet, + 4, Row); + + Result.Phrases[Index] = PushStringF(Arena, Phrase.Length, "%S", Phrase).ConstString; + Result.PhraseHashes[Index] = PhraseHash; + Result.Hue0[Index] = (r64)ParseFloat(Hue0Str); + Result.Hue1[Index] = (r64)ParseFloat(Hue1Str); + Result.Hue2[Index] = (r64)ParseFloat(Hue2Str); + } + + return Result; +} + +internal phrase_hue +PhraseHueMap_Get(phrase_hue_map Map, u64 PhraseHash) +{ + phrase_hue Result = {}; + + for (u32 i = 0; i < Map.Count; i++) + { + if (Map.PhraseHashes[i] == PhraseHash) + { + Result.Phrase = Map.Phrases[i]; + Result.PhraseHash = Map.PhraseHashes[i]; + Result.Hue0 = Map.Hue0[i]; + Result.Hue1 = Map.Hue1[i]; + Result.Hue2 = Map.Hue2[i]; + + break; + } + } + + return Result; +} + +#endif //PHRASE_HUE_MAP_H