V6RgbCctCommandHandler.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <V6RgbCctCommandHandler.h>
  2. bool V6RgbCctCommandHandler::handlePreset(
  3. MiLightClient* client,
  4. uint8_t commandLsb,
  5. uint32_t commandArg)
  6. {
  7. if (commandLsb == 0) {
  8. const uint8_t saturation = commandArg >> 24;
  9. const uint8_t color = (commandArg >> 16);
  10. const uint8_t brightness = (commandArg >> 8);
  11. client->updateBrightness(brightness);
  12. client->updateColorRaw(color);
  13. client->updateSaturation(saturation);
  14. } else if (commandLsb == 1) {
  15. const uint8_t brightness = (commandArg >> 16);
  16. const uint8_t kelvin = (commandArg >> 8);
  17. client->updateBrightness(brightness);
  18. client->updateTemperature(0x64 - kelvin);
  19. } else {
  20. return false;
  21. }
  22. return true;
  23. }
  24. bool V6RgbCctCommandHandler::handleCommand(
  25. MiLightClient* client,
  26. uint32_t command,
  27. uint32_t commandArg)
  28. {
  29. const uint8_t cmd = command & 0xFF;
  30. const uint8_t arg = commandArg >> 24;
  31. if (cmd == V2_STATUS) {
  32. switch (arg) {
  33. case V2_RGB_CCT_ON:
  34. case V2_RGB_CCT_OFF:
  35. client->updateStatus(arg == V2_RGB_CCT_ON ? ON : OFF);
  36. break;
  37. case V2_RGB_NIGHT_MODE:
  38. client->updateBrightness(0);
  39. break;
  40. case V2_RGB_CCT_SPEED_DOWN:
  41. client->modeSpeedDown();
  42. break;
  43. case V2_RGB_CCT_SPEED_UP:
  44. client->modeSpeedUp();
  45. break;
  46. default:
  47. return false;
  48. }
  49. return true;
  50. }
  51. switch (cmd) {
  52. case V2_COLOR:
  53. handleUpdateColor(client, commandArg);
  54. break;
  55. case V2_KELVIN:
  56. client->updateTemperature(100 - arg);
  57. break;
  58. case V2_BRIGHTNESS:
  59. client->updateBrightness(arg);
  60. break;
  61. case V2_SATURATION:
  62. client->updateSaturation(100 - arg);
  63. break;
  64. case V2_MODE:
  65. client->updateMode(arg-1);
  66. break;
  67. default:
  68. return false;
  69. }
  70. return true;
  71. }
  72. /*
  73. * Arguments are 32 bits. Most commands use the first byte, but color arguments
  74. * can use all four. Triggered in app when quickly transitioning through colors.
  75. */
  76. void V6RgbCctCommandHandler::handleUpdateColor(MiLightClient *client, uint32_t color) {
  77. for (int i = 3; i >= 0; i--) {
  78. const uint8_t argValue = (color >> (i*8)) & 0xFF;
  79. if (argValue == 0) {
  80. return;
  81. }
  82. client->updateColorRaw(argValue);
  83. }
  84. }