diff --git a/RGBController/E131ControllerDetect.cpp b/RGBController/E131ControllerDetect.cpp index fbeb9dca..78d7228a 100644 --- a/RGBController/E131ControllerDetect.cpp +++ b/RGBController/E131ControllerDetect.cpp @@ -56,6 +56,9 @@ void DetectE131Controllers(std::vector &rgb_controllers) bool new_device = false; + //Clear E131 device data + memset(&dev, 0, sizeof(E131Device)); + //Open settings file infile.open(filename); @@ -105,6 +108,10 @@ void DetectE131Controllers(std::vector &rgb_controllers) { dev.start_channel = atoi(value); } + else if(strcmp(argument, "keepalive_time") == 0) + { + dev.keepalive_time = atoi(value); + } else if(strcmp(argument, "rgb_order") == 0) { if(strcmp(value, "RGB") == 0) diff --git a/RGBController/RGBController_E131.cpp b/RGBController/RGBController_E131.cpp index 7e8ca353..5f292cf2 100644 --- a/RGBController/RGBController_E131.cpp +++ b/RGBController/RGBController_E131.cpp @@ -11,6 +11,8 @@ #include #include +using namespace std::chrono_literals; + RGBController_E131::RGBController_E131(std::vector device_list) { name = "E1.31 Streaming ACN Device"; @@ -28,10 +30,23 @@ RGBController_E131::RGBController_E131(std::vector device_list) sockfd = e131_socket(); + 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); + } + } + /*-----------------------------------------*\ | Add Universes | \*-----------------------------------------*/ @@ -162,6 +177,11 @@ RGBController_E131::RGBController_E131(std::vector device_list) zones[device_idx].matrix_map = new_map; } } + + if(keepalive_delay.count() > 0) + { + KeepaliveThread = new std::thread(&RGBController_E131::KeepaliveThreadFunction, this); + } } void RGBController_E131::SetupZones() @@ -212,6 +232,8 @@ void RGBController_E131::DeviceUpdateLEDs() { int color_idx = 0; + last_update_time = std::chrono::steady_clock::now(); + for(std::size_t device_idx = 0; device_idx < devices.size(); device_idx++) { unsigned int total_universes = ceil( ( ( devices[device_idx].num_leds * 3 ) + devices[device_idx].start_channel ) / 512.0f ); @@ -288,3 +310,15 @@ void RGBController_E131::DeviceUpdateMode() { } + +void RGBController_E131::KeepaliveThreadFunction() +{ + while(1) + { + if((std::chrono::steady_clock::now() - last_update_time) > ( keepalive_delay * 0.95f ) ) + { + UpdateLEDs(); + } + std::this_thread::sleep_for(keepalive_delay / 2); + } +} diff --git a/RGBController/RGBController_E131.h b/RGBController/RGBController_E131.h index 660f911e..f149d93a 100644 --- a/RGBController/RGBController_E131.h +++ b/RGBController/RGBController_E131.h @@ -10,6 +10,8 @@ #pragma once #include "RGBController.h" #include +#include +#include typedef unsigned int e131_rgb_order; @@ -43,6 +45,7 @@ struct E131Device unsigned int num_leds; unsigned int start_universe; unsigned int start_channel; + unsigned int keepalive_time; e131_rgb_order rgb_order; zone_type type; unsigned int matrix_width; @@ -66,10 +69,15 @@ public: void SetCustomMode(); void DeviceUpdateMode(); + void KeepaliveThreadFunction(); + private: std::vector devices; std::vector packets; std::vector dest_addrs; std::vector universes; int sockfd; + std::thread * KeepaliveThread; + std::chrono::milliseconds keepalive_delay; + std::chrono::time_point last_update_time; };