Initial DMX (Enttec OpenDMX USB) support and serial_port improvements

* Support DMX RGB lights (PAR lights, spotlights, wash lights, etc)
  * Configurable R/G/B channel and Brightness/Master channel
  * Add configurable parameters to serial_port needed to configure a port for DMX
  * Add DMX tab to settings
This commit is contained in:
Adam Honse 2023-05-06 08:06:19 +00:00
parent 8b4b2bacbc
commit 81aaf67ff0
14 changed files with 1318 additions and 48 deletions

View file

@ -0,0 +1,142 @@
#include "Detector.h"
#include "RGBController.h"
#include "RGBController_DMX.h"
#include "SettingsManager.h"
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <string>
/******************************************************************************************\
* *
* DetectDMXControllers *
* *
* Detect devices supported by the DMX driver *
* *
\******************************************************************************************/
void DetectDMXControllers()
{
json dmx_settings;
std::vector<std::vector<DMXDevice>> device_lists;
DMXDevice dev;
/*-------------------------------------------------*\
| Get DMX settings from settings manager |
\*-------------------------------------------------*/
dmx_settings = ResourceManager::get()->GetSettingsManager()->GetSettings("DMXDevices");
/*-------------------------------------------------*\
| If the DMX settings contains devices, process |
\*-------------------------------------------------*/
if(dmx_settings.contains("devices"))
{
for(unsigned int device_idx = 0; device_idx < dmx_settings["devices"].size(); device_idx++)
{
/*-------------------------------------------------*\
| Clear DMX device data |
\*-------------------------------------------------*/
dev.name = "";
dev.keepalive_time = 0;
if(dmx_settings["devices"][device_idx].contains("name"))
{
dev.name = dmx_settings["devices"][device_idx]["name"];
}
if(dmx_settings["devices"][device_idx].contains("port"))
{
dev.port = dmx_settings["devices"][device_idx]["port"];
}
if(dmx_settings["devices"][device_idx].contains("keepalive_time"))
{
dev.keepalive_time = dmx_settings["devices"][device_idx]["keepalive_time"];
}
if(dmx_settings["devices"][device_idx].contains("red_channel"))
{
dev.red_channel = dmx_settings["devices"][device_idx]["red_channel"];
}
if(dmx_settings["devices"][device_idx].contains("green_channel"))
{
dev.green_channel = dmx_settings["devices"][device_idx]["green_channel"];
}
if(dmx_settings["devices"][device_idx].contains("blue_channel"))
{
dev.blue_channel = dmx_settings["devices"][device_idx]["blue_channel"];
}
if(dmx_settings["devices"][device_idx].contains("brightness_channel"))
{
dev.brightness_channel = dmx_settings["devices"][device_idx]["brightness_channel"];
}
/*---------------------------------------------------------*\
| Determine whether to create a new list or add this device |
| to an existing list. A device is added to an existing |
| list if both devices share one or more universes for the |
| same output destination |
\*---------------------------------------------------------*/
bool device_added_to_existing_list = false;
/*---------------------------------------------------------*\
| Track grouping for all controllers. |
\*---------------------------------------------------------*/
for(unsigned int list_idx = 0; list_idx < device_lists.size(); list_idx++)
{
for(unsigned int device_idx = 0; device_idx < device_lists[list_idx].size(); device_idx++)
{
/*---------------------------------------------------------*\
| Check if the port used by this new device is the same as |
| in the existing device. If so, add the new device to the |
| existing list. |
\*---------------------------------------------------------*/
if(1)
{
device_lists[list_idx].push_back(dev);
device_added_to_existing_list = true;
break;
}
}
if(device_added_to_existing_list)
{
break;
}
}
/*---------------------------------------------------------*\
| If the device did not overlap with existing devices, |
| create a new list for it |
\*---------------------------------------------------------*/
if(!device_added_to_existing_list)
{
std::vector<DMXDevice> new_list;
new_list.push_back(dev);
device_lists.push_back(new_list);
}
}
for(unsigned int list_idx = 0; list_idx < device_lists.size(); list_idx++)
{
RGBController_DMX* rgb_controller;
rgb_controller = new RGBController_DMX(device_lists[list_idx]);
ResourceManager::get()->RegisterRGBController(rgb_controller);
}
}
} /* DetectDMXControllers() */
REGISTER_DETECTOR("DMX", DetectDMXControllers);

