RgbCctPacketFormatter.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // now make the temperature change
  41. command(RGB_CCT_KELVIN, cmdValue);
  42. // and return to our original mode
  43. if (ourState != NULL) {
  44. BulbMode originalBulbMode = ourState->getBulbMode();
  45. if ((settings->enableAutomaticModeSwitching) && (originalBulbMode != BulbMode::BULB_MODE_WHITE)) {
  46. switchMode(*ourState, originalBulbMode);
  47. }
  48. }
  49. }
  50. // update saturation. This only works when in Color mode, so if not in color we switch to color,
  51. // make the change, and switch back again.
  52. void RgbCctPacketFormatter::updateSaturation(uint8_t value) {
  53. // look up our current mode
  54. const GroupState* ourState = this->stateStore->get(this->deviceId, this->groupId, REMOTE_TYPE_RGB_CCT);
  55. BulbMode originalBulbMode = BulbMode::BULB_MODE_WHITE;
  56. if (ourState != NULL) {
  57. originalBulbMode = ourState->getBulbMode();
  58. // are we already in white? If not, change to white
  59. if ((settings->enableAutomaticModeSwitching) && (originalBulbMode != BulbMode::BULB_MODE_COLOR)) {
  60. updateHue(ourState->getHue());
  61. }
  62. }
  63. // now make the saturation change
  64. uint8_t remapped = value + RGB_CCT_SATURATION_OFFSET;
  65. command(RGB_CCT_SATURATION, remapped);
  66. if (ourState != NULL) {
  67. if ((settings->enableAutomaticModeSwitching) && (originalBulbMode != BulbMode::BULB_MODE_COLOR)) {
  68. switchMode(*ourState, originalBulbMode);
  69. }
  70. }
  71. }
  72. void RgbCctPacketFormatter::updateColorWhite() {
  73. // there is no direct white command, so let's look up our prior temperature and set that, which
  74. // causes the bulb to go white
  75. const GroupState* ourState = this->stateStore->get(this->deviceId, this->groupId, REMOTE_TYPE_RGB_CCT);
  76. uint8_t value =
  77. ourState == NULL
  78. ? 0
  79. : V2PacketFormatter::tov2scale(ourState->getKelvin(), RGB_CCT_KELVIN_REMOTE_END, 2);
  80. // issue command to set kelvin to prior value, which will drive to white
  81. command(RGB_CCT_KELVIN, value);
  82. }
  83. void RgbCctPacketFormatter::enableNightMode() {
  84. uint8_t arg = groupCommandArg(OFF, groupId);
  85. command(RGB_CCT_ON | 0x80, arg);
  86. }
  87. BulbId RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject result) {
  88. uint8_t packetCopy[V2_PACKET_LEN];
  89. memcpy(packetCopy, packet, V2_PACKET_LEN);
  90. V2RFEncoding::decodeV2Packet(packetCopy);
  91. BulbId bulbId(
  92. (packetCopy[2] << 8) | packetCopy[3],
  93. packetCopy[7],
  94. REMOTE_TYPE_RGB_CCT
  95. );
  96. uint8_t command = (packetCopy[V2_COMMAND_INDEX] & 0x7F);
  97. uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
  98. if (command == RGB_CCT_ON) {
  99. if ((packetCopy[V2_COMMAND_INDEX] & 0x80) == 0x80) {
  100. result["command"] = "night_mode";
  101. } else if (arg == RGB_CCT_MODE_SPEED_DOWN) {
  102. result["command"] = "mode_speed_down";
  103. } else if (arg == RGB_CCT_MODE_SPEED_UP) {
  104. result["command"] = "mode_speed_up";
  105. } else if (arg < 5) { // Group is not reliably encoded in group byte. Extract from arg byte
  106. result["state"] = "ON";
  107. bulbId.groupId = arg;
  108. } else {
  109. result["state"] = "OFF";
  110. bulbId.groupId = arg-5;
  111. }
  112. } else if (command == RGB_CCT_COLOR) {
  113. uint8_t rescaledColor = (arg - RGB_CCT_COLOR_OFFSET) % 0x100;
  114. uint16_t hue = Units::rescale<uint16_t, uint16_t>(rescaledColor, 360, 255.0);
  115. result["hue"] = hue;
  116. } else if (command == RGB_CCT_KELVIN) {
  117. uint8_t temperature = V2PacketFormatter::fromv2scale(arg, RGB_CCT_KELVIN_REMOTE_END, 2);
  118. result["color_temp"] = Units::whiteValToMireds(temperature, 100);
  119. // brightness == saturation
  120. } else if (command == RGB_CCT_BRIGHTNESS && arg >= (RGB_CCT_BRIGHTNESS_OFFSET - 15)) {
  121. uint8_t level = constrain(arg - RGB_CCT_BRIGHTNESS_OFFSET, 0, 100);
  122. result["brightness"] = Units::rescale<uint8_t, uint8_t>(level, 255, 100);
  123. } else if (command == RGB_CCT_SATURATION) {
  124. result["saturation"] = constrain(arg - RGB_CCT_SATURATION_OFFSET, 0, 100);
  125. } else if (command == RGB_CCT_MODE) {
  126. result["mode"] = arg;
  127. } else {
  128. result["button_id"] = command;
  129. result["argument"] = arg;
  130. }
  131. return bulbId;
  132. }