Skip to content
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

Add window-app for the nrfconnect platform #17678

Merged
merged 9 commits into from
May 5, 2022
Merged
2 changes: 1 addition & 1 deletion examples/all-clusters-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "AppTask.h"
#include "AppConfig.h"
#include "AppEvent.h"
#include "Utils.h"
#include "LEDUtil.h"
#include "binding-handler.h"

#include <app/server/OnboardingCodesUtil.h>
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ target_include_directories(app PRIVATE

target_sources(app PRIVATE
main/AppTask.cpp
main/LightingManager.cpp
main/main.cpp
main/ZclCallbacks.cpp
${GEN_DIR}/lighting-app/zap-generated/callback-stub.cpp
${GEN_DIR}/lighting-app/zap-generated/IMClusterCommandHandler.cpp
${NRFCONNECT_COMMON}/util/LEDWidget.cpp
${NRFCONNECT_COMMON}/util/PWMDevice.cpp
${NRFCONNECT_COMMON}/util/ThreadUtil.cpp)

chip_configure_data_model(app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
};

&pwm0 {
/delete-property/ ch0-pin;
/delete-property/ ch0-inverted;
ch1-pin = < 0xe >;
ch1-inverted;
};
41 changes: 20 additions & 21 deletions examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "AppConfig.h"
#include "AppEvent.h"
#include "LEDWidget.h"
#include "LightingManager.h"
#include "PWMDevice.h"
#include "ThreadUtil.h"

#include <app-common/zap-generated/attribute-id.h>
Expand Down Expand Up @@ -132,20 +132,19 @@ CHIP_ERROR AppTask::Init()

UpdateStatusLED();

// Initialize lighting manager
// Initialize lighting device (PWM)
uint8_t minLightLevel = kDefaultMinLevel;
Clusters::LevelControl::Attributes::MinLevel::Get(kLightEndpointId, &minLightLevel);

uint8_t maxLightLevel = kDefaultMaxLevel;
Clusters::LevelControl::Attributes::MaxLevel::Get(kLightEndpointId, &maxLightLevel);

int ret = LightingMgr().Init(LIGHTING_PWM_DEVICE, LIGHTING_PWM_CHANNEL, minLightLevel, maxLightLevel);
int ret = mPWMDevice.Init(LIGHTING_PWM_DEVICE, LIGHTING_PWM_CHANNEL, minLightLevel, maxLightLevel, maxLightLevel);
if (ret != 0)
{
return chip::System::MapErrorZephyr(ret);
}

LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted);
mPWMDevice.SetCallbacks(ActionInitiated, ActionCompleted);

// Initialize buttons
ret = dk_buttons_init(ButtonEventHandler);
Expand Down Expand Up @@ -210,21 +209,21 @@ CHIP_ERROR AppTask::StartApp()

void AppTask::LightingActionEventHandler(AppEvent * aEvent)
{
LightingManager::Action_t action = LightingManager::INVALID_ACTION;
int32_t actor = 0;
PWMDevice::Action_t action = PWMDevice::INVALID_ACTION;
int32_t actor = 0;

if (aEvent->Type == AppEvent::kEventType_Lighting)
{
action = static_cast<LightingManager::Action_t>(aEvent->LightingEvent.Action);
action = static_cast<PWMDevice::Action_t>(aEvent->LightingEvent.Action);
actor = aEvent->LightingEvent.Actor;
}
else if (aEvent->Type == AppEvent::kEventType_Button)
{
action = LightingMgr().IsTurnedOn() ? LightingManager::OFF_ACTION : LightingManager::ON_ACTION;
action = GetAppTask().mPWMDevice.IsTurnedOn() ? PWMDevice::OFF_ACTION : PWMDevice::ON_ACTION;
actor = AppEvent::kEventType_Button;
}

if (action != LightingManager::INVALID_ACTION && !LightingMgr().InitiateAction(action, actor, 0, NULL))
if (action != PWMDevice::INVALID_ACTION && GetAppTask().mPWMDevice.InitiateAction(action, actor, NULL))
LOG_INF("Action is already in progress or active.");
}

Expand Down Expand Up @@ -502,33 +501,33 @@ void AppTask::StartTimer(uint32_t aTimeoutInMs)
mFunctionTimerActive = true;
}

void AppTask::ActionInitiated(LightingManager::Action_t aAction, int32_t aActor)
void AppTask::ActionInitiated(PWMDevice::Action_t aAction, int32_t aActor)
{
if (aAction == LightingManager::ON_ACTION)
if (aAction == PWMDevice::ON_ACTION)
{
LOG_INF("Turn On Action has been initiated");
}
else if (aAction == LightingManager::OFF_ACTION)
else if (aAction == PWMDevice::OFF_ACTION)
{
LOG_INF("Turn Off Action has been initiated");
}
else if (aAction == LightingManager::LEVEL_ACTION)
else if (aAction == PWMDevice::LEVEL_ACTION)
{
LOG_INF("Level Action has been initiated");
}
}

