RgbwPacketFormatter.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <RgbwPacketFormatter.h>
  2. #include <Units.h>
  3. #define STATUS_COMMAND(status, groupId) ( RGBW_GROUP_1_ON + (((groupId) - 1)*2) + (status) )
  4. #define GROUP_FOR_STATUS_COMMAND(buttonId) ( ((buttonId) - 1) / 2 )
  5. #define STATUS_FOR_COMMAND(buttonId) ( ((buttonId) % 2) == 0 ? OFF : ON )
  6. bool RgbwPacketFormatter::canHandle(const uint8_t *packet, const size_t len) {
  7. return len == packetLength && (packet[0] & 0xF0) == RGBW_PROTOCOL_ID_BYTE;
  8. }
  9. void RgbwPacketFormatter::initializePacket(uint8_t* packet) {
  10. size_t packetPtr = 0;
  11. packet[packetPtr++] = RGBW_PROTOCOL_ID_BYTE;
  12. packet[packetPtr++] = deviceId >> 8;
  13. packet[packetPtr++] = deviceId & 0xFF;
  14. packet[packetPtr++] = 0;
  15. packet[packetPtr++] = (groupId & 0x07);
  16. packet[packetPtr++] = 0;
  17. packet[packetPtr++] = sequenceNum++;
  18. }
  19. void RgbwPacketFormatter::unpair() {
  20. PacketFormatter::updateStatus(ON);
  21. updateColorWhite();
  22. }
  23. void RgbwPacketFormatter::modeSpeedDown() {
  24. command(RGBW_SPEED_DOWN, 0);
  25. }
  26. void RgbwPacketFormatter::modeSpeedUp() {
  27. command(RGBW_SPEED_UP, 0);
  28. }
  29. void RgbwPacketFormatter::nextMode() {
  30. lastMode = (lastMode + 1) % RGBW_NUM_MODES;
  31. updateMode(lastMode);
  32. }
  33. void RgbwPacketFormatter::previousMode() {
  34. lastMode = (lastMode - 1) % RGBW_NUM_MODES;
  35. updateMode(lastMode);
  36. }
  37. void RgbwPacketFormatter::updateMode(uint8_t mode) {
  38. command(RGBW_DISCO_MODE, 0);
  39. currentPacket[0] = RGBW_PROTOCOL_ID_BYTE | mode;
  40. }
  41. void RgbwPacketFormatter::updateStatus(MiLightStatus status, uint8_t groupId) {
  42. command(STATUS_COMMAND(status, groupId), 0);
  43. }
  44. void RgbwPacketFormatter::updateBrightness(uint8_t value) {
  45. // Expect an input value in [0, 100]. Map it down to [0, 25].
  46. const uint8_t adjustedBrightness = Units::rescale(value, 25, 100);
  47. // The actual protocol uses a bizarre range where min is 16, max is 23:
  48. // [16, 15, ..., 0, 31, ..., 23]
  49. const uint8_t packetBrightnessValue = (
  50. ((31 - adjustedBrightness) + 17) % 32
  51. );
  52. command(RGBW_BRIGHTNESS, 0);
  53. currentPacket[RGBW_BRIGHTNESS_GROUP_INDEX] |= (packetBrightnessValue << 3);
  54. }
  55. void RgbwPacketFormatter::command(uint8_t command, uint8_t arg) {
  56. pushPacket();
  57. if (held) {
  58. command |= 0x80;
  59. }
  60. currentPacket[RGBW_COMMAND_INDEX] = command;
  61. }
  62. void RgbwPacketFormatter::updateHue(uint16_t value) {
  63. const int16_t remappedColor = (value + 40) % 360;
  64. updateColorRaw(Units::rescale(remappedColor, 255, 360));
  65. }
  66. void RgbwPacketFormatter::updateColorRaw(uint8_t value) {
  67. command(RGBW_COLOR, 0);
  68. currentPacket[RGBW_COLOR_INDEX] = value;
  69. }
  70. void RgbwPacketFormatter::updateColorWhite() {
  71. uint8_t button = RGBW_GROUP_1_MAX_LEVEL + ((groupId - 1)*2);
  72. command(button, 0);
  73. }
  74. void RgbwPacketFormatter::enableNightMode() {
  75. uint8_t button = STATUS_COMMAND(OFF, groupId);
  76. command(button, 0);
  77. command(button | 0x10, 0);
  78. }
  79. BulbId RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore) {
  80. uint8_t command = packet[RGBW_COMMAND_INDEX] & 0x7F;
  81. BulbId bulbId(
  82. (packet[1] << 8) | packet[2],
  83. packet[RGBW_BRIGHTNESS_GROUP_INDEX] & 0x7,
  84. REMOTE_TYPE_RGBW
  85. );
  86. if (command >= RGBW_ALL_ON && command <= RGBW_GROUP_4_OFF) {
  87. result["state"] = (STATUS_FOR_COMMAND(command) == ON) ? "ON" : "OFF";
  88. // Determine group ID from button ID for on/off. The remote's state is from
  89. // the last packet sent, not the current one, and that can be wrong for
  90. // on/off commands.
  91. bulbId.groupId = GROUP_FOR_STATUS_COMMAND(command);
  92. } else if (command >= RGBW_ALL_MAX_LEVEL && command <= RGBW_GROUP_4_MIN_LEVEL) {
  93. if ((command % 2) == 0) {
  94. result["state"] = "ON";
  95. result["command"] = "night_mode";
  96. } else {
  97. result["command"] = "white_mode";
  98. }
  99. bulbId.groupId = GROUP_FOR_STATUS_COMMAND(command & 0xF);
  100. } else if (command == RGBW_BRIGHTNESS) {
  101. uint8_t brightness = 31;
  102. brightness -= packet[RGBW_BRIGHTNESS_GROUP_INDEX] >> 3;
  103. brightness += 17;
  104. brightness %= 32;
  105. result["brightness"] = Units::rescale<uint8_t, uint8_t>(brightness, 255, 25);
  106. } else if (command == RGBW_COLOR) {
  107. uint16_t remappedColor = Units::rescale<uint16_t, uint16_t>(packet[RGBW_COLOR_INDEX], 360.0, 255.0);
  108. remappedColor = (remappedColor + 320) % 360;
  109. result["hue"] = remappedColor;
  110. } else if (command == RGBW_SPEED_DOWN) {
  111. result["command"] = "mode_speed_down";
  112. } else if (command == RGBW_SPEED_UP) {
  113. result["command"] = "mode_speed_up";
  114. } else if (command == RGBW_DISCO_MODE) {
  115. result["mode"] = packet[0] & ~RGBW_PROTOCOL_ID_BYTE;
  116. } else {
  117. result["button_id"] = command;
  118. }
  119. return bulbId;
  120. }
  121. void RgbwPacketFormatter::format(uint8_t const* packet, char* buffer) {
  122. PacketFormatter::formatV1Packet(packet, buffer);
  123. }