Initial LIFX commit
This commit is contained in:
parent
069e07c690
commit
0e750b6e9e
14 changed files with 715 additions and 0 deletions
131
Controllers/LIFXController/LIFXController.cpp
Normal file
131
Controllers/LIFXController/LIFXController.cpp
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
/*---------------------------------------------------------*\
|
||||
| Driver for LIFX |
|
||||
| |
|
||||
| Adam Honse (calcprogrammer1@gmail.com), 2/5/2022 |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include "LIFXController.h"
|
||||
#include "json.hpp"
|
||||
#include "hsv.h"
|
||||
|
||||
using json = nlohmann::json;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
LIFXController::LIFXController(std::string ip)
|
||||
{
|
||||
/*-----------------------------------------------------------------*\
|
||||
| Fill in location string with device's IP address |
|
||||
\*-----------------------------------------------------------------*/
|
||||
location = "IP: " + ip;
|
||||
|
||||
/*-----------------------------------------------------------------*\
|
||||
| Open a UDP client sending to the device's IP, port 56700 |
|
||||
\*-----------------------------------------------------------------*/
|
||||
port.udp_client(ip.c_str(), "56700");
|
||||
}
|
||||
|
||||
LIFXController::~LIFXController()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string LIFXController::GetLocation()
|
||||
{
|
||||
return(location);
|
||||
}
|
||||
|
||||
std::string LIFXController::GetName()
|
||||
{
|
||||
return("LIFX");
|
||||
}
|
||||
|
||||
std::string LIFXController::GetVersion()
|
||||
{
|
||||
return(module_name + " " + firmware_version);
|
||||
}
|
||||
|
||||
std::string LIFXController::GetManufacturer()
|
||||
{
|
||||
return("LIFX");
|
||||
}
|
||||
|
||||
std::string LIFXController::GetUniqueID()
|
||||
{
|
||||
return(module_mac);
|
||||
}
|
||||
|
||||
void LIFXController::SetColor(unsigned char red, unsigned char green, unsigned char blue)
|
||||
{
|
||||
RGBColor color = ToRGBColor(red, green, blue);
|
||||
hsv_t hsv;
|
||||
rgb2hsv(color, &hsv);
|
||||
|
||||
data = data_buf;
|
||||
memset( data, 0, 49 );
|
||||
|
||||
source = 2;
|
||||
sequence = 1;
|
||||
|
||||
unsigned char target[8] = {0};
|
||||
|
||||
FrameHeader( 49, true, false, 0, source );
|
||||
FrameAddress( target, false, false, sequence );
|
||||
ProtocolAddress( 102 );
|
||||
|
||||
unsigned char * set_color = &data[36];
|
||||
|
||||
unsigned short hue = hsv.hue * (65536/360);
|
||||
unsigned short saturation = hsv.saturation * (65536/256);
|
||||
unsigned short brightness = hsv.value * (65536/256);
|
||||
unsigned short kelvin = 3500;
|
||||
unsigned int duration = 0;
|
||||
|
||||
memcpy(&set_color[LIFX_SET_COLOR_OFFSET_HUE], &hue, sizeof(unsigned short));
|
||||
memcpy(&set_color[LIFX_SET_COLOR_OFFSET_SATURATION], &saturation, sizeof(unsigned short));
|
||||
memcpy(&set_color[LIFX_SET_COLOR_OFFSET_BRIGHTNESS], &brightness, sizeof(unsigned short));
|
||||
memcpy(&set_color[LIFX_SET_COLOR_OFFSET_KELVIN], &kelvin, sizeof(unsigned short));
|
||||
memcpy(&set_color[LIFX_SET_COLOR_OFFSET_DURATION], &duration, sizeof(unsigned int));
|
||||
|
||||
port.udp_write((char *)data, 49);
|
||||
}
|
||||
|
||||
void LIFXController::FrameHeader
|
||||
(
|
||||
unsigned short size,
|
||||
bool addressable,
|
||||
bool tagged,
|
||||
unsigned char origin,
|
||||
unsigned int source
|
||||
)
|
||||
{
|
||||
unsigned short protocol = 1024;
|
||||
memcpy(&data[LIFX_FRAME_HEADER_OFFSET_SIZE], &size, sizeof(unsigned short));
|
||||
memcpy(&data[LIFX_FRAME_HEADER_OFFSET_PROTOCOL], &protocol, sizeof(unsigned short));
|
||||
if(addressable) data[LIFX_FRAME_HEADER_OFFSET_FLAGS] |= (1 << 4);
|
||||
if(tagged) data[LIFX_FRAME_HEADER_OFFSET_FLAGS] |= (1 << 5);
|
||||
data[LIFX_FRAME_HEADER_OFFSET_FLAGS] |= origin << 6;
|
||||
memcpy(&data[LIFX_FRAME_HEADER_OFFSET_SOURCE], &source, sizeof(unsigned int));
|
||||
}
|
||||
|
||||
void LIFXController::FrameAddress
|
||||
(
|
||||
unsigned char * target,
|
||||
bool res_required,
|
||||
bool ack_required,
|
||||
unsigned char sequence
|
||||
)
|
||||
{
|
||||
memcpy(&data[LIFX_FRAME_ADDRESS_OFFSET_TARGET], target, 8);
|
||||
|
||||
data[LIFX_FRAME_ADDRESS_OFFSET_FLAGS] = (1 << 0) & res_required;
|
||||
data[LIFX_FRAME_ADDRESS_OFFSET_FLAGS] |= (1 << 1) & ack_required;
|
||||
data[LIFX_FRAME_ADDRESS_OFFSET_SEQUENCE] = sequence;
|
||||
}
|
||||
|
||||
void LIFXController::ProtocolAddress
|
||||
(
|
||||
unsigned short type
|
||||
)
|
||||
{
|
||||
memcpy(&data[LIFX_PROTOCOL_HEADER_OFFSET_TYPE], &type, sizeof(unsigned short));
|
||||
}
|
||||
91
Controllers/LIFXController/LIFXController.h
Normal file
91
Controllers/LIFXController/LIFXController.h
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/*---------------------------------------------------------*\
|
||||
| Definitions for LIFX |
|
||||
| |
|
||||
| Adam Honse (calcprogrammer1@gmail.com), 2/5/2022 |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include "RGBController.h"
|
||||
#include "net_port.h"
|
||||
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#pragma once
|
||||
|
||||
enum
|
||||
{
|
||||
LIFX_FRAME_HEADER_OFFSET_SIZE = 0, /* 2 bytes, size of the entire message in bytes */
|
||||
LIFX_FRAME_HEADER_OFFSET_PROTOCOL = 2, /* Protocol number, must be 1024 */
|
||||
LIFX_FRAME_HEADER_OFFSET_FLAGS = 3, /* Bits 0-3 are part of Protocol */
|
||||
/* Bit 4, addressable flag */
|
||||
/* Bit 5, tagged flag */
|
||||
/* Bit 6/7, origin value */
|
||||
LIFX_FRAME_HEADER_OFFSET_SOURCE = 4, /* Source identifier, unique value set by client*/
|
||||
LIFX_FRAME_ADDRESS_OFFSET_TARGET = 8, /* 6 byte device address (MAC) or zero */
|
||||
/* Last two bytes should be 0 */
|
||||
LIFX_FRAME_ADDRESS_OFFSET_FLAGS = 22, /* Bit 0, res_required flag */
|
||||
/* Bit 1, ack_required flag */
|
||||
LIFX_FRAME_ADDRESS_OFFSET_SEQUENCE = 23, /* Wrap around message sequence number */
|
||||
LIFX_PROTOCOL_HEADER_OFFSET_TYPE = 32, /* Message type determines the payload used */
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
LIFX_SET_COLOR_OFFSET_HUE = 1, /* 16-bit hue value */
|
||||
LIFX_SET_COLOR_OFFSET_SATURATION = 3, /* 16-bit saturation value */
|
||||
LIFX_SET_COLOR_OFFSET_BRIGHTNESS = 5, /* 16-bit brightness value */
|
||||
LIFX_SET_COLOR_OFFSET_KELVIN = 7, /* 16-bit kelvin value */
|
||||
LIFX_SET_COLOR_OFFSET_DURATION = 9, /* 32-bit brightness value */
|
||||
};
|
||||
|
||||
class LIFXController
|
||||
{
|
||||
public:
|
||||
LIFXController(std::string ip);
|
||||
~LIFXController();
|
||||
|
||||
std::string GetLocation();
|
||||
std::string GetName();
|
||||
std::string GetVersion();
|
||||
std::string GetManufacturer();
|
||||
std::string GetUniqueID();
|
||||
|
||||
void SetColor(unsigned char red, unsigned char green, unsigned char blue);
|
||||
|
||||
private:
|
||||
unsigned char data_buf[49];
|
||||
unsigned char* data;
|
||||
unsigned char sequence;
|
||||
unsigned int source;
|
||||
std::string firmware_version;
|
||||
std::string module_name;
|
||||
std::string module_mac;
|
||||
std::string location;
|
||||
net_port port;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Functions for filling in LIFX header |
|
||||
\*-----------------------------------------------------*/
|
||||
void FrameHeader
|
||||
(
|
||||
unsigned short size,
|
||||
bool addressable,
|
||||
bool tagged,
|
||||
unsigned char origin,
|
||||
unsigned int source
|
||||
);
|
||||
|
||||
void FrameAddress
|
||||
(
|
||||
unsigned char * target,
|
||||
bool res_required,
|
||||
bool ack_required,
|
||||
unsigned char sequence
|
||||
);
|
||||
|
||||
void ProtocolAddress
|
||||
(
|
||||
unsigned short type
|
||||
);
|
||||
};
|
||||
48
Controllers/LIFXController/LIFXControllerDetect.cpp
Normal file
48
Controllers/LIFXController/LIFXControllerDetect.cpp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include "Detector.h"
|
||||
#include "LIFXController.h"
|
||||
#include "RGBController.h"
|
||||
#include "RGBController_LIFX.h"
|
||||
#include "SettingsManager.h"
|
||||
#include <vector>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/******************************************************************************************\
|
||||
* *
|
||||
* DetectLIFXControllers *
|
||||
* *
|
||||
* Detect LIFX devices *
|
||||
* *
|
||||
\******************************************************************************************/
|
||||
|
||||
void DetectLIFXControllers(std::vector<RGBController*> &rgb_controllers)
|
||||
{
|
||||
json lifx_settings;
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Get LIFX settings from settings manager |
|
||||
\*-------------------------------------------------*/
|
||||
lifx_settings = ResourceManager::get()->GetSettingsManager()->GetSettings("LIFXDevices");
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| If the Wiz settings contains devices, process |
|
||||
\*-------------------------------------------------*/
|
||||
if(lifx_settings.contains("devices"))
|
||||
{
|
||||
for(unsigned int device_idx = 0; device_idx < lifx_settings["devices"].size(); device_idx++)
|
||||
{
|
||||
if(lifx_settings["devices"][device_idx].contains("ip"))
|
||||
{
|
||||
std::string lifx_ip = lifx_settings["devices"][device_idx]["ip"];
|
||||
|
||||
LIFXController* controller = new LIFXController(lifx_ip);
|
||||
RGBController_LIFX* rgb_controller = new RGBController_LIFX(controller);
|
||||
|
||||
rgb_controllers.push_back(rgb_controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} /* DetectLIFXControllers() */
|
||||
|
||||
REGISTER_DETECTOR("LIFX", DetectLIFXControllers);
|
||||
91
Controllers/LIFXController/RGBController_LIFX.cpp
Normal file
91
Controllers/LIFXController/RGBController_LIFX.cpp
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/*-----------------------------------------*\
|
||||
| RGBController_LIFX.cpp |
|
||||
| |
|
||||
| Generic RGB Interface for LIFX |
|
||||
| |
|
||||
| Adam Honse (CalcProgrammer1) 2/5/2022 |
|
||||
\*-----------------------------------------*/
|
||||
|
||||
#include "RGBController_LIFX.h"
|
||||
|
||||
RGBController_LIFX::RGBController_LIFX(LIFXController* controller_ptr)
|
||||
{
|
||||
controller = controller_ptr;
|
||||
|
||||
name = controller->GetManufacturer() + " " + controller->GetName();
|
||||
vendor = controller->GetManufacturer();
|
||||
type = DEVICE_TYPE_LIGHT;
|
||||
version = controller->GetVersion();
|
||||
description = "LIFX Device";
|
||||
serial = controller->GetUniqueID();
|
||||
location = controller->GetLocation();
|
||||
|
||||
mode Direct;
|
||||
Direct.name = "Direct";
|
||||
Direct.value = 0;
|
||||
Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR;
|
||||
Direct.color_mode = MODE_COLORS_PER_LED;
|
||||
modes.push_back(Direct);
|
||||
|
||||
SetupZones();
|
||||
}
|
||||
|
||||
RGBController_LIFX::~RGBController_LIFX()
|
||||
{
|
||||
delete controller;
|
||||
}
|
||||
|
||||
void RGBController_LIFX::SetupZones()
|
||||
{
|
||||
zone led_zone;
|
||||
led_zone.name = "RGB Light";
|
||||
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 = "RGB Light";
|
||||
|
||||
leds.push_back(new_led);
|
||||
|
||||
SetupColors();
|
||||
}
|
||||
|
||||
void RGBController_LIFX::ResizeZone(int /*zone*/, int /*new_size*/)
|
||||
{
|
||||
/*---------------------------------------------------------*\
|
||||
| This device does not support resizing zones |
|
||||
\*---------------------------------------------------------*/
|
||||
}
|
||||
|
||||
void RGBController_LIFX::DeviceUpdateLEDs()
|
||||
{
|
||||
unsigned char red = RGBGetRValue(colors[0]);
|
||||
unsigned char grn = RGBGetGValue(colors[0]);
|
||||
unsigned char blu = RGBGetBValue(colors[0]);
|
||||
|
||||
controller->SetColor(red, grn, blu);
|
||||
}
|
||||
|
||||
void RGBController_LIFX::UpdateZoneLEDs(int /*zone*/)
|
||||
{
|
||||
DeviceUpdateLEDs();
|
||||
}
|
||||
|
||||
void RGBController_LIFX::UpdateSingleLED(int /*led*/)
|
||||
{
|
||||
DeviceUpdateLEDs();
|
||||
}
|
||||
|
||||
void RGBController_LIFX::SetCustomMode()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void RGBController_LIFX::DeviceUpdateMode()
|
||||
{
|
||||
|
||||
}
|
||||
32
Controllers/LIFXController/RGBController_LIFX.h
Normal file
32
Controllers/LIFXController/RGBController_LIFX.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/*-----------------------------------------*\
|
||||
| RGBController_LIFX.h |
|
||||
| |
|
||||
| Generic RGB Interface for LIFX |
|
||||
| |
|
||||
| Adam Honse (CalcProgrammer1) 2/5/2022 |
|
||||
\*-----------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
#include "RGBController.h"
|
||||
#include "LIFXController.h"
|
||||
|
||||
class RGBController_LIFX : public RGBController
|
||||
{
|
||||
public:
|
||||
RGBController_LIFX(LIFXController* controller_ptr);
|
||||
~RGBController_LIFX();
|
||||
|
||||
void SetupZones();
|
||||
|
||||
void ResizeZone(int zone, int new_size);
|
||||
|
||||
void DeviceUpdateLEDs();
|
||||
void UpdateZoneLEDs(int zone);
|
||||
void UpdateSingleLED(int led);
|
||||
|
||||
void SetCustomMode();
|
||||
void DeviceUpdateMode();
|
||||
|
||||
private:
|
||||
LIFXController* controller;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue