| 183 | } |
| 184 | |
| 185 | void YeelightController::SetColor(unsigned char red, unsigned char green, unsigned char blue) |
| 186 | { |
| 187 | json command; |
| 188 | |
| 189 | /*-----------------------------------------------------------------*\ |
| 190 | | Yeelight doesn't seem to support proper RGB, it just uses RGB to | |
| 191 | | calculate hue and saturation. It doesn't affect brightness. To | |
| 192 | | work around this, determine the highest value and scale to 100 to | |
| 193 | | use as brightness | |
| 194 | \*-----------------------------------------------------------------*/ |
| 195 | float bright = red; |
| 196 | |
| 197 | if(green > bright) |
| 198 | { |
| 199 | bright = green; |
| 200 | } |
| 201 | |
| 202 | if(blue > bright) |
| 203 | { |
| 204 | bright = blue; |
| 205 | } |
| 206 | |
| 207 | bright = (100.0f * (bright / 255.0f)); |
| 208 | |
| 209 | /*-----------------------------------------------------------------*\ |
| 210 | | Calculate the RGB field as 0x00RRGGBB | |
| 211 | \*-----------------------------------------------------------------*/ |
| 212 | unsigned int rgb = (red << 16) | (green << 8) | (blue << 0); |
| 213 | |
| 214 | /*-----------------------------------------------------------------*\ |
| 215 | | Because of Yeelight's weird quirks with true RGB, we have to use | |
| 216 | | the Color Flow option but configure only one frame. Because the | |
| 217 | | set_cf option provides both RGB and brightness in one command, it | |
| 218 | | allows better RGB control than the set_rgb function. | |
| 219 | \*-----------------------------------------------------------------*/ |
| 220 | std::string cf = "50,1," + std::to_string(rgb) +"," + std::to_string((int)bright); |
| 221 | |
| 222 | /*-----------------------------------------------------------------*\ |
| 223 | | Fill in the set_cf command with the color flow string. | |
| 224 | \*-----------------------------------------------------------------*/ |
| 225 | command["id"] = 1; |
| 226 | command["method"] = "start_cf"; |
| 227 | command["params"][0] = 1; |
| 228 | command["params"][1] = 1; |
| 229 | command["params"][2] = cf; |
| 230 | |
| 231 | /*-----------------------------------------------------------------*\ |
| 232 | | Convert the JSON object to a string and write it | |
| 233 | \*-----------------------------------------------------------------*/ |
| 234 | std::string command_str = command.dump().append("\r\n"); |
| 235 | |
| 236 | if(music_mode) |
| 237 | { |
| 238 | send(*music_mode_sock, (char *)command_str.c_str(), command_str.length(), 0); |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | port.tcp_client_connect(); |
no test coverage detected