1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-31 01:42:02 +00:00

Finalized LightFX C++ project

Implemented JSON serialization
Implemented pipe transmission
Made sure both x86 and x64 compile
This commit is contained in:
SpoinkyNL 2016-12-21 00:45:48 +01:00
parent 4be324dc69
commit 3e9a86d893
8 changed files with 74 additions and 42 deletions

View File

@ -105,6 +105,7 @@
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(ProjectDir)includes;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>

View File

@ -3,7 +3,11 @@ using json = nlohmann::json;
LightFxDevice::LightFxDevice() LightFxDevice::LightFxDevice()
{ {
for (int i = 0; i < 6; i++)
{
Lights[i] = new LightFxLight();
}
} }
@ -12,20 +16,17 @@ LightFxDevice::~LightFxDevice()
} }
void LightFxDevice::SetLightFromInt(int lightIndex, const unsigned colorVal) void LightFxDevice::SetLightFromInt(int lightIndex, const unsigned colorVal)
{ {
Lights[lightIndex].Color->brightness = (colorVal >> 24) & 0xFF; Lights[lightIndex]->FromInt(colorVal);
Lights[lightIndex].Color->red = (colorVal >> 16) & 0xFF;
Lights[lightIndex].Color->green = (colorVal >> 8) & 0xFF;
Lights[lightIndex].Color->blue = colorVal & 0xFF;
} }
json LightFxDevice::GetJson() json LightFxDevice::GetJson()
{ {
json j; json j;
j["lights"] = {}; j["lights"] = {};
for (LightFxLight light : Lights) for (LightFxLight* light : Lights)
{ {
j["lights"].push_back(light.GetJson()); j["lights"].push_back(light->GetJson());
} }
return j; return j;

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "LightFxLight.h" #include "LightFxLight.h"
#include <json.hpp> #include "json.hpp"
using json = nlohmann::json; using json = nlohmann::json;
@ -11,5 +11,5 @@ public:
~LightFxDevice(); ~LightFxDevice();
void SetLightFromInt(int lightIndex, const unsigned colorVal); void SetLightFromInt(int lightIndex, const unsigned colorVal);
json GetJson(); json GetJson();
LightFxLight Lights[128]; LightFxLight* Lights[5];
}; };

View File

@ -3,6 +3,7 @@
LightFxLight::LightFxLight() LightFxLight::LightFxLight()
{ {
Color = new LFX_COLOR{0,0,0,0};
} }
@ -21,3 +22,11 @@ json LightFxLight::GetJson()
}; };
return j; return j;
} }
void LightFxLight::FromInt(const unsigned colorVal)
{
Color->brightness = (colorVal >> 24) & 0xFF;
Color->red = (colorVal >> 16) & 0xFF;
Color->green = (colorVal >> 8) & 0xFF;
Color->blue = colorVal & 0xFF;
}

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "includes/LFXDecl.h" #include "LFXDecl.h"
#include "json.hpp" #include "json.hpp"
using json = nlohmann::json; using json = nlohmann::json;
@ -10,5 +10,6 @@ public:
LightFxLight(); LightFxLight();
~LightFxLight(); ~LightFxLight();
json GetJson(); json GetJson();
PLFX_COLOR Color; void FromInt(const unsigned colorVal);
LFX_COLOR* Color;
}; };

View File

@ -6,6 +6,11 @@ using json = nlohmann::json;
LightFxState::LightFxState(char* game) LightFxState::LightFxState(char* game)
{ {
Game = game; Game = game;
for (int i = 0; i < 5; i++)
{
Devices[i] = new LightFxDevice();
}
LocationMaskLight = new LightFxLight();
} }
@ -13,16 +18,22 @@ LightFxState::~LightFxState()
{ {
} }
const char* LightFxState::Update() json LightFxState::GetJson()
{ {
json root;
json j; json j;
root["lightFxState"] = { j };
j["game"] = Game; j["game"] = Game;
j["mask"] = {
{ "location", LocationMask },
{ "light", LocationMaskLight->GetJson() }
};
j["devices"] = {}; j["devices"] = {};
for (LightFxDevice device : Devices) for (LightFxDevice* device : Devices)
{ {
j["devices"].push_back(device.GetJson()); j["devices"].push_back(device->GetJson());
} }
std::string s = j.dump(); return j;
return s.c_str();
} }

View File

@ -1,12 +1,17 @@
#pragma once #pragma once
#include "LightFxDevice.h" #include "LightFxDevice.h"
#include "json.hpp"
using json = nlohmann::json;
class LightFxState class LightFxState
{ {
public: public:
LightFxState(char* game); LightFxState(char* game);
~LightFxState(); ~LightFxState();
const char* Update(); json GetJson();
char* Game; char* Game;
LightFxDevice Devices[5]; LightFxDevice* Devices[5];
unsigned LocationMask;
LightFxLight* LocationMaskLight;
}; };

