RgbCctPacketFormatter.cpp 4.4 KB

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