Skip to content

Commit 11beba8

Browse files
samples: matter: Rework buttons and leds in manufacturer-specific
Leave just one button and LED for the sample and use separate attribute for button. Signed-off-by: Maciej Baczmanski <[email protected]>
1 parent d3f4ced commit 11beba8

File tree

12 files changed

+99
-128
lines changed

12 files changed

+99
-128
lines changed

samples/matter/manufacturer_specific/README.rst

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ Overview
3434
The sample starts the Bluetooth® LE advertising automatically and prepares the Matter device for commissioning into a Matter-enabled Thread network.
3535
The sample uses the **LED 1** to show the state of the connection.
3636
You can press **Button 1** to start the factory reset when needed.
37-
**Button 2** and **Button 3** are used to toggle the state of ``NordicDevkit`` cluster's attributes, ``LED2`` and ``LED3`` respectively.
38-
Matter command ``SetLED`` is used to control the state of ``LED2`` and ``LED3``.
39-
It takes two arguments: the LED number (``2`` or ``3`` for ``LED2`` or ``LED3`` attribute) and the action to be performed (``0`` to turn the LED off, ``1`` to turn it on, ``2`` to toggle the state).
40-
**LED 2** and **LED 3** reflect the state of the ``LED2`` and ``LED3``.
37+
**Button 2** is used to set the state of the ``NordicDevkit`` cluster's attribute, ``UserButton``.
38+
Matter command ``SetLED`` is used to control the state of ``UserLED``.
39+
It takes one argument - the action to be performed (``0`` to turn the LED off, ``1`` to turn it on, ``2`` to toggle the state).
40+
**LED 2** reflects the state of the ``UserLED``.
4141
``NordicDevkit`` cluster additionally introduces a writable ``DevKitName`` attribute, of string type.
4242

4343
.. group-tab:: nRF54 DKs
4444

4545
The sample starts the Bluetooth® LE advertising automatically and prepares the Matter device for commissioning into a Matter-enabled Thread network.
4646
The sample uses the **LED 0** to show the state of the connection.
4747
You can press **Button 0** to start the factory reset when needed.
48-
**Button 1** and **Button 2** are used to toggle the state of ``NordicDevkit`` cluster's attributes, ``LED2`` and ``LED3`` respectively.
49-
Matter command ``SetLED`` is used to control the state of ``LED2`` and ``LED3``.
50-
It takes two arguments: the LED number (``2`` or ``3`` for ``LED2`` or ``LED3`` attribute) and the action to be performed (``0`` to turn the LED off, ``1`` to turn it on, ``2`` to toggle the state).
51-
**LED 1** and **LED 2** reflect the state of the ``LED2`` and ``LED3``.
48+
**Button 1** is used to set the state of the ``NordicDevkit`` cluster's attribute, ``UserButton``.
49+
Matter command ``SetLED`` is used to control the state of ``UserLED``.
50+
It takes one argument - the action to be performed (``0`` to turn the LED off, ``1`` to turn it on, ``2`` to toggle the state).
51+
**LED 1** reflects the state of the ``UserLED``.
5252
``NordicDevkit`` cluster additionally introduces a writable ``DevKitName`` attribute, of string type.
5353

5454

@@ -114,19 +114,13 @@ User interface
114114
.. include:: /includes/matter_sample_state_led.txt
115115

116116
LED 2:
117-
Reflects the state of ``LED2`` attribute in the ``NordicDevkit`` cluster.
118-
119-
LED 3:
120-
Reflects the state of ``LED3`` attribute in the ``NordicDevkit`` cluster.
117+
Reflects the state of ``UserLED`` attribute in the ``NordicDevkit`` cluster.
121118

122119
Button 1:
123120
.. include:: /includes/matter_sample_button.txt
124121

125122
Button 2:
126-
Toggles the state of ``LED2`` attribute in the ``NordicDevkit`` cluster.
127-
128-
Button 3:
129-
Toggles the state of ``LED3`` attribute in the ``NordicDevkit`` cluster.
123+
Sets the state of ``UserButton`` attribute in the ``NordicDevkit`` cluster.
130124

131125
.. include:: /includes/matter_segger_usb.txt
132126

@@ -136,19 +130,13 @@ User interface
136130
.. include:: /includes/matter_sample_state_led.txt
137131

138132
LED 1:
139-
Reflects the state of ``LED2`` attribute in the ``NordicDevkit`` cluster.
140-
141-
LED 2:
142-
Reflects the state of ``LED3`` attribute in the ``NordicDevkit`` cluster.
133+
Reflects the state of ``UserLED`` attribute in the ``NordicDevkit`` cluster.
143134