View File

@ -4,28 +4,27 @@
#include <complex> #include <complex>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include "../LightFX2Artemis/LightFxState.h" #include "LightFxState.h"
#include "includes/LFX2.h" #include "LFX2.h"
#include "includes/LFXDecl.h" #include "LFXDecl.h"
#include "includes/log.h" #include "log.h"
using namespace std; using namespace std;
LightFxState* lightFxState; LightFxState* lightFxState;
char* GetGame()
{
CHAR szPath[MAX_PATH];
GetModuleFileNameA(NULL, szPath, MAX_PATH);
return szPath;
};
BOOL WINAPI DllMain(HINSTANCE hInst, DWORD fdwReason, LPVOID) BOOL WINAPI DllMain(HINSTANCE hInst, DWORD fdwReason, LPVOID)
{ {
if (fdwReason == DLL_PROCESS_ATTACH) if (fdwReason == DLL_PROCESS_ATTACH)
{ {
lightFxState = new LightFxState("Payday2"); // Get executing .exe path. Not very pretty but other methods messed JSON up
WCHAR ownPth[MAX_PATH];
HMODULE hModule = GetModuleHandle(NULL);
GetModuleFileName(hModule, ownPth, sizeof ownPth);
char* moduleName = new char[sizeof ownPth];
wcstombs(moduleName, ownPth, sizeof ownPth);
lightFxState = new LightFxState(moduleName);
FILELog::ReportingLevel() = logDEBUG1; FILELog::ReportingLevel() = logDEBUG1;
FILE* log_fd = fopen("log.txt", "w"); FILE* log_fd = fopen("log.txt", "w");
Output2FILE::Stream() = log_fd; Output2FILE::Stream() = log_fd;
@ -75,8 +74,15 @@ FN_DECLSPEC LFX_RESULT STDCALL LFX_Update()
{ {
FILE_LOG(logDEBUG1) << "Called LFX_Update()"; FILE_LOG(logDEBUG1) << "Called LFX_Update()";
const char* jsonString = lightFxState->Update(); // Get the JSON
FILE_LOG(logDEBUG1) << "JSON: " << jsonString; json j = lightFxState->GetJson();
// Transmit to Artemis
Transmit(j.dump().c_str());
// Only bother dumping it indented if actually debugging
if (FILELog::ReportingLevel() == logDEBUG1)
FILE_LOG(logDEBUG1) << "JSON: " << j.dump(4);
return LFX_SUCCESS; return LFX_SUCCESS;
} }
@ -126,7 +132,7 @@ FN_DECLSPEC LFX_RESULT STDCALL LFX_GetNumLights(const unsigned int devIndex, uns
FILE_LOG(logDEBUG1) << "Called LFX_GetNumLights()"; FILE_LOG(logDEBUG1) << "Called LFX_GetNumLights()";
// Just do one for now // Just do one for now
*numLights = 1; *numLights = 5;
return LFX_SUCCESS; return LFX_SUCCESS;
} }
@ -156,7 +162,7 @@ FN_DECLSPEC LFX_RESULT STDCALL LFX_GetLightColor(const unsigned int devIndex, co
{ {
FILE_LOG(logDEBUG1) << "Called LFX_GetLightColor()"; FILE_LOG(logDEBUG1) << "Called LFX_GetLightColor()";
*lightCol = *lightFxState->Devices[devIndex].Lights[lightIndex].Color; *lightCol = *lightFxState->Devices[devIndex]->Lights[lightIndex]->Color;
return LFX_SUCCESS; return LFX_SUCCESS;
} }
@ -164,18 +170,16 @@ FN_DECLSPEC LFX_RESULT STDCALL LFX_SetLightColor(const unsigned int devIndex, co
{ {
FILE_LOG(logDEBUG1) << "Called LFX_SetLightColor()"; FILE_LOG(logDEBUG1) << "Called LFX_SetLightColor()";
lightFxState->Devices[devIndex].Lights[lightIndex].Color = lightCol; lightFxState->Devices[devIndex]->Lights[lightIndex]->Color = lightCol;
return LFX_SUCCESS; return LFX_SUCCESS;
} }
FN_DECLSPEC LFX_RESULT STDCALL LFX_Light(const unsigned int locationMask, const unsigned int colorVal) FN_DECLSPEC LFX_RESULT STDCALL LFX_Light(const unsigned int locationMask, const unsigned int colorVal)
{ {
FILE_LOG(logDEBUG1) << "Called LFX_Light()"; FILE_LOG(logDEBUG1) << "Called LFX_Light(locationMask " << locationMask << ", colorVal " << colorVal << ")";
for (LightFxDevice device : lightFxState->Devices) lightFxState->LocationMask = locationMask;
{ lightFxState->LocationMaskLight->FromInt(colorVal);
device.SetLightFromInt(0, colorVal);
}
return LFX_SUCCESS; return LFX_SUCCESS;
} }