MiLightClient.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. void toggleStatus();
  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. const Settings* settings;
  76. PacketSentHandler packetSentHandler;
  77. EventHandler updateBeginHandler;
  78. EventHandler updateEndHandler;
  79. // Used to track auto repeat limiting
  80. unsigned long lastSend;
  81. int currentResendCount;
  82. unsigned int baseResendCount;
  83. // This will be pre-computed, but is simply:
  84. //
  85. // (sensitivity / 1000.0) * R
  86. //
  87. // Where R is the base number of repeats.
  88. size_t throttleMultiplier;
  89. /*
  90. * Calculates the number of resend packets based on when the last packet
  91. * was sent using this function:
  92. *
  93. * lastRepeatsValue + (millisSinceLastSend - THRESHOLD) * throttleMultiplier
  94. *
  95. * When the last send was more recent than THRESHOLD, the number of repeats
  96. * will be decreased to a minimum of zero. When less recent, it will be
  97. * increased up to a maximum of the default resend count.
  98. */
  99. void updateResendCount();
  100. MiLightRadio* switchRadio(const MiLightRemoteConfig* remoteConfig);
  101. uint8_t parseStatus(const JsonObject& object);
  102. void flushPacket();
  103. };
  104. #endif