Some code cleanup - set colors vector size for improved performance and add sleep to fix Hue Plus device update

master
Adam Honse 6 years ago
parent 7226a4164f
commit 4d5003f49d

@ -48,30 +48,29 @@ char* HuePlusController::GetLEDString()
unsigned int HuePlusController::GetStripsOnChannel(unsigned int channel)
{
unsigned int ret_val = 0;
if (serialport != NULL)
unsigned char serial_buf[] =
{
unsigned char *serial_buf;
serial_buf = new unsigned char[5];
0x8D, 0x00, 0x00, 0x00, 0x00
};
serial_buf[0] = 0x8D;
serial_buf[1] = channel;
unsigned int ret_val = 0;
serialport->serial_flush_rx();
serialport->serial_write((char *)serial_buf, 2);
serialport->serial_flush_tx();
/*-----------------------------------------------------*\
| Set channel in serial packet |
\*-----------------------------------------------------*/
serial_buf[0x01] = channel;
Sleep(50);
serialport->serial_flush_rx();
serialport->serial_write((char *)serial_buf, 2);
serialport->serial_flush_tx();
int bytes_read = serialport->serial_read((char *)serial_buf, 5);
Sleep(50);
if(bytes_read == 5)
{
ret_val = serial_buf[4];
}
int bytes_read = serialport->serial_read((char *)serial_buf, 5);
delete[] serial_buf;
if(bytes_read == 5)
{
ret_val = serial_buf[4];
}
return(ret_val);
@ -114,7 +113,6 @@ void HuePlusController::SetChannelEffect(unsigned int channel, unsigned int mode
0x00, 0x00, 0x00, 0x00,
0x00
};
int actual = 0;
/*-----------------------------------------------------*\
| Set channel in serial packet |
@ -140,40 +138,72 @@ void HuePlusController::SetChannelEffect(unsigned int channel, unsigned int mode
serialport->serial_write((char *)serial_buf, HUE_PLUS_PACKET_SIZE);
serialport->serial_flush_tx();
Sleep(1);
}
void HuePlusController::SetChannelLEDs(unsigned int channel, std::vector<RGBColor> colors)
{
if (serialport != NULL)
unsigned char serial_buf[] =
{
unsigned char *serial_buf;
serial_buf = new unsigned char[HUE_PLUS_PACKET_SIZE];
serial_buf[0] = 0x4B;
serial_buf[1] = channel;
serial_buf[2] = HUE_PLUS_MODE_FIXED;
serial_buf[3] = 0x00;
serial_buf[4] = 0x00;
for (int i = 5; i < HUE_PLUS_PACKET_SIZE; i++)
{
serial_buf[i] = 0x00;
}
for (unsigned int idx = 0; idx < (colors.size() * 3); idx += 3)
{
int pixel_idx = idx / 3;
RGBColor color = colors[pixel_idx];
serial_buf[idx + 5] = RGBGetGValue(color);
serial_buf[idx + 6] = RGBGetRValue(color);
serial_buf[idx + 7] = RGBGetBValue(color);
}
serialport->serial_write((char *)serial_buf, HUE_PLUS_PACKET_SIZE);
serialport->serial_flush_tx();
delete[] serial_buf;
0x4B, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00
};
/*-----------------------------------------------------*\
| Set channel in serial packet |
\*-----------------------------------------------------*/
serial_buf[0x01] = channel;
/*-----------------------------------------------------*\
| Set mode in serial packet |
\*-----------------------------------------------------*/
serial_buf[0x02] = HUE_PLUS_MODE_FIXED;
/*-----------------------------------------------------*\
| Fill in color data |
\*-----------------------------------------------------*/
for (unsigned int idx = 0; idx < (colors.size() * 3); idx += 3)
{
int pixel_idx = idx / 3;
RGBColor color = colors[pixel_idx];
serial_buf[idx + 5] = RGBGetGValue(color);
serial_buf[idx + 6] = RGBGetRValue(color);
serial_buf[idx + 7] = RGBGetBValue(color);
}
serialport->serial_write((char *)serial_buf, HUE_PLUS_PACKET_SIZE);
serialport->serial_flush_tx();
Sleep(1);
}

@ -141,6 +141,8 @@ RGBController_Aura::RGBController_Aura(AuraController * aura_ptr)
modes.push_back(aura_modes[i]);
}
colors.resize(aura->GetLEDCount());
for (std::size_t i = 0; i < aura->GetLEDCount(); i++)
{
aura_channels.push_back(aura->GetChannel(i));
@ -155,7 +157,7 @@ RGBController_Aura::RGBController_Aura(AuraController * aura_ptr)
unsigned char grn = aura->GetLEDGreen(i);
unsigned char blu = aura->GetLEDBlue(i);
colors.push_back(ToRGBColor(red, grn, blu));
colors[i] = ToRGBColor(red, grn, blu);
}
std::vector<unsigned char> aura_zones;

