RgbwPacketFormatter.cpp 4.7 KB

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