|
|
@@ -4,13 +4,23 @@
|
|
|
#include <RGBConverter.h>
|
|
|
#include <Units.h>
|
|
|
|
|
|
-MiLightClient::MiLightClient(MiLightRadioFactory* radioFactory, GroupStateStore& stateStore)
|
|
|
- : resendCount(MILIGHT_DEFAULT_RESEND_COUNT),
|
|
|
+MiLightClient::MiLightClient(
|
|
|
+ MiLightRadioFactory* radioFactory,
|
|
|
+ GroupStateStore& stateStore,
|
|
|
+ size_t throttleThreshold,
|
|
|
+ size_t throttleSensitivity,
|
|
|
+ size_t packetRepeatMinimum
|
|
|
+)
|
|
|
+ : baseResendCount(MILIGHT_DEFAULT_RESEND_COUNT),
|
|
|
currentRadio(NULL),
|
|
|
currentRemote(NULL),
|
|
|
numRadios(MiLightRadioConfig::NUM_CONFIGS),
|
|
|
packetSentHandler(NULL),
|
|
|
- stateStore(stateStore)
|
|
|
+ stateStore(stateStore),
|
|
|
+ lastSend(0),
|
|
|
+ throttleThreshold(throttleThreshold),
|
|
|
+ throttleSensitivity(throttleSensitivity),
|
|
|
+ packetRepeatMinimum(packetRepeatMinimum)
|
|
|
{
|
|
|
radios = new MiLightRadio*[numRadios];
|
|
|
|
|
|
@@ -66,6 +76,7 @@ void MiLightClient::prepare(const MiLightRemoteConfig* config,
|
|
|
const uint8_t groupId
|
|
|
) {
|
|
|
switchRadio(config);
|
|
|
+
|
|
|
this->currentRemote = config;
|
|
|
|
|
|
if (deviceId >= 0 && groupId >= 0) {
|
|
|
@@ -81,7 +92,9 @@ void MiLightClient::prepare(const MiLightRemoteType type,
|
|
|
}
|
|
|
|
|
|
void MiLightClient::setResendCount(const unsigned int resendCount) {
|
|
|
- this->resendCount = resendCount;
|
|
|
+ this->baseResendCount = resendCount;
|
|
|
+ this->currentResendCount = resendCount;
|
|
|
+ this->throttleMultiplier = ceil((throttleSensitivity / 1000.0) * this->baseResendCount);
|
|
|
}
|
|
|
|
|
|
bool MiLightClient::available() {
|
|
|
@@ -117,7 +130,7 @@ void MiLightClient::write(uint8_t packet[]) {
|
|
|
int iStart = millis();
|
|
|
#endif
|
|
|
|
|
|
- for (int i = 0; i < this->resendCount; i++) {
|
|
|
+ for (int i = 0; i < this->currentResendCount; i++) {
|
|
|
currentRadio->write(packet, currentRemote->packetFormatter->getPacketLength());
|
|
|
}
|
|
|
|
|
|
@@ -377,14 +390,19 @@ uint8_t MiLightClient::parseStatus(const JsonObject& object) {
|
|
|
return (strStatus.equalsIgnoreCase("on") || strStatus.equalsIgnoreCase("true")) ? ON : OFF;
|
|
|
}
|
|
|
|
|
|
+void MiLightClient::updateResendCount() {
|
|
|
+ unsigned long now = millis();
|
|
|
+ long millisSinceLastSend = now - lastSend;
|
|
|
+ long x = (millisSinceLastSend - throttleThreshold);
|
|
|
+ long delta = x * throttleMultiplier;
|
|
|
+
|
|
|
+ this->currentResendCount = constrain(this->currentResendCount + delta, packetRepeatMinimum, this->baseResendCount);
|
|
|
+ this->lastSend = now;
|
|
|
+}
|
|
|
+
|
|
|
void MiLightClient::flushPacket() {
|
|
|
PacketStream& stream = currentRemote->packetFormatter->buildPackets();
|
|
|
- const size_t prevNumRepeats = this->resendCount;
|
|
|
-
|
|
|
- // When sending multiple packets, normalize the number of repeats
|
|
|
- if (stream.numPackets > 1) {
|
|
|
- setResendCount(MILIGHT_DEFAULT_RESEND_COUNT);
|
|
|
- }
|
|
|
+ updateResendCount();
|
|
|
|
|
|
while (stream.hasNext()) {
|
|
|
write(stream.next());
|
|
|
@@ -394,7 +412,6 @@ void MiLightClient::flushPacket() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- setResendCount(prevNumRepeats);
|
|
|
currentRemote->packetFormatter->reset();
|
|
|
}
|
|
|
|