Skip to content

Commit d9e038d

Browse files
committed
feat: SmartButton
1 parent 9098807 commit d9e038d

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
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,131 @@
1+
#pragma once
2+
3+
#include "../SinricProRequest.h"
4+
#include "../EventLimiter.h"
5+
#include "../SinricProStrings.h"
6+
#include "../SinricProNamespace.h"
7+
8+
namespace SINRICPRO_NAMESPACE {
9+
10+
// String constants for button states and actions
11+
FSTR(BUTTONSTATE, state); // Button state key
12+
FSTR(BUTTONSTATE, singlePress); // Single press state value
13+
FSTR(BUTTONSTATE, doublePress); // Double press state value
14+
FSTR(BUTTONSTATE, longPress); // Long press state value
15+
FSTR(BUTTONSTATE, setSmartButtonState); // Set state action name
16+
17+
// Callback type definitions for different button press events
18+
using SmartButtonSinglePressCallback = std::function<bool(const String &)>;
19+
using SmartButtonDoublePressCallback = std::function<bool(const String &)>;
20+
using SmartButtonLongPressCallback = std::function<bool(const String &)>;
21+
22+
/**
23+
* @brief Controller class for managing smart button state and interactions
24+
*
25+
* @tparam T The device type that this controller is attached to
26+
*/
27+
template <typename T>
28+
class SmartButtonStateController {
29+
public:
30+
/**
31+
* @brief Construct a new Smart Button State Controller
32+
* Automatically registers the request handler with the device
33+
*/
34+
SmartButtonStateController();
35+
36+
/**
37+
* @brief Register callback for single press event from app
38+
* @param cb Callback function to handle single press
39+
*/
40+
void onSinglePress(SmartButtonSinglePressCallback cb);
41+
42+
/**
43+
* @brief Register callback for double press event from app
44+
* @param cb Callback function to handle double press
45+
*/
46+
void onDoublePress(SmartButtonDoublePressCallback cb);
47+
48+
/**
49+
* @brief Register callback for long press event from app
50+
* @param cb Callback function to handle long press
51+
*/
52+
void onLongPress(SmartButtonLongPressCallback cb);
53+
54+
protected:
55+
/**
56+
* @brief Handle incoming button state change requests
57+
* @param request The incoming request to process
58+
* @return true if request was handled successfully, false otherwise
59+
*/
60+
bool handleSmartButtonStateController(SinricProRequest &request);
61+
62+
private:
63+
SmartButtonSinglePressCallback smartButtonSinglePressCallback;
64+
SmartButtonDoublePressCallback smartButtonDoublePressCallback;
65+
SmartButtonLongPressCallback smartButtonLongPressCallback;
66+
67+
/**
68+
* returns true if states match, false otherwise
69+
*/
70+
inline bool isStateMatch(const SinricProRequest &request, const char* stateValue) {
71+
return request.request_value[FSTR_BUTTONSTATE_state] == stateValue;
72+
}
73+
};
74+
75+
// Implementation
76+
77+
template <typename T>
78+
SmartButtonStateController<T>::SmartButtonStateController() {
79+
T* device = static_cast<T*>(this);
80+
device->registerRequestHandler(
81+
std::bind(&SmartButtonStateController<T>::handleSmartButtonStateController,
82+
this,
83+
std::placeholders::_1)
84+
);
85+
}
86+
87+
template <typename T>
88+
void SmartButtonStateController<T>::onSinglePress(SmartButtonSinglePressCallback cb) {
89+
smartButtonSinglePressCallback = cb;
90+
}
91+
92+
template <typename T>
93+
void SmartButtonStateController<T>::onDoublePress(SmartButtonDoublePressCallback cb) {
94+
smartButtonDoublePressCallback = cb;
95+
}
96+
97+
template <typename T>
98+
void SmartButtonStateController<T>::onLongPress(SmartButtonLongPressCallback cb) {
99+
smartButtonLongPressCallback = cb;
100+
}
101+
102+
template <typename T>
103+
bool SmartButtonStateController<T>::handleSmartButtonStateController(SinricProRequest &request) {
104+
// Only process setSmartButtonState actions
105+
if (request.action != FSTR_BUTTONSTATE_setSmartButtonState) {
106+
return false;
107+
}
108+
109+
T* device = static_cast<T*>(this);
110+
bool success = false;
111+
112+
// Handle single press
113+
if (smartButtonSinglePressCallback && isStateMatch(request, FSTR_BUTTONSTATE_singlePress)) {
114+
success = smartButtonSinglePressCallback(device->deviceId);
115+
}
116+
// Handle double press
117+
else if (smartButtonDoublePressCallback && isStateMatch(request, FSTR_BUTTONSTATE_doublePress)) {
118+
success = smartButtonDoublePressCallback(device->deviceId);
119+
}
120+
// Handle long press
121+
else if (smartButtonLongPressCallback && isStateMatch(request, FSTR_BUTTONSTATE_longPress)) {
122+
success = smartButtonLongPressCallback(device->deviceId);
123+
}
124+
125+
return success;
126+
}
127+
128+
} // namespace SINRICPRO_NAMESPACE
129+
130+
template <typename T>
131+
using SmartButtonStateController = SINRICPRO_NAMESPACE::SmartButtonStateController<T>;

0 commit comments

Comments
 (0)