Skip to content

Commit

Permalink
Mbed OS capsense module support (#9398)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ATmobica authored and pull[bot] committed Sep 20, 2021
1 parent 719fbfa commit 1b0edcb
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions examples/lighting-app/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
79 changes: 63 additions & 16 deletions examples/lighting-app/mbed/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "LightingManager.h"
#include <app/server/OnboardingCodesUtil.h>

#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.
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Expand All @@ -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<LightingManager::Action_t>(aEvent->LightingEvent.Action);
Expand All @@ -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.");
}

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions examples/lighting-app/mbed/main/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct AppEvent
kEventType_Timer,
kEventType_Lighting,
kEventType_Install,
kEventType_Slider,
};

uint16_t Type;
Expand All @@ -52,6 +53,10 @@ struct AppEvent
uint8_t Action;
int32_t Actor;
} LightingEvent;
struct
{
uint8_t Value;
} SliderEvent;
};

EventHandler Handler;
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/mbed/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions examples/lighting-app/mbed/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

#include "AppTask.h"

#ifdef CAPSENSE_ENABLED
#include "capsense.h"
#endif

#include "mbedtls/platform.h"
#include <lib/support/CHIPMem.h>
#include <lib/support/logging/CHIPLogging.h>
Expand All @@ -37,6 +41,10 @@ int main()

mbed_logging_init();

#ifdef CAPSENSE_ENABLED
Capsense::getInstance().init();
#endif

ret = mbedtls_platform_setup(NULL);
if (ret)
{
Expand Down
4 changes: 2 additions & 2 deletions examples/lighting-app/mbed/mbed_app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
5 changes: 5 additions & 0 deletions examples/lock-app/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
31 changes: 19 additions & 12 deletions examples/lock-app/mbed/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "LEDWidget.h"
#include <app/server/OnboardingCodesUtil.h>

#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.
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions examples/lock-app/mbed/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

#include "AppTask.h"

#ifdef CAPSENSE_ENABLED
#include "capsense.h"
#endif

#include "mbedtls/platform.h"
#include <lib/support/CHIPMem.h>
#include <lib/support/logging/CHIPLogging.h>
Expand All @@ -37,6 +41,10 @@ int main()

mbed_logging_init();

#ifdef CAPSENSE_ENABLED
Capsense::getInstance().init();
#endif

ret = mbedtls_platform_setup(NULL);
if (ret)
{
Expand Down
6 changes: 3 additions & 3 deletions examples/lock-app/mbed/mbed_app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions third_party/mbed-os-cypress-capsense-button/repo
Submodule repo added at fef47a

0 comments on commit 1b0edcb

Please sign in to comment.