MiLightClient.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <MiLightClient.h>
  2. #include <MiLightRadioConfig.h>
  3. #include <Arduino.h>
  4. MiLightRadio* MiLightClient::switchRadio(const MiLightRadioType type) {
  5. RadioStack* stack = NULL;
  6. for (int i = 0; i < numRadios; i++) {
  7. if (radios[i]->config.type == type) {
  8. stack = radios[i];
  9. break;
  10. }
  11. }
  12. if (stack != NULL) {
  13. MiLightRadio *radio = stack->getRadio();
  14. if (currentRadio->config.type != stack->config.type) {
  15. radio->configure();
  16. }
  17. currentRadio = stack;
  18. formatter = stack->config.packetFormatter;
  19. return radio;
  20. } else {
  21. Serial.print("MiLightClient - tried to get radio for unknown type: ");
  22. Serial.println(type);
  23. }
  24. return NULL;
  25. }
  26. void MiLightClient::prepare(MiLightRadioConfig& config,
  27. const uint16_t deviceId,
  28. const uint8_t groupId) {
  29. switchRadio(config.type);
  30. if (deviceId >= 0 && groupId >= 0) {
  31. formatter->prepare(deviceId, groupId);
  32. }
  33. }
  34. void MiLightClient::setResendCount(const unsigned int resendCount) {
  35. this->resendCount = resendCount;
  36. }
  37. bool MiLightClient::available() {
  38. if (currentRadio == NULL) {
  39. return false;
  40. }
  41. return currentRadio->getRadio()->available();
  42. }
  43. void MiLightClient::read(uint8_t packet[]) {
  44. if (currentRadio == NULL) {
  45. return;
  46. }
  47. size_t length;
  48. currentRadio->getRadio()->read(packet, length);
  49. }
  50. void MiLightClient::write(uint8_t packet[]) {
  51. if (currentRadio == NULL) {
  52. return;
  53. }
  54. #ifdef DEBUG_PRINTF
  55. printf("Sending packet: ");
  56. for (int i = 0; i < currentRadio->config.getPacketLength(); i++) {
  57. printf("%02X", packet[i]);
  58. }
  59. printf("\n");
  60. #endif
  61. for (int i = 0; i < this->resendCount; i++) {
  62. currentRadio->getRadio()->write(packet, currentRadio->config.getPacketLength());
  63. }
  64. }
  65. void MiLightClient::updateColorRaw(const uint8_t color) {
  66. formatter->updateColorRaw(color);
  67. flushPacket();
  68. }
  69. void MiLightClient::updateHue(const uint16_t hue) {
  70. formatter->updateHue(hue);
  71. flushPacket();
  72. }
  73. void MiLightClient::updateBrightness(const uint8_t brightness) {
  74. formatter->updateBrightness(brightness);
  75. flushPacket();
  76. }
  77. void MiLightClient::updateStatus(MiLightStatus status, uint8_t groupId) {
  78. formatter->updateStatus(status, groupId);
  79. flushPacket();
  80. }
  81. void MiLightClient::updateStatus(MiLightStatus status) {
  82. formatter->updateStatus(status);
  83. flushPacket();
  84. }
  85. void MiLightClient::updateSaturation(const uint8_t value) {
  86. formatter->updateSaturation(value);
  87. flushPacket();
  88. }
  89. void MiLightClient::updateColorWhite() {
  90. formatter->updateColorWhite();
  91. flushPacket();
  92. }
  93. void MiLightClient::pair() {
  94. formatter->pair();
  95. flushPacket();
  96. }
  97. void MiLightClient::unpair() {
  98. formatter->unpair();
  99. flushPacket();
  100. }
  101. void MiLightClient::increaseBrightness() {
  102. formatter->increaseBrightness();
  103. flushPacket();
  104. }
  105. void MiLightClient::decreaseBrightness() {
  106. formatter->decreaseBrightness();
  107. flushPacket();
  108. }
  109. void MiLightClient::increaseTemperature() {
  110. formatter->increaseTemperature();
  111. flushPacket();
  112. }
  113. void MiLightClient::decreaseTemperature() {
  114. formatter->decreaseTemperature();
  115. flushPacket();
  116. }
  117. void MiLightClient::updateTemperature(const uint8_t temperature) {
  118. formatter->updateTemperature(temperature);
  119. flushPacket();
  120. }
  121. void MiLightClient::command(uint8_t command, uint8_t arg) {
  122. formatter->command(command, arg);
  123. flushPacket();
  124. }
  125. void MiLightClient::formatPacket(uint8_t* packet, char* buffer) {
  126. formatter->format(packet, buffer);
  127. }
  128. void MiLightClient::flushPacket() {
  129. PacketStream& stream = formatter->buildPackets();
  130. const size_t prevNumRepeats = this->resendCount;
  131. // When sending multiple packets, normalize the number of repeats
  132. if (stream.numPackets > 1) {
  133. setResendCount(MILIGHT_DEFAULT_RESEND_COUNT);
  134. }
  135. while (stream.hasNext()) {
  136. write(stream.next());
  137. if (stream.hasNext()) {
  138. delay(10);
  139. }
  140. }
  141. setResendCount(prevNumRepeats);
  142. formatter->reset();
  143. }