MiLightClient.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <functional>
  2. #include <Arduino.h>
  3. #include <MiLightRadio.h>
  4. #include <MiLightRadioFactory.h>
  5. #include <MiLightRemoteConfig.h>
  6. #include <Settings.h>
  7. #include <GroupStateStore.h>
  8. #include <PacketSender.h>
  9. #ifndef _MILIGHTCLIENT_H
  10. #define _MILIGHTCLIENT_H
  11. //#define DEBUG_PRINTF
  12. //#define DEBUG_CLIENT_COMMANDS // enable to show each individual change command (like hue, brightness, etc)
  13. // Used to determine RGB colros that are approximately white
  14. #define RGB_WHITE_THRESHOLD 10
  15. class MiLightClient {
  16. public:
  17. MiLightClient(
  18. RadioSwitchboard& radioSwitchboard,
  19. PacketSender& packetSender,
  20. GroupStateStore* stateStore,
  21. Settings& settings
  22. );
  23. ~MiLightClient() { }
  24. typedef std::function<void(void)> EventHandler;
  25. void prepare(const MiLightRemoteConfig* remoteConfig, const uint16_t deviceId = -1, const uint8_t groupId = -1);
  26. void prepare(const MiLightRemoteType type, const uint16_t deviceId = -1, const uint8_t groupId = -1);
  27. void setResendCount(const unsigned int resendCount);
  28. bool available();
  29. size_t read(uint8_t packet[]);
  30. void write(uint8_t packet[]);
  31. void setHeld(bool held);
  32. // Common methods
  33. void updateStatus(MiLightStatus status);
  34. void updateStatus(MiLightStatus status, uint8_t groupId);
  35. void pair();
  36. void unpair();
  37. void command(uint8_t command, uint8_t arg);
  38. void updateMode(uint8_t mode);
  39. void nextMode();
  40. void previousMode();
  41. void modeSpeedDown();
  42. void modeSpeedUp();
  43. void toggleStatus();
  44. // RGBW methods
  45. void updateHue(const uint16_t hue);
  46. void updateBrightness(const uint8_t brightness);
  47. void updateColorWhite();
  48. void updateColorRaw(const uint8_t color);
  49. void enableNightMode();
  50. // CCT methods
  51. void updateTemperature(const uint8_t colorTemperature);
  52. void decreaseTemperature();
  53. void increaseTemperature();
  54. void increaseBrightness();
  55. void decreaseBrightness();
  56. void updateSaturation(const uint8_t saturation);
  57. void update(JsonObject object);
  58. void handleCommand(const String& command);
  59. void handleEffect(const String& effect);
  60. void onUpdateBegin(EventHandler handler);
  61. void onUpdateEnd(EventHandler handler);
  62. size_t getNumRadios() const;
  63. std::shared_ptr<MiLightRadio> switchRadio(size_t radioIx);
  64. std::shared_ptr<MiLightRadio> switchRadio(const MiLightRemoteConfig* remoteConfig);
  65. MiLightRemoteConfig& currentRemoteConfig() const;
  66. // Call to override the number of packet repeats that are sent. Clear with clearRepeatsOverride
  67. void setRepeatsOverride(size_t repeatsOverride);
  68. // Clear the repeats override so that the default is used
  69. void clearRepeatsOverride();
  70. protected:
  71. RadioSwitchboard& radioSwitchboard;
  72. std::vector<std::shared_ptr<MiLightRadio>> radios;
  73. std::shared_ptr<MiLightRadio> currentRadio;
  74. const MiLightRemoteConfig* currentRemote;
  75. EventHandler updateBeginHandler;
  76. EventHandler updateEndHandler;
  77. GroupStateStore* stateStore;
  78. Settings& settings;
  79. PacketSender& packetSender;
  80. // If set, override the number of packet repeats used.
  81. size_t repeatsOverride;
  82. uint8_t parseStatus(JsonObject object);
  83. void flushPacket();
  84. };
  85. #endif