144135
Button 0:
145136
.. include:: /includes/matter_sample_button.txt
146137

147138
Button 1:
148-
Toggles the state of ``LED2`` attribute in the ``NordicDevkit`` cluster.
149-
150-
Button 2:
151-
Toggles the state of ``LED3`` attribute in the ``NordicDevkit`` cluster.
139+
Sets the state of ``UserButton`` attribute in the ``NordicDevkit`` cluster.
152140

153141
.. include:: /includes/matter_segger_usb.txt
154142

@@ -222,37 +210,49 @@ To test ``NordicDevkit`` cluster's attributes and commands, complete the followi
222210
1. |connect_kit|
223211
#. |connect_terminal_ANSI|
224212
#. Commission an accessory with node ID equal to 1 to the Matter network by following the steps described in the `Commissioning the device`_ section.
225-
#. Read the attributes by index using the chip-tool:
213+
#. Run the chip-tool in the interactive mode:
226214

227215
.. parsed-literal::
228216
:class: highlight
229217
230-
chip-tool any read-by-id read-by-id 0xFFF1FC01 *attribute-id* 1 1
218+
chip-tool interactive start
219+
220+
#. Read the attributes by index:
231221

232-
* *attribute-id* is the attribute's ID, equal to ``1`` for ``DevKitName``, ``2`` for ``LED2`` and ``3`` for ``LED3`` attributes for ``NordicDevkit`` cluster in this sample.
222+
.. parsed-literal::
223+
:class: highlight
224+
225+
any read-by-id read-by-id 0xFFF1FC01 *attribute-id* 1 1
226+
227+
* *attribute-id* is the attribute's ID, equal to ``1`` for ``DevKitName``, ``2`` for ``UserLED`` and ``3`` for ``UserButton`` attributes for the ``NordicDevkit`` cluster in this sample.
233228
#. Verify that all attributes have been read correctly and are equal to the default values defined in cluster's configuration.
234229
#. Write the ``DevkitName`` attribute:
235230

236231
.. parsed-literal::
237-
:class: highlight
232+
:class: highlight
238233
239-
chip-tool any write-by-id 0xFFF1FC01 1 "NewName" 1 1
234+
any write-by-id 0xFFF1FC01 1 "NewName" 1 1
240235
241236
#. Read the ``DevkitName`` attribute again to check if it has changed.
242-
#. Press the button assigned to the LED attribute you want to control, check if the LED state changes and if the attribute value is updated.
243237
#. Send the ``SetLED`` command to the device to control the LED state:
244238

245239
.. parsed-literal::
246-
:class: highlight
240+
:class: highlight
247241
248-
chip-tool any command-by-id 0xFFF1FC01 0 '{ "0x0": "u:*led-id*", "0x1": "u:*action*" }' 1 1
242+
any command-by-id 0xFFF1FC01 0 '{ "0x0": "u:*action*" }' 1 1
249243
250-
* *led-id* is the LED attribute's ID, ``2`` for ``LED2`` and ``3`` for ``LED3``.
251244
* *action* is the action that should be performed on LED attribute: ``0`` to turn the LED off, ``1`` to turn it on, ``2`` to toggle the state.
252245

253246
#. Verify that the LED state has changed and the attribute value is updated.
247+
#. Subscribe to the ``UserButton`` attribute to monitor the button state:
248+
249+
.. parsed-literal::
250+
:class: highlight
251+
252+
any subscribe-by-id 0xFFF1FC01 3 0 120 1 1
254253
255-
#. Reboot the device and check if the attributes are persisting after joining the network.
254+
#. Press the button assigned to the ``UserButton``, check if the attribute state is updated in the chip-tool.
255+
#. Reboot the device, restart chip-tool and check if the attributes are persisting after joining the network.
256256

257257
Upgrading the device firmware
258258
=============================

samples/matter/manufacturer_specific/src/app_task.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,35 @@ using namespace ::chip::DeviceLayer;
2828
namespace
2929
{
3030
#define BUTTON2_MASK DK_BTN2_MSK
31-
#define BUTTON3_MASK DK_BTN3_MSK
3231
constexpr EndpointId kEndpointId = 1;
3332
} /* namespace */
3433

