FUT091PacketFormatter.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <FUT091PacketFormatter.h>
  2. #include <V2RFEncoding.h>
  3. #include <Units.h>
  4. static const uint8_t BRIGHTNESS_SCALE_MAX = 0x97;
  5. static const uint8_t KELVIN_SCALE_MAX = 0xC5;
  6. void FUT091PacketFormatter::updateBrightness(uint8_t value) {
  7. command(static_cast<uint8_t>(FUT091Command::BRIGHTNESS), V2PacketFormatter::tov2scale(value, BRIGHTNESS_SCALE_MAX, 2));
  8. }
  9. void FUT091PacketFormatter::updateTemperature(uint8_t value) {
  10. command(static_cast<uint8_t>(FUT091Command::KELVIN), V2PacketFormatter::tov2scale(value, KELVIN_SCALE_MAX, 2, false));
  11. }
  12. void FUT091PacketFormatter::enableNightMode() {
  13. uint8_t arg = groupCommandArg(OFF, groupId);
  14. command(static_cast<uint8_t>(FUT091Command::ON_OFF) | 0x80, arg);
  15. }
  16. BulbId FUT091PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& result) {
  17. uint8_t packetCopy[V2_PACKET_LEN];
  18. memcpy(packetCopy, packet, V2_PACKET_LEN);
  19. V2RFEncoding::decodeV2Packet(packetCopy);
  20. BulbId bulbId(
  21. (packetCopy[2] << 8) | packetCopy[3],
  22. packetCopy[7],
  23. REMOTE_TYPE_FUT091
  24. );
  25. uint8_t command = (packetCopy[V2_COMMAND_INDEX] & 0x7F);
  26. uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
  27. if (command == (uint8_t)FUT091Command::ON_OFF) {
  28. if ((packetCopy[V2_COMMAND_INDEX] & 0x80) == 0x80) {
  29. result["command"] = "night_mode";
  30. } else if (arg < 5) { // Group is not reliably encoded in group byte. Extract from arg byte
  31. result["state"] = "ON";
  32. bulbId.groupId = arg;
  33. } else {
  34. result["state"] = "OFF";
  35. bulbId.groupId = arg-5;
  36. }
  37. } else if (command == (uint8_t)FUT091Command::BRIGHTNESS) {
  38. uint8_t level = V2PacketFormatter::fromv2scale(arg, BRIGHTNESS_SCALE_MAX, 2, true, 0x13);
  39. result["brightness"] = Units::rescale<uint8_t, uint8_t>(level, 255, 100);
  40. } else if (command == (uint8_t)FUT091Command::KELVIN) {
  41. uint8_t kelvin = V2PacketFormatter::fromv2scale(arg, KELVIN_SCALE_MAX, 2, false, 0x13);
  42. result["color_temp"] = Units::whiteValToMireds(kelvin, 100);
  43. } else {
  44. result["button_id"] = command;
  45. result["argument"] = arg;
  46. }
  47. return bulbId;
  48. }