CctPacketFormatter.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <CctPacketFormatter.h>
  2. #include <MiLightButtons.h>
  3. void CctPacketFormatter::initializePacket(uint8_t* packet) {
  4. size_t packetPtr = 0;
  5. packet[packetPtr++] = CCT;
  6. packet[packetPtr++] = deviceId >> 8;
  7. packet[packetPtr++] = deviceId & 0xFF;
  8. packet[packetPtr++] = groupId;
  9. packet[packetPtr++] = 0;
  10. packet[packetPtr++] = sequenceNum;
  11. packet[packetPtr++] = sequenceNum++;
  12. }
  13. void CctPacketFormatter::updateBrightness(uint8_t value) {
  14. valueByStepFunction(
  15. &PacketFormatter::increaseBrightness,
  16. &PacketFormatter::decreaseBrightness,
  17. CCT_INTERVALS,
  18. value / CCT_INTERVALS
  19. );
  20. }
  21. void CctPacketFormatter::updateTemperature(uint8_t value) {
  22. valueByStepFunction(
  23. &PacketFormatter::increaseTemperature,
  24. &PacketFormatter::decreaseTemperature,
  25. CCT_INTERVALS,
  26. value / CCT_INTERVALS
  27. );
  28. }
  29. void CctPacketFormatter::command(uint8_t command, uint8_t arg) {
  30. pushPacket();
  31. if (held) {
  32. command |= 0x80;
  33. }
  34. currentPacket[CCT_COMMAND_INDEX] = command;
  35. }
  36. void CctPacketFormatter::updateStatus(MiLightStatus status, uint8_t groupId) {
  37. command(getCctStatusButton(groupId, status), 0);
  38. }
  39. void CctPacketFormatter::increaseTemperature() {
  40. command(CCT_TEMPERATURE_UP, 0);
  41. }
  42. void CctPacketFormatter::decreaseTemperature() {
  43. command(CCT_TEMPERATURE_DOWN, 0);
  44. }
  45. void CctPacketFormatter::increaseBrightness() {
  46. command(CCT_BRIGHTNESS_UP, 0);
  47. }
  48. void CctPacketFormatter::decreaseBrightness() {
  49. command(CCT_BRIGHTNESS_DOWN, 0);
  50. }
  51. void CctPacketFormatter::enableNightMode() {
  52. command(getCctStatusButton(groupId, OFF) | 0x10, 0);
  53. }
  54. uint8_t CctPacketFormatter::getCctStatusButton(uint8_t groupId, MiLightStatus status) {
  55. uint8_t button = 0;
  56. if (status == ON) {
  57. switch(groupId) {
  58. case 0:
  59. button = CCT_ALL_ON;
  60. break;
  61. case 1:
  62. button = CCT_GROUP_1_ON;
  63. break;
  64. case 2:
  65. button = CCT_GROUP_2_ON;
  66. break;
  67. case 3:
  68. button = CCT_GROUP_3_ON;
  69. break;
  70. case 4:
  71. button = CCT_GROUP_4_ON;
  72. break;
  73. }
  74. } else {
  75. switch(groupId) {
  76. case 0:
  77. button = CCT_ALL_OFF;
  78. break;
  79. case 1:
  80. button = CCT_GROUP_1_OFF;
  81. break;
  82. case 2:
  83. button = CCT_GROUP_2_OFF;
  84. break;
  85. case 3:
  86. button = CCT_GROUP_3_OFF;
  87. break;
  88. case 4:
  89. button = CCT_GROUP_4_OFF;
  90. break;
  91. }
  92. }
  93. return button;
  94. }
  95. uint8_t CctPacketFormatter::cctCommandIdToGroup(uint8_t command) {
  96. switch (command & 0xF) {
  97. case CCT_GROUP_1_ON:
  98. case CCT_GROUP_1_OFF:
  99. return 1;
  100. case CCT_GROUP_2_ON:
  101. case CCT_GROUP_2_OFF:
  102. return 2;
  103. case CCT_GROUP_3_ON:
  104. case CCT_GROUP_3_OFF:
  105. return 3;
  106. case CCT_GROUP_4_ON:
  107. case CCT_GROUP_4_OFF:
  108. return 4;
  109. case CCT_ALL_ON:
  110. case CCT_ALL_OFF:
  111. return 0;
  112. }
  113. return 255;
  114. }
  115. MiLightStatus CctPacketFormatter::cctCommandToStatus(uint8_t command) {
  116. switch (command & 0xF) {
  117. case CCT_GROUP_1_ON:
  118. case CCT_GROUP_2_ON:
  119. case CCT_GROUP_3_ON:
  120. case CCT_GROUP_4_ON:
  121. case CCT_ALL_ON:
  122. return ON;
  123. case CCT_GROUP_1_OFF:
  124. case CCT_GROUP_2_OFF:
  125. case CCT_GROUP_3_OFF:
  126. case CCT_GROUP_4_OFF:
  127. case CCT_ALL_OFF:
  128. return OFF;
  129. }
  130. }
  131. void CctPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result) {
  132. uint8_t command = packet[CCT_COMMAND_INDEX] & 0x7F;
  133. result["device_id"] = (packet[1] << 8) | packet[2];
  134. result["device_type"] = "cct";
  135. result["group_id"] = packet[3];
  136. uint8_t onOffGroupId = cctCommandIdToGroup(command);
  137. if (onOffGroupId < 255) {
  138. result["state"] = cctCommandToStatus(command) == ON ? "ON" : "OFF";
  139. }
  140. }
  141. void CctPacketFormatter::format(uint8_t const* packet, char* buffer) {
  142. PacketFormatter::formatV1Packet(packet, buffer);
  143. }