Add a thread to RGBController to asynchronously perform device updates. Only implemented for UpdateLEDs for now

This commit is contained in:
Adam Honse 2020-04-25 23:45:48 -05:00
parent 4d6706ce61
commit e3e6c0347e
72 changed files with 206 additions and 112 deletions

View file

@ -1,6 +1,55 @@
#include "RGBController.h"
#include <cstring>
//Include thread libraries for Windows or Linux
#ifdef WIN32
#include <process.h>
#else
#include "pthread.h"
#include "unistd.h"
#endif
//Thread functions have different types in Windows and Linux
#ifdef WIN32
#define THREAD static void
#define THREADRETURN
#else
#define THREAD static void*
#define THREADRETURN return(NULL);
#endif
#ifdef WIN32
#include <Windows.h>
#else
#include <unistd.h>
static void Sleep(unsigned int milliseconds)
{
usleep(1000 * milliseconds);
}
#endif
THREAD devicecallthread_thread(void *param)
{
RGBController* controller = static_cast<RGBController*>(param);
controller->DeviceCallThread();
THREADRETURN
}
RGBController::RGBController()
{
/*-----------------------------------------------------*\
| The RGBController class starts a thread to handle |
| asynchronous device updating |
\*-----------------------------------------------------*/
#ifdef WIN32
_beginthread(keepalive_thread, 0, this);
#else
pthread_t thread;
pthread_create(&thread, NULL, &devicecallthread_thread, this);
#endif
}
unsigned char * RGBController::GetDeviceDescription()
{
unsigned int data_ptr = 0;
@ -1179,6 +1228,33 @@ void RGBController::SetMode(int mode)
UpdateMode();
}
void RGBController::UpdateLEDs()
{
CallFlag_UpdateLEDs = true;
}
void RGBController::DeviceUpdateLEDs()
{
}
void RGBController::DeviceCallThread()
{
CallFlag_UpdateLEDs = false;
while(1)
{
if(CallFlag_UpdateLEDs)
{
DeviceUpdateLEDs();
CallFlag_UpdateLEDs = false;
}
else
{
Sleep(1);
}
}
}
std::string device_type_to_str(device_type type)
{
switch(type)