MiLightClient.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. }
  67. }
  68. void MiLightClient::writeRgbw(
  69. const uint16_t deviceId,
  70. const uint8_t color,
  71. const uint8_t brightness,
  72. const uint8_t groupId,
  73. const uint8_t button) {
  74. MiLightPacket packet;
  75. packet.deviceType = RGBW;;
  76. packet.deviceId = deviceId;
  77. packet.b1 = color;
  78. packet.b2 = (brightness << 3) | (groupId & 0x07);
  79. packet.b3 = button;
  80. packet.sequenceNum = nextSequenceNum();
  81. write(RGBW, packet);
  82. }
  83. void MiLightClient::updateColorRaw(const uint16_t deviceId, const uint8_t groupId, const uint16_t color) {
  84. writeRgbw(deviceId, color, 0, groupId, RGBW_COLOR);
  85. }
  86. void MiLightClient::updateHue(const uint16_t deviceId, const uint8_t groupId, const uint16_t hue) {
  87. // Map color as a Hue value in [0, 359] to [0, 255]. The protocol also has
  88. // 0 being roughly magenta (#FF00FF)
  89. const int16_t remappedColor = (hue + 40) % 360;
  90. const uint8_t adjustedColor = round(remappedColor * (255 / 360.0));
  91. writeRgbw(deviceId, adjustedColor, 0, groupId, RGBW_COLOR);
  92. }
  93. void MiLightClient::updateBrightness(const uint16_t deviceId, const uint8_t groupId, const uint8_t brightness) {
  94. // Expect an input value in [0, 100]. Map it down to [0, 25].
  95. const uint8_t adjustedBrightness = round(brightness * (25 / 100.0));
  96. // The actual protocol uses a bizarre range where min is 16, max is 23:
  97. // [16, 15, ..., 0, 31, ..., 23]
  98. const uint8_t packetBrightnessValue = (
  99. ((31 - adjustedBrightness) + 17) % 32
  100. );
  101. writeRgbw(deviceId, 0, packetBrightnessValue, groupId, RGBW_BRIGHTNESS);
  102. }
  103. void MiLightClient::updateStatus(const uint16_t deviceId, const uint8_t groupId, MiLightStatus status) {
  104. uint8_t button = RGBW_GROUP_1_ON + ((groupId - 1)*2) + status;
  105. writeRgbw(deviceId, 0, 0, groupId, button);
  106. }
  107. void MiLightClient::updateColorWhite(const uint16_t deviceId, const uint8_t groupId) {
  108. uint8_t button = RGBW_GROUP_1_MAX_LEVEL + ((groupId - 1)*2);
  109. pressButton(deviceId, groupId, button);
  110. }
  111. void MiLightClient::pair(const uint16_t deviceId, const uint8_t groupId) {
  112. updateStatus(deviceId, groupId, ON);
  113. }
  114. void MiLightClient::unpair(const uint16_t deviceId, const uint8_t groupId) {
  115. updateStatus(deviceId, groupId, ON);
  116. delay(1);
  117. updateColorWhite(deviceId, groupId);
  118. }
  119. void MiLightClient::pressButton(const uint16_t deviceId, const uint8_t groupId, const uint8_t button) {
  120. writeRgbw(deviceId, 0, 0, groupId, button);
  121. }
  122. void MiLightClient::allOn(const uint16_t deviceId) {
  123. writeRgbw(deviceId, 0, 0, 0, RGBW_ALL_ON);
  124. }
  125. void MiLightClient::allOff(const uint16_t deviceId) {
  126. writeRgbw(deviceId, 0, 0, 0, RGBW_ALL_OFF);
  127. }