RgbwPacketFormatter.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <RgbwPacketFormatter.h>
  2. #include <Units.h>
  3. #include <MiLightCommands.h>
  4. #define STATUS_COMMAND(status, groupId) ( RGBW_GROUP_1_ON + (((groupId) - 1)*2) + (status) )
  5. #define GROUP_FOR_STATUS_COMMAND(buttonId) ( ((buttonId) - 1) / 2 )
  6. #define STATUS_FOR_COMMAND(buttonId) ( ((buttonId) % 2) == 0 ? OFF : ON )
  7. bool RgbwPacketFormatter::canHandle(const uint8_t *packet, const size_t len) {
  8. return len == packetLength && (packet[0] & 0xF0) == RGBW_PROTOCOL_ID_BYTE;
  9. }
  10. void RgbwPacketFormatter::initializePacket(uint8_t* packet) {
  11. size_t packetPtr = 0;
  12. packet[packetPtr++] = RGBW_PROTOCOL_ID_BYTE;
  13. packet[packetPtr++] = deviceId >> 8;
  14. packet[packetPtr++] = deviceId & 0xFF;
  15. packet[packetPtr++] = 0;
  16. packet[packetPtr++] = (groupId & 0x07);
  17. packet[packetPtr++] = 0;
  18. packet[packetPtr++] = sequenceNum++;
  19. }
  20. void RgbwPacketFormatter::unpair() {
  21. PacketFormatter::updateStatus(ON);
  22. updateColorWhite();
  23. }
  24. void RgbwPacketFormatter::modeSpeedDown() {
  25. command(RGBW_SPEED_DOWN, 0);
  26. }
  27. void RgbwPacketFormatter::modeSpeedUp() {
  28. command(RGBW_SPEED_UP, 0);
  29. }
  30. void RgbwPacketFormatter::nextMode() {
  31. updateMode((currentMode() + 1) % RGBW_NUM_MODES);
  32. }
  33. void RgbwPacketFormatter::previousMode() {
  34. updateMode((currentMode() + RGBW_NUM_MODES - 1) % RGBW_NUM_MODES);
  35. }
  36. uint8_t RgbwPacketFormatter::currentMode() {
  37. const GroupState* state = stateStore->get(deviceId, groupId, REMOTE_TYPE_RGBW);
  38. return state != NULL ? state->getMode() : 0;
  39. }
  40. void RgbwPacketFormatter::updateMode(uint8_t mode) {
  41. command(RGBW_DISCO_MODE, 0);
  42. currentPacket[0] = RGBW_PROTOCOL_ID_BYTE | mode;
  43. }
  44. void RgbwPacketFormatter::updateStatus(MiLightStatus status, uint8_t groupId) {
  45. command(STATUS_COMMAND(status, groupId), 0);
  46. }
  47. void RgbwPacketFormatter::updateBrightness(uint8_t value) {
  48. // Expect an input value in [0, 100]. Map it down to [0, 25].
  49. const uint8_t adjustedBrightness = Units::rescale(value, 25, 100);
  50. // The actual protocol uses a bizarre range where min is 16, max is 23:
  51. // [16, 15, ..., 0, 31, ..., 23]
  52. const uint8_t packetBrightnessValue = (
  53. ((31 - adjustedBrightness) + 17) % 32
  54. );
  55. command(RGBW_BRIGHTNESS, 0);
  56. currentPacket[RGBW_BRIGHTNESS_GROUP_INDEX] |= (packetBrightnessValue << 3);
  57. }
  58. void RgbwPacketFormatter::command(uint8_t command, uint8_t arg) {
  59. pushPacket();
  60. if (held) {
  61. command |= 0x80;
  62. }
  63. currentPacket[RGBW_COMMAND_INDEX] = command;
  64. }
  65. void RgbwPacketFormatter::updateHue(uint16_t value) {
  66. const int16_t remappedColor = (value + 40) % 360;
  67. updateColorRaw(Units::rescale(remappedColor, 255, 360));
  68. }
  69. void RgbwPacketFormatter::updateColorRaw(uint8_t value) {
  70. command(RGBW_COLOR, 0);
  71. currentPacket[RGBW_COLOR_INDEX] = value;
  72. }
  73. void RgbwPacketFormatter::updateColorWhite() {
  74. uint8_t button = RGBW_GROUP_1_MAX_LEVEL + ((groupId - 1)*2);
  75. command(button, 0);
  76. }
  77. void RgbwPacketFormatter::enableNightMode() {
  78. uint8_t button = STATUS_COMMAND(OFF, groupId);
  79. // Bulbs must be OFF for night mode to work in RGBW.
  80. // Turn it off if it isn't already off.
  81. const GroupState* state = stateStore->get(deviceId, groupId, REMOTE_TYPE_RGBW);
  82. if (state == NULL || state->getState() == MiLightStatus::ON) {
  83. command(button, 0);
  84. }
  85. // Night mode command has 0x10 bit set, but is otherwise
  86. // a repeat of the OFF command.
  87. command(button | 0x10, 0);
  88. }
  89. BulbId RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject result) {
  90. uint8_t command = packet[RGBW_COMMAND_INDEX] & 0x7F;
  91. BulbId bulbId(
  92. (packet[1] << 8) | packet[2],
  93. packet[RGBW_BRIGHTNESS_GROUP_INDEX] & 0x7,
  94. REMOTE_TYPE_RGBW
  95. );
  96. if (command >= RGBW_ALL_ON && command <= RGBW_GROUP_4_OFF) {
  97. result[GroupStateFieldNames::STATE] = (STATUS_FOR_COMMAND(command) == ON) ? "ON" : "OFF";
  98. // Determine group ID from button ID for on/off. The remote's state is from
  99. // the last packet sent, not the current one, and that can be wrong for
  100. // on/off commands.
  101. bulbId.groupId = GROUP_FOR_STATUS_COMMAND(command);
  102. } else if (command & 0x10) {
  103. if ((command % 2) == 0) {
  104. result[GroupStateFieldNames::COMMAND] = MiLightCommandNames::NIGHT_MODE;
  105. } else {
  106. result[GroupStateFieldNames::COMMAND] = MiLightCommandNames::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[GroupStateFieldNames::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[GroupStateFieldNames::HUE] = remappedColor;
  119. } else if (command == RGBW_SPEED_DOWN) {
  120. result[GroupStateFieldNames::COMMAND] = MiLightCommandNames::MODE_SPEED_DOWN;
  121. } else if (command == RGBW_SPEED_UP) {
  122. result[GroupStateFieldNames::COMMAND] = MiLightCommandNames::MODE_SPEED_UP;
  123. } else if (command == RGBW_DISCO_MODE) {
  124. result[GroupStateFieldNames::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. }