|
|
@@ -1,89 +1,110 @@
|
|
|
#include <MiLightClient.h>
|
|
|
+#include <MiLightRadioConfig.h>
|
|
|
|
|
|
-void MiLightClient::deserializePacket(const uint8_t rawPacket[], MiLightPacket& packet) {
|
|
|
- uint8_t ptr = 0;
|
|
|
+MiLightRadio* MiLightClient::getRadio(const MiLightRadioType type) {
|
|
|
+ MiLightRadio* radio = NULL;
|
|
|
|
|
|
- packet.deviceType = rawPacket[ptr++];
|
|
|
- packet.deviceId = (rawPacket[ptr++] << 8) | rawPacket[ptr++];
|
|
|
- packet.color = rawPacket[ptr++];
|
|
|
-
|
|
|
- packet.brightness = rawPacket[ptr] >> 3;
|
|
|
- packet.groupId = rawPacket[ptr++] & 0x07;
|
|
|
-
|
|
|
- packet.button = rawPacket[ptr++];
|
|
|
- packet.sequenceNum = rawPacket[ptr++];
|
|
|
-}
|
|
|
-
|
|
|
-void MiLightClient::serializePacket(uint8_t rawPacket[], const MiLightPacket& packet) {
|
|
|
- uint8_t ptr = 0;
|
|
|
-
|
|
|
- rawPacket[ptr++] = packet.deviceType;
|
|
|
+ if (type == RGBW) {
|
|
|
+ return rgbwRadio->getRadio();
|
|
|
+ } else if (type == CCT) {
|
|
|
+ return cctRadio->getRadio();
|
|
|
+ } else if (type == RGBW_CCT) {
|
|
|
+ return rgbwCctRadio->getRadio();
|
|
|
+ }
|
|
|
|
|
|
- // big endian
|
|
|
- rawPacket[ptr++] = packet.deviceId >> 8;
|
|
|
- rawPacket[ptr++] = packet.deviceId & 0xFF;
|
|
|
+ if (radio != NULL) {
|
|
|
+ radio->configure();
|
|
|
+ }
|
|
|
|
|
|
- rawPacket[ptr++] = packet.color;
|
|
|
- rawPacket[ptr++] = (packet.brightness << 3) | (packet.groupId & 0x07);
|
|
|
- rawPacket[ptr++] = packet.button;
|
|
|
- rawPacket[ptr++] = packet.sequenceNum;
|
|
|
+ return radio;
|
|
|
}
|
|
|
|
|
|
uint8_t MiLightClient::nextSequenceNum() {
|
|
|
return sequenceNum++;
|
|
|
}
|
|
|
|
|
|
-bool MiLightClient::available() {
|
|
|
+bool MiLightClient::available(const MiLightRadioType radioType) {
|
|
|
+ MiLightRadio* radio = getRadio(radioType);
|
|
|
+ radio->begin();
|
|
|
+
|
|
|
+ if (radio == NULL) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
return radio->available();
|
|
|
}
|
|
|
|
|
|
-void MiLightClient::read(MiLightPacket& packet) {
|
|
|
- uint8_t packetBytes[MILIGHT_PACKET_LENGTH];
|
|
|
+void MiLightClient::read(const MiLightRadioType radioType, uint8_t packet[]) {
|
|
|
+ MiLightRadio* radio = getRadio(radioType);
|
|
|
+
|
|
|
+ if (radio == NULL) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
size_t length;
|
|
|
- radio->read(packetBytes, length);
|
|
|
- deserializePacket(packetBytes, packet);
|
|
|
+ radio->read(packet, length);
|
|
|
}
|
|
|
|
|
|
-void MiLightClient::write(MiLightPacket& packet, const unsigned int resendCount) {
|
|
|
- uint8_t packetBytes[MILIGHT_PACKET_LENGTH];
|
|
|
- serializePacket(packetBytes, packet);
|
|
|
+void MiLightClient::write(const MiLightRadioType radioType,
|
|
|
+ uint8_t packet[],
|
|
|
+ const unsigned int resendCount) {
|
|
|
+
|
|
|
+ MiLightRadio* radio = getRadio(radioType);
|
|
|
+
|
|
|
+ if (radio == NULL) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
for (int i = 0; i < resendCount; i++) {
|
|
|
- radio->write(packetBytes, MILIGHT_PACKET_LENGTH);
|
|
|
+ radio->write(packet, MILIGHT_PACKET_LENGTH);
|
|
|
+ yield();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-void MiLightClient::write(
|
|
|
+void MiLightClient::writeRgbw(
|
|
|
const uint16_t deviceId,
|
|
|
const uint8_t color,
|
|
|
const uint8_t brightness,
|
|
|
const uint8_t groupId,
|
|
|
- const MiLightButton button) {
|
|
|
-
|
|
|
- // Expect an input value in [0, 100]. Map it down to [0, 25].
|
|
|
- const uint8_t adjustedBrightness = round(brightness * (25 / 100.0));
|
|
|
+ const uint8_t button,
|
|
|
+ const unsigned int resendCount) {
|
|
|
|
|
|
- // The actual protocol uses a bizarre range where min is 16, max is 23:
|
|
|
- // [16, 15, ..., 0, 31, ..., 23]
|
|
|
- const uint8_t packetBrightnessValue = (
|
|
|
- ((31 - adjustedBrightness) + 17) % 32
|
|
|
- );
|
|
|
+ uint8_t packet[MilightRgbwConfig.packetLength];
|
|
|
+ size_t packetPtr = 0;
|
|
|
|
|
|
- MiLightPacket packet;
|
|
|
- packet.deviceType = MiLightDeviceType::RGBW;
|
|
|
- packet.deviceId = deviceId;
|
|
|
- packet.color = color;
|
|
|
- packet.brightness = packetBrightnessValue;
|
|
|
- packet.groupId = groupId;
|
|
|
- packet.button = button;
|
|
|
- packet.sequenceNum = nextSequenceNum();
|
|
|
+ packet[packetPtr++] = RGBW;
|
|
|
+ packet[packetPtr++] = deviceId >> 8;
|
|
|
+ packet[packetPtr++] = deviceId & 0xFF;
|
|
|
+ packet[packetPtr++] = color;
|
|
|
+ packet[packetPtr++] = (brightness << 3) | (groupId & 0x07);
|
|
|
+ packet[packetPtr++] = button;
|
|
|
+ packet[packetPtr++] = nextSequenceNum();
|
|
|
|
|
|
- write(packet);
|
|
|
+ write(RGBW, packet, resendCount);
|
|
|
+}
|
|
|
+
|
|
|
+void MiLightClient::writeCct(const uint16_t deviceId,
|
|
|
+ const uint8_t groupId,
|
|
|
+ const uint8_t button,
|
|
|
+ const unsigned int resendCount) {
|
|
|
+
|
|
|
+ uint8_t packet[MilightRgbwConfig.packetLength];
|
|
|
+ uint8_t sequenceNum = nextSequenceNum();
|
|
|
+ size_t packetPtr = 0;
|
|
|
+
|
|
|
+ packet[packetPtr++] = CCT;
|
|
|
+ packet[packetPtr++] = deviceId >> 8;
|
|
|
+ packet[packetPtr++] = deviceId & 0xFF;
|
|
|
+ packet[packetPtr++] = groupId;
|
|
|
+ packet[packetPtr++] = button;
|
|
|
+ packet[packetPtr++] = sequenceNum;
|
|
|
+ packet[packetPtr++] = sequenceNum;
|
|
|
+
|
|
|
+ write(CCT, packet, resendCount);
|
|
|
}
|
|
|
|
|
|
void MiLightClient::updateColorRaw(const uint16_t deviceId, const uint8_t groupId, const uint16_t color) {
|
|
|
- write(deviceId, color, 0, groupId, COLOR);
|
|
|
+ writeRgbw(deviceId, color, 0, groupId, RGBW_COLOR);
|
|
|
}
|
|
|
|
|
|
void MiLightClient::updateHue(const uint16_t deviceId, const uint8_t groupId, const uint16_t hue) {
|
|
|
@@ -92,41 +113,155 @@ void MiLightClient::updateHue(const uint16_t deviceId, const uint8_t groupId, co
|
|
|
const int16_t remappedColor = (hue + 40) % 360;
|
|
|
const uint8_t adjustedColor = round(remappedColor * (255 / 360.0));
|
|
|
|
|
|
- write(deviceId, adjustedColor, 0, groupId, COLOR);
|
|
|
+ writeRgbw(deviceId, adjustedColor, 0, groupId, RGBW_COLOR);
|
|
|
}
|
|
|
|
|
|
void MiLightClient::updateBrightness(const uint16_t deviceId, const uint8_t groupId, const uint8_t brightness) {
|
|
|
- write(deviceId, 0, brightness, groupId, BRIGHTNESS);
|
|
|
+ // Expect an input value in [0, 100]. Map it down to [0, 25].
|
|
|
+ const uint8_t adjustedBrightness = round(brightness * (25 / 100.0));
|
|
|
+
|
|
|
+ // The actual protocol uses a bizarre range where min is 16, max is 23:
|
|
|
+ // [16, 15, ..., 0, 31, ..., 23]
|
|
|
+ const uint8_t packetBrightnessValue = (
|
|
|
+ ((31 - adjustedBrightness) + 17) % 32
|
|
|
+ );
|
|
|
+
|
|
|
+ writeRgbw(deviceId, 0, packetBrightnessValue, groupId, RGBW_BRIGHTNESS);
|
|
|
}
|
|
|
|
|
|
-void MiLightClient::updateStatus(const uint16_t deviceId, const uint8_t groupId, MiLightStatus status) {
|
|
|
- uint8_t button = MiLightButton::GROUP_1_ON + ((groupId - 1)*2) + status;
|
|
|
- write(deviceId, 0, 0, groupId, static_cast<MiLightButton>(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) {
|
|
|
- uint8_t button = MiLightButton::GROUP_1_MAX_LEVEL + ((groupId - 1)*2);
|
|
|
- pressButton(deviceId, groupId, static_cast<MiLightButton>(button));
|
|
|
+ uint8_t button = RGBW_GROUP_1_MAX_LEVEL + ((groupId - 1)*2);
|
|
|
+ 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) {
|
|
|
+ for (int i = 0; i < 5; i++) {
|
|
|
+ updateStatus(CCT, deviceId, groupId, ON);
|
|
|
+ delay(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-void MiLightClient::pressButton(const uint16_t deviceId, const uint8_t groupId, MiLightButton button) {
|
|
|
- write(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 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 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, 10);
|
|
|
+}
|
|
|
+
|
|
|
+void MiLightClient::decreaseCctBrightness(const uint16_t deviceId, const uint8_t groupId) {
|
|
|
+ writeCct(deviceId, groupId, CCT_BRIGHTNESS_DOWN, 10);
|
|
|
+}
|
|
|
+
|
|
|
+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::allOn(const uint16_t deviceId) {
|
|
|
- write(deviceId, 0, 0, 0, ALL_ON);
|
|
|
+void MiLightClient::increaseTemperature(const uint16_t deviceId, const uint8_t groupId) {
|
|
|
+ writeCct(deviceId, groupId, CCT_TEMPERATURE_UP, 10);
|
|
|
}
|
|
|
|
|
|
-void MiLightClient::allOff(const uint16_t deviceId) {
|
|
|
- write(deviceId, 0, 0, 0, ALL_OFF);
|
|
|
+void MiLightClient::decreaseTemperature(const uint16_t deviceId, const uint8_t groupId) {
|
|
|
+ writeCct(deviceId, groupId, CCT_TEMPERATURE_DOWN, 10);
|
|
|
+}
|
|
|
+
|
|
|
+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;
|
|
|
+ }
|
|
|
}
|