FUT089PacketFormatter.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. void FUT089PacketFormatter::updateHue(uint16_t value) {
  17. uint8_t remapped = Units::rescale(value, 255, 360);
  18. updateColorRaw(remapped);
  19. }
  20. void FUT089PacketFormatter::updateColorRaw(uint8_t value) {
  21. command(FUT089_COLOR, FUT089_COLOR_OFFSET + value);
  22. }
  23. void FUT089PacketFormatter::updateTemperature(uint8_t value) {
  24. updateColorWhite();
  25. command(FUT089_KELVIN, 100 - value);
  26. }
  27. void FUT089PacketFormatter::updateSaturation(uint8_t value) {
  28. command(FUT089_SATURATION, 100 - value);
  29. }
  30. void FUT089PacketFormatter::updateColorWhite() {
  31. command(FUT089_ON, FUT089_WHITE_MODE);
  32. }
  33. void FUT089PacketFormatter::enableNightMode() {
  34. uint8_t arg = groupCommandArg(OFF, groupId);
  35. command(FUT089_ON | 0x80, arg);
  36. }
  37. BulbId FUT089PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& result, GroupStateStore* stateStore) {
  38. uint8_t packetCopy[V2_PACKET_LEN];
  39. memcpy(packetCopy, packet, V2_PACKET_LEN);
  40. V2RFEncoding::decodeV2Packet(packetCopy);
  41. BulbId bulbId(
  42. (packetCopy[2] << 8) | packetCopy[3],
  43. packetCopy[7],
  44. REMOTE_TYPE_FUT089
  45. );
  46. uint8_t command = (packetCopy[V2_COMMAND_INDEX] & 0x7F);
  47. uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
  48. if (command == FUT089_ON) {
  49. if (arg == FUT089_MODE_SPEED_DOWN) {
  50. result["command"] = "mode_speed_down";
  51. } else if (arg == FUT089_MODE_SPEED_UP) {
  52. result["command"] = "mode_speed_up";
  53. } else if (arg == FUT089_WHITE_MODE) {
  54. result["command"] = "white_mode";
  55. } else if (arg <= 8) { // Group is not reliably encoded in group byte. Extract from arg byte
  56. result["state"] = "ON";
  57. bulbId.groupId = arg;
  58. } else if (arg >= 9 && arg <= 17) {
  59. result["state"] = "OFF";
  60. bulbId.groupId = arg-9;
  61. }
  62. } else if (command == FUT089_COLOR) {
  63. uint8_t rescaledColor = (arg - FUT089_COLOR_OFFSET) % 0x100;
  64. uint16_t hue = Units::rescale<uint16_t, uint16_t>(rescaledColor, 360, 255.0);
  65. result["hue"] = hue;
  66. } else if (command == FUT089_BRIGHTNESS) {
  67. uint8_t level = constrain(arg, 0, 100);
  68. result["brightness"] = Units::rescale<uint8_t, uint8_t>(level, 255, 100);
  69. // saturation == kelvin. arg ranges are the same, so can't distinguish
  70. // without using state
  71. } else if (command == FUT089_SATURATION) {
  72. GroupState& state = stateStore->get(bulbId);
  73. if (state.getBulbMode() == BULB_MODE_COLOR) {
  74. result["saturation"] = 100 - constrain(arg, 0, 100);
  75. } else {
  76. result["color_temp"] = Units::whiteValToMireds(100 - arg, 100);
  77. }
  78. } else if (command == FUT089_MODE) {
  79. result["mode"] = arg;
  80. } else {
  81. result["button_id"] = command;
  82. result["argument"] = arg;
  83. }
  84. return bulbId;
  85. }