MiLightClient.h 3.5 KB

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