MiLightClient.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. for (int i = 0; i < 5; i++) {
  138. updateStatus(CCT, deviceId, groupId, ON);
  139. delay(1);
  140. }
  141. }
  142. }
  143. void MiLightClient::pressButton(const MiLightRadioType type, const uint16_t deviceId, const uint8_t groupId, const uint8_t button) {
  144. if (type == RGBW) {
  145. writeRgbw(deviceId, 0, 0, groupId, button);
  146. } else if (type == CCT) {
  147. writeCct(deviceId, groupId, button);
  148. }
  149. }
  150. void MiLightClient::allOn(const MiLightRadioType type, const uint16_t deviceId) {
  151. if (type == RGBW) {
  152. writeRgbw(deviceId, 0, 0, 0, RGBW_ALL_ON);
  153. } else if (type == CCT) {
  154. writeCct(deviceId, 0, CCT_ALL_ON);
  155. }
  156. }
  157. void MiLightClient::allOff(const MiLightRadioType type, const uint16_t deviceId) {
  158. if (type == RGBW) {
  159. writeRgbw(deviceId, 0, 0, 0, RGBW_ALL_OFF);
  160. } else if (type == CCT) {
  161. writeCct(deviceId, 0, CCT_ALL_OFF);
  162. }
  163. }
  164. void MiLightClient::increaseCctBrightness(const uint16_t deviceId, const uint8_t groupId) {
  165. writeCct(deviceId, groupId, CCT_BRIGHTNESS_UP);
  166. }
  167. void MiLightClient::decreaseCctBrightness(const uint16_t deviceId, const uint8_t groupId) {
  168. writeCct(deviceId, groupId, CCT_BRIGHTNESS_DOWN);
  169. }
  170. void MiLightClient::updateCctBrightness(const uint16_t deviceId, const uint8_t groupId, const uint8_t brightness) {
  171. for (int i = 0; i < MILIGHT_CCT_INTERVALS; i++) {
  172. decreaseCctBrightness(deviceId, groupId);
  173. }
  174. for (int i = 0; i < brightness/10; i++) {
  175. increaseCctBrightness(deviceId, groupId);
  176. }
  177. }
  178. void MiLightClient::increaseTemperature(const uint16_t deviceId, const uint8_t groupId) {
  179. writeCct(deviceId, groupId, CCT_TEMPERATURE_UP);
  180. }
  181. void MiLightClient::decreaseTemperature(const uint16_t deviceId, const uint8_t groupId) {
  182. writeCct(deviceId, groupId, CCT_TEMPERATURE_DOWN);
  183. }
  184. void MiLightClient::updateTemperature(const uint16_t deviceId, const uint8_t groupId, const uint8_t temperature) {
  185. for (int i = 0; i < MILIGHT_CCT_INTERVALS; i++) {
  186. decreaseTemperature(deviceId, groupId);
  187. }
  188. for (int i = 0; i < temperature; i++) {
  189. increaseTemperature(deviceId, groupId);
  190. }
  191. }
  192. uint8_t MiLightClient::getCctStatusButton(uint8_t groupId, MiLightStatus status) {
  193. uint8_t button = 0;
  194. if (status == ON) {
  195. switch(groupId) {
  196. case 1:
  197. button = CCT_GROUP_1_ON;
  198. break;
  199. case 2:
  200. button = CCT_GROUP_2_ON;
  201. break;
  202. case 3:
  203. button = CCT_GROUP_3_ON;
  204. break;
  205. case 4:
  206. button = CCT_GROUP_4_ON;
  207. break;
  208. }
  209. } else {
  210. switch(groupId) {
  211. case 1:
  212. button = CCT_GROUP_1_OFF;
  213. break;
  214. case 2:
  215. button = CCT_GROUP_2_OFF;
  216. break;
  217. case 3:
  218. button = CCT_GROUP_3_OFF;
  219. break;
  220. case 4:
  221. button = CCT_GROUP_4_OFF;
  222. break;
  223. }
  224. }
  225. return button;
  226. }
  227. MiLightRadioType MiLightClient::getRadioType(const String& typeName) {
  228. if (typeName.equalsIgnoreCase("rgbw")) {
  229. return RGBW;
  230. } else if (typeName.equalsIgnoreCase("cct")) {
  231. return CCT;
  232. } else {
  233. return UNKNOWN;
  234. }
  235. }