Adding Elgato Keylight

Code style changes by Adam Honse <calcprogrammer1@gmail.com>
This commit is contained in:
TheReal Monkey 2022-11-15 00:30:15 +00:00 committed by Adam Honse
parent a64894445d
commit 0b9825ca19
14 changed files with 615 additions and 0 deletions

View file

@ -0,0 +1,110 @@
/*---------------------------------------------------------*\
| Driver for Elgato Key Light |
| |
| Monks (imtherealestmonkey@gmail.com), 11/03/2021 |
\*---------------------------------------------------------*/
#include "ElgatoKeyLightController.h"
#include "json.hpp"
#include <iostream>
using json = nlohmann::json;
ElgatoKeyLightController::ElgatoKeyLightController(std::string ip)
{
/*-----------------------------------------------------------------*\
| Fill in location string with device's IP address |
\*-----------------------------------------------------------------*/
location = "IP: " + ip;
/*-----------------------------------------------------------------*\
| Open a TCP client sending to the device's IP, port 9123 |
\*-----------------------------------------------------------------*/
port.tcp_client(ip.c_str(), "9123");
}
ElgatoKeyLightController::~ElgatoKeyLightController()
{
}
std::string ElgatoKeyLightController::GetLocation()
{
return(location);
}
std::string ElgatoKeyLightController::GetName()
{
return("Elgato KeyLight");
}
std::string ElgatoKeyLightController::GetVersion()
{
return("");
}
std::string ElgatoKeyLightController::GetManufacturer()
{
return("Elgato");
}
std::string ElgatoKeyLightController::GetUniqueID()
{
return("");
}
void ElgatoKeyLightController::SetColor(hsv_t hsv_color)
{
// Weird elgato color format
int k_value = HSVToK(hsv_color.hue);
port.tcp_client_connect();
std::string buf = GetRequest(hsv_color.value, k_value);
port.tcp_client_write((char *)buf.c_str(), buf.length() + 1);
port.tcp_close();
}
std::string ElgatoKeyLightController::GetRequest(int brightness, int temperature)
{
json command;
command["numberOfLights"] = 1;
auto lights = json::array();
lights.push_back(json::object({ {"on", 1}, {"temperature", temperature}, {"brightness", brightness}}));
command["lights"] = lights;
std::string command_str = command.dump();
std::string buf = "PUT /elgato/lights HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: " +
std::to_string(command_str.length()) +
"\r\nConnection: close\r\n\r\n" + command_str + "\r\n\r\n";
return(buf);
}
int ElgatoKeyLightController::HSVToK(int hue)
{
int k_value;
if(hue <= 60 && hue >= 0)
{
k_value = 2900;
}
else if(hue >= 61 && hue <= 120)
{
k_value = 4000;
}
else if(hue >= 121 && hue <= 180)
{
k_value = 5000;
}
else if(hue >= 181 && hue <= 240)
{
k_value = 6000;
}
else
{
k_value = 7000;
}
return k_value;
}

View file

@ -0,0 +1,37 @@
/*---------------------------------------------------------*\
| Definitions for Elgato Key Light |
| |
| Monks (imtherealestmonkey@gmail.com), 11/11/2021 |
\*---------------------------------------------------------*/
#include "RGBController.h"
#include "net_port.h"
#include "hsv.h"
#include <cmath>
#include <string>
#include <thread>
#include <vector>
#pragma once
class ElgatoKeyLightController
{
public:
ElgatoKeyLightController(std::string ip);
~ElgatoKeyLightController();
std::string GetLocation();
std::string GetName();
std::string GetVersion();
std::string GetManufacturer();
std::string GetUniqueID();
void SetColor(hsv_t hsv_color);
private:
std::string GetRequest(int brightness, int temperature);
int HSVToK(int hue);
std::string location;
net_port port;
};

View file

