From c23ccf222b8260ff0ef057cc8c8a90cf3c8832e4 Mon Sep 17 00:00:00 2001 From: Artur Tynecki <77382963+ATmobica@users.noreply.github.com> Date: Wed, 15 Sep 2021 02:07:46 +0200 Subject: [PATCH] Mbed OS capsense module support (#9398) * Add mbed cypress capsense lib to third_party Add capsesne support to ligthing-app and lock-app * Update mbed-os-cypress-capsense-button lib version * Changes restyle * Restore gitmodules file formatting * Remove sleep workaround and restyle --- .gitmodules | 4 + examples/lighting-app/mbed/CMakeLists.txt | 5 ++ examples/lighting-app/mbed/main/AppTask.cpp | 79 +++++++++++++++---- .../lighting-app/mbed/main/include/AppEvent.h | 5 ++ .../lighting-app/mbed/main/include/AppTask.h | 2 + examples/lighting-app/mbed/main/main.cpp | 8 ++ examples/lighting-app/mbed/mbed_app.json | 4 +- examples/lock-app/mbed/CMakeLists.txt | 5 ++ examples/lock-app/mbed/main/AppTask.cpp | 31 +++++--- examples/lock-app/mbed/main/main.cpp | 8 ++ examples/lock-app/mbed/mbed_app.json | 6 +- .../mbed-os-cypress-capsense-button/repo | 1 + 12 files changed, 125 insertions(+), 33 deletions(-) create mode 160000 third_party/mbed-os-cypress-capsense-button/repo diff --git a/.gitmodules b/.gitmodules index 1a555556f64788..8d180217361302 100644 --- a/.gitmodules +++ b/.gitmodules @@ -95,6 +95,10 @@ path = third_party/mbed-os-posix-socket/repo url = https://github.com/ARMmbed/mbed-os-posix-socket.git branch = main +[submodule "mbed-os-cypress-capsense-button"] + path = third_party/mbed-os-cypress-capsense-button/repo + url = https://github.com/ARMmbed/mbed-os-cypress-capsense-button.git + branch = main [submodule "p6/abstraction-rtos"] path = third_party/p6/p6_sdk/libs/abstraction-rtos url = https://github.com/Infineon/abstraction-rtos diff --git a/examples/lighting-app/mbed/CMakeLists.txt b/examples/lighting-app/mbed/CMakeLists.txt index 09b957bc817706..95382b0918a451 100644 --- a/examples/lighting-app/mbed/CMakeLists.txt +++ b/examples/lighting-app/mbed/CMakeLists.txt @@ -121,6 +121,11 @@ if("WHD" IN_LIST MBED_TARGET_LABELS) ) endif() +if("capsense" IN_LIST MBED_TARGET_LABELS) + add_subdirectory(${CHIP_ROOT}/third_party/mbed-os-cypress-capsense-button/repo ./capsense_build) + target_link_libraries(${APP_TARGET} capsense) +endif() + mbed_set_post_build(${APP_TARGET}) option(VERBOSE_BUILD "Have a verbose build process") diff --git a/examples/lighting-app/mbed/main/AppTask.cpp b/examples/lighting-app/mbed/main/AppTask.cpp index 8494c3f7060404..043b1791e7f783 100644 --- a/examples/lighting-app/mbed/main/AppTask.cpp +++ b/examples/lighting-app/mbed/main/AppTask.cpp @@ -21,6 +21,10 @@ #include "LightingManager.h" #include +#ifdef CAPSENSE_ENABLED +#include "capsense.h" +#endif + // FIXME: Undefine the `sleep()` function included by the CHIPDeviceLayer.h // from unistd.h to avoid a conflicting declaration with the `sleep()` provided // by Mbed-OS in mbed_power_mgmt.h. @@ -58,13 +62,16 @@ static LEDWidget sStatusLED(MBED_CONF_APP_SYSTEM_STATE_LED); static mbed::InterruptIn sLightingButton(LIGHTING_BUTTON); static mbed::InterruptIn sFunctionButton(FUNCTION_BUTTON); - +#ifdef CAPSENSE_ENABLED +static mbed::CapsenseButton CapFunctionButton(Capsense::getInstance(), 0); +static mbed::CapsenseButton CapLockButton(Capsense::getInstance(), 1); +static mbed::CapsenseSlider CapSlider(Capsense::getInstance()); +#endif static bool sIsWiFiStationProvisioned = false; static bool sIsWiFiStationEnabled = false; static bool sIsWiFiStationConnected = false; static bool sIsPairedToAccount = false; static bool sHaveBLEConnections = false; -static bool sHaveServiceConnectivity = false; static mbed::Timeout sFunctionTimer; @@ -95,10 +102,17 @@ int AppTask::Init() //------------- // Initialize button +#ifdef CAPSENSE_ENABLED + CapFunctionButton.fall(mbed::callback(this, &AppTask::FunctionButtonPressEventHandler)); + CapFunctionButton.rise(mbed::callback(this, &AppTask::FunctionButtonReleaseEventHandler)); + CapLockButton.fall(mbed::callback(this, &AppTask::LightingButtonPressEventHandler)); + CapSlider.on_move(mbed::callback(this, &AppTask::SliderEventHandler)); +#else sLightingButton.fall(mbed::callback(this, &AppTask::LightingButtonPressEventHandler)); sFunctionButton.fall(mbed::callback(this, &AppTask::FunctionButtonPressEventHandler)); sFunctionButton.rise(mbed::callback(this, &AppTask::FunctionButtonReleaseEventHandler)); - //---------------- +#endif + // Initialize lighting manager LightingMgr().Init(MBED_CONF_APP_LIGHTING_STATE_LED); LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted); @@ -153,20 +167,14 @@ int AppTask::StartApp() sIsWiFiStationEnabled = ConnectivityMgr().IsWiFiStationEnabled(); sIsWiFiStationConnected = ConnectivityMgr().IsWiFiStationConnected(); sHaveBLEConnections = (ConnectivityMgr().NumBLEConnections() != 0); - sHaveServiceConnectivity = ConnectivityMgr().HaveServiceConnectivity(); PlatformMgr().UnlockChipStack(); } - // Consider the system to be "fully connected" if it has service - // connectivity and it is able to interact with the service on a regular basis. - bool isFullyConnected = sHaveServiceConnectivity; - // Update the status LED if factory reset has not been initiated. // - // If system has "full connectivity", keep the LED On constantly. + // If system is connected to Wi-Fi station, keep the LED On constantly. // - // If thread and service provisioned, but not attached to the thread network yet OR no - // connectivity to the service OR subscriptions are not fully established + // If Wi-Fi is provisioned, but not connected to Wi-Fi station yet // THEN blink the LED Off for a short period of time. // // If the system has ble connection(s) uptill the stage above, THEN blink the LEDs at an even @@ -175,12 +183,11 @@ int AppTask::StartApp() // Otherwise, blink the LED ON for a very short time. if (sAppTask.mFunction != kFunction_FactoryReset) { - if (isFullyConnected) + if (sIsWiFiStationConnected) { sStatusLED.Set(true); } - else if (sIsWiFiStationProvisioned && sIsWiFiStationEnabled && sIsPairedToAccount && - (!sIsWiFiStationConnected || !isFullyConnected)) + else if (sIsWiFiStationProvisioned && sIsWiFiStationEnabled && sIsPairedToAccount && !sIsWiFiStationConnected) { sStatusLED.Blink(950, 50); } @@ -202,7 +209,7 @@ void AppTask::LightingActionEventHandler(AppEvent * aEvent) { LightingManager::Action_t action = LightingManager::INVALID_ACTION; int32_t actor = 0; - + uint8_t value = 0; if (aEvent->Type == AppEvent::kEventType_Lighting) { action = static_cast(aEvent->LightingEvent.Action); @@ -213,8 +220,14 @@ void AppTask::LightingActionEventHandler(AppEvent * aEvent) action = LightingMgr().IsTurnedOn() ? LightingManager::OFF_ACTION : LightingManager::ON_ACTION; actor = AppEvent::kEventType_Button; } + else if (aEvent->Type == AppEvent::kEventType_Slider) + { + action = LightingManager::LEVEL_ACTION; + actor = AppEvent::kEventType_Slider; + value = aEvent->SliderEvent.Value; + } - if (action != LightingManager::INVALID_ACTION && !LightingMgr().InitiateAction(action, actor, 0, NULL)) + if (action != LightingManager::INVALID_ACTION && !LightingMgr().InitiateAction(action, actor, 0, &value)) ChipLogProgress(NotSpecified, "Action is already in progress or active."); } @@ -248,6 +261,40 @@ void AppTask::FunctionButtonReleaseEventHandler() sAppTask.PostEvent(&button_event); } +void AppTask::ButtonEventHandler(uint32_t id, bool pushed) +{ + if (id > 1) + { + ChipLogError(NotSpecified, "Wrong button ID"); + return; + } + + AppEvent button_event; + button_event.Type = AppEvent::kEventType_Button; + button_event.ButtonEvent.Pin = id == 0 ? LIGHTING_BUTTON : FUNCTION_BUTTON; + button_event.ButtonEvent.Action = pushed ? BUTTON_PUSH_EVENT : BUTTON_RELEASE_EVENT; + + if (id == 0) + { + button_event.Handler = LightingActionEventHandler; + } + else + { + button_event.Handler = FunctionHandler; + } + + sAppTask.PostEvent(&button_event); +} + +void AppTask::SliderEventHandler(int slider_pos) +{ + AppEvent slider_event; + slider_event.Type = AppEvent::kEventType_Slider; + slider_event.SliderEvent.Value = slider_pos; + slider_event.Handler = LightingActionEventHandler; + sAppTask.PostEvent(&slider_event); +} + void AppTask::ActionInitiated(LightingManager::Action_t aAction, int32_t aActor) { if (aAction == LightingManager::ON_ACTION) diff --git a/examples/lighting-app/mbed/main/include/AppEvent.h b/examples/lighting-app/mbed/main/include/AppEvent.h index bd411a3313efee..f30b819e6feaf7 100644 --- a/examples/lighting-app/mbed/main/include/AppEvent.h +++ b/examples/lighting-app/mbed/main/include/AppEvent.h @@ -32,6 +32,7 @@ struct AppEvent kEventType_Timer, kEventType_Lighting, kEventType_Install, + kEventType_Slider, }; uint16_t Type; @@ -52,6 +53,10 @@ struct AppEvent uint8_t Action; int32_t Actor; } LightingEvent; + struct + { + uint8_t Value; + } SliderEvent; }; EventHandler Handler; diff --git a/examples/lighting-app/mbed/main/include/AppTask.h b/examples/lighting-app/mbed/main/include/AppTask.h index b016696e93f6df..1ffaf097f40375 100644 --- a/examples/lighting-app/mbed/main/include/AppTask.h +++ b/examples/lighting-app/mbed/main/include/AppTask.h @@ -50,6 +50,8 @@ class AppTask void LightingButtonPressEventHandler(void); void FunctionButtonPressEventHandler(void); void FunctionButtonReleaseEventHandler(void); + void ButtonEventHandler(uint32_t id, bool pushed); + void SliderEventHandler(int slider_pos); void TimerEventHandler(void); void StartTimer(uint32_t aTimeoutInMs); diff --git a/examples/lighting-app/mbed/main/main.cpp b/examples/lighting-app/mbed/main/main.cpp index dcdc747239f0c2..d482d5d33afebb 100644 --- a/examples/lighting-app/mbed/main/main.cpp +++ b/examples/lighting-app/mbed/main/main.cpp @@ -19,6 +19,10 @@ #include "AppTask.h" +#ifdef CAPSENSE_ENABLED +#include "capsense.h" +#endif + #include "mbedtls/platform.h" #include #include @@ -37,6 +41,10 @@ int main() mbed_logging_init(); +#ifdef CAPSENSE_ENABLED + Capsense::getInstance().init(); +#endif + ret = mbedtls_platform_setup(NULL); if (ret) { diff --git a/examples/lighting-app/mbed/mbed_app.json b/examples/lighting-app/mbed/mbed_app.json index 7d2b985b698029..3893ee4bc13324 100644 --- a/examples/lighting-app/mbed/mbed_app.json +++ b/examples/lighting-app/mbed/mbed_app.json @@ -30,11 +30,11 @@ "NL_ASSERT_EXPECT_FLAGS=NL_ASSERT_FLAG_LOG", "WHD_PRINT_DISABLE" ], + "target.components_add": ["capsense"], "led-active-state": 0, "system-state-led": "LED1", "lighting-state-led": "P9_6", - "lighting-button": "USER_BUTTON", - "function-button": "P9_7" + "lighting-button": "USER_BUTTON" } }, "config": { diff --git a/examples/lock-app/mbed/CMakeLists.txt b/examples/lock-app/mbed/CMakeLists.txt index 07f08335ac9658..4674b163e3f54b 100644 --- a/examples/lock-app/mbed/CMakeLists.txt +++ b/examples/lock-app/mbed/CMakeLists.txt @@ -124,6 +124,11 @@ if("WHD" IN_LIST MBED_TARGET_LABELS) ) endif() +if("capsense" IN_LIST MBED_TARGET_LABELS) + add_subdirectory(${CHIP_ROOT}/third_party/mbed-os-cypress-capsense-button/repo ./capsense_build) + target_link_libraries(${APP_TARGET} capsense) +endif() + mbed_set_post_build(${APP_TARGET}) option(VERBOSE_BUILD "Have a verbose build process") diff --git a/examples/lock-app/mbed/main/AppTask.cpp b/examples/lock-app/mbed/main/AppTask.cpp index df3e88189c7ab4..b860f2e50fe7fb 100644 --- a/examples/lock-app/mbed/main/AppTask.cpp +++ b/examples/lock-app/mbed/main/AppTask.cpp @@ -21,6 +21,10 @@ #include "LEDWidget.h" #include +#ifdef CAPSENSE_ENABLED +#include "capsense.h" +#endif + // FIXME: Undefine the `sleep()` function included by the CHIPDeviceLayer.h // from unistd.h to avoid a conflicting declaration with the `sleep()` provided // by Mbed-OS in mbed_power_mgmt.h. @@ -58,15 +62,19 @@ constexpr uint32_t kPublishServicePeriodUs = 5000000; static LEDWidget sStatusLED(MBED_CONF_APP_SYSTEM_STATE_LED); static LEDWidget sLockLED(MBED_CONF_APP_LOCK_STATE_LED); +#ifdef CAPSENSE_ENABLED +static mbed::CapsenseButton CapFunctionButton(Capsense::getInstance(), 0); +static mbed::CapsenseButton CapLockButton(Capsense::getInstance(), 1); +#else static mbed::InterruptIn sLockButton(LOCK_BUTTON); static mbed::InterruptIn sFunctionButton(FUNCTION_BUTTON); +#endif static bool sIsWiFiStationProvisioned = false; static bool sIsWiFiStationEnabled = false; static bool sIsWiFiStationConnected = false; static bool sIsPairedToAccount = false; static bool sHaveBLEConnections = false; -static bool sHaveServiceConnectivity = false; static mbed::Timeout sFunctionTimer; @@ -99,9 +107,15 @@ int AppTask::Init() sLockLED.Set(!BoltLockMgr().IsUnlocked()); // Initialize buttons +#ifdef CAPSENSE_ENABLED + CapFunctionButton.fall(mbed::callback(this, &AppTask::FunctionButtonPressEventHandler)); + CapFunctionButton.rise(mbed::callback(this, &AppTask::FunctionButtonReleaseEventHandler)); + CapLockButton.fall(mbed::callback(this, &AppTask::LockButtonPressEventHandler)); +#else sLockButton.fall(mbed::callback(this, &AppTask::LockButtonPressEventHandler)); sFunctionButton.fall(mbed::callback(this, &AppTask::FunctionButtonPressEventHandler)); sFunctionButton.rise(mbed::callback(this, &AppTask::FunctionButtonReleaseEventHandler)); +#endif // Initialize lock manager BoltLockMgr().Init(); @@ -156,20 +170,14 @@ int AppTask::StartApp() sIsWiFiStationEnabled = ConnectivityMgr().IsWiFiStationEnabled(); sIsWiFiStationConnected = ConnectivityMgr().IsWiFiStationConnected(); sHaveBLEConnections = (ConnectivityMgr().NumBLEConnections() != 0); - sHaveServiceConnectivity = ConnectivityMgr().HaveServiceConnectivity(); PlatformMgr().UnlockChipStack(); } - // Consider the system to be "fully connected" if it has service - // connectivity and it is able to interact with the service on a regular basis. - bool isFullyConnected = sHaveServiceConnectivity; - // Update the status LED if factory reset has not been initiated. // - // If system has "full connectivity", keep the LED On constantly. + // If system is connected to Wi-Fi station, keep the LED On constantly. // - // If thread and service provisioned, but not attached to the thread network yet OR no - // connectivity to the service OR subscriptions are not fully established + // If Wi-Fi is provisioned, but not connected to Wi-Fi station yet // THEN blink the LED Off for a short period of time. // // If the system has ble connection(s) uptill the stage above, THEN blink the LEDs at an even @@ -178,12 +186,11 @@ int AppTask::StartApp() // Otherwise, blink the LED ON for a very short time. if (sAppTask.mFunction != kFunction_FactoryReset) { - if (isFullyConnected) + if (sIsWiFiStationConnected) { sStatusLED.Set(true); } - else if (sIsWiFiStationProvisioned && sIsWiFiStationEnabled && sIsPairedToAccount && - (!sIsWiFiStationConnected || !isFullyConnected)) + else if (sIsWiFiStationProvisioned && sIsWiFiStationEnabled && sIsPairedToAccount && !sIsWiFiStationConnected) { sStatusLED.Blink(950, 50); } diff --git a/examples/lock-app/mbed/main/main.cpp b/examples/lock-app/mbed/main/main.cpp index dcdc747239f0c2..d482d5d33afebb 100644 --- a/examples/lock-app/mbed/main/main.cpp +++ b/examples/lock-app/mbed/main/main.cpp @@ -19,6 +19,10 @@ #include "AppTask.h" +#ifdef CAPSENSE_ENABLED +#include "capsense.h" +#endif + #include "mbedtls/platform.h" #include #include @@ -37,6 +41,10 @@ int main() mbed_logging_init(); +#ifdef CAPSENSE_ENABLED + Capsense::getInstance().init(); +#endif + ret = mbedtls_platform_setup(NULL); if (ret) { diff --git a/examples/lock-app/mbed/mbed_app.json b/examples/lock-app/mbed/mbed_app.json index 748871574e8a96..13fb8bc0f74796 100644 --- a/examples/lock-app/mbed/mbed_app.json +++ b/examples/lock-app/mbed/mbed_app.json @@ -30,11 +30,11 @@ "NL_ASSERT_EXPECT_FLAGS=NL_ASSERT_FLAG_LOG", "WHD_PRINT_DISABLE" ], - "led-active-state": 1, + "target.components_add": ["capsense"], + "led-active-state": 0, "system-state-led": "LED1", "lock-state-led": "P9_6", - "lock-button": "USER_BUTTON", - "function-button": "P9_7" + "lock-button": "USER_BUTTON" } }, "config": { diff --git a/third_party/mbed-os-cypress-capsense-button/repo b/third_party/mbed-os-cypress-capsense-button/repo new file mode 160000 index 00000000000000..fef47a05455670 --- /dev/null +++ b/third_party/mbed-os-cypress-capsense-button/repo @@ -0,0 +1 @@ +Subproject commit fef47a05455670206f6c2eb4958cb1f08d698bc1