MCPcopy Create free account
hub / github.com/CalcProgrammer1/OpenRGB / SetColor

Method SetColor

Controllers/KasaSmartController/KasaSmartController.cpp:149–214  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

147}
148
149void KasaSmartController::SetColor(unsigned char red, unsigned char green, unsigned char blue)
150{
151 if(!is_initialized)
152 {
153 return;
154 }
155
156 RGBColor color = ToRGBColor(red, green, blue);
157 hsv_t hsv;
158 rgb2hsv(color, &hsv);
159
160 /*------------------------------------------*\
161 | Normalize case where hue is "-1" undefined |
162 \*------------------------------------------*/
163 unsigned int normalized_hue = hsv.hue;
164 if(hsv.hue == (unsigned int)-1)
165 {
166 normalized_hue = 0;
167 }
168 /*--------------------------------------------------*\
169 | Kasa smart lights take values out of 100 for these |
170 \*--------------------------------------------------*/
171 unsigned int normalized_saturation = hsv.saturation * 100 / 255;
172 unsigned int normalized_value = hsv.value * 100 / 255;
173
174
175 /*-------------------*\
176 | Open TCP connection |
177 \*-------------------*/
178 if(!port.connected && !port.tcp_client_connect() && ++retry_count >= KASA_SMART_MAX_CONNECTION_ATTEMPTS)
179 {
180 is_initialized = false;
181 return;
182 }
183
184 /*----------------------------*\
185 | Hack to handle/emulate black |
186 \*----------------------------*/
187 if(normalized_saturation == 0 && normalized_value == 0)
188 {
189 TurnOff();
190 return;
191 }
192
193 /*------------------------------*\
194 | Format set light state command |
195 \*------------------------------*/
196 const std::string set_lightstate_command_format(KASA_SMART_SET_LIGHT_STATE_COMMAND_FORMAT);
197 int size = std::snprintf(nullptr, 0, set_lightstate_command_format.c_str(), normalized_hue, normalized_saturation, normalized_value) + 1;
198 if(size <= 0)
199 {
200 port.tcp_close();
201 return;
202 }
203 char* buf = new char[size];
204 std::snprintf(buf, size, set_lightstate_command_format.c_str(), normalized_hue, normalized_saturation, normalized_value);
205 std::string set_lightstate_command(buf, buf + size - 1);
206 delete[] buf;

Callers 1

DeviceUpdateLEDsMethod · 0.45

Calls 4

rgb2hsvFunction · 0.85
snprintfFunction · 0.85
tcp_client_connectMethod · 0.80
tcp_closeMethod · 0.80

Tested by

no test coverage detected