V6RgbCctCommandHandler.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 & 0x7F;
  30. const uint8_t arg = commandArg >> 24;
  31. client->setHeld((command & 0x80) == 0x80);
  32. if (cmd == V2_STATUS) {
  33. switch (arg) {
  34. case V2_RGB_CCT_ON:
  35. case V2_RGB_CCT_OFF:
  36. client->updateStatus(arg == V2_RGB_CCT_ON ? ON : OFF);
  37. break;
  38. case V2_RGB_NIGHT_MODE:
  39. client->updateBrightness(0);
  40. break;
  41. case V2_RGB_CCT_SPEED_DOWN:
  42. client->modeSpeedDown();
  43. break;
  44. case V2_RGB_CCT_SPEED_UP:
  45. client->modeSpeedUp();
  46. break;
  47. default:
  48. return false;
  49. }
  50. return true;
  51. }
  52. switch (cmd) {
  53. case V2_COLOR:
  54. handleUpdateColor(client, commandArg);
  55. break;
  56. case V2_KELVIN:
  57. client->updateTemperature(100 - arg);
  58. break;
  59. case V2_BRIGHTNESS:
  60. client->updateBrightness(arg);
  61. break;
  62. case V2_SATURATION:
  63. client->updateSaturation(100 - arg);
  64. break;
  65. case V2_MODE:
  66. client->updateMode(arg-1);
  67. break;
  68. default:
  69. return false;
  70. }
  71. return true;
  72. }
  73. /*
  74. * Arguments are 32 bits. Most commands use the first byte, but color arguments
  75. * can use all four. Triggered in app when quickly transitioning through colors.
  76. */
  77. void V6RgbCctCommandHandler::handleUpdateColor(MiLightClient *client, uint32_t color) {
  78. for (int i = 3; i >= 0; i--) {
  79. const uint8_t argValue = (color >> (i*8)) & 0xFF;
  80. if (argValue == 0) {
  81. return;
  82. }
  83. client->updateColorRaw(argValue);
  84. }
  85. }