RgbCctPacketFormatter.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <RgbCctPacketFormatter.h>
  2. #define V2_OFFSET(byte, key, jumpStart) ( \
  3. V2_OFFSETS[byte-1][key%4] \
  4. + \
  5. ((jumpStart > 0 && key >= jumpStart && key <= jumpStart+0x80) ? 0x80 : 0) \
  6. )
  7. uint8_t const RgbCctPacketFormatter::V2_OFFSETS[][4] = {
  8. { 0x45, 0x1F, 0x14, 0x5C },
  9. { 0x2B, 0xC9, 0xE3, 0x11 },
  10. { 0xEE, 0xDE, 0x0B, 0xAA },
  11. { 0xAF, 0x03, 0x1D, 0xF3 },
  12. { 0x1A, 0xE2, 0xF0, 0xD1 },
  13. { 0x04, 0xD8, 0x71, 0x42 },
  14. { 0xAF, 0x04, 0xDD, 0x07 },
  15. { 0xE1, 0x93, 0xB8, 0xE4 }
  16. };
  17. void RgbCctPacketFormatter::reset() {
  18. size_t packetPtr = 0;
  19. packet[packetPtr++] = 0x00;
  20. packet[packetPtr++] = 0x20;
  21. packet[packetPtr++] = deviceId >> 8;
  22. packet[packetPtr++] = deviceId & 0xFF;
  23. packet[packetPtr++] = 0;
  24. packet[packetPtr++] = 0;
  25. packet[packetPtr++] = sequenceNum++;
  26. packet[packetPtr++] = groupId;
  27. packet[packetPtr++] = 0;
  28. }
  29. void RgbCctPacketFormatter::command(uint8_t command, uint8_t arg) {
  30. packet[RGB_CCT_COMMAND_INDEX] = command;
  31. packet[RGB_CCT_ARGUMENT_INDEX] = arg;
  32. }
  33. void RgbCctPacketFormatter::updateStatus(MiLightStatus status, uint8_t groupId) {
  34. command(RGB_CCT_ON, groupId + (status == OFF ? 5 : 0));
  35. }
  36. void RgbCctPacketFormatter::updateBrightness(uint8_t brightness) {
  37. command(RGB_CCT_BRIGHTNESS, 0x8F + brightness);
  38. }
  39. void RgbCctPacketFormatter::updateHue(uint16_t value) {
  40. uint8_t remapped = rescale(value, 255, 360) + 0xA;
  41. updateColorRaw(remapped);
  42. }
  43. void RgbCctPacketFormatter::updateColorRaw(uint8_t value) {
  44. command(RGB_CCT_COLOR, value);
  45. }
  46. void RgbCctPacketFormatter::updateTemperature(uint8_t value) {
  47. command(RGB_CCT_KELVIN, (0xCC + value)*2);
  48. }
  49. void RgbCctPacketFormatter::updateSaturation(uint8_t value) {
  50. uint8_t remapped = 0x71 - value;
  51. command(RGB_CCT_SATURATION, remapped);
  52. }
  53. void RgbCctPacketFormatter::updateColorWhite() {
  54. updateTemperature(0);
  55. }
  56. uint8_t* RgbCctPacketFormatter::buildPacket() {
  57. encodeV2Packet(packet);
  58. return packet;
  59. }
  60. uint8_t RgbCctPacketFormatter::xorKey(uint8_t key) {
  61. // Generate most significant nibble
  62. const uint8_t shift = (key & 0x0F) < 0x04 ? 0 : 1;
  63. const uint8_t x = (((key & 0xF0) >> 4) + shift + 6) % 8;
  64. const uint8_t msn = (((4 + x) ^ 1) & 0x0F) << 4;
  65. // Generate least significant nibble
  66. const uint8_t lsn = ((((key & 0xF) + 4)^2) & 0x0F);
  67. return ( msn | lsn );
  68. }
  69. uint8_t RgbCctPacketFormatter::decodeByte(uint8_t byte, uint8_t s1, uint8_t xorKey, uint8_t s2) {
  70. uint8_t value = byte - s2;
  71. value = value ^ xorKey;
  72. value = value - s1;
  73. return value;
  74. }
  75. uint8_t RgbCctPacketFormatter::encodeByte(uint8_t byte, uint8_t s1, uint8_t xorKey, uint8_t s2) {
  76. uint8_t value = (byte + s1) % 0x100;
  77. value = value ^ xorKey;
  78. value = (value + s2) % 0x100;
  79. return value;
  80. }
  81. void RgbCctPacketFormatter::decodeV2Packet(uint8_t *packet) {
  82. uint8_t key = xorKey(packet[0]);
  83. for (size_t i = 1; i <= 8; i++) {
  84. packet[i] = decodeByte(packet[i], 0, key, V2_OFFSET(i, packet[0], V2_OFFSET_JUMP_START));
  85. }
  86. }
  87. void RgbCctPacketFormatter::encodeV2Packet(uint8_t *packet) {
  88. uint8_t key = xorKey(packet[0]);
  89. uint8_t sum = key;
  90. uint8_t command = packet[4];
  91. size_t ptr = 0;
  92. for (size_t i = 1; i <= 7; i++) {
  93. sum += packet[i];
  94. packet[i] = encodeByte(packet[i], 0, key, V2_OFFSET(i, packet[0], V2_OFFSET_JUMP_START));
  95. }
  96. packet[8] = encodeByte(sum, 3, key, V2_OFFSET(8, packet[0], 0));
  97. }
  98. void RgbCctPacketFormatter::format(uint8_t const* packet, char* buffer) {
  99. buffer += sprintf(buffer, "Raw packet: ");
  100. for (int i = 0; i < packetLength; i++) {
  101. buffer += sprintf(buffer, "%02X ", packet[i]);
  102. }
  103. uint8_t decodedPacket[packetLength];
  104. memcpy(decodedPacket, packet, packetLength);
  105. decodeV2Packet(decodedPacket);
  106. buffer += sprintf(buffer, "\n\nDecoded:\n");
  107. buffer += sprintf(buffer, "Key : %02X\n", decodedPacket[0]);
  108. buffer += sprintf(buffer, "b1 : %02X\n", decodedPacket[1]);
  109. buffer += sprintf(buffer, "ID : %02X%02X\n", decodedPacket[2], decodedPacket[3]);
  110. buffer += sprintf(buffer, "Command : %02X\n", decodedPacket[4]);
  111. buffer += sprintf(buffer, "Argument : %02X\n", decodedPacket[5]);
  112. buffer += sprintf(buffer, "Sequence : %02X\n", decodedPacket[6]);
  113. buffer += sprintf(buffer, "Group : %02X\n", decodedPacket[7]);
  114. buffer += sprintf(buffer, "Checksum : %02X", decodedPacket[8]);
  115. }