|
@@ -84,10 +84,10 @@ void MiLightClient::write(const MiLightRadioType radioType,
|
|
|
|
|
|
|
|
for (int i = 0; i < resendCount; i++) {
|
|
for (int i = 0; i < resendCount; i++) {
|
|
|
radio->write(packetBytes, MILIGHT_PACKET_LENGTH);
|
|
radio->write(packetBytes, MILIGHT_PACKET_LENGTH);
|
|
|
|
|
+ yield();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
void MiLightClient::writeRgbw(
|
|
void MiLightClient::writeRgbw(
|
|
|
const uint16_t deviceId,
|
|
const uint16_t deviceId,
|
|
|
const uint8_t color,
|
|
const uint8_t color,
|
|
@@ -96,7 +96,7 @@ void MiLightClient::writeRgbw(
|
|
|
const uint8_t button) {
|
|
const uint8_t button) {
|
|
|
|
|
|
|
|
MiLightPacket packet;
|
|
MiLightPacket packet;
|
|
|
- packet.deviceType = RGBW;;
|
|
|
|
|
|
|
+ packet.deviceType = RGBW;
|
|
|
packet.deviceId = deviceId;
|
|
packet.deviceId = deviceId;
|
|
|
packet.b1 = color;
|
|
packet.b1 = color;
|
|
|
packet.b2 = (brightness << 3) | (groupId & 0x07);
|
|
packet.b2 = (brightness << 3) | (groupId & 0x07);
|
|
@@ -105,6 +105,21 @@ void MiLightClient::writeRgbw(
|
|
|
|
|
|
|
|
write(RGBW, packet);
|
|
write(RGBW, packet);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+void MiLightClient::writeCct(const uint16_t deviceId,
|
|
|
|
|
+ const uint8_t groupId,
|
|
|
|
|
+ const uint8_t button) {
|
|
|
|
|
+
|
|
|
|
|
+ MiLightPacket packet;
|
|
|
|
|
+ packet.deviceType = CCT;
|
|
|
|
|
+ packet.deviceId = deviceId;
|
|
|
|
|
+ packet.b1 = groupId;
|
|
|
|
|
+ packet.b2 = button;
|
|
|
|
|
+ packet.b3 = nextSequenceNum();
|
|
|
|
|
+ packet.sequenceNum = packet.b3;
|
|
|
|
|
+
|
|
|
|
|
+ write(CCT, packet);
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
void MiLightClient::updateColorRaw(const uint16_t deviceId, const uint8_t groupId, const uint16_t color) {
|
|
void MiLightClient::updateColorRaw(const uint16_t deviceId, const uint8_t groupId, const uint16_t color) {
|
|
|
writeRgbw(deviceId, color, 0, groupId, RGBW_COLOR);
|
|
writeRgbw(deviceId, color, 0, groupId, RGBW_COLOR);
|
|
@@ -132,34 +147,138 @@ void MiLightClient::updateBrightness(const uint16_t deviceId, const uint8_t grou
|
|
|
writeRgbw(deviceId, 0, packetBrightnessValue, groupId, RGBW_BRIGHTNESS);
|
|
writeRgbw(deviceId, 0, packetBrightnessValue, groupId, RGBW_BRIGHTNESS);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightClient::updateStatus(const uint16_t deviceId, const uint8_t groupId, MiLightStatus status) {
|
|
|
|
|
- uint8_t button = RGBW_GROUP_1_ON + ((groupId - 1)*2) + status;
|
|
|
|
|
- writeRgbw(deviceId, 0, 0, groupId, button);
|
|
|
|
|
|
|
+void MiLightClient::updateStatus(const MiLightRadioType type, const uint16_t deviceId, const uint8_t groupId, MiLightStatus status) {
|
|
|
|
|
+ if (type == RGBW) {
|
|
|
|
|
+ uint8_t button = RGBW_GROUP_1_ON + ((groupId - 1)*2) + status;
|
|
|
|
|
+ writeRgbw(deviceId, 0, 0, groupId, button);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ writeCct(deviceId, groupId, getCctStatusButton(groupId, status));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MiLightClient::updateColorWhite(const uint16_t deviceId, const uint8_t groupId) {
|
|
void MiLightClient::updateColorWhite(const uint16_t deviceId, const uint8_t groupId) {
|
|
|
uint8_t button = RGBW_GROUP_1_MAX_LEVEL + ((groupId - 1)*2);
|
|
uint8_t button = RGBW_GROUP_1_MAX_LEVEL + ((groupId - 1)*2);
|
|
|
- pressButton(deviceId, groupId, button);
|
|
|
|
|
|
|
+ pressButton(RGBW, deviceId, groupId, button);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightClient::pair(const uint16_t deviceId, const uint8_t groupId) {
|
|
|
|
|
- updateStatus(deviceId, groupId, ON);
|
|
|
|
|
|
|
+void MiLightClient::pair(const MiLightRadioType type, const uint16_t deviceId, const uint8_t groupId) {
|
|
|
|
|
+ updateStatus(type, deviceId, groupId, ON);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightClient::unpair(const uint16_t deviceId, const uint8_t groupId) {
|
|
|
|
|
- updateStatus(deviceId, groupId, ON);
|
|
|
|
|
- delay(1);
|
|
|
|
|
- updateColorWhite(deviceId, groupId);
|
|
|
|
|
|
|
+void MiLightClient::unpair(const MiLightRadioType type, const uint16_t deviceId, const uint8_t groupId) {
|
|
|
|
|
+ if (type == RGBW) {
|
|
|
|
|
+ updateStatus(RGBW, deviceId, groupId, ON);
|
|
|
|
|
+ delay(1);
|
|
|
|
|
+ updateColorWhite(deviceId, groupId);
|
|
|
|
|
+ } else if (type == CCT) {
|
|
|
|
|
+ // Leading nibble is a "held" modifier
|
|
|
|
|
+ uint8_t button = 0x10 | getCctStatusButton(groupId, ON);
|
|
|
|
|
+ pressButton(CCT, deviceId, groupId, button);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightClient::pressButton(const uint16_t deviceId, const uint8_t groupId, const uint8_t button) {
|
|
|
|
|
- writeRgbw(deviceId, 0, 0, groupId, button);
|
|
|
|
|
|
|
+void MiLightClient::pressButton(const MiLightRadioType type, const uint16_t deviceId, const uint8_t groupId, const uint8_t button) {
|
|
|
|
|
+ if (type == RGBW) {
|
|
|
|
|
+ writeRgbw(deviceId, 0, 0, groupId, button);
|
|
|
|
|
+ } else if (type == CCT) {
|
|
|
|
|
+ writeCct(deviceId, groupId, button);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightClient::allOn(const uint16_t deviceId) {
|
|
|
|
|
- writeRgbw(deviceId, 0, 0, 0, RGBW_ALL_ON);
|
|
|
|
|
|
|
+void MiLightClient::allOn(const MiLightRadioType type, const uint16_t deviceId) {
|
|
|
|
|
+ if (type == RGBW) {
|
|
|
|
|
+ writeRgbw(deviceId, 0, 0, 0, RGBW_ALL_ON);
|
|
|
|
|
+ } else if (type == CCT) {
|
|
|
|
|
+ writeCct(deviceId, 0, CCT_ALL_ON);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightClient::allOff(const uint16_t deviceId) {
|
|
|
|
|
- writeRgbw(deviceId, 0, 0, 0, RGBW_ALL_OFF);
|
|
|
|
|
|
|
+void MiLightClient::allOff(const MiLightRadioType type, const uint16_t deviceId) {
|
|
|
|
|
+ if (type == RGBW) {
|
|
|
|
|
+ writeRgbw(deviceId, 0, 0, 0, RGBW_ALL_OFF);
|
|
|
|
|
+ } else if (type == CCT) {
|
|
|
|
|
+ writeCct(deviceId, 0, CCT_ALL_OFF);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void MiLightClient::increaseCctBrightness(const uint16_t deviceId, const uint8_t groupId) {
|
|
|
|
|
+ writeCct(deviceId, groupId, CCT_BRIGHTNESS_UP);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void MiLightClient::decreaseCctBrightness(const uint16_t deviceId, const uint8_t groupId) {
|
|
|
|
|
+ writeCct(deviceId, groupId, CCT_BRIGHTNESS_DOWN);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void MiLightClient::updateCctBrightness(const uint16_t deviceId, const uint8_t groupId, const uint8_t brightness) {
|
|
|
|
|
+ for (int i = 0; i < MILIGHT_CCT_INTERVALS; i++) {
|
|
|
|
|
+ decreaseCctBrightness(deviceId, groupId);
|
|
|
|
|
+ }
|
|
|
|
|
+ for (int i = 0; i < brightness/10; i++) {
|
|
|
|
|
+ increaseCctBrightness(deviceId, groupId);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void MiLightClient::increaseTemperature(const uint16_t deviceId, const uint8_t groupId) {
|
|
|
|
|
+ writeCct(deviceId, groupId, CCT_TEMPERATURE_UP);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void MiLightClient::decreaseTemperature(const uint16_t deviceId, const uint8_t groupId) {
|
|
|
|
|
+ writeCct(deviceId, groupId, CCT_TEMPERATURE_DOWN);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void MiLightClient::updateTemperature(const uint16_t deviceId, const uint8_t groupId, const uint8_t temperature) {
|
|
|
|
|
+ for (int i = 0; i < MILIGHT_CCT_INTERVALS; i++) {
|
|
|
|
|
+ decreaseTemperature(deviceId, groupId);
|
|
|
|
|
+ }
|
|
|
|
|
+ for (int i = 0; i < temperature; i++) {
|
|
|
|
|
+ increaseTemperature(deviceId, groupId);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+uint8_t MiLightClient::getCctStatusButton(uint8_t groupId, MiLightStatus status) {
|
|
|
|
|
+ uint8_t button = 0;
|
|
|
|
|
+
|
|
|
|
|
+ if (status == ON) {
|
|
|
|
|
+ switch(groupId) {
|
|
|
|
|
+ case 1:
|
|
|
|
|
+ button = CCT_GROUP_1_ON;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 2:
|
|
|
|
|
+ button = CCT_GROUP_2_ON;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 3:
|
|
|
|
|
+ button = CCT_GROUP_3_ON;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 4:
|
|
|
|
|
+ button = CCT_GROUP_4_ON;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ switch(groupId) {
|
|
|
|
|
+ case 1:
|
|
|
|
|
+ button = CCT_GROUP_1_OFF;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 2:
|
|
|
|
|
+ button = CCT_GROUP_2_OFF;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 3:
|
|
|
|
|
+ button = CCT_GROUP_3_OFF;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 4:
|
|
|
|
|
+ button = CCT_GROUP_4_OFF;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return button;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+MiLightRadioType MiLightClient::getRadioType(const String& typeName) {
|
|
|
|
|
+ if (typeName.equalsIgnoreCase("rgbw")) {
|
|
|
|
|
+ return RGBW;
|
|
|
|
|
+ } else if (typeName.equalsIgnoreCase("cct")) {
|
|
|
|
|
+ return CCT;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return UNKNOWN;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|