35-
static void ButtonEventHandler(Nrf::ButtonState button_state, Nrf::ButtonMask has_changed)
34+
static void ButtonEventHandler(Nrf::ButtonState /* unused */, Nrf::ButtonMask has_changed)
3635
{
37-
if(!ConnectivityMgrImpl().IsIPv6NetworkProvisioned() || !ConnectivityMgrImpl().IsIPv6NetworkEnabled()) {
38-
return;
39-
}
40-
4136
/* Handle button press */
42-
if (BUTTON2_MASK & has_changed && BUTTON2_MASK & button_state) {
43-
Nrf::GetBoard().GetLED(Nrf::DeviceLeds::LED2).Invert();
44-
} else if (BUTTON3_MASK & has_changed && BUTTON3_MASK & button_state) {
45-
Nrf::GetBoard().GetLED(Nrf::DeviceLeds::LED3).Invert();
46-
} else {
47-
return;
37+
if (ConnectivityMgrImpl().IsIPv6NetworkProvisioned() && ConnectivityMgrImpl().IsIPv6NetworkEnabled() &&
38+
BUTTON2_MASK & has_changed) {
39+
AppTask::Instance().UpdateClusterState();
4840
}
49-
50-
AppTask::Instance().UpdateClusterState();
5141
}
5242

5343
void AppTask::UpdateClusterState()
5444
{
5545
SystemLayer().ScheduleLambda([] {
5646
Protocols::InteractionModel::Status status;
47+
Nrf::ButtonState button_state;
48+
49+
dk_read_buttons(&button_state, nullptr);
5750

58-
status = Clusters::NordicDevKitCluster::Attributes::Led2::Set(kEndpointId,
51+
status = Clusters::NordicDevKitCluster::Attributes::UserLED::Set(kEndpointId,
5952
Nrf::GetBoard().GetLED(Nrf::DeviceLeds::LED2).GetState());
6053

6154
if (status != Protocols::InteractionModel::Status::Success) {
6255
LOG_ERR("Updating NordicDevkit cluster failed: %x", to_underlying(status));
6356
}
6457

65-
status = Clusters::NordicDevKitCluster::Attributes::Led3::Set(kEndpointId,
66-
Nrf::GetBoard().GetLED(Nrf::DeviceLeds::LED3).GetState());
58+
status = Clusters::NordicDevKitCluster::Attributes::UserButton::Set(kEndpointId,
59+
BUTTON2_MASK & button_state);
6760

6861
if (status != Protocols::InteractionModel::Status::Success) {
6962
LOG_ERR("Updating NordicDevkit cluster failed: %x", to_underlying(status));

samples/matter/manufacturer_specific/src/default_zap/NordicDevKitCluster.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
<description>The NordicDevKitCluster cluster showcases a cluster manufacturer extensions</description>
1515
<command source="client" code="0x00" name="SetLED" optional="false">
1616
<description>Change LED state</description>
17-
<arg name="LEDID" type="int8u" optional="false"/>
1817
<arg name="Action" type="LEDActionEnum" optional="false"/>
1918
</command>
2019
<attribute side="server" code="0x01" define="DEV_KIT_NAME" type="char_string" writable="true" optional="false" default="Nordic Development Kit">DevKitName</attribute>
21-
<attribute side="server" code="0x02" define="LED_2" type="boolean" length="0" writable="false" reportable="false" isNullable="false" default="false" optional="false" apiMaturity="NaN">LED2<description/><access op="read" role="view"/></attribute>
22-
<attribute side="server" code="0x03" define="LED_3" type="boolean" length="0" writable="false" reportable="false" isNullable="false" default="false" optional="false" apiMaturity="NaN">LED3<description/><access op="read" role="view"/></attribute>
20+
<attribute side="server" code="0x02" define="USER_LED" type="boolean" length="0" writable="false" reportable="false" isNullable="false" default="false" optional="false" apiMaturity="NaN">UserLED<description/><access op="read" role="view"/></attribute>
21+
<attribute side="server" code="0x03" define="USER_BUTTON" type="boolean" length="0" writable="false" reportable="false" isNullable="false" default="false" optional="false" apiMaturity="NaN">UserButton<description/><access op="read" role="view"/></attribute>
2322
</cluster>
2423
<deviceType>
2524
<name>nordic-dev-kit</name>

samples/matter/manufacturer_specific/src/default_zap/manufacturer_specific.zap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,7 +2681,7 @@
26812681
"reportableChange": 0
26822682
},
26832683
{
2684-
"name": "LED2",
2684+
"name": "UserLED",
26852685
"code": 2,
26862686
"mfgCode": null,
26872687
"side": "server",
@@ -2697,13 +2697,13 @@
26972697
"reportableChange": 0
26982698
},
26992699
{
2700-
"name": "LED3",
2700+
"name": "UserButton",
27012701
"code": 3,
27022702
"mfgCode": null,
27032703
"side": "server",
27042704
"type": "boolean",
27052705
"included": 1,
2706-
"storageOption": "NVM",
2706+
"storageOption": "RAM",
27072707
"singleton": 0,
27082708
"bounded": 0,
27092709
"defaultValue": "false",
@@ -2722,7 +2722,7 @@
27222722
"storageOption": "External",
27232723
"singleton": 0,
27242724
"bounded": 0,
2725-
"defaultValue": null,
2725+
"defaultValue": "",
27262726
"reportable": 1,
27272727
"minInterval": 1,
27282728
"maxInterval": 65534,
@@ -2738,7 +2738,7 @@
27382738
"storageOption": "External",
27392739
"singleton": 0,
27402740
"bounded": 0,
2741-
"defaultValue": null,
2741+
"defaultValue": "",
27422742
"reportable": 1,
27432743
"minInterval": 1,
27442744
"maxInterval": 65534,
@@ -2754,7 +2754,7 @@
27542754
"storageOption": "External",
27552755
"singleton": 0,
27562756
"bounded": 0,
2757-
"defaultValue": null,
2757+
"defaultValue": "",
27582758
"reportable": 1,
27592759
"minInterval": 1,
27602760
"maxInterval": 65534,
@@ -2770,7 +2770,7 @@
27702770
"storageOption": "External",
27712771
"singleton": 0,
27722772
"bounded": 0,
2773-
"defaultValue": null,
2773+
"defaultValue": "",
27742774
"reportable": 1,
27752775
"minInterval": 1,
27762776
"maxInterval": 65534,

samples/matter/manufacturer_specific/src/default_zap/zap-generated/access.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
0x00000031, /* Cluster: Network Commissioning, Attribute: LastConnectErrorValue, Privilege: administer */ \
4444
0x0000003E, /* Cluster: Operational Credentials, Attribute: NOCs, Privilege: administer */ \
4545
/* Cluster: Group Key Management, Attribute: GroupKeyMap, Privilege: view */ \
46-
/* Cluster: NordicDevKitCluster, Attribute: LED2, Privilege: view */ \
47-
/* Cluster: NordicDevKitCluster, Attribute: LED3, Privilege: view */ \
46+
/* Cluster: NordicDevKitCluster, Attribute: UserLED, Privilege: view */ \
47+
/* Cluster: NordicDevKitCluster, Attribute: UserButton, Privilege: view */ \
4848
}
4949

5050
// Parallel array data (cluster, *attribute*, privilege) for read attribute
@@ -63,8 +63,8 @@
6363
0x00000007, /* Cluster: Network Commissioning, Attribute: LastConnectErrorValue, Privilege: administer */ \
6464
0x00000000, /* Cluster: Operational Credentials, Attribute: NOCs, Privilege: administer */ \
6565
/* Cluster: Group Key Management, Attribute: GroupKeyMap, Privilege: view */ \
66-
/* Cluster: NordicDevKitCluster, Attribute: LED2, Privilege: view */ \
67-
/* Cluster: NordicDevKitCluster, Attribute: LED3, Privilege: view */ \
66+
/* Cluster: NordicDevKitCluster, Attribute: UserLED, Privilege: view */ \
67+
/* Cluster: NordicDevKitCluster, Attribute: UserButton, Privilege: view */ \
6868
}
6969

7070
// Parallel array data (cluster, attribute, *privilege*) for read attribute
@@ -83,8 +83,8 @@
8383
chip::Access::Privilege::kAdminister, /* Cluster: Network Commissioning, Attribute: LastConnectErrorValue, Privilege: administer */ \
8484
chip::Access::Privilege::kAdminister, /* Cluster: Operational Credentials, Attribute: NOCs, Privilege: administer */ \
8585
/* Cluster: Group Key Management, Attribute: GroupKeyMap, Privilege: view */ \
86-
/* Cluster: NordicDevKitCluster, Attribute: LED2, Privilege: view */ \
87-
/* Cluster: NordicDevKitCluster, Attribute: LED3, Privilege: view */ \
86+
/* Cluster: NordicDevKitCluster, Attribute: UserLED, Privilege: view */ \
87+
/* Cluster: NordicDevKitCluster, Attribute: UserButton, Privilege: view */ \
8888
}
8989

9090
////////////////////////////////////////////////////////////////////////////////

samples/matter/manufacturer_specific/src/default_zap/zap-generated/app-common/zap-generated/attributes/Accessors.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44625,7 +44625,7 @@ namespace app
4462544625

4462644626
} // namespace DevKitName
4462744627

44628-
namespace Led2
44628+
namespace UserLED
4462944629
{
4463044630

4463144631
Protocols::InteractionModel::Status Get(EndpointId endpoint, bool *value)
@@ -44679,9 +44679,9 @@ namespace app
4467944679
writable, ZCL_BOOLEAN_ATTRIBUTE_TYPE);
4468044680
}
4468144681

44682-
} // namespace Led2
44682+
} // namespace UserLED
4468344683

