RgbCctPacketFormatter.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <RgbCctPacketFormatter.h>
  2. #include <V2RFEncoding.h>
  3. #include <Units.h>
  4. void RgbCctPacketFormatter::modeSpeedDown() {
  5. command(RGB_CCT_ON, RGB_CCT_MODE_SPEED_DOWN);
  6. }
  7. void RgbCctPacketFormatter::modeSpeedUp() {
  8. command(RGB_CCT_ON, RGB_CCT_MODE_SPEED_UP);
  9. }
  10. void RgbCctPacketFormatter::updateMode(uint8_t mode) {
  11. lastMode = mode;
  12. command(RGB_CCT_MODE, mode);
  13. }
  14. void RgbCctPacketFormatter::nextMode() {
  15. updateMode((lastMode+1)%RGB_CCT_NUM_MODES);
  16. }
  17. void RgbCctPacketFormatter::previousMode() {
  18. updateMode((lastMode-1)%RGB_CCT_NUM_MODES);
  19. }
  20. void RgbCctPacketFormatter::updateBrightness(uint8_t brightness) {
  21. command(RGB_CCT_BRIGHTNESS, RGB_CCT_BRIGHTNESS_OFFSET + brightness);
  22. }
  23. // change the hue (which may also change to color mode).
  24. void RgbCctPacketFormatter::updateHue(uint16_t value) {
  25. uint8_t remapped = Units::rescale(value, 255, 360);
  26. updateColorRaw(remapped);
  27. }
  28. void RgbCctPacketFormatter::updateColorRaw(uint8_t value) {
  29. command(RGB_CCT_COLOR, RGB_CCT_COLOR_OFFSET + value);
  30. }
  31. void RgbCctPacketFormatter::updateTemperature(uint8_t value) {
  32. // Packet scale is [0x94, 0x92, .. 0, .., 0xCE, 0xCC]. Increments of 2.
  33. // From coolest to warmest.
  34. uint8_t cmdValue = V2PacketFormatter::tov2scale(value, RGB_CCT_KELVIN_REMOTE_END, 2);
  35. // when updating temperature, the bulb switches to white. If we are not already
  36. // in white mode, that makes changing temperature annoying because the current hue/mode
  37. // is lost. So lookup our current bulb mode, and if needed, reset the hue/mode after
  38. // changing the temperature
  39. const GroupState* ourState = this->stateStore->get(this->deviceId, this->groupId, REMOTE_TYPE_RGB_CCT);
  40. BulbMode originalBulbMode = ourState->getBulbMode();
  41. // now make the temperature change
  42. command(RGB_CCT_KELVIN, cmdValue);
  43. // and return to our original mode
  44. if ((settings->enableAutomaticModeSwitching) && (originalBulbMode != BulbMode::BULB_MODE_WHITE)) {
  45. switchMode(*ourState, originalBulbMode);
  46. }
  47. }
  48. // update saturation. This only works when in Color mode, so if not in color we switch to color,
  49. // make the change, and switch back again.
  50. void RgbCctPacketFormatter::updateSaturation(uint8_t value) {
  51. // look up our current mode
  52. const GroupState* ourState = this->stateStore->get(this->deviceId, this->groupId, REMOTE_TYPE_RGB_CCT);
  53. BulbMode originalBulbMode = ourState->getBulbMode();
  54. // are we already in white? If not, change to white
  55. if ((settings->enableAutomaticModeSwitching) && (originalBulbMode != BulbMode::BULB_MODE_COLOR)) {
  56. updateHue(ourState->getHue());
  57. }
  58. // now make the saturation change
  59. uint8_t remapped = value + RGB_CCT_SATURATION_OFFSET;
  60. command(RGB_CCT_SATURATION, remapped);
  61. if ((settings->enableAutomaticModeSwitching) && (originalBulbMode != BulbMode::BULB_MODE_COLOR)) {
  62. switchMode(*ourState, originalBulbMode);
  63. }
  64. }
  65. void RgbCctPacketFormatter::updateColorWhite() {
  66. // there is no direct white command, so let's look up our prior temperature and set that, which
  67. // causes the bulb to go white
  68. const GroupState* ourState = this->stateStore->get(this->deviceId, this->groupId, REMOTE_TYPE_RGB_CCT);
  69. uint8_t value = V2PacketFormatter::tov2scale(ourState->getKelvin(), RGB_CCT_KELVIN_REMOTE_END, 2);
  70. // issue command to set kelvin to prior value, which will drive to white
  71. command(RGB_CCT_KELVIN, value);
  72. }
  73. void RgbCctPacketFormatter::enableNightMode() {
  74. uint8_t arg = groupCommandArg(OFF, groupId);
  75. command(RGB_CCT_ON | 0x80, arg);
  76. }
  77. BulbId RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& result) {
  78. uint8_t packetCopy[V2_PACKET_LEN];
  79. memcpy(packetCopy, packet, V2_PACKET_LEN);
  80. V2RFEncoding::decodeV2Packet(packetCopy);
  81. BulbId bulbId(
  82. (packetCopy[2] << 8) | packetCopy[3],
  83. packetCopy[7],
  84. REMOTE_TYPE_RGB_CCT
  85. );
  86. uint8_t command = (packetCopy[V2_COMMAND_INDEX] & 0x7F);
  87. uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
  88. if (command == RGB_CCT_ON) {
  89. if ((packetCopy[V2_COMMAND_INDEX] & 0x80) == 0x80) {
  90. result["command"] = "night_mode";
  91. } else if (arg == RGB_CCT_MODE_SPEED_DOWN) {
  92. result["command"] = "mode_speed_down";
  93. } else if (arg == RGB_CCT_MODE_SPEED_UP) {
  94. result["command"] = "mode_speed_up";
  95. } else if (arg < 5) { // Group is not reliably encoded in group byte. Extract from arg byte
  96. result["state"] = "ON";
  97. bulbId.groupId = arg;
  98. } else {
  99. result["state"] = "OFF";
  100. bulbId.groupId = arg-5;
  101. }
  102. } else if (command == RGB_CCT_COLOR) {
  103. uint8_t rescaledColor = (arg - RGB_CCT_COLOR_OFFSET) % 0x100;
  104. uint16_t hue = Units::rescale<uint16_t, uint16_t>(rescaledColor, 360, 255.0);
  105. result["hue"] = hue;
  106. } else if (command == RGB_CCT_KELVIN) {
  107. uint8_t temperature = V2PacketFormatter::fromv2scale(arg, RGB_CCT_KELVIN_REMOTE_END, 2);
  108. result["color_temp"] = Units::whiteValToMireds(temperature, 100);
  109. // brightness == saturation
  110. } else if (command == RGB_CCT_BRIGHTNESS && arg >= (RGB_CCT_BRIGHTNESS_OFFSET - 15)) {
  111. uint8_t level = constrain(arg - RGB_CCT_BRIGHTNESS_OFFSET, 0, 100);
  112. result["brightness"] = Units::rescale<uint8_t, uint8_t>(level, 255, 100);
  113. } else if (command == RGB_CCT_SATURATION) {
  114. result["saturation"] = constrain(arg - RGB_CCT_SATURATION_OFFSET, 0, 100);
  115. } else if (command == RGB_CCT_MODE) {
  116. result["mode"] = arg;
  117. } else {
  118. result["button_id"] = command;
  119. result["argument"] = arg;
  120. }
  121. return bulbId;
  122. }