Skip to content

Commit

Permalink
[M5Stack] Add a screen to control the OnOff cluster (#6256)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and pull[bot] committed Aug 20, 2021
1 parent cfaa8df commit d0aea0f
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include <support/ErrorStr.h>

#include <app/clusters/door-lock-server/door-lock-server.h>
#include <app/clusters/on-off-server/on-off-server.h>
#include <app/clusters/temperature-measurement-server/temperature-measurement-server.h>

using namespace ::chip;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit d0aea0f

Please sign in to comment.