CctPacketFormatter.cpp 4.1 KB

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