Skip to content

Commit

Permalink
Pull request #70: fixed silabs custom examples
Browse files Browse the repository at this point in the history
Merge in WMN_TOOLS/matter from fix_custom_examples to silabs

Squashed commit of the following:

commit 3136a0d145c0279f6db6a11de987c59ee40e7951
Author: Hussein Elsherbini <huelsher@silabs.com>
Date:   Fri Sep 16 12:55:45 2022 -0400

    removed some comments and undid some changes ... part 2
    JIRA: MATTER-693

commit 3177b416b6f1143629eed0649d14a35b4efbb94c
Author: Hussein Elsherbini <huelsher@silabs.com>
Date:   Fri Sep 16 12:46:20 2022 -0400

    removed some comments in apptask.cpp and undid some changes to baseapplication.cpp
    JIRA: MATTER-693

commit eb0ba402fabb4b1a63c47593287a5c277788a045
Author: Hussein Elsherbini <huelsher@silabs.com>
Date:   Thu Sep 15 11:19:21 2022 -0400

    modified Build.gn file and removed unnecessary code from source files
    JIRA: MATTER-693

... and 8 more commits
  • Loading branch information
HusseinElsherbini authored and jmartinez-silabs committed Feb 15, 2023
1 parent b0e2169 commit 9ac98c0
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 871 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"cwd": "${workspaceRoot}",
"executable": "${workspaceFolder}/out/${input:efr32Examples}-app/${input:efr32Boards}/chip-efr32-${input:efr32Examples}-example.out",
"serverpath": "/Applications/SEGGER/JLink_V758e/JLinkGDBServerCLExe",
"serverpath": "/Applications/SEGGER/JLink_V780/JLinkGDBServerCLExe",
"servertype": "jlink",
"device": "${input:efr32MCU}",
"interface": "swd",
Expand Down
7 changes: 4 additions & 3 deletions scripts/examples/gn_efr32_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ else
CHIP_ROOT="$MATTER_ROOT"
fi


source "$CHIP_ROOT/scripts/activate.sh"

set -x
Expand Down Expand Up @@ -196,9 +197,9 @@ else
USE_WIFI=true
fi

optArgs+=$1" "
shift
;;
optArgs+=$1" "
shift
;;
esac
done

Expand Down
33 changes: 25 additions & 8 deletions silabs_examples/sl-newLight/efr32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ if (efr32_board == "BRD4166A" || efr32_board == "BRD4180A") {
show_qr_code = false
}

