MiLightClient.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include <MiLightClient.h>
  2. MiLightRadio* MiLightClient::getRadio(const MiLightRadioType type) {
  3. MiLightRadio* radio = NULL;
  4. if (type == RGBW) {
  5. return rgbwRadio->getRadio();
  6. } else if (type == CCT) {
  7. return cctRadio->getRadio();
  8. }
  9. if (radio != NULL) {
  10. radio->configure();
  11. }
  12. return radio;
  13. }
  14. void MiLightClient::deserializePacket(const uint8_t rawPacket[], MiLightPacket& packet) {
  15. uint8_t ptr = 0;
  16. packet.deviceType = rawPacket[ptr++];
  17. packet.deviceId = (rawPacket[ptr++] << 8) | rawPacket[ptr++];
  18. packet.b1 = rawPacket[ptr++];
  19. packet.b2 = rawPacket[ptr++];
  20. packet.b3 = rawPacket[ptr++];
  21. packet.sequenceNum = rawPacket[ptr++];
  22. }
  23. void MiLightClient::serializePacket(uint8_t rawPacket[], const MiLightPacket& packet) {
  24. uint8_t ptr = 0;
  25. rawPacket[ptr++] = packet.deviceType;
  26. // big endian
  27. rawPacket[ptr++] = packet.deviceId >> 8;
  28. rawPacket[ptr++] = packet.deviceId & 0xFF;
  29. rawPacket[ptr++] = packet.b1;
  30. rawPacket[ptr++] = packet.b2;
  31. rawPacket[ptr++] = packet.b3;
  32. rawPacket[ptr++] = packet.sequenceNum;
  33. }
  34. uint8_t MiLightClient::nextSequenceNum() {
  35. return sequenceNum++;
  36. }
  37. bool MiLightClient::available(const MiLightRadioType radioType) {
  38. MiLightRadio* radio = getRadio(radioType);
  39. radio->begin();
  40. if (radio == NULL) {
  41. return false;
  42. }
  43. return radio->available();
  44. }
  45. void MiLightClient::read(const MiLightRadioType radioType, MiLightPacket& packet) {
  46. MiLightRadio* radio = getRadio(radioType);
  47. if (radio == NULL) {
  48. return;
  49. }
  50. uint8_t packetBytes[MILIGHT_PACKET_LENGTH];
  51. size_t length;
  52. radio->read(packetBytes, length);
  53. deserializePacket(packetBytes, packet);
  54. }
  55. void MiLightClient::write(const MiLightRadioType radioType,
  56. MiLightPacket& packet,
  57. const unsigned int resendCount) {
  58. uint8_t packetBytes[MILIGHT_PACKET_LENGTH];
  59. serializePacket(packetBytes, packet);
  60. MiLightRadio* radio = getRadio(radioType);
  61. if (radio == NULL) {
  62. return;
  63. }
  64. for (int i = 0; i < resendCount; i++) {
  65. radio->write(packetBytes, MILIGHT_PACKET_LENGTH);
  66. yield();
  67. }
  68. }
  69. void MiLightClient::writeRgbw(
  70. const uint16_t deviceId,
  71. const uint8_t color,
  72. const uint8_t brightness,
  73. const uint8_t groupId,
  74. const uint8_t button) {
  75. MiLightPacket packet;
  76. packet.deviceType = RGBW;
  77. packet.deviceId = deviceId;
  78. packet.b1 = color;
  79. packet.b2 = (brightness << 3) | (groupId & 0x07);
  80. packet.b3 = button;
  81. packet.sequenceNum = nextSequenceNum();
  82. write(RGBW, packet);
  83. }
  84. void MiLightClient::writeCct(const uint16_t deviceId,
  85. const uint8_t groupId,
  86. const uint8_t button) {
  87. MiLightPacket packet;
  88. packet.deviceType = CCT;
  89. packet.deviceId = deviceId;
  90. packet.b1 = groupId;
  91. packet.b2 = button;
  92. packet.b3 = nextSequenceNum();
  93. packet.sequenceNum = packet.b3;
  94. write(CCT, packet);
  95. }
  96. void MiLightClient::updateColorRaw(const uint16_t deviceId, const uint8_t groupId, const uint16_t color) {
  97. writeRgbw(deviceId, color, 0, groupId, RGBW_COLOR);
  98. }
  99. void MiLightClient::updateHue(const uint16_t deviceId, const uint8_t groupId, const uint16_t hue) {
  100. // Map color as a Hue value in [0, 359] to [0, 255]. The protocol also has
  101. // 0 being roughly magenta (#FF00FF)
  102. const int16_t remappedColor = (hue + 40) % 360;
  103. const uint8_t adjustedColor = round(remappedColor * (255 / 360.0));
  104. writeRgbw(deviceId, adjustedColor, 0, groupId, RGBW_COLOR);
  105. }
  106. void MiLightClient::updateBrightness(const uint16_t deviceId, const uint8_t groupId, const uint8_t brightness) {
  107. // Expect an input value in [0, 100]. Map it down to [0, 25].
  108. const uint8_t adjustedBrightness = round(brightness * (25 / 100.0));
  109. // The actual protocol uses a bizarre range where min is 16, max is 23:
  110. // [16, 15, ..., 0, 31, ..., 23]
  111. const uint8_t packetBrightnessValue = (
  112. ((31 - adjustedBrightness) + 17) % 32
  113. );
  114. writeRgbw(deviceId, 0, packetBrightnessValue, groupId, RGBW_BRIGHTNESS);
  115. }
  116. void MiLightClient::updateStatus(const MiLightRadioType type, const uint16_t deviceId, const uint8_t groupId, MiLightStatus status) {
  117. if (type == RGBW) {
  118. uint8_t button = RGBW_GROUP_1_ON + ((groupId - 1)*2) + status;
  119. writeRgbw(deviceId, 0, 0, groupId, button);
  120. } else {
  121. writeCct(deviceId, groupId, getCctStatusButton(groupId, status));
  122. }
  123. }
  124. void MiLightClient::updateColorWhite(const uint16_t deviceId, const uint8_t groupId) {
  125. uint8_t button = RGBW_GROUP_1_MAX_LEVEL + ((groupId - 1)*2);
  126. pressButton(RGBW, deviceId, groupId, button);
  127. }
  128. void MiLightClient::pair(const MiLightRadioType type, const uint16_t deviceId, const uint8_t groupId) {
  129. updateStatus(type, deviceId, groupId, ON);
  130. }
  131. void MiLightClient::unpair(const MiLightRadioType type, const uint16_t deviceId, const uint8_t groupId) {
  132. if (type == RGBW) {
  133. updateStatus(RGBW, deviceId, groupId, ON);
  134. delay(1);
  135. updateColorWhite(deviceId, groupId);
  136. } else if (type == CCT) {
  137. // Leading nibble is a "held" modifier
  138. uint8_t button = 0x10 | getCctStatusButton(groupId, ON);
  139. pressButton(CCT, deviceId, groupId, button);
  140. }
  141. }
  142. void MiLightClient::pressButton(const MiLightRadioType type, const uint16_t deviceId, const uint8_t groupId, const uint8_t button) {
  143. if (type == RGBW) {
  144. writeRgbw(deviceId, 0, 0, groupId, button);
  145. } else if (type == CCT) {
  146. writeCct(deviceId, groupId, button);
  147. }
  148. }
  149. void MiLightClient::allOn(const MiLightRadioType type, const uint16_t deviceId) {
  150. if (type == RGBW) {
  151. writeRgbw(deviceId, 0, 0, 0, RGBW_ALL_ON);
  152. } else if (type == CCT) {
  153. writeCct(deviceId, 0, CCT_ALL_ON);
  154. }
  155. }
  156. void MiLightClient::allOff(const MiLightRadioType type, const uint16_t deviceId) {
  157. if (type == RGBW) {
  158. writeRgbw(deviceId, 0, 0, 0, RGBW_ALL_OFF);
  159. } else if (type == CCT) {
  160. writeCct(deviceId, 0, CCT_ALL_OFF);
  161. }
  162. }
  163. void MiLightClient::increaseCctBrightness(const uint16_t deviceId, const uint8_t groupId) {
  164. writeCct(deviceId, groupId, CCT_BRIGHTNESS_UP);
  165. }
  166. void MiLightClient::decreaseCctBrightness(const uint16_t deviceId, const uint8_t groupId) {
  167. writeCct(deviceId, groupId, CCT_BRIGHTNESS_DOWN);
  168. }
  169. void MiLightClient::updateCctBrightness(const uint16_t deviceId, const uint8_t groupId, const uint8_t brightness) {
  170. for (int i = 0; i < MILIGHT_CCT_INTERVALS; i++) {
  171. decreaseCctBrightness(deviceId, groupId);
  172. }
  173. for (int i = 0; i < brightness/10; i++) {
  174. increaseCctBrightness(deviceId, groupId);
  175. }
  176. }
  177. void MiLightClient::increaseTemperature(const uint16_t deviceId, const uint8_t groupId) {
  178. writeCct(deviceId, groupId, CCT_TEMPERATURE_UP);
  179. }
  180. void MiLightClient::decreaseTemperature(const uint16_t deviceId, const uint8_t groupId) {
  181. writeCct(deviceId, groupId, CCT_TEMPERATURE_DOWN);
  182. }
  183. void MiLightClient::updateTemperature(const uint16_t deviceId, const uint8_t groupId, const uint8_t temperature) {
  184. for (int i = 0; i < MILIGHT_CCT_INTERVALS; i++) {
  185. decreaseTemperature(deviceId, groupId);
  186. }
  187. for (int i = 0; i < temperature; i++) {
  188. increaseTemperature(deviceId, groupId);
  189. }
  190. }
  191. uint8_t MiLightClient::getCctStatusButton(uint8_t groupId, MiLightStatus status) {
  192. uint8_t button = 0;
  193. if (status == ON) {
  194. switch(groupId) {
  195. case 1:
  196. button = CCT_GROUP_1_ON;
  197. break;
  198. case 2:
  199. button = CCT_GROUP_2_ON;
  200. break;
  201. case 3:
  202. button = CCT_GROUP_3_ON;
  203. break;
  204. case 4:
  205. button = CCT_GROUP_4_ON;
  206. break;
  207. }
  208. } else {
  209. switch(groupId) {
  210. case 1:
  211. button = CCT_GROUP_1_OFF;
  212. break;
  213. case 2:
  214. button = CCT_GROUP_2_OFF;
  215. break;
  216. case 3:
  217. button = CCT_GROUP_3_OFF;
  218. break;
  219. case 4:
  220. button = CCT_GROUP_4_OFF;
  221. break;
  222. }
  223. }
  224. return button;
  225. }
  226. MiLightRadioType MiLightClient::getRadioType(const String& typeName) {
  227. if (typeName.equalsIgnoreCase("rgbw")) {
  228. return RGBW;
  229. } else if (typeName.equalsIgnoreCase("cct")) {
  230. return CCT;
  231. } else {
  232. return UNKNOWN;
  233. }
  234. }