From 3ba3a24226de57a14a948cd520385a52f510ed2e Mon Sep 17 00:00:00 2001 From: Chris Mullins Date: Sat, 15 Jul 2017 13:01:29 -0700 Subject: [PATCH 1/7] RGBW: determine group from button ID for on/off commands --- lib/MiLight/RgbwPacketFormatter.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/MiLight/RgbwPacketFormatter.cpp b/lib/MiLight/RgbwPacketFormatter.cpp index 2f2c5b1a..714ceb76 100644 --- a/lib/MiLight/RgbwPacketFormatter.cpp +++ b/lib/MiLight/RgbwPacketFormatter.cpp @@ -3,6 +3,7 @@ #include #define STATUS_COMMAND(status, groupId) ( RGBW_GROUP_1_ON + ((groupId - 1)*2) + status ) +#define GROUP_FOR_STATUS_COMMAND(buttonId) ( (buttonId - 1) / 2 ) void RgbwPacketFormatter::initializePacket(uint8_t* packet) { size_t packetPtr = 0; @@ -101,6 +102,10 @@ void RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result) if (command >= RGBW_ALL_ON && command <= RGBW_GROUP_4_OFF) { result["state"] = (command % 2) ? "ON" : "OFF"; + // Determine group ID from button ID for on/off. The remote's state is from + // the last packet sent, not the current one, and that can be wrong for + // on/off commands. + result["group_id"] = GROUP_FOR_STATUS_COMMAND(command); } else if (command == RGBW_BRIGHTNESS) { uint8_t brightness = 31; brightness -= packet[RGBW_BRIGHTNESS_GROUP_INDEX] >> 3; From 84918c4b3ed88ce025b13a2a91eca32ff5243a86 Mon Sep 17 00:00:00 2001 From: Chris Mullins Date: Sat, 15 Jul 2017 13:02:38 -0700 Subject: [PATCH 2/7] send button ID for unhandled commands --- lib/MiLight/RgbwPacketFormatter.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/MiLight/RgbwPacketFormatter.cpp b/lib/MiLight/RgbwPacketFormatter.cpp index 714ceb76..3dae959b 100644 --- a/lib/MiLight/RgbwPacketFormatter.cpp +++ b/lib/MiLight/RgbwPacketFormatter.cpp @@ -116,6 +116,8 @@ void RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result) uint16_t remappedColor = Units::rescale(packet[RGBW_COLOR_INDEX], 360.0, 255.0); remappedColor = (remappedColor + 320) % 360; result["hue"] = remappedColor; + } else { + result["button_id"] = command; } if (! result.containsKey("state")) { From 65ff9b6e25d89747f628065def0b37dab50d33bf Mon Sep 17 00:00:00 2001 From: Chris Mullins Date: Sat, 15 Jul 2017 13:06:56 -0700 Subject: [PATCH 3/7] handle mode speed, mode for RGBW --- lib/MiLight/RgbwPacketFormatter.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/MiLight/RgbwPacketFormatter.cpp b/lib/MiLight/RgbwPacketFormatter.cpp index 3dae959b..1e1f037d 100644 --- a/lib/MiLight/RgbwPacketFormatter.cpp +++ b/lib/MiLight/RgbwPacketFormatter.cpp @@ -116,6 +116,12 @@ void RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result) uint16_t remappedColor = Units::rescale(packet[RGBW_COLOR_INDEX], 360.0, 255.0); remappedColor = (remappedColor + 320) % 360; result["hue"] = remappedColor; + } else if (command == RGBW_SPEED_DOWN) { + result["command"] = "mode_speed_down"; + } else if (command == RGBW_SPEED_UP) { + result["command"] = "mode_speed_up"; + } else if (command == RGBW_DISCO_MODE) { + result["mode"] = packet[0] & ~RGBW; } else { result["button_id"] = command; } From 4d94c3d71ceb3bbfa46051358da03d9b51994cde Mon Sep 17 00:00:00 2001 From: Chris Mullins Date: Sat, 15 Jul 2017 14:29:49 -0700 Subject: [PATCH 4/7] RGB_CCT: handle mode, mode speed up/down in packet update handler --- lib/MiLight/RgbCctPacketFormatter.cpp | 12 ++++++++++-- platformio.ini | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/MiLight/RgbCctPacketFormatter.cpp b/lib/MiLight/RgbCctPacketFormatter.cpp index e8ad2625..329a7c17 100644 --- a/lib/MiLight/RgbCctPacketFormatter.cpp +++ b/lib/MiLight/RgbCctPacketFormatter.cpp @@ -124,8 +124,11 @@ void RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul uint8_t arg = packetCopy[RGB_CCT_ARGUMENT_INDEX]; if (command == RGB_CCT_ON) { - // Group is not reliably encoded in group byte. Extract from arg byte - if (arg < 5) { + if (arg == RGB_CCT_MODE_SPEED_DOWN) { + result["command"] = "mode_speed_down"; + } else if (arg == RGB_CCT_MODE_SPEED_UP) { + result["command"] = "mode_speed_up"; + } else if (arg < 5) { // Group is not reliably encoded in group byte. Extract from arg byte result["state"] = "ON"; result["group_id"] = arg; } else { @@ -158,6 +161,11 @@ void RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul result["brightness"] = Units::rescale(level, 255, 100); } else if (command == RGB_CCT_SATURATION) { result["saturation"] = constrain(arg - RGB_CCT_SATURATION_OFFSET, 0, 100); + } else if (command == RGB_CCT_MODE) { + result["mode"] = arg; + } else { + result["button_id"] = command; + result["argument"] = arg; } if (! result.containsKey("state")) { diff --git a/platformio.ini b/platformio.ini index d4373758..6708b61e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,7 +18,7 @@ lib_deps_external = ArduinoJson PubSubClient https://github.com/ratkins/RGBConverter -build_flags = !python .get_version.py +build_flags = !python .get_version.py -DMQTT_MAX_PACKET_SIZE=200 # -D MQTT_DEBUG # -D MILIGHT_UDP_DEBUG # -D DEBUG_PRINTF From 19b5e5dcfedb64bd22f34d815e5c2f25569f2090 Mon Sep 17 00:00:00 2001 From: Chris Mullins Date: Sat, 15 Jul 2017 14:52:12 -0700 Subject: [PATCH 5/7] RGB: handle mode, mode speed up/down in packet update handler --- lib/MiLight/RgbPacketFormatter.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/MiLight/RgbPacketFormatter.cpp b/lib/MiLight/RgbPacketFormatter.cpp index 167d2525..1f51df4f 100644 --- a/lib/MiLight/RgbPacketFormatter.cpp +++ b/lib/MiLight/RgbPacketFormatter.cpp @@ -94,6 +94,16 @@ void RgbPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result) uint16_t remappedColor = Units::rescale(packet[RGB_COLOR_INDEX], 360.0, 255.0); remappedColor = (remappedColor + 320) % 360; result["hue"] = remappedColor; + } else if (command == RGB_MODE_DOWN) { + result["command"] = "previous_mode"; + } else if (command == RGB_MODE_UP) { + result["command"] = "next_mode"; + } else if (command == RGB_SPEED_DOWN) { + result["command"] = "mode_speed_down"; + } else if (command == RGB_SPEED_UP) { + result["command"] = "mode_speed_up"; + } else { + result["button_id"] = command; } if (! result.containsKey("state")) { From d9822cf444816a492acac2da5940af5574fd9a5e Mon Sep 17 00:00:00 2001 From: Chris Mullins Date: Sat, 15 Jul 2017 15:07:28 -0700 Subject: [PATCH 6/7] CCT: brightness up/down, temperature up/down --- lib/MiLight/CctPacketFormatter.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/MiLight/CctPacketFormatter.cpp b/lib/MiLight/CctPacketFormatter.cpp index 3784aa96..51349f65 100644 --- a/lib/MiLight/CctPacketFormatter.cpp +++ b/lib/MiLight/CctPacketFormatter.cpp @@ -158,6 +158,18 @@ void CctPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result) result["state"] = cctCommandToStatus(command) == ON ? "ON" : "OFF"; } + if (command == CCT_BRIGHTNESS_DOWN) { + result["command"] = "brightness_down"; + } else if (command == CCT_BRIGHTNESS_UP) { + result["command"] = "brightness_up"; + } else if (command == CCT_TEMPERATURE_DOWN) { + result["command"] = "temperature_down"; + } else if (command == CCT_TEMPERATURE_UP) { + result["command"] = "temperature_up"; + } else { + result["button_id"] = command; + } + if (! result.containsKey("state")) { result["state"] = "ON"; } From 9869fcc4c3bd25554455b089e9ac50ccf167d9a9 Mon Sep 17 00:00:00 2001 From: Chris Mullins Date: Sat, 15 Jul 2017 15:23:01 -0700 Subject: [PATCH 7/7] CCT: dont treat on/off as unhandled --- lib/MiLight/CctPacketFormatter.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/MiLight/CctPacketFormatter.cpp b/lib/MiLight/CctPacketFormatter.cpp index 51349f65..fc5b9b93 100644 --- a/lib/MiLight/CctPacketFormatter.cpp +++ b/lib/MiLight/CctPacketFormatter.cpp @@ -156,9 +156,7 @@ void CctPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result) uint8_t onOffGroupId = cctCommandIdToGroup(command); if (onOffGroupId < 255) { result["state"] = cctCommandToStatus(command) == ON ? "ON" : "OFF"; - } - - if (command == CCT_BRIGHTNESS_DOWN) { + } else if (command == CCT_BRIGHTNESS_DOWN) { result["command"] = "brightness_down"; } else if (command == CCT_BRIGHTNESS_UP) { result["command"] = "brightness_up";