Skip to content

Commit

Permalink
[Infineon] Integrate Door Lock Alarm Event (#23852)
Browse files Browse the repository at this point in the history
* [Infineon] Integrate Door Lock Alarm Event

Fixes #23942

* Restyled by clang-format

* Update examples/lock-app/infineon/cyw30739/src/ButtonHandler.cpp

Co-authored-by: Boris Zbarsky <[email protected]>

Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Feb 8, 2024
1 parent e33e13e commit 1278274
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 49 deletions.
1 change: 1 addition & 0 deletions examples/lock-app/infineon/cyw30739/include/LockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class LockManager
{
LOCK_ACTION = 0,
UNLOCK_ACTION,
LOCK_JAMMED,

INVALID_ACTION
} Action;
Expand Down
49 changes: 27 additions & 22 deletions examples/lock-app/infineon/cyw30739/src/ButtonHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,48 +56,53 @@ wiced_result_t app_button_init(void)
memset(app_buttons, 0, (sizeof(button_manager_button_t) * APP_MAX_BUTTON_DEF));

app_button_configurations[ON_OFF_BUTTON].button = PLATFORM_BUTTON_1;
app_button_configurations[ON_OFF_BUTTON].button_event_mask = BUTTON_CLICK_EVENT;
app_button_configurations[ON_OFF_BUTTON].button_event_mask = (BUTTON_CLICK_EVENT | BUTTON_LONG_DURATION_EVENT);
app_buttons[ON_OFF_BUTTON].configuration = &app_button_configurations[ON_OFF_BUTTON];

result = wiced_button_manager_init(&app_button_manager, &app_button_manager_configuration, app_buttons, 1);

if (result != WICED_SUCCESS)
{
printf("button_manager_init failed (%d)\n", result);
ChipLogProgress(Zcl, "button_manager_init failed (%d)\n", result);
}
return result;
}

void app_button_event_handler(const button_manager_button_t * button_mgr, button_manager_event_t event,
button_manager_button_state_t state)
{
// printf("app_button_event_handler. button=%d, event=%d, state=%d\n", button_mgr[ON_OFF_BUTTON].configuration->button, event,
// state);
bool initiated = false;
LockManager::Action_t action;
CHIP_ERROR err = CHIP_NO_ERROR;
if (button_mgr[0].configuration->button == PLATFORM_BUTTON_1 && event == BUTTON_CLICK_EVENT && state == BUTTON_STATE_RELEASED)

ChipLogProgress(Zcl, "app_button_event_handler. button=%d, event=%d, state=%d\n",
button_mgr[ON_OFF_BUTTON].configuration->button, event, state);

/* This app is interested in PLATFORM_BUTTON_1 only */
if (button_mgr[0].configuration->button != PLATFORM_BUTTON_1)
return;

/* This callback is invoked both for held and released state, we want to process on the released event to avoid duplication */
if (state != BUTTON_STATE_RELEASED)
return;

/* single click to Lock/Unlock
long press to generate Jammed event */
if (event == BUTTON_CLICK_EVENT)
{
if (LockMgr().NextState() == true)
{
action = LockManager::LOCK_ACTION;
}
else
{
action = LockManager::UNLOCK_ACTION;
}
action = (LockMgr().NextState() == true) ? LockManager::LOCK_ACTION : LockManager::UNLOCK_ACTION;
}
else
else if (event == BUTTON_LONG_DURATION_EVENT)
{
err = CHIP_ERROR_UNEXPECTED_EVENT;
action = LockManager::LOCK_JAMMED;
}
if (err == CHIP_NO_ERROR)
else
{
initiated = LockMgr().InitiateAction(AppEvent::kEventType_Button, action);
return;
}

if (!initiated)
{
printf("Action is already in progress or active.");
}
initiated = LockMgr().InitiateAction(AppEvent::kEventType_Button, action);
if (!initiated)
{
ChipLogProgress(Zcl, "Action is already in progress or active.");
}
}
10 changes: 9 additions & 1 deletion examples/lock-app/infineon/cyw30739/src/LockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ bool LockManager::InitiateAction(int32_t aActor, Action_t aAction)
bool action_initiated = false;
State_t new_state;

