RgbwPacketFormatter.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // Bulbs must be OFF for night mode to work in RGBW.
  79. // Turn it off if it isn't already off.
  80. const GroupState* state = stateStore->get(deviceId, groupId, REMOTE_TYPE_RGBW);
  81. if (state == NULL || state->getState() == MiLightStatus::ON) {
  82. command(button, 0);
  83. }
  84. // Night mode command has 0x10 bit set, but is otherwise
  85. // a repeat of the OFF command.
  86. command(button | 0x10, 0);
  87. }
  88. BulbId RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject result) {
  89. uint8_t command = packet[RGBW_COMMAND_INDEX] & 0x7F;
  90. BulbId bulbId(
  91. (packet[1] << 8) | packet[2],
  92. packet[RGBW_BRIGHTNESS_GROUP_INDEX] & 0x7,
  93. REMOTE_TYPE_RGBW
  94. );
  95. if (command >= RGBW_ALL_ON && command <= RGBW_GROUP_4_OFF) {
  96. result["state"] = (STATUS_FOR_COMMAND(command) == ON) ? "ON" : "OFF";
  97. // Determine group ID from button ID for on/off. The remote's state is from
  98. // the last packet sent, not the current one, and that can be wrong for
  99. // on/off commands.
  100. bulbId.groupId = GROUP_FOR_STATUS_COMMAND(command);
  101. } else if (command >= RGBW_ALL_MAX_LEVEL && command <= RGBW_GROUP_4_MIN_LEVEL) {
  102. if ((command % 2) == 0) {
  103. result["state"] = "ON";
  104. result["command"] = "night_mode";
  105. } else {
  106. result["command"] = "set_white";
  107. }
  108. bulbId.groupId = GROUP_FOR_STATUS_COMMAND(command & 0xF);
  109. } else if (command == RGBW_BRIGHTNESS) {
  110. uint8_t brightness = 31;
  111. brightness -= packet[RGBW_BRIGHTNESS_GROUP_INDEX] >> 3;
  112. brightness += 17;
  113. brightness %= 32;
  114. result["brightness"] = Units::rescale<uint8_t, uint8_t>(brightness, 255, 25);
  115. } else if (command == RGBW_COLOR) {
  116. uint16_t remappedColor = Units::rescale<uint16_t, uint16_t>(packet[RGBW_COLOR_INDEX], 360.0, 255.0);
  117. remappedColor = (remappedColor + 320) % 360;
  118. result["hue"] = remappedColor;
  119. } else if (command == RGBW_SPEED_DOWN) {
  120. result["command"] = "mode_speed_down";
  121. } else if (command == RGBW_SPEED_UP) {
  122. result["command"] = "mode_speed_up";
  123. } else if (command == RGBW_DISCO_MODE) {
  124. result["mode"] = packet[0] & ~RGBW_PROTOCOL_ID_BYTE;
  125. } else {
  126. result["button_id"] = command;
  127. }
  128. return bulbId;
  129. }
  130. void RgbwPacketFormatter::format(uint8_t const* packet, char* buffer) {
  131. PacketFormatter::formatV1Packet(packet, buffer);
  132. }