@ -0,0 +1,48 @@
#include "Detector.h"
#include "ElgatoKeyLightController.h"
#include "RGBController.h"
#include "RGBController_ElgatoKeyLight.h"
#include "SettingsManager.h"
#include <vector>
#include <stdio.h>
#include <stdlib.h>
/******************************************************************************************\
* *
* DetectElgatoKeyLightControllers *
* *
* Detect Elgato KeyLight devices *
* *
\******************************************************************************************/
void DetectElgatoKeyLightControllers(std::vector<RGBController*> &rgb_controllers)
{
json elgato_keylight_settings;
/*-------------------------------------------------*\
| Get KeyLight settings from settings manager |
\*-------------------------------------------------*/
elgato_keylight_settings = ResourceManager::get()->GetSettingsManager()->GetSettings("ElgatoKeyLightDevices");
/*----------------------------------------------------------*\
| If the Elgato Key Light settings contains devices, process|
\*----------------------------------------------------------*/
if(elgato_keylight_settings.contains("devices"))
{
for(unsigned int device_idx = 0; device_idx < elgato_keylight_settings["devices"].size(); device_idx++)
{
if(elgato_keylight_settings["devices"][device_idx].contains("ip"))
{
std::string elgato_keylight_ip = elgato_keylight_settings["devices"][device_idx]["ip"];
ElgatoKeyLightController* controller = new ElgatoKeyLightController(elgato_keylight_ip);
RGBController_ElgatoKeyLight* rgb_controller = new RGBController_ElgatoKeyLight(controller);
rgb_controllers.push_back(rgb_controller);
}
}
}
} /* DetectElgatoKeyLightControllers() */
REGISTER_DETECTOR("ElgatoKeyLight", DetectElgatoKeyLightControllers);

View file

@ -0,0 +1,86 @@
/*------------------------------------------------*\
| RGBController_ElgatoKeyLight.cpp |
| |
| Generic RGB Interface for ElgatoKeyLight |
| |
| Monks (@iamtherealestmonkey) 11/03/2021 |
\*------------------------------------------------*/
#include "RGBController_ElgatoKeyLight.h"
#include "hsv.h"
RGBController_ElgatoKeyLight::RGBController_ElgatoKeyLight(ElgatoKeyLightController* controller_ptr)
{
controller = controller_ptr;
name = controller->GetName();
vendor = controller->GetManufacturer();
type = DEVICE_TYPE_LIGHT;
version = controller->GetVersion();
description = "Elgato KeyLight Device";
serial = controller->GetUniqueID();
location = controller->GetLocation();
mode Static;
Static.name = "Static";
Static.value = 0;
Static.flags = MODE_FLAG_HAS_MODE_SPECIFIC_COLOR;
Static.color_mode = MODE_COLORS_PER_LED;
modes.push_back(Static);
SetupZones();
}
RGBController_ElgatoKeyLight::~RGBController_ElgatoKeyLight()
{
delete controller;
}
void RGBController_ElgatoKeyLight::SetupZones()
{
zone led_zone;
led_zone.name = "Keylight";
led_zone.type = ZONE_TYPE_SINGLE;
led_zone.leds_min = 1;
led_zone.leds_max = 1;
led_zone.leds_count = 1;
led_zone.matrix_map = NULL;
zones.push_back(led_zone);
led new_led;
new_led.name = "Keylight";
leds.push_back(new_led);
SetupColors();
}
void RGBController_ElgatoKeyLight::ResizeZone(int /*zone*/, int /*new_size*/)
{
/*---------------------------------------------------------*\
| This device does not support resizing zones |
\*---------------------------------------------------------*/
}
void RGBController_ElgatoKeyLight::DeviceUpdateLEDs()
{
RGBColor rgb_color = colors[0];
hsv_t hsv_color;
rgb2hsv(rgb_color, &hsv_color);
controller->SetColor(hsv_color);
}
void RGBController_ElgatoKeyLight::UpdateZoneLEDs(int /*zone*/)
{
DeviceUpdateLEDs();
}
void RGBController_ElgatoKeyLight::UpdateSingleLED(int /*led*/)
{
DeviceUpdateLEDs();
}
void RGBController_ElgatoKeyLight::DeviceUpdateMode()
{
}

View file

@ -0,0 +1,31 @@
/*--------------------------------------------------*\
| RGBController_ElgatoKeyLight.h |
| |
| Generic RGB Interface for Elgato KeyLight |
| |
| Monks (imtherealestmonkey@gmail.com) 11/1/2021 |
\*--------------------------------------------------*/
#pragma once
#include "RGBController.h"
#include "ElgatoKeyLightController.h"
class RGBController_ElgatoKeyLight : public RGBController
{
public:
RGBController_ElgatoKeyLight(ElgatoKeyLightController* controller_ptr);
~RGBController_ElgatoKeyLight();
void SetupZones();
void ResizeZone(int zone, int new_size);
void DeviceUpdateLEDs();
void UpdateZoneLEDs(int zone);
void UpdateSingleLED(int led);
void DeviceUpdateMode();
private:
ElgatoKeyLightController* controller;
};