MiLightClient.cpp 7.3 KB

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