MiLightClient.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. 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. size_t getNumRadios() const;
  64. MiLightRadio* switchRadio(size_t radioIx);
  65. MiLightRemoteConfig& currentRemoteConfig() const;
  66. protected:
  67. MiLightRadio** radios;
  68. MiLightRadio* currentRadio;
  69. const MiLightRemoteConfig* currentRemote;
  70. const size_t numRadios;
  71. PacketSentHandler packetSentHandler;
  72. GroupStateStore& stateStore;
  73. // Used to track auto repeat limiting
  74. unsigned long lastSend;
  75. int currentResendCount;
  76. unsigned int baseResendCount;
  77. int packetRepeatMinimum;
  78. size_t throttleThreshold;
  79. size_t throttleSensitivity;
  80. // This will be pre-computed, but is simply:
  81. //
  82. // (sensitivity / 1000.0) * R
  83. //
  84. // Where R is the base number of repeats.
  85. size_t throttleMultiplier;
  86. /*
  87. * Calculates the number of resend packets based on when the last packet
  88. * was sent using this function:
  89. *
  90. * lastRepeatsValue + (millisSinceLastSend - THRESHOLD) * throttleMultiplier
  91. *
  92. * When the last send was more recent than THRESHOLD, the number of repeats
  93. * will be decreased to a minimum of zero. When less recent, it will be
  94. * increased up to a maximum of the default resend count.
  95. */
  96. void updateResendCount();
  97. MiLightRadio* switchRadio(const MiLightRemoteConfig* remoteConfig);
  98. uint8_t parseStatus(const JsonObject& object);
  99. void flushPacket();
  100. };
  101. #endif