MiLightClient.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #include <MiLightClient.h>
  2. #include <MiLightRadioConfig.h>
  3. #include <Arduino.h>
  4. #include <RGBConverter.h>
  5. #include <Units.h>
  6. MiLightClient::MiLightClient(MiLightRadioFactory* radioFactory, GroupStateStore& stateStore)
  7. : resendCount(MILIGHT_DEFAULT_RESEND_COUNT),
  8. currentRadio(NULL),
  9. currentRemote(NULL),
  10. numRadios(MiLightRadioConfig::NUM_CONFIGS),
  11. packetSentHandler(NULL),
  12. stateStore(stateStore)
  13. {
  14. radios = new MiLightRadio*[numRadios];
  15. for (size_t i = 0; i < numRadios; i++) {
  16. radios[i] = radioFactory->create(MiLightRadioConfig::ALL_CONFIGS[i]);
  17. }
  18. }
  19. void MiLightClient::begin() {
  20. for (size_t i = 0; i < numRadios; i++) {
  21. radios[i]->begin();
  22. }
  23. switchRadio(static_cast<size_t>(0));
  24. }
  25. void MiLightClient::setHeld(bool held) {
  26. currentRemote->packetFormatter->setHeld(held);
  27. }
  28. size_t MiLightClient::getNumRadios() const {
  29. return numRadios;
  30. }
  31. MiLightRadio* MiLightClient::switchRadio(size_t radioIx) {
  32. if (radioIx >= getNumRadios()) {
  33. return NULL;
  34. }
  35. if (this->currentRadio != radios[radioIx]) {
  36. this->currentRadio = radios[radioIx];
  37. this->currentRadio->configure();
  38. }
  39. return this->currentRadio;
  40. }
  41. MiLightRadio* MiLightClient::switchRadio(const MiLightRemoteConfig* remoteConfig) {
  42. MiLightRadio* radio;
  43. for (int i = 0; i < numRadios; i++) {
  44. if (&this->radios[i]->config() == &remoteConfig->radioConfig) {
  45. radio = switchRadio(i);
  46. break;
  47. }
  48. }
  49. return radio;
  50. }
  51. void MiLightClient::prepare(const MiLightRemoteConfig* config,
  52. const uint16_t deviceId,
  53. const uint8_t groupId
  54. ) {
  55. switchRadio(config);
  56. this->currentRemote = config;
  57. if (deviceId >= 0 && groupId >= 0) {
  58. currentRemote->packetFormatter->prepare(deviceId, groupId);
  59. }
  60. }
  61. void MiLightClient::prepare(const MiLightRemoteType type,
  62. const uint16_t deviceId,
  63. const uint8_t groupId
  64. ) {
  65. prepare(MiLightRemoteConfig::fromType(type));
  66. }
  67. void MiLightClient::setResendCount(const unsigned int resendCount) {
  68. this->resendCount = resendCount;
  69. }
  70. bool MiLightClient::available() {
  71. if (currentRadio == NULL) {
  72. return false;
  73. }
  74. return currentRadio->available();
  75. }
  76. size_t MiLightClient::read(uint8_t packet[]) {
  77. if (currentRadio == NULL) {
  78. return 0;
  79. }
  80. size_t length;
  81. currentRadio->read(packet, length);
  82. return length;
  83. }
  84. void MiLightClient::write(uint8_t packet[]) {
  85. if (currentRadio == NULL) {
  86. return;
  87. }
  88. #ifdef DEBUG_PRINTF
  89. printf("Sending packet: ");
  90. for (int i = 0; i < currentRemote->packetFormatter->getPacketLength(); i++) {
  91. printf("%02X", packet[i]);
  92. }
  93. printf("\n");
  94. int iStart = millis();
  95. #endif
  96. for (int i = 0; i < this->resendCount; i++) {
  97. currentRadio->write(packet, currentRemote->packetFormatter->getPacketLength());
  98. }
  99. if (this->packetSentHandler) {
  100. this->packetSentHandler(packet, *currentRemote);
  101. }
  102. #ifdef DEBUG_PRINTF
  103. int iElapsed = millis() - iStart;
  104. Serial.print("Elapsed: ");
  105. Serial.println(iElapsed);
  106. #endif
  107. }
  108. void MiLightClient::updateColorRaw(const uint8_t color) {
  109. currentRemote->packetFormatter->updateColorRaw(color);
  110. flushPacket();
  111. }
  112. void MiLightClient::updateHue(const uint16_t hue) {
  113. currentRemote->packetFormatter->updateHue(hue);
  114. flushPacket();
  115. }
  116. void MiLightClient::updateBrightness(const uint8_t brightness) {
  117. currentRemote->packetFormatter->updateBrightness(brightness);
  118. flushPacket();
  119. }
  120. void MiLightClient::updateMode(uint8_t mode) {
  121. currentRemote->packetFormatter->updateMode(mode);
  122. flushPacket();
  123. }
  124. void MiLightClient::nextMode() {
  125. currentRemote->packetFormatter->nextMode();
  126. flushPacket();
  127. }
  128. void MiLightClient::previousMode() {
  129. currentRemote->packetFormatter->previousMode();
  130. flushPacket();
  131. }
  132. void MiLightClient::modeSpeedDown() {
  133. currentRemote->packetFormatter->modeSpeedDown();
  134. flushPacket();
  135. }
  136. void MiLightClient::modeSpeedUp() {
  137. currentRemote->packetFormatter->modeSpeedUp();
  138. flushPacket();
  139. }
  140. void MiLightClient::updateStatus(MiLightStatus status, uint8_t groupId) {
  141. currentRemote->packetFormatter->updateStatus(status, groupId);
  142. flushPacket();
  143. }
  144. void MiLightClient::updateStatus(MiLightStatus status) {
  145. currentRemote->packetFormatter->updateStatus(status);
  146. flushPacket();
  147. }
  148. void MiLightClient::updateSaturation(const uint8_t value) {
  149. currentRemote->packetFormatter->updateSaturation(value);
  150. flushPacket();
  151. }
  152. void MiLightClient::updateColorWhite() {
  153. currentRemote->packetFormatter->updateColorWhite();
  154. flushPacket();
  155. }
  156. void MiLightClient::enableNightMode() {
  157. currentRemote->packetFormatter->enableNightMode();
  158. flushPacket();
  159. }
  160. void MiLightClient::pair() {
  161. currentRemote->packetFormatter->pair();
  162. flushPacket();
  163. }
  164. void MiLightClient::unpair() {
  165. currentRemote->packetFormatter->unpair();
  166. flushPacket();
  167. }
  168. void MiLightClient::increaseBrightness() {
  169. currentRemote->packetFormatter->increaseBrightness();
  170. flushPacket();
  171. }
  172. void MiLightClient::decreaseBrightness() {
  173. currentRemote->packetFormatter->decreaseBrightness();
  174. flushPacket();
  175. }
  176. void MiLightClient::increaseTemperature() {
  177. currentRemote->packetFormatter->increaseTemperature();
  178. flushPacket();
  179. }
  180. void MiLightClient::decreaseTemperature() {
  181. currentRemote->packetFormatter->decreaseTemperature();
  182. flushPacket();
  183. }
  184. void MiLightClient::updateTemperature(const uint8_t temperature) {
  185. currentRemote->packetFormatter->updateTemperature(temperature);
  186. flushPacket();
  187. }
  188. void MiLightClient::command(uint8_t command, uint8_t arg) {
  189. currentRemote->packetFormatter->command(command, arg);
  190. flushPacket();
  191. }
  192. void MiLightClient::update(const JsonObject& request) {
  193. const uint8_t parsedStatus = this->parseStatus(request);
  194. // Always turn on first
  195. if (parsedStatus == ON) {
  196. this->updateStatus(ON);
  197. }
  198. if (request.containsKey("command")) {
  199. this->handleCommand(request["command"]);
  200. }
  201. if (request.containsKey("commands")) {
  202. JsonArray& commands = request["commands"];
  203. if (commands.success()) {
  204. for (size_t i = 0; i < commands.size(); i++) {
  205. this->handleCommand(commands.get<String>(i));
  206. }
  207. }
  208. }
  209. //Homeassistant - Handle effect
  210. if (request.containsKey("effect")) {
  211. this->handleEffect(request["effect"]);
  212. }
  213. if (request.containsKey("hue")) {
  214. this->updateHue(request["hue"]);
  215. }
  216. if (request.containsKey("saturation")) {
  217. this->updateSaturation(request["saturation"]);
  218. }
  219. // Convert RGB to HSV
  220. if (request.containsKey("color")) {
  221. JsonObject& color = request["color"];
  222. uint8_t r = color["r"];
  223. uint8_t g = color["g"];
  224. uint8_t b = color["b"];
  225. //If close to white
  226. if( r > 256 - RGB_WHITE_BOUNDARY && g > 256 - RGB_WHITE_BOUNDARY && b > 256 - RGB_WHITE_BOUNDARY) {
  227. this->updateColorWhite();
  228. } else {
  229. double hsv[3];
  230. RGBConverter converter;
  231. converter.rgbToHsv(r, g, b, hsv);
  232. uint16_t hue = round(hsv[0]*360);
  233. uint8_t saturation = round(hsv[1]*100);
  234. this->updateHue(hue);
  235. this->updateSaturation(saturation);
  236. }
  237. }
  238. if (request.containsKey("level")) {
  239. this->updateBrightness(request["level"]);
  240. }
  241. // HomeAssistant
  242. if (request.containsKey("brightness")) {
  243. uint8_t scaledBrightness = Units::rescale(request.get<uint8_t>("brightness"), 100, 255);
  244. this->updateBrightness(scaledBrightness);
  245. }
  246. if (request.containsKey("temperature")) {
  247. this->updateTemperature(request["temperature"]);
  248. }
  249. // HomeAssistant
  250. if (request.containsKey("color_temp")) {
  251. this->updateTemperature(
  252. Units::miredsToWhiteVal(request["color_temp"], 100)
  253. );
  254. }
  255. if (request.containsKey("mode")) {
  256. this->updateMode(request["mode"]);
  257. }
  258. // Raw packet command/args
  259. if (request.containsKey("button_id") && request.containsKey("argument")) {
  260. this->command(request["button_id"], request["argument"]);
  261. }
  262. // Always turn off last
  263. if (parsedStatus == OFF) {
  264. this->updateStatus(OFF);
  265. }
  266. }
  267. void MiLightClient::handleCommand(const String& command) {
  268. if (command == "unpair") {
  269. this->unpair();
  270. } else if (command == "pair") {
  271. this->pair();
  272. } else if (command == "set_white") {
  273. this->updateColorWhite();
  274. } else if (command == "night_mode") {
  275. this->enableNightMode();
  276. } else if (command == "level_up") {
  277. this->increaseBrightness();
  278. } else if (command == "level_down") {
  279. this->decreaseBrightness();
  280. } else if (command == "temperature_up") {
  281. this->increaseTemperature();
  282. } else if (command == "temperature_down") {
  283. this->decreaseTemperature();
  284. } else if (command == "next_mode") {
  285. this->nextMode();
  286. } else if (command == "previous_mode") {
  287. this->previousMode();
  288. } else if (command == "mode_speed_down") {
  289. this->modeSpeedDown();
  290. } else if (command == "mode_speed_up") {
  291. this->modeSpeedUp();
  292. }
  293. }
  294. void MiLightClient::handleEffect(const String& effect) {
  295. if (effect == "night_mode") {
  296. this->enableNightMode();
  297. } else if (effect == "white") {
  298. this->updateColorWhite();
  299. }
  300. }
  301. uint8_t MiLightClient::parseStatus(const JsonObject& object) {
  302. String strStatus;
  303. if (object.containsKey("status")) {
  304. strStatus = object.get<char*>("status");
  305. } else if (object.containsKey("state")) {
  306. strStatus = object.get<char*>("state");
  307. } else {
  308. return 255;
  309. }
  310. return (strStatus.equalsIgnoreCase("on") || strStatus.equalsIgnoreCase("true")) ? ON : OFF;
  311. }
  312. void MiLightClient::flushPacket() {
  313. PacketStream& stream = currentRemote->packetFormatter->buildPackets();
  314. const size_t prevNumRepeats = this->resendCount;
  315. // When sending multiple packets, normalize the number of repeats
  316. if (stream.numPackets > 1) {
  317. setResendCount(MILIGHT_DEFAULT_RESEND_COUNT);
  318. }
  319. while (stream.hasNext()) {
  320. write(stream.next());
  321. if (stream.hasNext()) {
  322. delay(10);
  323. }
  324. }
  325. setResendCount(prevNumRepeats);
  326. currentRemote->packetFormatter->reset();
  327. }
  328. void MiLightClient::onPacketSent(PacketSentHandler handler) {
  329. this->packetSentHandler = handler;
  330. }