MiLightUdpServer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <MiLightUdpServer.h>
  2. MiLightUdpServer::MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
  3. : client(client),
  4. port(port),
  5. deviceId(deviceId),
  6. lastGroup(0)
  7. { }
  8. MiLightUdpServer::~MiLightUdpServer() {
  9. stop();
  10. }
  11. void MiLightUdpServer::begin() {
  12. socket.begin(this->port);
  13. }
  14. void MiLightUdpServer::stop() {
  15. socket.stop();
  16. }
  17. void MiLightUdpServer::handleClient() {
  18. const size_t packetSize = socket.parsePacket();
  19. if (packetSize) {
  20. if (packetSize >= 2 && packetSize <= 3) {
  21. socket.read(packetBuffer, packetSize);
  22. #ifdef MILIGHT_UDP_DEBUG
  23. Serial.print("Handling command: ");
  24. Serial.print(String(packetBuffer[0], HEX));
  25. Serial.print(" ");
  26. Serial.println(String(packetBuffer[1], HEX));
  27. #endif
  28. handleCommand(packetBuffer[0], packetBuffer[1]);
  29. } else {
  30. Serial.print("Error, unexpected packet length (should always be 2-3, was: ");
  31. Serial.println(packetSize);
  32. }
  33. }
  34. }
  35. void MiLightUdpServer::handleCommand(uint8_t command, uint8_t commandArg) {
  36. if (command >= UDP_RGBW_GROUP_1_ON && command <= UDP_RGBW_GROUP_4_OFF) {
  37. const MiLightStatus status = (command % 2) == 1 ? ON : OFF;
  38. const uint8_t groupId = (command - UDP_RGBW_GROUP_1_ON + 2)/2;
  39. client->updateStatus(RGBW, deviceId, groupId, status);
  40. this->lastGroup = groupId;
  41. } else if (command >= UDP_RGBW_GROUP_ALL_WHITE && command <= UDP_RGBW_GROUP_4_WHITE) {
  42. const uint8_t groupId = (command - UDP_RGBW_GROUP_ALL_WHITE)/2;
  43. client->updateColorWhite(deviceId, groupId);
  44. this->lastGroup = groupId;
  45. } else if (uint8_t cctGroup = cctCommandIdToGroup(command)) {
  46. client->updateStatus(
  47. CCT,
  48. deviceId,
  49. cctGroup,
  50. cctCommandToStatus(command)
  51. );
  52. this->lastGroup = cctGroup;
  53. }
  54. else {
  55. switch (command) {
  56. case UDP_RGBW_ALL_ON:
  57. client->allOn(RGBW, deviceId);
  58. break;
  59. case UDP_RGBW_ALL_OFF:
  60. client->allOff(RGBW, deviceId);
  61. break;
  62. case UDP_RGBW_COLOR:
  63. // UDP color is shifted by 0xC8 from 2.4 GHz color, and the spectrum is
  64. // flipped (R->B->G instead of R->G->B)
  65. client->updateColorRaw(deviceId, this->lastGroup, 0xFF-(commandArg + 0x35));
  66. break;
  67. case UDP_RGBW_DISCO_MODE:
  68. pressButton(this->lastGroup, RGBW_DISCO_MODE);
  69. break;
  70. case UDP_RGBW_SPEED_DOWN:
  71. pressButton(this->lastGroup, RGBW_SPEED_DOWN);
  72. break;
  73. case UDP_RGBW_SPEED_UP:
  74. pressButton(this->lastGroup, RGBW_SPEED_UP);
  75. break;
  76. case UDP_RGBW_BRIGHTNESS:
  77. // map [2, 27] --> [0, 100]
  78. client->updateBrightness(
  79. deviceId,
  80. this->lastGroup,
  81. round(((commandArg - 2) / 25.0)*100)
  82. );
  83. break;
  84. case UDP_CCT_BRIGHTNESS_DOWN:
  85. client->decreaseCctBrightness(deviceId, this->lastGroup);
  86. break;
  87. case UDP_CCT_BRIGHTNESS_UP:
  88. client->increaseCctBrightness(deviceId, this->lastGroup);
  89. break;
  90. case UDP_CCT_TEMPERATURE_DOWN:
  91. client->decreaseTemperature(deviceId, this->lastGroup);
  92. break;
  93. case UDP_CCT_TEMPERATURE_UP:
  94. client->increaseTemperature(deviceId, this->lastGroup);
  95. break;
  96. default:
  97. Serial.print("MiLightUdpServer - Unhandled command: ");
  98. Serial.println(command);
  99. }
  100. }
  101. }
  102. void MiLightUdpServer::pressButton(uint8_t group, uint8_t button) {
  103. client->writeRgbw(deviceId, 0, 0, group, button);
  104. }
  105. uint8_t MiLightUdpServer::cctCommandIdToGroup(uint8_t command) {
  106. switch (command) {
  107. case UDP_CCT_GROUP_1_ON:
  108. case UDP_CCT_GROUP_1_OFF:
  109. return 1;
  110. case UDP_CCT_GROUP_2_ON:
  111. case UDP_CCT_GROUP_2_OFF:
  112. return 2;
  113. case UDP_CCT_GROUP_3_ON:
  114. case UDP_CCT_GROUP_3_OFF:
  115. return 3;
  116. case UDP_CCT_GROUP_4_ON:
  117. case UDP_CCT_GROUP_4_OFF:
  118. return 4;
  119. }
  120. return 0;
  121. }
  122. MiLightStatus MiLightUdpServer::cctCommandToStatus(uint8_t command) {
  123. switch (command) {
  124. case UDP_CCT_GROUP_1_ON:
  125. case UDP_CCT_GROUP_2_ON:
  126. case UDP_CCT_GROUP_3_ON:
  127. case UDP_CCT_GROUP_4_ON:
  128. return ON;
  129. case UDP_CCT_GROUP_1_OFF:
  130. case UDP_CCT_GROUP_2_OFF:
  131. case UDP_CCT_GROUP_3_OFF:
  132. case UDP_CCT_GROUP_4_OFF:
  133. return OFF;
  134. }
  135. }