MiLightClient.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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::updateMode(uint8_t mode) {
  78. formatter->updateMode(mode);
  79. flushPacket();
  80. }
  81. void MiLightClient::nextMode() {
  82. formatter->nextMode();
  83. flushPacket();
  84. }
  85. void MiLightClient::previousMode() {
  86. formatter->previousMode();
  87. flushPacket();
  88. }
  89. void MiLightClient::modeSpeedDown() {
  90. formatter->modeSpeedDown();
  91. flushPacket();
  92. }
  93. void MiLightClient::modeSpeedUp() {
  94. formatter->modeSpeedUp();
  95. flushPacket();
  96. }
  97. void MiLightClient::updateStatus(MiLightStatus status, uint8_t groupId) {
  98. formatter->updateStatus(status, groupId);
  99. flushPacket();
  100. }
  101. void MiLightClient::updateStatus(MiLightStatus status) {
  102. formatter->updateStatus(status);
  103. flushPacket();
  104. }
  105. void MiLightClient::updateSaturation(const uint8_t value) {
  106. formatter->updateSaturation(value);
  107. flushPacket();
  108. }
  109. void MiLightClient::updateColorWhite() {
  110. formatter->updateColorWhite();
  111. flushPacket();
  112. }
  113. void MiLightClient::pair() {
  114. formatter->pair();
  115. flushPacket();
  116. }
  117. void MiLightClient::unpair() {
  118. formatter->unpair();
  119. flushPacket();
  120. }
  121. void MiLightClient::increaseBrightness() {
  122. formatter->increaseBrightness();
  123. flushPacket();
  124. }
  125. void MiLightClient::decreaseBrightness() {
  126. formatter->decreaseBrightness();
  127. flushPacket();
  128. }
  129. void MiLightClient::increaseTemperature() {
  130. formatter->increaseTemperature();
  131. flushPacket();
  132. }
  133. void MiLightClient::decreaseTemperature() {
  134. formatter->decreaseTemperature();
  135. flushPacket();
  136. }
  137. void MiLightClient::updateTemperature(const uint8_t temperature) {
  138. formatter->updateTemperature(temperature);
  139. flushPacket();
  140. }
  141. void MiLightClient::command(uint8_t command, uint8_t arg) {
  142. formatter->command(command, arg);
  143. flushPacket();
  144. }
  145. void MiLightClient::formatPacket(uint8_t* packet, char* buffer) {
  146. formatter->format(packet, buffer);
  147. }
  148. void MiLightClient::flushPacket() {
  149. PacketStream& stream = formatter->buildPackets();
  150. const size_t prevNumRepeats = this->resendCount;
  151. // When sending multiple packets, normalize the number of repeats
  152. if (stream.numPackets > 1) {
  153. setResendCount(MILIGHT_DEFAULT_RESEND_COUNT);
  154. }
  155. while (stream.hasNext()) {
  156. write(stream.next());
  157. if (stream.hasNext()) {
  158. delay(10);
  159. }
  160. }
  161. setResendCount(prevNumRepeats);
  162. formatter->reset();
  163. }