44684-
namespace Led3
44684+
namespace UserButton
4468544685
{
4468644686

4468744687
Protocols::InteractionModel::Status Get(EndpointId endpoint, bool *value)
@@ -44735,7 +44735,7 @@ namespace app
4473544735
writable, ZCL_BOOLEAN_ATTRIBUTE_TYPE);
4473644736
}
4473744737

44738-
} // namespace Led3
44738+
} // namespace UserButton
4473944739

4474044740
namespace FeatureMap
4474144741
{

samples/matter/manufacturer_specific/src/default_zap/zap-generated/app-common/zap-generated/attributes/Accessors.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8833,23 +8833,23 @@ namespace app
88338833
Set(EndpointId endpoint, chip::CharSpan value, MarkAttributeDirty markDirty);
88348834
} // namespace DevKitName
88358835

8836-
namespace Led2
8836+
namespace UserLED
88378837
{
88388838
Protocols::InteractionModel::Status Get(EndpointId endpoint,
88398839
bool *value); // boolean
88408840
Protocols::InteractionModel::Status Set(EndpointId endpoint, bool value);
88418841
Protocols::InteractionModel::Status Set(EndpointId endpoint, bool value,
88428842
MarkAttributeDirty markDirty);
8843-
} // namespace Led2
8843+
} // namespace UserLED
88448844

