MiLightClient.cpp 7.4 KB

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