From d0aea0f6d22ce31d86af37f846f4c00dcaa27c7e Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Fri, 23 Apr 2021 15:52:32 +0200 Subject: [PATCH] [M5Stack] Add a screen to control the OnOff cluster (#6256) --- examples/all-clusters-app/esp32/main/main.cpp | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 45829351eced36..8bf81ea32ada72 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -56,6 +56,7 @@ #include #include +#include #include using namespace ::chip; @@ -153,6 +154,12 @@ class EditAttributeListModel : public ListScreen::Model { return std::get<1>(std::get<1>(std::get<1>(devices[deviceIndex])[endpointIndex])[clusterIndex])[attributeIndex]; } + bool IsBooleanAttribute() + { + auto & attribute = this->attribute(); + auto & value = std::get<1>(attribute); + return value == "On" || value == "Off"; + } virtual std::string GetTitle() { auto & attribute = this->attribute(); @@ -162,8 +169,15 @@ class EditAttributeListModel : public ListScreen::Model snprintf(buffer, sizeof(buffer), "%s : %s", name.c_str(), value.c_str()); return buffer; } - virtual int GetItemCount() { return 2; } - virtual std::string GetItemText(int i) { return i == 0 ? "+" : "-"; } + virtual int GetItemCount() { return IsBooleanAttribute() ? 1 : 2; } + virtual std::string GetItemText(int i) + { + if (IsBooleanAttribute()) + { + return "Toggle"; + } + return i == 0 ? "+" : "-"; + } virtual void ItemAction(int i) { auto & attribute = this->attribute(); @@ -184,6 +198,19 @@ class EditAttributeListModel : public ListScreen::Model } value = buffer; } + else if (IsBooleanAttribute()) + { + auto & name = std::get<0>(attribute); + auto & cluster = std::get<0>(std::get<1>(std::get<1>(devices[deviceIndex])[endpointIndex])[i]); + value = (value == "On") ? "Off" : "On"; + + if (name == "OnOff" && cluster == "OnOff") + { + uint8_t attributeValue = (value == "On") ? 1 : 0; + emberAfWriteServerAttribute(endpointIndex + 1, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, + (uint8_t *) &attributeValue, ZCL_BOOLEAN_ATTRIBUTE_TYPE); + } + } else { auto & name = std::get<0>(attribute); @@ -348,6 +375,14 @@ void SetupPretendDevices() AddCluster("Step Counter"); AddAttribute("Steps", "9876"); + AddDevice("Light Bulb"); + AddEndpoint("1"); + AddCluster("OnOff"); + AddAttribute("OnOff", "Off"); + AddEndpoint("2"); + AddCluster("OnOff"); + AddAttribute("OnOff", "Off"); + AddDevice("Thermometer"); AddEndpoint("External"); AddCluster("Thermometer");