Skip to content

Commit 1e8be36

Browse files
committed
v1.2
• included StopMotionCommand
1 parent 86c3161 commit 1e8be36

File tree

4 files changed

+157
-31
lines changed

4 files changed

+157
-31
lines changed

include/Beltwinder.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#ifndef BELTWINDER_H
2+
#define BELTWINDER_H
3+
4+
#pragma once
5+
6+
#include <Arduino.h>
7+
#include "Matter.h"
8+
#include <app/server/OnboardingCodesUtil.h>
9+
#include <app/clusters/window-covering-server/window-covering-server.h>
10+
#include <credentials/examples/DeviceAttestationCredsExample.h>
11+
#include "DelegateStopMotion.h"
12+
13+
//Define debug level in Config.h
14+
#include <DebugUtils.h>
15+
16+
#include <esp_log.h>
17+
#include <esp_err.h>
18+
19+
using namespace chip;
20+
using namespace chip::app::Clusters;
21+
using namespace esp_matter;
22+
using namespace esp_matter::endpoint;
23+
using namespace esp_matter::attribute;
24+
using namespace esp_matter::cluster;
25+
using namespace chip::app::Clusters::WindowCovering;
26+
27+
//Function prototypes
28+
extern void setupPins();
29+
extern void loadCount();
30+
extern void loadMaxCount();
31+
extern void up();
32+
extern void down();
33+
extern void saveCount(int count);
34+
extern void setMaxCount();
35+
extern void newPosition(int newPercentage);
36+
extern void countPosition();
37+
extern void calibrationRun();
38+
extern void setCurrentDirection();
39+
extern void sendCurrentPosition(int count, int maxCount);
40+
extern void handlePosCertainty();
41+
extern void moveToNewPos();
42+
extern void handleCalibration();
43+
extern void bufferNewPosition();
44+
extern void set_window_covering_position(esp_matter_attr_val_t *current);
45+
extern void set_onoff_attribute_value(esp_matter_attr_val_t *onoff_value, uint16_t switch_endpoint_id_1);
46+
extern void set_operational_state(chip::EndpointId endpoint, OperationalState newState);
47+
extern esp_matter_attr_val_t get_onoff_attribute_value();
48+
49+
// -- Variables
50+
extern int dir, count, lastCount, maxCount, newCount, newMaxCount, lastSendPercentage, lastTargetPosition, bufferedPercentage, filteredPercentage, newPercentage;
51+
extern const int filterDuration;
52+
extern float percentage;
53+
extern bool maxCountReloaded, newPercentageReceived, bufferedValueSaved, calButton, remote, posCertain, calMode, calUp, calDown, lastSendPercentageInit, newCountInit, maxCountInit, counted, posChange, posRunUp, posRunDown;
54+
extern unsigned long lastUpdateTime;
55+
56+
// Custom keys for your data
57+
extern const char * kMaxCountKey;
58+
extern const char * kCountKey;
59+
60+
extern uint16_t window_covering_endpoint_id;
61+
extern uint16_t switch_endpoint_id_1;
62+
extern attribute_t *attribute_ref_target;
63+
extern attribute_t *attribute_ref_1;
64+
65+
extern CoveringDelegate covering_delegate;
66+
67+
#endif // BELTWINDER_H

include/Config.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33

44
#pragma once
55

6-
// -- Variables
7-
int dir = 0, count = 0, lastCount = 0, maxCount, newCount = 0, newMaxCount = 0, lastSendPercentage = 0, lastTargetPosition = 0, bufferedPercentage = 0, filteredPercentage = 0, newPercentage = 0;
8-
const int filterDuration = 1500;
9-
float percentage = 0;
10-
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;
11-
unsigned long lastUpdateTime = 0;
12-
136
// Pin definitions
147
#define PULSECOUNTER 18 // Pulse counter
158
#define MOTOR_UP 17 // Motor drives up
169
#define MOTOR_DOWN 16 // Motor drives down
1710
#define BUTTON_UP 21 // Button for up
1811
#define BUTTON_DOWN 22 // Button for down
1912

13+
// Define Logoutput
14+
#define ESP_LOG_LEVEL ESP_LOG_DEBUG
15+
#define DEBUGLEVEL 3
16+
2017
#endif // CONFIG_H

include/DelegateStopMotion.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef DELEGATESTOPMOTION_H
2+
#define DELEGATESTOPMOTION_H
3+
4+
#pragma once
5+
6+
#include "Beltwinder.h"
7+
#include <DebugUtils.h>
8+
9+
// -- Variables
10+
extern int dir;
11+
extern bool remote,posRunUp, posRunDown;
12+
13+
extern void up();
14+
extern void down();
15+
16+
class CoveringDelegate : public chip::app::Clusters::WindowCovering::Delegate{
17+
public:
18+
19+
CHIP_ERROR HandleMovement(chip::app::Clusters::WindowCovering::WindowCoveringType type){
20+
return CHIP_NO_ERROR;
21+
}
22+
23+
CHIP_ERROR HandleStopMotion(){
24+
//Handle stopping of covering here
25+
DEBUGPRINTLN2("[BELTWINDER] Received StopMotionCommand");
26+
if(posRunUp || posRunDown){
27+
return CHIP_ERROR_IN_PROGRESS;
28+
}
29+
remote = false;
30+
if (dir == -1) {
31+
down();
32+
} else if (dir == 1) {
33+
up();
34+
}
35+
return CHIP_ERROR_IN_PROGRESS;
36+
}
37+
38+
~CoveringDelegate() = default;
39+
};
40+
41+
#endif // DELEGATESTOPMOTION_H

src/main.cpp

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,20 @@
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
2219
const char * kMaxCountKey = "MaxCount";
2320
const 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
3523
const uint32_t WINDOW_COVERING_CLUSTER_ID = WindowCovering::Id;
3624
const uint32_t WINDOW_COVERING_ATTRIBUTE_ID_TARGET = WindowCovering::Attributes::TargetPositionLiftPercent100ths::Id;
@@ -51,6 +39,8 @@ uint16_t switch_endpoint_id_1 = 0;
5139
attribute_t *attribute_ref_target;
5240
attribute_t *attribute_ref_1;
5341

42+
CoveringDelegate covering_delegate;
43+
5444
static 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!)
129120
void 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
*********************************************************************************/
296287
void 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
466487
void setMaxCount()
467488
{
@@ -855,4 +876,4 @@ void loadCount()
855876
}
856877

857878
DEBUGPRINTLN2("Loaded config 'count' = " + String(count));
858-
}
879+
}

0 commit comments

Comments
 (0)