if (aAction == LOCK_JAMMED)
{
ChipLogProgress(Zcl, "Sending a lock jammed event");

/* Generating Door Lock Jammed event */
DoorLockServer::Instance().SendLockAlarmEvent(1, DlAlarmCode::kLockJammed);
return true;
}

// Initiate Lock/Unlock Action only when the previous one is complete.
if (mState == kState_LockCompleted && aAction == UNLOCK_ACTION)
{
Expand All @@ -197,7 +206,6 @@ bool LockManager::InitiateAction(int32_t aActor, Action_t aAction)

if (action_initiated)
{

StartTimer(ACTUATOR_MOVEMENT_PERIOS_MS);

// Since the timer started successfully, update the state and trigger callback
Expand Down
3 changes: 2 additions & 1 deletion examples/lock-app/infineon/psoc6/include/AppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@

#define APP_LOCK_BUTTON CYBSP_USER_BTN1
#define APP_FUNCTION_BUTTON CYBSP_USER_BTN2
#define APP_BUTTON_MIN_ASSERT_TIME_MS 30
#define APP_BUTTON_MIN_ASSERT_TIME_MS 500

#define APP_BUTTON_PRESSED 0
#define APP_BUTTON_RELEASED 1
#define APP_BUTTON_LONG_PRESS 2

#define SYSTEM_STATE_LED CYBSP_USER_LED1
#define LOCK_STATE_LED CYBSP_USER_LED2
Expand Down
4 changes: 2 additions & 2 deletions examples/lock-app/infineon/psoc6/include/ButtonHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ButtonHandler

private:
static void GpioInit(void);
static void lock_button_callback(void * handler_arg, cyhal_gpio_event_t event);
static void func_button_callback(void * handler_arg, cyhal_gpio_event_t event);
static void LockButtonCallback(void * handler_arg, cyhal_gpio_event_t event);
static void FuncButtonCallback(void * handler_arg, cyhal_gpio_event_t event);
static void TimerCallback(TimerHandle_t xTimer);
};
8 changes: 8 additions & 0 deletions examples/lock-app/infineon/psoc6/include/CHIPProjectConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@
*/
#define CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE 1

/**
* CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC
*
* Enables synchronizing the device's real time clock with a remote Chip Time service
* using the Chip Time Sync protocol.
*/
#define CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC 0

/**
* CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER
*
Expand Down
25 changes: 20 additions & 5 deletions examples/lock-app/infineon/psoc6/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,31 @@ void AppTask::LockActionEventHandler(AppEvent * event)
}

case AppEvent::kEventType_Button: {
if (LockMgr().NextState() == true)

P6_LOG("%s [Action: %d]", __FUNCTION__, event->ButtonEvent.Action);

if (event->ButtonEvent.Action == APP_BUTTON_LONG_PRESS)
{
action = LockManager::LOCK_ACTION;
P6_LOG("Sending a lock jammed event");

/* Generating Door Lock Jammed event */
DoorLockServer::Instance().SendLockAlarmEvent(1 /* Endpoint Id */, DlAlarmCode::kLockJammed);

return;
}
else
{
action = LockManager::UNLOCK_ACTION;
}
if (LockMgr().NextState() == true)
{
action = LockManager::LOCK_ACTION;
}
else
{
action = LockManager::UNLOCK_ACTION;
}

actor = AppEvent::kEventType_Button;
actor = AppEvent::kEventType_Button;
}
break;
}

