-
Notifications
You must be signed in to change notification settings - Fork 131
feat: SmartButton #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: SmartButton #405
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d9e038d
feat: SmartButton
kakopappa 4c48333
feat: SmartButton
kakopappa baa541b
feat: refactor to use a single callback
kakopappa 330fbd5
feat: refactor to use onButtonPress
kakopappa 49bfa01
feat: smart button
kakopappa 0592d3e
fix: comments
kakopappa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| name=SinricPro | ||
| version=3.2.1 | ||
| version=3.3.1 | ||
| author=Boris Jaeger <[email protected]> | ||
| maintainer=Boris Jaeger <[email protected]> | ||
| sentence=Library for https://sinric.pro - simple way to connect your device to alexa | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| #pragma once | ||
|
|
||
| #include "../SinricProRequest.h" | ||
| #include "../EventLimiter.h" | ||
| #include "../SinricProStrings.h" | ||
| #include "../SinricProNamespace.h" | ||
|
|
||
| namespace SINRICPRO_NAMESPACE { | ||
|
|
||
| // String constants for button states and actions | ||
| FSTR(BUTTONSTATE, state); // Button state key | ||
| FSTR(BUTTONSTATE, singlePress); // Single press state value | ||
| FSTR(BUTTONSTATE, doublePress); // Double press state value | ||
| FSTR(BUTTONSTATE, longPress); // Long press state value | ||
| FSTR(BUTTONSTATE, setSmartButtonState); // Set state action name | ||
|
|
||
| // Callback type definitions for different button press events | ||
| using SmartButtonSinglePressCallback = std::function<bool(const String &)>; | ||
| using SmartButtonDoublePressCallback = std::function<bool(const String &)>; | ||
| using SmartButtonLongPressCallback = std::function<bool(const String &)>; | ||
|
|
||
| /** | ||
| * @brief Controller class for managing smart button state and interactions | ||
| * | ||
| * @tparam T The device type that this controller is attached to | ||
| */ | ||
| template <typename T> | ||
| class SmartButtonStateController { | ||
| public: | ||
| /** | ||
| * @brief Construct a new Smart Button State Controller | ||
| * Automatically registers the request handler with the device | ||
| */ | ||
| SmartButtonStateController(); | ||
|
|
||
| /** | ||
| * @brief Register callback for single press event from app | ||
| * @param cb Callback function to handle single press | ||
| */ | ||
| void onSinglePress(SmartButtonSinglePressCallback cb); | ||
|
|
||
| /** | ||
| * @brief Register callback for double press event from app | ||
| * @param cb Callback function to handle double press | ||
| */ | ||
| void onDoublePress(SmartButtonDoublePressCallback cb); | ||
|
|
||
| /** | ||
| * @brief Register callback for long press event from app | ||
| * @param cb Callback function to handle long press | ||
| */ | ||
| void onLongPress(SmartButtonLongPressCallback cb); | ||
|
|
||
| protected: | ||
| /** | ||
| * @brief Handle incoming button state change requests | ||
| * @param request The incoming request to process | ||
| * @return true if request was handled successfully, false otherwise | ||
| */ | ||
| bool handleSmartButtonStateController(SinricProRequest &request); | ||
|
|
||
| private: | ||
| SmartButtonSinglePressCallback smartButtonSinglePressCallback; | ||
| SmartButtonDoublePressCallback smartButtonDoublePressCallback; | ||
| SmartButtonLongPressCallback smartButtonLongPressCallback; | ||
|
|
||
| /** | ||
| * returns true if states match, false otherwise | ||
| */ | ||
| inline bool isStateMatch(const SinricProRequest &request, const char* stateValue) { | ||
| return request.request_value[FSTR_BUTTONSTATE_state] == stateValue; | ||
| } | ||
| }; | ||
|
|
||
| // Implementation | ||
|
|
||
| template <typename T> | ||
| SmartButtonStateController<T>::SmartButtonStateController() { | ||
| T* device = static_cast<T*>(this); | ||
| device->registerRequestHandler( | ||
| std::bind(&SmartButtonStateController<T>::handleSmartButtonStateController, | ||
| this, | ||
| std::placeholders::_1) | ||
| ); | ||
| } | ||
|
|
||
| template <typename T> | ||
| void SmartButtonStateController<T>::onSinglePress(SmartButtonSinglePressCallback cb) { | ||
| smartButtonSinglePressCallback = cb; | ||
| } | ||
|
|
||
| template <typename T> | ||
| void SmartButtonStateController<T>::onDoublePress(SmartButtonDoublePressCallback cb) { | ||
| smartButtonDoublePressCallback = cb; | ||
| } | ||
|
|
||
| template <typename T> | ||
| void SmartButtonStateController<T>::onLongPress(SmartButtonLongPressCallback cb) { | ||
| smartButtonLongPressCallback = cb; | ||
| } | ||
|
|
||
| template <typename T> | ||
| bool SmartButtonStateController<T>::handleSmartButtonStateController(SinricProRequest &request) { | ||
| // Only process setSmartButtonState actions | ||
| if (request.action != FSTR_BUTTONSTATE_setSmartButtonState) { | ||
| return false; | ||
| } | ||
|
|
||
| T* device = static_cast<T*>(this); | ||
| bool success = false; | ||
|
|
||
| // Handle single press | ||
| if (smartButtonSinglePressCallback && isStateMatch(request, FSTR_BUTTONSTATE_singlePress)) { | ||
| success = smartButtonSinglePressCallback(device->deviceId); | ||
| } | ||
| // Handle double press | ||
| else if (smartButtonDoublePressCallback && isStateMatch(request, FSTR_BUTTONSTATE_doublePress)) { | ||
| success = smartButtonDoublePressCallback(device->deviceId); | ||
| } | ||
| // Handle long press | ||
| else if (smartButtonLongPressCallback && isStateMatch(request, FSTR_BUTTONSTATE_longPress)) { | ||
| success = smartButtonLongPressCallback(device->deviceId); | ||
| } | ||
|
|
||
| return success; | ||
| } | ||
|
|
||
| } // namespace SINRICPRO_NAMESPACE | ||
|
|
||
| template <typename T> | ||
| using SmartButtonStateController = SINRICPRO_NAMESPACE::SmartButtonStateController<T>; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.