if (efr32_board == "BRD4166A" || efr32_board == "BRD4160A" || efr32_board == "BRD2207A") {
if (efr32_board == "BRD4166A" || efr32_board == "BRD2601B") {
rgb_led = true
}

Expand Down Expand Up @@ -171,15 +171,16 @@ efr32_executable("newLight_app") {
defines = []

sources = [
"${examples_plat_dir}/BaseApplication.cpp",
"${examples_plat_dir}/LEDWidget.cpp",
"${examples_plat_dir}/efr32_utils.cpp",
"${examples_plat_dir}/heap_4_silabs.c",
"${examples_plat_dir}/init_efrPlatform.cpp",
"${examples_plat_dir}/matter_config.cpp",
"light_modules/LightingManager.cpp",
"src/AppTask.cpp",
"src/ZclCallbacks.cpp",
"src/main.cpp",
"light_modules/LightingManager.cpp",
]

if (chip_enable_pw_rpc || chip_build_libshell || enable_openthread_cli) {
Expand All @@ -188,6 +189,7 @@ efr32_executable("newLight_app") {

deps = [
":sdk",
"${chip_root}/examples/providers:device_info_provider",
"${chip_root}/silabs_examples/sl-newLight/newLight_DataModel_config",
"${chip_root}/src/lib",
"${chip_root}/src/setup_payload",
Expand All @@ -196,14 +198,15 @@ efr32_executable("newLight_app") {
# OpenThread Settings
if (chip_enable_openthread) {
deps += [
"${chip_root}/third_party/openthread/platforms:libopenthread-platform",
"${chip_root}/third_party/openthread/platforms:libopenthread-platform-utils",
"${chip_root}/third_party/openthread:openthread",
"${chip_root}/third_party/openthread:openthread-platform",
"${examples_plat_dir}:efr-matter-shell",
]
if (chip_openthread_ftd) {
deps += [
"${chip_root}/third_party/openthread/repo:libopenthread-cli-ftd",
"${chip_root}/third_party/openthread/repo:libopenthread-ftd",
"${chip_root}/third_party/openthread:openthread",
"${chip_root}/third_party/openthread:openthread-platform",
"${examples_plat_dir}:efr-matter-shell",
]
} else {
deps += [
Expand Down Expand Up @@ -248,8 +251,16 @@ efr32_executable("newLight_app") {
}

if (lcd_on) {
sources += [ "${examples_plat_dir}/display/lcd.c" ]
defines += [ "DISPLAY_ENABLED" ]
sources += [
"${examples_plat_dir}/display/demo-ui.c",
"${examples_plat_dir}/display/lcd.cpp",
]

include_dirs += [ "${examples_plat_dir}/display" ]
defines += [
"DISPLAY_ENABLED",
"IS_DEMO_LIGHT=1",
]
if (show_qr_code) {
defines += [ "QR_CODE_ENABLED" ]

Expand Down Expand Up @@ -333,6 +344,12 @@ efr32_executable("newLight_app") {
]
}

# Attestation Credentials

if (chip_build_platform_attestation_credentials_provider) {
deps += [ "${examples_plat_dir}:efr32-attestation-credentials" ]
}

output_dir = root_out_dir
}

Expand Down
3 changes: 3 additions & 0 deletions silabs_examples/sl-newLight/efr32/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ chip_enable_ota_requestor = true
pw_log_BACKEND = "${chip_root}/src/lib/support/pw_log_chip"
pw_assert_BACKEND = "$dir_pw_assert_log"
chip_enable_openthread = true

openthread_external_platform =
"${chip_root}/third_party/openthread/platforms/efr32:libopenthread-efr32"
60 changes: 17 additions & 43 deletions silabs_examples/sl-newLight/efr32/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@

#include <stdbool.h>
#include <stdint.h>

#include "BaseApplication.h"
#include "AppEvent.h"
#include "FreeRTOS.h"
#include "LightingManager.h"
#include "sl_simple_button_instances.h"

#include "LightingManager.h"
#include "timers.h" // provides FreeRTOS timer support
#include <app/clusters/identify-server/identify-server.h>
#include <ble/BLEEndPoint.h>
#include <lib/core/CHIPError.h>
#include <platform/CHIPDeviceLayer.h>

// Application-defined error codes in the CHIP_ERROR space.
Expand All @@ -39,59 +40,32 @@
#define APP_ERROR_START_TIMER_FAILED CHIP_APPLICATION_ERROR(0x05)
#define APP_ERROR_STOP_TIMER_FAILED CHIP_APPLICATION_ERROR(0x06)

class AppTask
class AppTask : public BaseApplication
{

public:
CHIP_ERROR StartAppTask();

AppTask() = default;
static AppTask & GetAppTask() { return sAppTask; }
static void AppTaskMain(void * pvParameter);
CHIP_ERROR StartAppTask();


void ButtonEventHandler(const sl_button_t * buttonHandle, uint8_t btnAction) override;
static void OnIdentifyStart(Identify * identify);
static void OnIdentifyStop(Identify * identify);

void PostLightActionRequest(int32_t aActor, LightingManager::Action_t aAction);
void PostLightControlActionRequest(int32_t aActor, LightingManager::Action_t aAction, uint8_t value);
void PostEvent(const AppEvent * event);

void ButtonEventHandler(const sl_button_t * buttonHandle, uint8_t btnAction);

private:
friend AppTask & GetAppTask(void);

CHIP_ERROR Init();
static AppTask sAppTask;

static void ActionInitiated(LightingManager::Action_t aAction, int32_t aActor);
static void ActionCompleted(LightingManager::Action_t aAction);

void CancelTimer(void);

void DispatchEvent(AppEvent * event);

static void FunctionTimerEventHandler(AppEvent * aEvent);
static void FunctionHandler(AppEvent * aEvent);
static void LightActionEventHandler(AppEvent * aEvent);
static void LightControlEventHandler(AppEvent * aEvent);
static void TimerEventHandler(TimerHandle_t xTimer);

static void UpdateClusterState(intptr_t context);

void StartTimer(uint32_t aTimeoutMs);

enum Function_t
{
kFunction_NoneSelected = 0,
kFunction_SoftwareUpdate = 0,
kFunction_StartBleAdv = 1,
kFunction_FactoryReset = 2,

kFunction_Invalid
} Function;

Function_t mFunction;
bool mFunctionTimerActive;
bool mSyncClusterToButtonAction;

static AppTask sAppTask;
static void ButtonHandler(AppEvent * aEvent);
CHIP_ERROR Init();
};

inline AppTask & GetAppTask(void)
{
return AppTask::sAppTask;
}
101 changes: 93 additions & 8 deletions silabs_examples/sl-newLight/efr32/light_modules/LightingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,28 @@
#include "AppTask.h"
#include <FreeRTOS.h>

#include <app/clusters/on-off-server/on-off-server.h>

using namespace chip;
using namespace ::chip::DeviceLayer;

LightingManager LightingManager::sLight;

TimerHandle_t sLightTimer;

namespace {

/**********************************************************
* OffWithEffect Callbacks
*********************************************************/

OnOffEffect gEffect = {
chip::EndpointId{ 1 },
LightMgr().OnTriggerOffWithEffect,
EMBER_ZCL_ON_OFF_EFFECT_IDENTIFIER_DELAYED_ALL_OFF,
static_cast<uint8_t>(EMBER_ZCL_ON_OFF_DELAYED_ALL_OFF_EFFECT_VARIANT_FADE_TO_OFF_IN_0P8_SECONDS),
};

} // namespace

CHIP_ERROR LightingManager::Init()
{
// Create FreeRTOS sw timer for light timer.
Expand All @@ -58,6 +71,7 @@ CHIP_ERROR LightingManager::Init()
mAutoTurnOffTimerArmed = false;
mAutoTurnOff = false;
mAutoTurnOffDuration = 0;
mOffEffectArmed = false;

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -94,13 +108,13 @@ bool LightingManager::InitiateAction(int32_t aActor, Action_t aAction)
State_t new_state;

// Initiate Turn On/Off Action only when the previous one is complete.
if (mState == kState_OffCompleted && aAction == ON_ACTION)
if (((mState == kState_OffCompleted) || mOffEffectArmed) && aAction == ON_ACTION)
{
action_initiated = true;

new_state = kState_OnInitiated;
}
else if (mState == kState_OnCompleted && aAction == OFF_ACTION)
else if (mState == kState_OnCompleted && aAction == OFF_ACTION && mOffEffectArmed == false)
{
action_initiated = true;

Expand All @@ -118,6 +132,12 @@ bool LightingManager::InitiateAction(int32_t aActor, Action_t aAction)
CancelTimer();
}

if (mOffEffectArmed && new_state == kState_OnInitiated)
{
CancelTimer();
mOffEffectArmed = false;
}

StartTimer(ACTUATOR_MOVEMENT_PERIOS_MS);

// Since the timer started successfully, update the state and trigger callback
Expand All @@ -131,7 +151,6 @@ bool LightingManager::InitiateAction(int32_t aActor, Action_t aAction)

return action_initiated;
}

bool LightingManager::InitiateActionLight(int32_t aActor, Action_t aAction, uint16_t endpoint, uint8_t value)
{
bool action_initiated = false;
Expand All @@ -142,7 +161,7 @@ bool LightingManager::InitiateActionLight(int32_t aActor, Action_t aAction, uint
case MOVE_TO_HUE:
case MOVE_TO_SAT:
action_initiated = true;
GetAppTask().PostLightControlActionRequest(aActor, aAction, value);
AppTask::GetAppTask().PostLightControlActionRequest(aActor, aAction, value);
break;
default:
break;
Expand Down Expand Up @@ -193,17 +212,21 @@ void LightingManager::TimerEventHandler(TimerHandle_t xTimer)
{
event.Handler = AutoTurnOffTimerEventHandler;
}
else if (light->mOffEffectArmed)
{
event.Handler = OffEffectTimerEventHandler;
}
else
{
event.Handler = ActuatorMovementTimerEventHandler;
}
GetAppTask().PostEvent(&event);
AppTask::GetAppTask().PostEvent(&event);
}

void LightingManager::AutoTurnOffTimerEventHandler(AppEvent * aEvent)
{
LightingManager * light = static_cast<LightingManager *>(aEvent->TimerEvent.Context);
int32_t actor = 0;
int32_t actor = AppEvent::kEventType_Timer;

// Make sure auto turn off timer is still armed.
if (!light->mAutoTurnOffTimerArmed)
Expand All @@ -218,6 +241,24 @@ void LightingManager::AutoTurnOffTimerEventHandler(AppEvent * aEvent)
light->InitiateAction(actor, OFF_ACTION);
}

void LightingManager::OffEffectTimerEventHandler(AppEvent * aEvent)
{
LightingManager * light = static_cast<LightingManager *>(aEvent->TimerEvent.Context);
int32_t actor = AppEvent::kEventType_Timer;

// Make sure auto turn off timer is still armed.
if (!light->mOffEffectArmed)
{
return;
}

light->mOffEffectArmed = false;

EFR32_LOG("OffEffect completed");

light->InitiateAction(actor, OFF_ACTION);
}

void LightingManager::ActuatorMovementTimerEventHandler(AppEvent * aEvent)
{
Action_t actionCompleted = INVALID_ACTION;
Expand Down Expand Up @@ -253,3 +294,47 @@ void LightingManager::ActuatorMovementTimerEventHandler(AppEvent * aEvent)
}
}
}

void LightingManager::OnTriggerOffWithEffect(OnOffEffect * effect)
{
chip::app::Clusters::OnOff::OnOffEffectIdentifier effectId = effect->mEffectIdentifier;
uint8_t effectVariant = effect->mEffectVariant;
uint32_t offEffectDuration = 0;

// Temporary print outs and delay to test OffEffect behaviour
// Until dimming is supported for dev boards.
if (effectId == EMBER_ZCL_ON_OFF_EFFECT_IDENTIFIER_DELAYED_ALL_OFF)
{
if (effectVariant == EMBER_ZCL_ON_OFF_DELAYED_ALL_OFF_EFFECT_VARIANT_FADE_TO_OFF_IN_0P8_SECONDS)
{
offEffectDuration = 800;
ChipLogProgress(Zcl, "EMBER_ZCL_ON_OFF_DELAYED_ALL_OFF_EFFECT_VARIANT_FADE_TO_OFF_IN_0P8_SECONDS");
}
else if (effectVariant == EMBER_ZCL_ON_OFF_DELAYED_ALL_OFF_EFFECT_VARIANT_NO_FADE)
{
offEffectDuration = 800;
ChipLogProgress(Zcl, "EMBER_ZCL_ON_OFF_DELAYED_ALL_OFF_EFFECT_VARIANT_NO_FADE");
}
else if (effectVariant ==
EMBER_ZCL_ON_OFF_DELAYED_ALL_OFF_EFFECT_VARIANT_50_PERCENT_DIM_DOWN_IN_0P8_SECONDS_THEN_FADE_TO_OFF_IN_12_SECONDS)
{
offEffectDuration = 12800;
ChipLogProgress(Zcl,
"EMBER_ZCL_ON_OFF_DELAYED_ALL_OFF_EFFECT_VARIANT_50_PERCENT_DIM_DOWN_IN_0P8_SECONDS_THEN_FADE_TO_OFF_"
"IN_12_SECONDS");
}
}
else if (effectId == EMBER_ZCL_ON_OFF_EFFECT_IDENTIFIER_DYING_LIGHT)
{
if (effectVariant ==
EMBER_ZCL_ON_OFF_DYING_LIGHT_EFFECT_VARIANT_20_PERCENTER_DIM_UP_IN_0P5_SECONDS_THEN_FADE_TO_OFF_IN_1_SECOND)
{
offEffectDuration = 1500;
ChipLogProgress(
Zcl, "EMBER_ZCL_ON_OFF_DYING_LIGHT_EFFECT_VARIANT_20_PERCENTER_DIM_UP_IN_0P5_SECONDS_THEN_FADE_TO_OFF_IN_1_SECOND");
}
}

LightMgr().mOffEffectArmed = true;
LightMgr().StartTimer(offEffectDuration);
}
Loading

0 comments on commit 9ac98c0

Please sign in to comment.