Expand Down
51 changes: 33 additions & 18 deletions examples/lock-app/infineon/psoc6/src/ButtonHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ TimerHandle_t buttonTimers[kButtonCount]; // FreeRTOS timers used for debouncing
void ButtonHandler::Init(void)
{
GpioInit();

// Create FreeRTOS sw timers for debouncing buttons.
for (uint8_t i = 0; i < kButtonCount; i++)
{
Expand All @@ -48,58 +49,72 @@ void ButtonHandler::Init(void)
void ButtonHandler::GpioInit(void)
{
cy_rslt_t result = CY_RSLT_SUCCESS;
static cyhal_gpio_callback_data_t lockButtonCbData;
static cyhal_gpio_callback_data_t funcButtonCbData;

// Set up button GPIOs to input with pullups.
result = cyhal_gpio_init(APP_LOCK_BUTTON, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF);
if (result != CY_RSLT_SUCCESS)
{
printf(" cyhal_gpio_init failed for APP_LOCK_BUTTON\r\n");
}

result = cyhal_gpio_init(APP_FUNCTION_BUTTON, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF);
if (result != CY_RSLT_SUCCESS)
{
printf(" cyhal_gpio_init failed for APP_FUNCTION_BUTTON\r\n");
}

/* Configure GPIO interrupt. */
static cyhal_gpio_callback_data_t lock_button_cbdata;
static cyhal_gpio_callback_data_t func_button_cbdata;
lock_button_cbdata.callback = lock_button_callback;
lock_button_cbdata.callback_arg = NULL;
cyhal_gpio_register_callback(APP_LOCK_BUTTON, &lock_button_cbdata);
func_button_cbdata.callback = func_button_callback;
func_button_cbdata.callback_arg = NULL;
cyhal_gpio_register_callback(APP_FUNCTION_BUTTON, &func_button_cbdata);
lockButtonCbData.callback = LockButtonCallback;
lockButtonCbData.callback_arg = NULL;
cyhal_gpio_register_callback(APP_LOCK_BUTTON, &lockButtonCbData);

funcButtonCbData.callback = FuncButtonCallback;
funcButtonCbData.callback_arg = NULL;
cyhal_gpio_register_callback(APP_FUNCTION_BUTTON, &funcButtonCbData);

cyhal_gpio_enable_event(APP_LOCK_BUTTON, CYHAL_GPIO_IRQ_FALL, GPIO_INTERRUPT_PRIORITY, true);
cyhal_gpio_enable_event(APP_FUNCTION_BUTTON, CYHAL_GPIO_IRQ_FALL, GPIO_INTERRUPT_PRIORITY, true);
}
void ButtonHandler::lock_button_callback(void * handler_arg, cyhal_gpio_event_t event)

void ButtonHandler::LockButtonCallback(void * handler_arg, cyhal_gpio_event_t event)
{
portBASE_TYPE taskWoken = pdFALSE;
xTimerStartFromISR(buttonTimers[APP_LOCK_BUTTON_IDX], &taskWoken);
}

void ButtonHandler::func_button_callback(void * handler_arg, cyhal_gpio_event_t event)
void ButtonHandler::FuncButtonCallback(void * handler_arg, cyhal_gpio_event_t event)
{
portBASE_TYPE taskWoken = pdFALSE;
xTimerStartFromISR(buttonTimers[APP_FUNCTION_BUTTON_IDX], &taskWoken);
}

void ButtonHandler::TimerCallback(TimerHandle_t xTimer)
{
// Get the button index of the expired timer and call button event helper.
uint32_t timerId;
uint8_t buttonevent;
uint32_t timerId = 0;
uint8_t buttonevent = 0;

/* Get the button index of the expired timer and call button event helper. */
timerId = (uint32_t) pvTimerGetTimerID(xTimer);
if (timerId)
{
buttonevent = cyhal_gpio_read(APP_FUNCTION_BUTTON);
if (buttonevent)
{
GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_PRESSED);
}
}
else
{
buttonevent = cyhal_gpio_read(APP_LOCK_BUTTON);
}

if (!buttonevent)
{
GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_PRESSED);
if (!buttonevent)
{
GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_LONG_PRESS);
}
else
{
GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_PRESSED);
}
}
}

0 comments on commit 1278274

Please sign in to comment.