55*/
66
77#include < Arduino.h>
8- #include " Matter.h"
9- #include < app/server/OnboardingCodesUtil.h>
10- #include < app/clusters/window-covering-server/window-covering-server.h>
11- #include < credentials/examples/DeviceAttestationCredsExample.h>
128#include " Config.h"
9+ #include " BeltWinder.h"
1310
14- // Define debug level
15- # define DEBUGLEVEL 3
16- # include < DebugUtils.h >
17-
18- # include < esp_log.h >
19- # include < esp_err.h >
11+ // -- Variables
12+ int dir = 0 , count = 0 , lastCount = 0 , maxCount, newCount = 0 , newMaxCount = 0 , lastSendPercentage = 0 , lastTargetPosition = 0 , bufferedPercentage = 0 , filteredPercentage = 0 , newPercentage = 0 ;
13+ const int filterDuration = 1500 ;
14+ float percentage = 0 ;
15+ bool maxCountReloaded = false , newPercentageReceived = false , bufferedValueSaved = false , calButton = false , remote = false , posCertain = false , calMode = false , calUp = false , calDown = false , lastSendPercentageInit = false , newCountInit = false , maxCountInit = false , counted = false , posChange = false ,posRunUp = false , posRunDown = false ;
16+ unsigned long lastUpdateTime = 0 ;
2017
2118// Custom keys for your data
2219const char * kMaxCountKey = " MaxCount" ;
2320const char * kCountKey = " Count" ;
2421
25- using namespace chip ;
26- using namespace chip ::app::Clusters;
27- using namespace esp_matter ;
28- using namespace esp_matter ::endpoint;
29- using namespace esp_matter ::attribute;
30- using namespace esp_matter ::cluster;
31-
32- using namespace chip ::app::Clusters::WindowCovering;
33-
3422// Cluster and attribute ID used by Matter WindowCovering Device
3523const uint32_t WINDOW_COVERING_CLUSTER_ID = WindowCovering::Id;
3624const uint32_t WINDOW_COVERING_ATTRIBUTE_ID_TARGET = WindowCovering::Attributes::TargetPositionLiftPercent100ths::Id;
@@ -51,6 +39,8 @@ uint16_t switch_endpoint_id_1 = 0;
5139attribute_t *attribute_ref_target;
5240attribute_t *attribute_ref_1;
5341
42+ CoveringDelegate covering_delegate;
43+
5444static const char TAG[] = " WindowCovering-App" ;
5545
5646// Function prototypes
@@ -125,6 +115,7 @@ static esp_err_t on_attribute_update(attribute::callback_type_t type, uint16_t e
125115 return ESP_OK;
126116}
127117
118+
128119// Sets the value of the WindowCovering attribute for the current position (both target and current attributes must be sent!)
129120void set_window_covering_position (esp_matter_attr_val_t *current)
130121{
@@ -295,21 +286,22 @@ void set_window_covering_safetystatus(chip::EndpointId endpoint, SafetyStatus ne
295286 *********************************************************************************/
296287void setup () {
297288 Serial.begin (115200 );
298- esp_log_level_set (" *" , ESP_LOG_DEBUG );
289+ esp_log_level_set (" *" , ESP_LOG_LEVEL );
299290
300291 DEBUGPRINTLN1 (" [BELTWINDER] Start" );
301292
302293 setupPins ();
303294
304295 // Create onoff Matter node
305296 node::config_t node_config;
306- on_off_light ::config_t light_config ;
297+ on_off_plugin_unit ::config_t switch_config ;
307298 node_t *node = node::create (&node_config, on_attribute_update, on_identification);
308-
299+
309300 // Create Window Covering device
310301 window_covering_device::config_t window_config;
302+ window_covering_device::config_t window_covering_device_config (static_cast <uint8_t >(chip::app::Clusters::WindowCovering::EndProductType::kRollerShutter ));
311303 endpoint_t *endpoint = window_covering_device::create (node, &window_config, ENDPOINT_FLAG_NONE, NULL );
312- endpoint_t *plugin_unit_endpoint_id_1 = on_off_light ::create (node, &light_config , ENDPOINT_FLAG_NONE, NULL );
304+ endpoint_t *switch_endpoint_id_1 = on_off_plugin_unit ::create (node, &switch_config , ENDPOINT_FLAG_NONE, NULL );
313305
314306 // Get attribute reference
315307 attribute_ref_target = attribute::get (cluster::get (endpoint, WINDOW_COVERING_CLUSTER_ID), WINDOW_COVERING_ATTRIBUTE_ID_TARGET);
@@ -320,6 +312,7 @@ void setup() {
320312
321313 // Configure Window Covering cluster features
322314 cluster_t *cluster = cluster::get (endpoint, WindowCovering::Id);
315+ command_t *command = on_off::command::create_off (cluster);
323316 cluster::window_covering::feature::lift::config_t lift;
324317 cluster::window_covering::feature::position_aware_lift::config_t position_aware_lift;
325318 // nullable<uint8_t> percentage = nullable<uint8_t>(0);
@@ -330,6 +323,12 @@ void setup() {
330323 cluster::window_covering::feature::lift::add (cluster, &lift);
331324 cluster::window_covering::feature::position_aware_lift::add (cluster, &position_aware_lift);
332325
326+ // Implementing StopMotion Command
327+ esp_matter::endpoint::enable (endpoint);
328+ covering_delegate = CoveringDelegate ();
329+ covering_delegate.SetEndpoint (window_covering_endpoint_id);
330+ chip::app::Clusters::WindowCovering::SetDefaultDelegate ((chip::EndpointId) window_covering_endpoint_id, &covering_delegate);
331+
333332 // Setup DAC (this is good place to also set custom commission data, passcodes etc.)
334333 esp_matter::set_custom_dac_provider (chip::Credentials::Examples::GetExampleDACProvider ());
335334
@@ -462,6 +461,28 @@ void down() {
462461 DEBUGPRINTLN2 (" [BELTWINDER] Exiting down() function." );
463462}
464463
464+
465+ // Function for StopMotion
466+ void stopMotion () {
467+ DEBUGPRINTLN2 (" [BELTWINDER] Entering stopMotion() function." );
468+
469+ if (dir != 0 ) {
470+ DEBUGPRINTLN2 (" [BELTWINDER] Direction is not stall" );
471+
472+ // Simulated button press "down"
473+ DEBUGPRINTLN2 (" [BELTWINDER] Simulating button press 'down' to stop Motion." );
474+ digitalWrite (BUTTON_DOWN, LOW);
475+ delay (300 );
476+ digitalWrite (BUTTON_DOWN, HIGH);
477+ delay (500 );
478+ } else {
479+ DEBUGPRINTLN2 (" [BELTWINDER] Direction is 0, 'stopMotion()' function skipped." );
480+ }
481+
482+ DEBUGPRINTLN2 (" [BELTWINDER] Exiting stopMotion() function." );
483+ }
484+
485+
465486// Saving the lower position
466487void setMaxCount ()
467488{
@@ -855,4 +876,4 @@ void loadCount()
855876 }
856877
857878 DEBUGPRINTLN2 (" Loaded config 'count' = " + String (count));
858- }
879+ }
0 commit comments