RgbwPacketFormatter.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. void RgbwPacketFormatter::initializePacket(uint8_t* packet) {
  6. size_t packetPtr = 0;
  7. packet[packetPtr++] = RGBW_PROTOCOL_ID_BYTE;
  8. packet[packetPtr++] = deviceId >> 8;
  9. packet[packetPtr++] = deviceId & 0xFF;
  10. packet[packetPtr++] = 0;
  11. packet[packetPtr++] = (groupId & 0x07);
  12. packet[packetPtr++] = 0;
  13. packet[packetPtr++] = sequenceNum++;
  14. }
  15. void RgbwPacketFormatter::unpair() {
  16. PacketFormatter::updateStatus(ON);
  17. updateColorWhite();
  18. }
  19. void RgbwPacketFormatter::modeSpeedDown() {
  20. command(RGBW_SPEED_DOWN, 0);
  21. }
  22. void RgbwPacketFormatter::modeSpeedUp() {
  23. command(RGBW_SPEED_UP, 0);
  24. }
  25. void RgbwPacketFormatter::nextMode() {
  26. lastMode = (lastMode + 1) % RGBW_NUM_MODES;
  27. updateMode(lastMode);
  28. }
  29. void RgbwPacketFormatter::previousMode() {
  30. lastMode = (lastMode - 1) % RGBW_NUM_MODES;
  31. updateMode(lastMode);
  32. }
  33. void RgbwPacketFormatter::updateMode(uint8_t mode) {
  34. command(RGBW_DISCO_MODE, 0);
  35. currentPacket[0] = RGBW_PROTOCOL_ID_BYTE | mode;
  36. }
  37. void RgbwPacketFormatter::updateStatus(MiLightStatus status, uint8_t groupId) {
  38. command(STATUS_COMMAND(status, groupId), 0);
  39. }
  40. void RgbwPacketFormatter::updateBrightness(uint8_t value) {
  41. // Expect an input value in [0, 100]. Map it down to [0, 25].
  42. const uint8_t adjustedBrightness = Units::rescale(value, 25, 100);
  43. // The actual protocol uses a bizarre range where min is 16, max is 23:
  44. // [16, 15, ..., 0, 31, ..., 23]
  45. const uint8_t packetBrightnessValue = (
  46. ((31 - adjustedBrightness) + 17) % 32
  47. );
  48. command(RGBW_BRIGHTNESS, 0);
  49. currentPacket[RGBW_BRIGHTNESS_GROUP_INDEX] |= (packetBrightnessValue << 3);
  50. }
  51. void RgbwPacketFormatter::command(uint8_t command, uint8_t arg) {
  52. pushPacket();
  53. if (held) {
  54. command |= 0x80;
  55. }
  56. currentPacket[RGBW_COMMAND_INDEX] = command;
  57. }
  58. void RgbwPacketFormatter::updateHue(uint16_t value) {
  59. const int16_t remappedColor = (value + 40) % 360;
  60. updateColorRaw(Units::rescale(remappedColor, 255, 360));
  61. }
  62. void RgbwPacketFormatter::updateColorRaw(uint8_t value) {
  63. command(RGBW_COLOR, 0);
  64. currentPacket[RGBW_COLOR_INDEX] = value;
  65. }
  66. void RgbwPacketFormatter::updateColorWhite() {
  67. uint8_t button = RGBW_GROUP_1_MAX_LEVEL + ((groupId - 1)*2);
  68. command(button, 0);
  69. }
  70. void RgbwPacketFormatter::enableNightMode() {
  71. if (groupId == 0) {
  72. //Serial.print("Sending RGBW_ALL_NIGHT... Group ");
  73. command(RGBW_ALL_NIGHT, 0);
  74. } else if (groupId == 1) {
  75. //Serial.print("Sending RGBW_GROUP_1_NIGHT... Group ");
  76. command(RGBW_GROUP_1_NIGHT, 0);
  77. } else if (groupId == 2) {
  78. //Serial.print("Sending RGBW_GROUP_2_NIGHT... Group ");
  79. command(RGBW_GROUP_2_NIGHT, 0);
  80. } else if (groupId == 3) {
  81. //Serial.print("Sending RGBW_GROUP_3_NIGHT... Group ");
  82. command(RGBW_GROUP_3_NIGHT, 0);
  83. } else if (groupId == 4) {
  84. //Serial.print("Sending RGBW_GROUP_4_NIGHT... Group ");
  85. command(RGBW_GROUP_4_NIGHT, 0);
  86. }
  87. //Serial.println(groupId);
  88. }
  89. BulbId RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore) {
  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["state"] = (command % 2) ? "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 == 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. }