RgbCctPacketFormatter.cpp 5.8 KB

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