View file

@ -0,0 +1,233 @@
/*-----------------------------------------*\
| RGBController_DMX.cpp |
| |
| Generic RGB Interface for OpenAuraSDK |
| DMX interface |
| |
| Adam Honse (CalcProgrammer1) 4/30/2023 |
\*-----------------------------------------*/
#include "RGBController_DMX.h"
#include <math.h>
#include "LogManager.h"
using namespace std::chrono_literals;
/**------------------------------------------------------------------*\
@name DMX Devices
@category LEDStrip
@type Serial
@save :x:
@direct :white_check_mark:
@effects :x:
@detectors DetectDMXControllers
@comment
\*-------------------------------------------------------------------*/
RGBController_DMX::RGBController_DMX(std::vector<DMXDevice> device_list)
{
devices = device_list;
name = "DMX Device Group";
type = DEVICE_TYPE_LEDSTRIP;
description = "DMX Device";
location = "DMX: " + devices[0].port;
/*-----------------------------------------*\
| If this controller only represents a |
| single device, use the device name for the|
| controller name |
\*-----------------------------------------*/
if(devices.size() == 1)
{
name = devices[0].name;
}
/*-----------------------------------------*\
| Open OpenDMX port |
\*-----------------------------------------*/
port = new serial_port(devices[0].port.c_str(), 250000, SERIAL_PORT_PARITY_NONE, SERIAL_PORT_SIZE_8, SERIAL_PORT_STOP_BITS_2, false);
/*-----------------------------------------*\
| Clear the RTS signal, which enables the |
| OpenDMX RS-485 drive enable |
\*-----------------------------------------*/
port->serial_set_rts(false);
/*-----------------------------------------*\
| Set up modes |
\*-----------------------------------------*/
mode Direct;
Direct.name = "Direct";
Direct.value = 0;
Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR | MODE_FLAG_HAS_BRIGHTNESS;
Direct.color_mode = MODE_COLORS_PER_LED;
Direct.brightness = 255;
Direct.brightness_min = 0;
Direct.brightness_max = 255;
modes.push_back(Direct);
keepalive_delay = 0ms;
SetupZones();
for (std::size_t device_idx = 0; device_idx < devices.size(); device_idx++)
{
/*-----------------------------------------*\
| Update keepalive delay |
\*-----------------------------------------*/
if(devices[device_idx].keepalive_time > 0)
{
if(keepalive_delay.count() == 0 || keepalive_delay.count() > devices[device_idx].keepalive_time)
{
keepalive_delay = std::chrono::milliseconds(devices[device_idx].keepalive_time);
}
}
}
if(keepalive_delay.count() > 0)
{
keepalive_thread_run = 1;
keepalive_thread = new std::thread(&RGBController_DMX::KeepaliveThreadFunction, this);
}
else
{
keepalive_thread_run = 0;
keepalive_thread = nullptr;
}
}
RGBController_DMX::~RGBController_DMX()
{
if(keepalive_thread != nullptr)
{
keepalive_thread_run = 0;
keepalive_thread->join();
delete keepalive_thread;
}
/*---------------------------------------------------------*\
| Delete the matrix map |
\*---------------------------------------------------------*/
for(unsigned int zone_index = 0; zone_index < zones.size(); zone_index++)
{
if(zones[zone_index].matrix_map != NULL)
{
if(zones[zone_index].matrix_map->map != NULL)
{
delete zones[zone_index].matrix_map->map;
}
delete zones[zone_index].matrix_map;
}
}
}
void RGBController_DMX::SetupZones()
{
/*-----------------------------------------*\
| Add Zones |
\*-----------------------------------------*/
for(std::size_t zone_idx = 0; zone_idx < devices.size(); zone_idx++)
{
zone led_zone;
led_zone.name = devices[zone_idx].name;
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);
}
/*-----------------------------------------*\
| Add LEDs |
\*-----------------------------------------*/
for(std::size_t zone_idx = 0; zone_idx < zones.size(); zone_idx++)
{
for(std::size_t led_idx = 0; led_idx < zones[zone_idx].leds_count; led_idx++)
{
led new_led;
new_led.name = zones[zone_idx].name + " LED ";
new_led.name.append(std::to_string(led_idx));
leds.push_back(new_led);
}
}
SetupColors();
}
void RGBController_DMX::ResizeZone(int /*zone*/, int /*new_size*/)
{
/*---------------------------------------------------------*\
| This device does not support resizing zones |
\*---------------------------------------------------------*/
}
void RGBController_DMX::DeviceUpdateLEDs()
{
int color_idx = 0;
last_update_time = std::chrono::steady_clock::now();
unsigned char dmx_data[513];
memset(dmx_data, 0, sizeof(dmx_data));
for(unsigned int device_idx = 0; device_idx < devices.size(); device_idx++)
{
if(devices[device_idx].brightness_channel > 0)
{
dmx_data[devices[device_idx].brightness_channel] = modes[0].brightness;
}
if(devices[device_idx].red_channel > 0)
{
dmx_data[devices[device_idx].red_channel] = RGBGetRValue(colors[device_idx]);
}
if(devices[device_idx].green_channel > 0)
{
dmx_data[devices[device_idx].green_channel] = RGBGetGValue(colors[device_idx]);
}
if(devices[device_idx].blue_channel > 0)
{
dmx_data[devices[device_idx].blue_channel] = RGBGetBValue(colors[device_idx]);
}
}
port->serial_break();
port->serial_write((char*)&dmx_data, sizeof(dmx_data));
}
void RGBController_DMX::UpdateZoneLEDs(int /*zone*/)
{
DeviceUpdateLEDs();
}
void RGBController_DMX::UpdateSingleLED(int /*led*/)
{
DeviceUpdateLEDs();
}
void RGBController_DMX::DeviceUpdateMode()
{
}
void RGBController_DMX::KeepaliveThreadFunction()
{
while(keepalive_thread_run.load())
{
if((std::chrono::steady_clock::now() - last_update_time) > ( keepalive_delay * 0.95f ) )
{
UpdateLEDs();
}
std::this_thread::sleep_for(keepalive_delay / 2);
}
}

