MiLightClient.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef _MILIGHTCLIENT_H
  9. #define _MILIGHTCLIENT_H
  10. //#define DEBUG_PRINTF
  11. //#define DEBUG_CLIENT_COMMANDS // enable to show each individual change command (like hue, brightness, etc)
  12. #define MILIGHT_DEFAULT_RESEND_COUNT 10
  13. // Used to determine RGB colros that are approximately white
  14. #define RGB_WHITE_THRESHOLD 10
  15. class MiLightClient {
  16. public:
  17. MiLightClient(
  18. std::shared_ptr<MiLightRadioFactory> radioFactory,
  19. GroupStateStore* stateStore,
  20. Settings* settings
  21. );
  22. ~MiLightClient() { }
  23. typedef std::function<void(uint8_t* packet, const MiLightRemoteConfig& config)> PacketSentHandler;
  24. typedef std::function<void(void)> EventHandler;
  25. void begin();
  26. void prepare(const MiLightRemoteConfig* remoteConfig, const uint16_t deviceId = -1, const uint8_t groupId = -1);
  27. void prepare(const MiLightRemoteType type, const uint16_t deviceId = -1, const uint8_t groupId = -1);
  28. void setResendCount(const unsigned int resendCount);
  29. bool available();
  30. size_t read(uint8_t packet[]);
  31. void write(uint8_t packet[]);
  32. void setHeld(bool held);
  33. // Common methods
  34. void updateStatus(MiLightStatus status);
  35. void updateStatus(MiLightStatus status, uint8_t groupId);
  36. void pair();
  37. void unpair();
  38. void command(uint8_t command, uint8_t arg);
  39. void updateMode(uint8_t mode);
  40. void nextMode();
  41. void previousMode();
  42. void modeSpeedDown();
  43. void modeSpeedUp();
  44. void toggleStatus();
  45. // RGBW methods
  46. void updateHue(const uint16_t hue);
  47. void updateBrightness(const uint8_t brightness);
  48. void updateColorWhite();
  49. void updateColorRaw(const uint8_t color);
  50. void enableNightMode();
  51. // CCT methods
  52. void updateTemperature(const uint8_t colorTemperature);
  53. void decreaseTemperature();
  54. void increaseTemperature();
  55. void increaseBrightness();
  56. void decreaseBrightness();
  57. void updateSaturation(const uint8_t saturation);
  58. void update(JsonObject object);
  59. void handleCommand(const String& command);
  60. void handleEffect(const String& effect);
  61. void onPacketSent(PacketSentHandler handler);
  62. void onUpdateBegin(EventHandler handler);
  63. void onUpdateEnd(EventHandler handler);
  64. size_t getNumRadios() const;
  65. std::shared_ptr<MiLightRadio> switchRadio(size_t radioIx);
  66. std::shared_ptr<MiLightRadio> switchRadio(const MiLightRemoteConfig* remoteConfig);
  67. MiLightRemoteConfig& currentRemoteConfig() const;
  68. protected:
  69. std::vector<std::shared_ptr<MiLightRadio>> radios;
  70. std::shared_ptr<MiLightRadio> currentRadio;
  71. const MiLightRemoteConfig* currentRemote;
  72. PacketSentHandler packetSentHandler;
  73. EventHandler updateBeginHandler;
  74. EventHandler updateEndHandler;
  75. GroupStateStore* stateStore;
  76. const Settings* settings;
  77. // Used to track auto repeat limiting
  78. unsigned long lastSend;
  79. uint8_t currentResendCount;
  80. unsigned int baseResendCount;
  81. // This will be pre-computed, but is simply:
  82. //
  83. // (sensitivity / 1000.0) * R
  84. //
  85. // Where R is the base number of repeats.
  86. size_t throttleMultiplier;
  87. /*
  88. * Calculates the number of resend packets based on when the last packet
  89. * was sent using this function:
  90. *
  91. * lastRepeatsValue + (millisSinceLastSend - THRESHOLD) * throttleMultiplier
  92. *
  93. * When the last send was more recent than THRESHOLD, the number of repeats
  94. * will be decreased to a minimum of zero. When less recent, it will be
  95. * increased up to a maximum of the default resend count.
  96. */
  97. void updateResendCount();
  98. uint8_t parseStatus(JsonObject object);
  99. void flushPacket();
  100. };
  101. #endif