void AppTask::ActionCompleted(LightingManager::Action_t aAction, int32_t aActor)
void AppTask::ActionCompleted(PWMDevice::Action_t aAction, int32_t aActor)
{
if (aAction == LightingManager::ON_ACTION)
if (aAction == PWMDevice::ON_ACTION)
{
LOG_INF("Turn On Action has been completed");
}
else if (aAction == LightingManager::OFF_ACTION)
else if (aAction == PWMDevice::OFF_ACTION)
{
LOG_INF("Turn Off Action has been completed");
}
else if (aAction == LightingManager::LEVEL_ACTION)
else if (aAction == PWMDevice::LEVEL_ACTION)
{
LOG_INF("Level Action has been completed");
}
Expand All @@ -539,7 +538,7 @@ void AppTask::ActionCompleted(LightingManager::Action_t aAction, int32_t aActor)
}
}

void AppTask::PostLightingActionRequest(LightingManager::Action_t aAction)
void AppTask::PostLightingActionRequest(PWMDevice::Action_t aAction)
{
AppEvent event;
event.Type = AppEvent::kEventType_Lighting;
Expand Down Expand Up @@ -571,14 +570,14 @@ void AppTask::DispatchEvent(AppEvent * aEvent)
void AppTask::UpdateClusterState()
{
// write the new on/off value
EmberAfStatus status = Clusters::OnOff::Attributes::OnOff::Set(kLightEndpointId, LightingMgr().IsTurnedOn());
EmberAfStatus status = Clusters::OnOff::Attributes::OnOff::Set(kLightEndpointId, mPWMDevice.IsTurnedOn());

if (status != EMBER_ZCL_STATUS_SUCCESS)
{
LOG_ERR("Updating on/off cluster failed: %x", status);
}

status = Clusters::LevelControl::Attributes::CurrentLevel::Set(kLightEndpointId, LightingMgr().GetLevel());
status = Clusters::LevelControl::Attributes::CurrentLevel::Set(kLightEndpointId, mPWMDevice.GetLevel());

if (status != EMBER_ZCL_STATUS_SUCCESS)
{
Expand Down
8 changes: 4 additions & 4 deletions examples/lighting-app/nrfconnect/main/ZclCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

#include "AppTask.h"
#include "LightingManager.h"
#include "PWMDevice.h"

#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
Expand All @@ -36,13 +36,13 @@ void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath &
if (clusterId == OnOff::Id && attributeId == OnOff::Attributes::OnOff::Id)
{
ChipLogProgress(Zcl, "Cluster OnOff: attribute OnOff set to %u", *value);
LightingMgr().InitiateAction(*value ? LightingManager::ON_ACTION : LightingManager::OFF_ACTION,
AppEvent::kEventType_Lighting, size, value);
GetAppTask().GetLightingDevice().InitiateAction(*value ? PWMDevice::ON_ACTION : PWMDevice::OFF_ACTION,
AppEvent::kEventType_Lighting, value);
}
else if (clusterId == LevelControl::Id && attributeId == LevelControl::Attributes::CurrentLevel::Id)
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
{
ChipLogProgress(Zcl, "Cluster LevelControl: attribute CurrentLevel set to %u", *value);
LightingMgr().InitiateAction(LightingManager::LEVEL_ACTION, AppEvent::kEventType_Lighting, size, value);
GetAppTask().GetLightingDevice().InitiateAction(PWMDevice::LEVEL_ACTION, AppEvent::kEventType_Lighting, value);
}
}

Expand Down
10 changes: 6 additions & 4 deletions examples/lighting-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "AppEvent.h"
#include "LEDWidget.h"
#include "LightingManager.h"
#include "PWMDevice.h"

#include <platform/CHIPDeviceLayer.h>

Expand All @@ -42,12 +42,13 @@ class AppTask
public:
CHIP_ERROR StartApp();

void PostLightingActionRequest(LightingManager::Action_t aAction);
void PostLightingActionRequest(PWMDevice::Action_t aAction);
void PostEvent(AppEvent * event);
void UpdateClusterState();

static void IdentifyStartHandler(Identify *);
static void IdentifyStopHandler(Identify *);
PWMDevice & GetLightingDevice() { return mPWMDevice; }

private:
#ifdef CONFIG_CHIP_PW_RPC
Expand All @@ -57,8 +58,8 @@ class AppTask
friend AppTask & GetAppTask(void);
CHIP_ERROR Init();

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

void CancelTimer(void);

Expand Down Expand Up @@ -95,6 +96,7 @@ class AppTask

Function_t mFunction = kFunction_NoneSelected;
bool mFunctionTimerActive = false;
PWMDevice mPWMDevice;
static AppTask sAppTask;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

#include "LightingManager.h"
#include "PWMDevice.h"

#include "AppConfig.h"

Expand All @@ -28,19 +28,14 @@

LOG_MODULE_DECLARE(app, CONFIG_MATTER_LOG_LEVEL);

LightingManager LightingManager::sLight;

int LightingManager::Init(const device * pwmDevice, uint32_t pwmChannel, uint8_t minLevel, uint8_t maxLevel)
int PWMDevice::Init(const device * aPWMDevice, uint32_t aPWMChannel, uint8_t aMinLevel, uint8_t aMaxLevel, uint8_t aDefaultLevel)
{
// We use a gpioPin instead of a LEDWidget here because we want to use PWM
// and other features instead of just on/off.

mState = kState_On;
mMinLevel = minLevel;
mMaxLevel = maxLevel;
mLevel = maxLevel;
mPwmDevice = pwmDevice;
mPwmChannel = pwmChannel;
mMinLevel = aMinLevel;
mMaxLevel = aMaxLevel;
mLevel = aDefaultLevel;
mPwmDevice = aPWMDevice;
mPwmChannel = aPWMChannel;

if (!device_is_ready(mPwmDevice))
{
Expand All @@ -52,13 +47,13 @@ int LightingManager::Init(const device * pwmDevice, uint32_t pwmChannel, uint8_t
return 0;
}

void LightingManager::SetCallbacks(LightingCallback_fn aActionInitiated_CB, LightingCallback_fn aActionCompleted_CB)
void PWMDevice::SetCallbacks(PWMCallback aActionInitiatedClb, PWMCallback aActionCompletedClb)
{
mActionInitiated_CB = aActionInitiated_CB;
mActionCompleted_CB = aActionCompleted_CB;
mActionInitiatedClb = aActionInitiatedClb;
mActionCompletedClb = aActionCompletedClb;
}

bool LightingManager::InitiateAction(Action_t aAction, int32_t aActor, uint16_t size, uint8_t * value)
bool PWMDevice::InitiateAction(Action_t aAction, int32_t aActor, uint8_t * aValue)
{
// TODO: this function is called InitiateAction because we want to implement some features such as ramping up here.
bool action_initiated = false;
Expand All @@ -75,10 +70,10 @@ bool LightingManager::InitiateAction(Action_t aAction, int32_t aActor, uint16_t
action_initiated = true;
new_state = kState_Off;
}
else if (aAction == LEVEL_ACTION && *value != mLevel)
else if (aAction == LEVEL_ACTION && *aValue != mLevel)
{
action_initiated = true;
if (*value == 0)
if (*aValue == 0)
{
new_state = kState_Off;
}
Expand All @@ -90,9 +85,9 @@ bool LightingManager::InitiateAction(Action_t aAction, int32_t aActor, uint16_t

if (action_initiated)
{
if (mActionInitiated_CB)
if (mActionInitiatedClb)
{
mActionInitiated_CB(aAction, aActor);
mActionInitiatedClb(aAction, aActor);
}

if (aAction == ON_ACTION || aAction == OFF_ACTION)
Expand All @@ -101,32 +96,33 @@ bool LightingManager::InitiateAction(Action_t aAction, int32_t aActor, uint16_t
}
else if (aAction == LEVEL_ACTION)
{
SetLevel(*value);
mState = new_state;
SetLevel(*aValue);
}

if (mActionCompleted_CB)
if (mActionCompletedClb)
{
mActionCompleted_CB(aAction, aActor);
mActionCompletedClb(aAction, aActor);
}
}

return action_initiated;
}

void LightingManager::SetLevel(uint8_t aLevel)
void PWMDevice::SetLevel(uint8_t aLevel)
{
LOG_INF("Setting brightness level to %u", aLevel);
mLevel = aLevel;
UpdateLight();
}

void LightingManager::Set(bool aOn)
void PWMDevice::Set(bool aOn)
{
mState = aOn ? kState_On : kState_Off;
UpdateLight();
}

void LightingManager::UpdateLight()
void PWMDevice::UpdateLight()
{
constexpr uint32_t kPwmWidthUs = 20000u;
const uint8_t maxEffectiveLevel = mMaxLevel - mMinLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <array>

// A lightweight wrrapper for unused LEDs
// A lightweight wrapper for unused LEDs
template <uint8_t size>
class UnusedLedsWrapper
{
Expand Down
Loading