View file

@ -0,0 +1,52 @@
/*-----------------------------------------*\
| RGBController_DMX.h |
| |
| Generic RGB Interface for OpenAuraSDK |
| DMX interface |
| |
| Adam Honse (CalcProgrammer1) 4/30/2023 |
\*-----------------------------------------*/
#pragma once
#include "RGBController.h"
#include "serial_port.h"
#include <chrono>
#include <thread>
struct DMXDevice
{
std::string name;
std::string port;
unsigned int keepalive_time;
unsigned int red_channel;
unsigned int green_channel;
unsigned int blue_channel;
unsigned int brightness_channel;
};
class RGBController_DMX : public RGBController
{
public:
RGBController_DMX(std::vector<DMXDevice> device_list);
~RGBController_DMX();
void SetupZones();
void ResizeZone(int zone, int new_size);
void DeviceUpdateLEDs();
void UpdateZoneLEDs(int zone);
void UpdateSingleLED(int led);
void DeviceUpdateMode();
void KeepaliveThreadFunction();
private:
std::vector<DMXDevice> devices;
serial_port * port;
std::thread * keepalive_thread;
std::atomic<bool> keepalive_thread_run;
std::chrono::milliseconds keepalive_delay;
std::chrono::time_point<std::chrono::steady_clock> last_update_time;
};