MiLightClient.h 3.5 KB

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