FUT089PacketFormatter.cpp 5.7 KB

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