FUT089PacketFormatter.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. void 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. const uint16_t deviceId = (packetCopy[2] << 8) | packetCopy[3];
  42. const uint8_t groupId = packetCopy[7];
  43. result["device_id"] = deviceId;
  44. result["group_id"] = groupId;
  45. result["device_type"] = "fut089";
  46. uint8_t command = (packetCopy[V2_COMMAND_INDEX] & 0x7F);
  47. uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
  48. // only need state for saturation and kelvin (they have the same command ID)
  49. GroupState* state = NULL;
  50. if (command == FUT089_SATURATION) {
  51. GroupId group(deviceId, groupId, REMOTE_TYPE_FUT089);
  52. state = stateStore->get(group);
  53. }
  54. if (command == FUT089_ON) {
  55. if (arg == FUT089_MODE_SPEED_DOWN) {
  56. result["command"] = "mode_speed_down";
  57. } else if (arg == FUT089_MODE_SPEED_UP) {
  58. result["command"] = "mode_speed_up";
  59. } else if (arg == FUT089_WHITE_MODE) {
  60. result["command"] = "white_mode";
  61. } else if (arg <= 8) { // Group is not reliably encoded in group byte. Extract from arg byte
  62. result["state"] = "ON";
  63. result["group_id"] = arg;
  64. } else if (arg >= 9 && arg <= 17) {
  65. result["state"] = "OFF";
  66. result["group_id"] = arg-9;
  67. }
  68. } else if (command == FUT089_COLOR) {
  69. uint8_t rescaledColor = (arg - FUT089_COLOR_OFFSET) % 0x100;
  70. uint16_t hue = Units::rescale<uint16_t, uint16_t>(rescaledColor, 360, 255.0);
  71. result["hue"] = hue;
  72. } else if (command == FUT089_BRIGHTNESS) {
  73. uint8_t level = constrain(arg, 0, 100);
  74. result["brightness"] = Units::rescale<uint8_t, uint8_t>(level, 255, 100);
  75. // saturation == kelvin. arg ranges are the same, so can't distinguish
  76. // without using state
  77. } else if (command == FUT089_SATURATION) {
  78. if (state->getBulbMode() == BULB_MODE_COLOR) {
  79. result["saturation"] = 100 - constrain(arg, 0, 100);
  80. } else {
  81. result["color_temp"] = Units::whiteValToMireds(100 - arg, 100);
  82. }
  83. } else if (command == FUT089_MODE) {
  84. result["mode"] = arg;
  85. } else {
  86. result["button_id"] = command;
  87. result["argument"] = arg;
  88. }
  89. if (! result.containsKey("state")) {
  90. result["state"] = "ON";
  91. }
  92. }