Skip to content

Commit 2ad7bff

Browse files
authored
Merge pull request #405 from sinricpro/feat-smart-button
feat: SmartButton
2 parents 906c9ac + 0592d3e commit 2ad7bff

File tree

5 files changed

+123
-3
lines changed

5 files changed

+123
-3
lines changed

changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## Version 3.3.1
4+
- Support SmartButton.
5+
36
## Version 3.2.1
47
- Fixed Arduino Lint errors
58
- LICENSE.txt added

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"maintainer": true
1414
}
1515
],
16-
"version": "3.2.1",
16+
"version": "3.3.1",
1717
"frameworks": "arduino",
1818
"platforms": [
1919
"espressif8266",

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SinricPro
2-
version=3.2.1
2+
version=3.3.1
33
author=Boris Jaeger <[email protected]>
44
maintainer=Boris Jaeger <[email protected]>
55
sentence=Library for https://sinric.pro - simple way to connect your device to alexa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#pragma once
2+
3+
#include "../SinricProRequest.h"
4+
#include "../EventLimiter.h"
5+
#include "../SinricProStrings.h"
6+
#include "../SinricProNamespace.h"
7+
8+
/**
9+
* @brief Enum defining the different types of button press events
10+
*/
11+
enum class SmartButtonPressType {
12+
SINGLE_PRESS,
13+
DOUBLE_PRESS,
14+
LONG_PRESS
15+
};
16+
17+
namespace SINRICPRO_NAMESPACE {
18+
19+
FSTR(BUTTONSTATE, state); // Button state key
20+
FSTR(BUTTONSTATE, singlePress); // Single press state value
21+
FSTR(BUTTONSTATE, doublePress); // Double press state value
22+
FSTR(BUTTONSTATE, longPress); // Long press state value
23+
FSTR(BUTTONSTATE, setSmartButtonState); // Set state action name
24+
25+
// Callback type definition for button press events
26+
using SmartButtonPressCallback = std::function<bool(const String&, SmartButtonPressType)>;
27+
28+
/**
29+
* @brief Controller class for managing smart button state and interactions
30+
*
31+
* @tparam T The device type that this controller is attached to
32+
*/
33+
template <typename T>
34+
class SmartButtonStateController {
35+
public:
36+
/**
37+
* @brief Construct a new Smart Button State Controller
38+
* Automatically registers the request handler with the device
39+
*/
40+
SmartButtonStateController();
41+
42+
/**
43+
* @brief Register callback for button press events from the app
44+
* @param cb Callback function to handle button press events
45+
*/
46+
void onButtonPress(SmartButtonPressCallback cb);
47+
48+
protected:
49+
/**
50+
* @brief Handle incoming button state change requests
51+
* @param request The incoming request to process
52+
* @return true if request was handled successfully, false otherwise
53+
*/
54+
bool handleSmartButtonStateController(SinricProRequest &request);
55+
56+
private:
57+
SmartButtonPressCallback buttonPressCallback;
58+
59+
/**
60+
* @brief Convert string state to SmartButtonPressType enum
61+
* @param stateStr The state string from the request
62+
* @return corresponding SmartButtonPressType enum value
63+
*/
64+
SmartButtonPressType getSmartButtonPressType(const String& stateStr);
65+
};
66+
67+
template <typename T>
68+
SmartButtonStateController<T>::SmartButtonStateController() {
69+
T* device = static_cast<T*>(this);
70+
device->registerRequestHandler(
71+
std::bind(&SmartButtonStateController<T>::handleSmartButtonStateController,
72+
this,
73+
std::placeholders::_1)
74+
);
75+
}
76+
77+
template <typename T>
78+
void SmartButtonStateController<T>::onButtonPress(SmartButtonPressCallback cb) {
79+
buttonPressCallback = cb;
80+
}
81+
82+
template <typename T>
83+
SmartButtonPressType SmartButtonStateController<T>::getSmartButtonPressType(const String& stateStr) {
84+
if (stateStr == FSTR_BUTTONSTATE_singlePress) {
85+
return SmartButtonPressType::SINGLE_PRESS;
86+
} else if (stateStr == FSTR_BUTTONSTATE_doublePress) {
87+
return SmartButtonPressType::DOUBLE_PRESS;
88+
} else {
89+
return SmartButtonPressType::LONG_PRESS;
90+
}
91+
}
92+
93+
template <typename T>
94+
bool SmartButtonStateController<T>::handleSmartButtonStateController(SinricProRequest &request) {
95+
// Only process setSmartButtonState actions
96+
if (request.action != FSTR_BUTTONSTATE_setSmartButtonState || !buttonPressCallback) {
97+
return false;
98+
}
99+
100+
T* device = static_cast<T*>(this);
101+
String stateStr = request.request_value[FSTR_BUTTONSTATE_state];
102+
103+
// Only process valid button states
104+
if (stateStr != FSTR_BUTTONSTATE_singlePress &&
105+
stateStr != FSTR_BUTTONSTATE_doublePress &&
106+
stateStr != FSTR_BUTTONSTATE_longPress) {
107+
return false;
108+
}
109+
110+
SmartButtonPressType pressType = getSmartButtonPressType(stateStr);
111+
return buttonPressCallback(device->deviceId, pressType);
112+
}
113+
114+
} // namespace SINRICPRO_NAMESPACE
115+
116+
template <typename T>
117+
using SmartButtonStateController = SINRICPRO_NAMESPACE::SmartButtonStateController<T>;

src/SinricProVersion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// Version Configuration
77
#define SINRICPRO_VERSION_MAJOR 3
8-
#define SINRICPRO_VERSION_MINOR 2
8+
#define SINRICPRO_VERSION_MINOR 3
99
#define SINRICPRO_VERSION_REVISION 1
1010
#define SINRICPRO_VERSION STR(SINRICPRO_VERSION_MAJOR) "." STR(SINRICPRO_VERSION_MINOR) "." STR(SINRICPRO_VERSION_REVISION)
1111
#define SINRICPRO_VERSION_STR "SinricPro (v" SINRICPRO_VERSION ")"

0 commit comments

Comments
 (0)