MiLightClient.h 3.5 KB

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