RgbCctPacketFormatter.cpp 4.3 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::initializePacket(uint8_t* packet) {
  18. size_t packetPtr = 0;
  19. // Always encode with 0x00 key. No utility in varying it.
  20. packet[packetPtr++] = 0x00;
  21. packet[packetPtr++] = 0x20;
  22. packet[packetPtr++] = deviceId >> 8;
  23. packet[packetPtr++] = deviceId & 0xFF;
  24. packet[packetPtr++] = 0;
  25. packet[packetPtr++] = 0;
  26. packet[packetPtr++] = sequenceNum++;
  27. packet[packetPtr++] = groupId;
  28. packet[packetPtr++] = 0;
  29. }
  30. void RgbCctPacketFormatter::command(uint8_t command, uint8_t arg) {
  31. pushPacket();
  32. currentPacket[RGB_CCT_COMMAND_INDEX] = command;
  33. currentPacket[RGB_CCT_ARGUMENT_INDEX] = arg;
  34. }
  35. void RgbCctPacketFormatter::updateStatus(MiLightStatus status, uint8_t groupId) {
  36. command(RGB_CCT_ON, groupId + (status == OFF ? 5 : 0));
  37. }
  38. void RgbCctPacketFormatter::updateBrightness(uint8_t brightness) {
  39. command(RGB_CCT_BRIGHTNESS, 0x8F + brightness);
  40. }
  41. void RgbCctPacketFormatter::updateHue(uint16_t value) {
  42. uint8_t remapped = rescale(value, 255, 360);
  43. updateColorRaw(remapped);
  44. }
  45. void RgbCctPacketFormatter::updateColorRaw(uint8_t value) {
  46. command(RGB_CCT_COLOR, 0x5F + value);
  47. }
  48. void RgbCctPacketFormatter::updateTemperature(uint8_t value) {
  49. command(RGB_CCT_KELVIN, 0x94 - (value*2));
  50. }
  51. void RgbCctPacketFormatter::updateSaturation(uint8_t value) {
  52. uint8_t remapped = value + 0xD;
  53. command(RGB_CCT_SATURATION, remapped);
  54. }
  55. void RgbCctPacketFormatter::updateColorWhite() {
  56. updateTemperature(0);
  57. }
  58. void RgbCctPacketFormatter::finalizePacket(uint8_t* packet) {
  59. encodeV2Packet(packet);
  60. }
  61. uint8_t RgbCctPacketFormatter::xorKey(uint8_t key) {
  62. // Generate most significant nibble
  63. const uint8_t shift = (key & 0x0F) < 0x04 ? 0 : 1;
  64. const uint8_t x = (((key & 0xF0) >> 4) + shift + 6) % 8;
  65. const uint8_t msn = (((4 + x) ^ 1) & 0x0F) << 4;
  66. // Generate least significant nibble
  67. const uint8_t lsn = ((((key & 0xF) + 4)^2) & 0x0F);
  68. return ( msn | lsn );
  69. }
  70. uint8_t RgbCctPacketFormatter::decodeByte(uint8_t byte, uint8_t s1, uint8_t xorKey, uint8_t s2) {
  71. uint8_t value = byte - s2;
  72. value = value ^ xorKey;
  73. value = value - s1;
  74. return value;
  75. }
  76. uint8_t RgbCctPacketFormatter::encodeByte(uint8_t byte, uint8_t s1, uint8_t xorKey, uint8_t s2) {
  77. uint8_t value = byte + s1;
  78. value = value ^ xorKey;
  79. value = value + s2;
  80. return value;
  81. }
  82. void RgbCctPacketFormatter::decodeV2Packet(uint8_t *packet) {
  83. uint8_t key = xorKey(packet[0]);
  84. for (size_t i = 1; i <= 8; i++) {
  85. packet[i] = decodeByte(packet[i], 0, key, V2_OFFSET(i, packet[0], V2_OFFSET_JUMP_START));
  86. }
  87. }
  88. void RgbCctPacketFormatter::encodeV2Packet(uint8_t *packet) {
  89. uint8_t key = xorKey(packet[0]);
  90. uint8_t sum = key;
  91. for (size_t i = 1; i <= 7; i++) {
  92. sum += packet[i];
  93. packet[i] = encodeByte(packet[i], 0, key, V2_OFFSET(i, packet[0], V2_OFFSET_JUMP_START));
  94. }
  95. packet[8] = encodeByte(sum, 3, key, V2_OFFSET(8, packet[0], 0));
  96. }
  97. void RgbCctPacketFormatter::format(uint8_t const* packet, char* buffer) {
  98. buffer += sprintf(buffer, "Raw packet: ");
  99. for (int i = 0; i < packetLength; i++) {
  100. buffer += sprintf(buffer, "%02X ", packet[i]);
  101. }
  102. uint8_t decodedPacket[packetLength];
  103. memcpy(decodedPacket, packet, packetLength);
  104. decodeV2Packet(decodedPacket);
  105. buffer += sprintf(buffer, "\n\nDecoded:\n");
  106. buffer += sprintf(buffer, "Key : %02X\n", decodedPacket[0]);
  107. buffer += sprintf(buffer, "b1 : %02X\n", decodedPacket[1]);
  108. buffer += sprintf(buffer, "ID : %02X%02X\n", decodedPacket[2], decodedPacket[3]);
  109. buffer += sprintf(buffer, "Command : %02X\n", decodedPacket[4]);
  110. buffer += sprintf(buffer, "Argument : %02X\n", decodedPacket[5]);
  111. buffer += sprintf(buffer, "Sequence : %02X\n", decodedPacket[6]);
  112. buffer += sprintf(buffer, "Group : %02X\n", decodedPacket[7]);
  113. buffer += sprintf(buffer, "Checksum : %02X", decodedPacket[8]);
  114. }