8845-
namespace Led3
8845+
namespace UserButton
88468846
{
88478847
Protocols::InteractionModel::Status Get(EndpointId endpoint,
88488848
bool *value); // boolean
88498849
Protocols::InteractionModel::Status Set(EndpointId endpoint, bool value);
88508850
Protocols::InteractionModel::Status Set(EndpointId endpoint, bool value,
88518851
MarkAttributeDirty markDirty);
8852-
} // namespace Led3
8852+
} // namespace UserButton
88538853

88548854
namespace FeatureMap
88558855
{

samples/matter/manufacturer_specific/src/default_zap/zap-generated/app-common/zap-generated/cluster-objects.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27711,7 +27711,6 @@ namespace app
2771127711
CHIP_ERROR Type::Encode(TLV::TLVWriter &aWriter, TLV::Tag aTag) const
2771227712
{
2771327713
DataModel::WrappedStructEncoder encoder{ aWriter, aTag };
27714-
encoder.Encode(to_underlying(Fields::kLedid), ledid);
2771527714
encoder.Encode(to_underlying(Fields::kAction), action);
2771627715
return encoder.Finalize();
2771727716
}
@@ -27728,9 +27727,7 @@ namespace app
2772827727
CHIP_ERROR err = CHIP_NO_ERROR;
2772927728
const uint8_t __context_tag = std::get<uint8_t>(__element);
2773027729

27731-
if (__context_tag == to_underlying(Fields::kLedid)) {
27732-
err = DataModel::Decode(reader, ledid);
27733-
} else if (__context_tag == to_underlying(Fields::kAction)) {
27730+
if (__context_tag == to_underlying(Fields::kAction)) {
2773427731
err = DataModel::Decode(reader, action);
2773527732
} else {
2773627733
}
@@ -27749,10 +27746,10 @@ namespace app
2774927746
switch (path.mAttributeId) {
2775027747
case Attributes::DevKitName::TypeInfo::GetAttributeId():
2775127748
return DataModel::Decode(reader, devKitName);
27752-
case Attributes::Led2::TypeInfo::GetAttributeId():
27753-
return DataModel::Decode(reader, led2);
27754-
case Attributes::Led3::TypeInfo::GetAttributeId():
27755-
return DataModel::Decode(reader, led3);
27749+
case Attributes::UserLED::TypeInfo::GetAttributeId():
27750+
return DataModel::Decode(reader, userLED);
27751+
case Attributes::UserButton::TypeInfo::GetAttributeId():
27752+
return DataModel::Decode(reader, userButton);
2775627753
case Attributes::GeneratedCommandList::TypeInfo::GetAttributeId():
2775727754
return DataModel::Decode(reader, generatedCommandList);
2775827755
case Attributes::AcceptedCommandList::TypeInfo::GetAttributeId():

0 commit comments

Comments
 (0)