Skip to content

Commit d8c2389

Browse files
Apps feedback
1 parent c48d962 commit d8c2389

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed
82.6 KB
Loading

sld601-matter-application-development/matter-application-cluster-logic.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Enable the On/Off cluster (0x0006) as both a Server and a Client. This should ad
4242

4343
By configuring both a client and server for the On/Off cluster, the device should be able to transmit On/Off commands and store On/Off attributes. For this guide, we will mainly focus on the attribute storing and manipulation.
4444

45-
Save the .zap file. The tool will automatically generate the necessary code files for your application, reflecting the new cluster configuration. For more information, reference [ZCL Advanced Platform (ZAP) Tool for Matter](https://docs.silabs.com/matter/2.3.1/matter-references/matter-zap).
45+
Save the .zap file. The tool will automatically generate the necessary code files for your application, reflecting the new cluster configuration. For more information, reference [ZCL Advanced Platform (ZAP) Tool for Matter](https://docs.silabs.com/matter/latest/matter-references/matter-zap).
4646

4747

4848
## Step 3: Analyze the Auto-generated Code
@@ -58,28 +58,30 @@ You will also notice that following the cluster enablement, a corresponding comp
5858

5959
## Step 4: Add Application Logic
6060

61-
Locate your projects src/AppTask.cpp file. This file acts as the central hub for application-specific logic, initialization, and event processing in a Matter application on Silicon Labs platforms. We will begin by creating two helper functions, one that starts a one-shot timer to expire in 10 seconds and invokes a handler, **OnOffTmrExpiryHandler**.
61+
Locate your projects src/AppTask.cpp file. This file acts as the central hub for application-specific logic, initialization, and event processing in a Matter application on Silicon Labs platforms. We will begin by creating two helper functions, one that starts a one-shot timer to expire in 10 seconds and the handler function, **OnOffTmrExpiryHandler**.
6262

6363
```C++
6464
#include "app-common/zap-generated/attributes/Accessors.h"
6565
#include "timers.h"
6666
TimerHandle_t OnOffAttributeChangeTimer;
6767

6868
void OnOffTmrExpiryHandler(TimerHandle_t xTimer){
69-
static bool current_state = 1; //initialize as on
70-
current_state ^= 1;
71-
chip::app::Clusters::OnOff::Attributes::OnOff::Set(1, current_state);
69+
static bool current_state = 1; // Initialize as on
70+
current_state ^= 1; // Toggle the current state of OnOff on timer expiry
71+
SILABS_LOG("OnOff attribute toggled to %d", current_state);
72+
chip::app::Clusters::OnOff::Attributes::OnOff::Set(1, current_state); // Modify the attribute
7273
}
7374

7475
void OnOffTmrStart(){
7576

77+
// Initiate timer
7678
OnOffAttributeChangeTimer = xTimerCreate("OnOffTmr", // timer name
7779
pdMS_TO_TICKS(10000), // == default timer period (mS)
78-
true, // no timer reload (==one-shot)
80+
false, // no timer reload (==one-shot)
7981
nullptr, // init timer id = app task obj context
8082
OnOffTmrExpiryHandler); // timer callback handler
8183

82-
xTimerStart(OnOffAttributeChangeTimer, 1);
84+
xTimerStart(OnOffAttributeChangeTimer, 1); // Start timer
8385
}
8486
```
8587
@@ -94,10 +96,9 @@ void AppTask::OnOffAttributeWriteStartTimer()
9496
}
9597
```
9698

97-
This function will have to be defined in AppTask.h as well.
99+
This function will have to be defined in AppTask.h as well as part of the AppTask class.
98100

99-
Now locate the MatterPostAttributeChangeCallback() function in the src/ZclCallbacks.cpp file. This function is called by the application framework after it changes an attribute value.
100-
Modify it as below:
101+
Now locate the MatterPostAttributeChangeCallback() function in the src/ZclCallbacks.cpp file. This function is called by the application framework after it changes an attribute value. Since we are modifying OnOff attribute in the OnOffTmrExpiryHandler(), this callback will be used to re-initiate the timer such that the attribute is continuously being toggled. To do this, the AppTask::OnOffAttributeWriteStartTimer() can be called. OnOffAttributeWriteStartTimer() is part of the AppTask context. In order to implement this function in the we must first get the AppTask instance with AppTask::GetAppTask(). To do this, modify the MatterPostAttributeChangeCallback() as shown below:
101102

102103
```C++
103104
void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
@@ -106,17 +107,21 @@ void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath &
106107
ClusterId clusterId = attributePath.mClusterId;
107108
AttributeId attributeId = attributePath.mAttributeId;
108109

109-
// More auto-generated code
110+
// Auto-generated code
110111

111112
if (clusterId == OnOff::Id && attributeId == OnOff::Attributes::OnOff::Id){
112113
AppTask::GetAppTask().OnOffAttributeWriteStartTimer();
113114
}
114115

115116
}
116117
```
117-
Make sure to #include "AppTask.h" at the top of ZclCallbacks.cpp to call the AppTask:: function.
118+
Make sure to #include "AppTask.h" at the top of ZclCallbacks.cpp to call the AppTask::GetAppTask() function. For more information on the AppTask, reference AppTask.h.
118119
119-
The last step is to add a call to OnOffTmrStart() at the end of the AppTask::AppInit() function to start the attribute write sequence.
120+
The last step is to add a call to OnOffTmrStart() at the end of the AppTask::AppInit() function to start the attribute write sequence. Below is the code flow:
121+
122+
![Code Flow](./images/ClusterLogic6.jpg)
123+
124+
In the flow chart above, OnOffAttributeWriteStartTimer() calls OnOffTmrStart() to restart the timer.
120125
121126
## Step 5: Interact with the On/Off Cluster
122127

0 commit comments

Comments
 (0)