FUT089PacketFormatter.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <FUT089PacketFormatter.h>
  2. #include <V2RFEncoding.h>
  3. #include <Units.h>
  4. void FUT089PacketFormatter::modeSpeedDown() {
  5. command(FUT089_ON, FUT089_MODE_SPEED_DOWN);
  6. }
  7. void FUT089PacketFormatter::modeSpeedUp() {
  8. command(FUT089_ON, FUT089_MODE_SPEED_UP);
  9. }
  10. void FUT089PacketFormatter::updateMode(uint8_t mode) {
  11. command(FUT089_MODE, mode);
  12. }
  13. void FUT089PacketFormatter::updateBrightness(uint8_t brightness) {
  14. command(FUT089_BRIGHTNESS, brightness);
  15. }
  16. // change the hue (which may also change to color mode).
  17. void FUT089PacketFormatter::updateHue(uint16_t value) {
  18. uint8_t remapped = Units::rescale(value, 255, 360);
  19. updateColorRaw(remapped);
  20. }
  21. void FUT089PacketFormatter::updateColorRaw(uint8_t value) {
  22. command(FUT089_COLOR, FUT089_COLOR_OFFSET + value);
  23. }
  24. // change the temperature (kelvin). Note that temperature and saturation share the same command
  25. // number (7), and they change which they do based on the mode of the lamp (white vs. color mode).
  26. // To make this command work, we need to switch to white mode, make the change, and then flip
  27. // back to the original mode.
  28. void FUT089PacketFormatter::updateTemperature(uint8_t value) {
  29. // look up our current mode
  30. GroupState ourState = this->stateStore->get(this->deviceId, this->groupId, REMOTE_TYPE_FUT089);
  31. BulbMode originalBulbMode = ourState.getBulbMode();
  32. // are we already in white? If not, change to white
  33. if (originalBulbMode != BulbMode::BULB_MODE_WHITE) {
  34. updateColorWhite();
  35. }
  36. // now make the temperature change
  37. command(FUT089_KELVIN, 100 - value);
  38. // and return to our original mode
  39. if ((settings->enableAutomaticModeSwitching) && (originalBulbMode != BulbMode::BULB_MODE_WHITE)) {
  40. switchMode(ourState, originalBulbMode);
  41. }
  42. }
  43. // change the saturation. Note that temperature and saturation share the same command
  44. // number (7), and they change which they do based on the mode of the lamp (white vs. color mode).
  45. // Therefore, if we are not in color mode, we need to switch to color mode, make the change,
  46. // and switch back to the original mode.
  47. void FUT089PacketFormatter::updateSaturation(uint8_t value) {
  48. // look up our current mode
  49. GroupState ourState = this->stateStore->get(this->deviceId, this->groupId, REMOTE_TYPE_FUT089);
  50. BulbMode originalBulbMode = ourState.getBulbMode();
  51. // are we already in color? If not, we need to flip modes
  52. if ((settings->enableAutomaticModeSwitching) && (originalBulbMode != BulbMode::BULB_MODE_COLOR)) {
  53. updateHue(ourState.getHue());
  54. }
  55. // now make the saturation change
  56. command(FUT089_SATURATION, 100 - value);
  57. // and revert back if necessary
  58. if ((settings->enableAutomaticModeSwitching) && (originalBulbMode != BulbMode::BULB_MODE_COLOR)) {
  59. switchMode(ourState, originalBulbMode);
  60. }
  61. }
  62. void FUT089PacketFormatter::updateColorWhite() {
  63. command(FUT089_ON, FUT089_WHITE_MODE);
  64. }
  65. void FUT089PacketFormatter::enableNightMode() {
  66. uint8_t arg = groupCommandArg(OFF, groupId);
  67. command(FUT089_ON | 0x80, arg);
  68. }
  69. BulbId FUT089PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& result) {
  70. if (stateStore == NULL) {
  71. Serial.println(F("ERROR: stateStore not set. Prepare was not called! **THIS IS A BUG**"));
  72. BulbId fakeId(0, 0, REMOTE_TYPE_FUT089);
  73. return fakeId;
  74. }
  75. uint8_t packetCopy[V2_PACKET_LEN];
  76. memcpy(packetCopy, packet, V2_PACKET_LEN);
  77. V2RFEncoding::decodeV2Packet(packetCopy);
  78. BulbId bulbId(
  79. (packetCopy[2] << 8) | packetCopy[3],
  80. packetCopy[7],
  81. REMOTE_TYPE_FUT089
  82. );
  83. uint8_t command = (packetCopy[V2_COMMAND_INDEX] & 0x7F);
  84. uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
  85. if (command == FUT089_ON) {
  86. if ((packetCopy[V2_COMMAND_INDEX] & 0x80) == 0x80) {
  87. result["command"] = "night_mode";
  88. } else if (arg == FUT089_MODE_SPEED_DOWN) {
  89. result["command"] = "mode_speed_down";
  90. } else if (arg == FUT089_MODE_SPEED_UP) {
  91. result["command"] = "mode_speed_up";
  92. } else if (arg == FUT089_WHITE_MODE) {
  93. result["command"] = "set_white";
  94. } else if (arg <= 8) { // Group is not reliably encoded in group byte. Extract from arg byte
  95. result["state"] = "ON";
  96. bulbId.groupId = arg;
  97. } else if (arg >= 9 && arg <= 17) {
  98. result["state"] = "OFF";
  99. bulbId.groupId = arg-9;
  100. }
  101. } else if (command == FUT089_COLOR) {
  102. uint8_t rescaledColor = (arg - FUT089_COLOR_OFFSET) % 0x100;
  103. uint16_t hue = Units::rescale<uint16_t, uint16_t>(rescaledColor, 360, 255.0);
  104. result["hue"] = hue;
  105. } else if (command == FUT089_BRIGHTNESS) {
  106. uint8_t level = constrain(arg, 0, 100);
  107. result["brightness"] = Units::rescale<uint8_t, uint8_t>(level, 255, 100);
  108. // saturation == kelvin. arg ranges are the same, so can't distinguish
  109. // without using state
  110. } else if (command == FUT089_SATURATION) {
  111. GroupState& state = stateStore->get(bulbId);
  112. if (state.getBulbMode() == BULB_MODE_COLOR) {
  113. result["saturation"] = 100 - constrain(arg, 0, 100);
  114. } else {
  115. result["color_temp"] = Units::whiteValToMireds(100 - arg, 100);
  116. }
  117. } else if (command == FUT089_MODE) {
  118. result["mode"] = arg;
  119. } else {
  120. result["button_id"] = command;
  121. result["argument"] = arg;
  122. }
  123. return bulbId;
  124. }