@ -113,6 +113,8 @@ RGBController_CorsairPro::RGBController_CorsairPro(CorsairProController* corsair
modes.push_back(corsair_modes[i]);
}
colors.resize(corsair->GetLEDCount());
for (unsigned int i = 0; i < corsair->GetLEDCount(); i++)
{
led* new_led = new led();
@ -120,7 +122,6 @@ RGBController_CorsairPro::RGBController_CorsairPro(CorsairProController* corsair
new_led->name = "Corsair Pro LED";
leds.push_back(*new_led);
colors.push_back(0x00000000);
}
zone new_zone;

@ -21,8 +21,20 @@ RGBController_Hue2::RGBController_Hue2(Hue2Controller* hue2_ptr)
led_mode.name = "Custom";
modes.push_back(led_mode);
unsigned int led_idx = 0;
/*-------------------------------------------------*\
| Set size of colors array |
\*-------------------------------------------------*/
unsigned int led_count = 0;
for (unsigned int channel_idx = 0; channel_idx < HUE_2_NUM_CHANNELS; channel_idx++)
{
led_count += hue2->channel_leds[channel_idx];
}
colors.resize(led_count);
/*-------------------------------------------------*\
| Set zones and leds |
\*-------------------------------------------------*/
unsigned int led_idx = 0;
for (unsigned int channel_idx = 0; channel_idx < HUE_2_NUM_CHANNELS; channel_idx++)
{
if(hue2->channel_leds[channel_idx] > 0)
@ -40,8 +52,6 @@ RGBController_Hue2::RGBController_Hue2(Hue2Controller* hue2_ptr)
for (unsigned int led_ch_idx = 0; led_ch_idx < hue2->channel_leds[channel_idx]; led_ch_idx++)
{
colors.push_back(0x00000000);
char led_idx_string[3];
sprintf(led_idx_string, "%d", led_ch_idx + 1);
@ -117,7 +127,10 @@ void RGBController_Hue2::UpdateZoneLEDs(int zone)
}
}
hue2->SetChannelLEDs(channel, channel_colors);
if(channel_colors.size() > 0)
{
hue2->SetChannelLEDs(channel, channel_colors);
}
}
void RGBController_Hue2::UpdateSingleLED(int led)
@ -133,5 +146,9 @@ void RGBController_Hue2::UpdateSingleLED(int led)
channel_colors.push_back(colors[color]);
}
}
hue2->SetChannelLEDs(channel, channel_colors);
if(channel_colors.size() > 0)
{
hue2->SetChannelLEDs(channel, channel_colors);
}
}

@ -23,8 +23,20 @@ RGBController_HuePlus::RGBController_HuePlus(HuePlusController* hueplus_ptr)
led_mode.name = "Custom";
modes.push_back(led_mode);
unsigned int led_idx = 0;
/*-------------------------------------------------*\
| Set size of colors array |
\*-------------------------------------------------*/
unsigned int led_count = 0;
for (unsigned int channel_idx = 0; channel_idx < HUE_PLUS_NUM_CHANNELS; channel_idx++)
{
led_count += hueplus->channel_leds[channel_idx];
}
colors.resize(led_count);
/*-------------------------------------------------*\
| Set zones and leds |
\*-------------------------------------------------*/
unsigned int led_idx = 0;
for (int channel_idx = 0; channel_idx < HUE_PLUS_NUM_CHANNELS; channel_idx++)
{
if(hueplus->channel_leds[channel_idx] > 0)
@ -42,8 +54,6 @@ RGBController_HuePlus::RGBController_HuePlus(HuePlusController* hueplus_ptr)
for (unsigned led_ch_idx = 0; led_ch_idx < hueplus->channel_leds[channel_idx]; led_ch_idx++)
{
colors.push_back(0x00000000);
char led_idx_string[3];
sprintf(led_idx_string, "%d", led_ch_idx + 1);
@ -119,7 +129,10 @@ void RGBController_HuePlus::UpdateZoneLEDs(int zone)
}
}
hueplus->SetChannelLEDs(channel, channel_colors);
if(channel_colors.size() > 0)
{
hueplus->SetChannelLEDs(channel, channel_colors);
}
}
void RGBController_HuePlus::UpdateSingleLED(int led)
@ -135,5 +148,9 @@ void RGBController_HuePlus::UpdateSingleLED(int led)
channel_colors.push_back(colors[color]);
}
}
hueplus->SetChannelLEDs(channel, channel_colors);
if(channel_colors.size() > 0)
{
hueplus->SetChannelLEDs(channel, channel_colors);
}
}

Loading…
Cancel
Save