Skip to content

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 6 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Version 3.3.1
- Support SmartButton.

## Version 3.2.1
- Fixed Arduino Lint errors
- LICENSE.txt added
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"maintainer": true
}
],
"version": "3.2.1",
"version": "3.3.1",
"frameworks": "arduino",
"platforms": [
"espressif8266",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
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
Expand Down
117 changes: 117 additions & 0 deletions src/Capabilities/SmartButtonStateController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#pragma once

#include "../SinricProRequest.h"
#include "../EventLimiter.h"
#include "../SinricProStrings.h"
#include "../SinricProNamespace.h"

/**
* @brief Enum defining the different types of button press events
*/
enum class SmartButtonPressType {
SINGLE_PRESS,
DOUBLE_PRESS,
LONG_PRESS
};

namespace SINRICPRO_NAMESPACE {

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 definition for button press events
using SmartButtonPressCallback = std::function<bool(const String&, SmartButtonPressType)>;

/**
* @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 button press events from the app
* @param cb Callback function to handle button press events
*/
void onButtonPress(SmartButtonPressCallback 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:
SmartButtonPressCallback buttonPressCallback;

/**
* @brief Convert string state to SmartButtonPressType enum
* @param stateStr The state string from the request
* @return corresponding SmartButtonPressType enum value
*/
SmartButtonPressType getSmartButtonPressType(const String& stateStr);
};

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>::onButtonPress(SmartButtonPressCallback cb) {
buttonPressCallback = cb;
}

template <typename T>
SmartButtonPressType SmartButtonStateController<T>::getSmartButtonPressType(const String& stateStr) {
if (stateStr == FSTR_BUTTONSTATE_singlePress) {
return SmartButtonPressType::SINGLE_PRESS;
} else if (stateStr == FSTR_BUTTONSTATE_doublePress) {
return SmartButtonPressType::DOUBLE_PRESS;
} else {
return SmartButtonPressType::LONG_PRESS;
}
}

template <typename T>
bool SmartButtonStateController<T>::handleSmartButtonStateController(SinricProRequest &request) {
// Only process setSmartButtonState actions
if (request.action != FSTR_BUTTONSTATE_setSmartButtonState || !buttonPressCallback) {
return false;
}

T* device = static_cast<T*>(this);
String stateStr = request.request_value[FSTR_BUTTONSTATE_state];

// Only process valid button states
if (stateStr != FSTR_BUTTONSTATE_singlePress &&
stateStr != FSTR_BUTTONSTATE_doublePress &&
stateStr != FSTR_BUTTONSTATE_longPress) {
return false;
}

SmartButtonPressType pressType = getSmartButtonPressType(stateStr);
return buttonPressCallback(device->deviceId, pressType);
}

} // namespace SINRICPRO_NAMESPACE

template <typename T>
using SmartButtonStateController = SINRICPRO_NAMESPACE::SmartButtonStateController<T>;
2 changes: 1 addition & 1 deletion src/SinricProVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

// Version Configuration
#define SINRICPRO_VERSION_MAJOR 3
#define SINRICPRO_VERSION_MINOR 2
#define SINRICPRO_VERSION_MINOR 3
#define SINRICPRO_VERSION_REVISION 1
#define SINRICPRO_VERSION STR(SINRICPRO_VERSION_MAJOR) "." STR(SINRICPRO_VERSION_MINOR) "." STR(SINRICPRO_VERSION_REVISION)
#define SINRICPRO_VERSION_STR "SinricPro (v" SINRICPRO_VERSION ")"
Expand Down
Loading