Skip to content

Commit

Permalink
Merge branch 'master' into topic/pucn_add_doorlockfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 authored Sep 21, 2022
2 parents 107d7ca + 388c27c commit 7ae2127
Show file tree
Hide file tree
Showing 22 changed files with 151 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#define APP_LIGHT_BUTTON CYBSP_USER_BTN1
#define APP_FUNCTION_BUTTON CYBSP_USER_BTN2
#define APP_BUTTON_DEBOUNCE_PERIOD_MS 200
#define APP_BUTTON_MIN_ASSERT_TIME_MS 30

#define APP_BUTTON_PRESSED 0
#define APP_BUTTON_RELEASED 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void ButtonHandler::Init(void)
for (uint8_t i = 0; i < kButtonCount; i++)
{
buttonTimers[i] = xTimerCreate("BtnTmr", // Just a text name, not used by the RTOS kernel
APP_BUTTON_DEBOUNCE_PERIOD_MS, // timer period
APP_BUTTON_MIN_ASSERT_TIME_MS, // timer period
false, // no timer reload (==one-shot)
(void *) (int) i, // init timer id = button index
TimerCallback // timer callback handler (all buttons use
Expand Down Expand Up @@ -90,7 +90,7 @@ 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 = 0;
uint8_t buttonevent = 1;
timerId = (uint32_t) pvTimerGetTimerID(xTimer);

switch (timerId)
Expand All @@ -106,7 +106,7 @@ void ButtonHandler::TimerCallback(TimerHandle_t xTimer)
break;
}

if (buttonevent)
if (!buttonevent)
{
GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_PRESSED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#define APP_LIGHT_BUTTON CYBSP_USER_BTN1
#define APP_FUNCTION_BUTTON CYBSP_USER_BTN2
#define APP_BUTTON_DEBOUNCE_PERIOD_MS 200
#define APP_BUTTON_MIN_ASSERT_TIME_MS 30

#define APP_BUTTON_PRESSED 0
#define APP_BUTTON_RELEASED 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void ButtonHandler::Init(void)
for (uint8_t i = 0; i < kButtonCount; i++)
{
buttonTimers[i] = xTimerCreate("BtnTmr", // Just a text name, not used by the RTOS kernel
APP_BUTTON_DEBOUNCE_PERIOD_MS, // timer period
APP_BUTTON_MIN_ASSERT_TIME_MS, // timer period
false, // no timer reload (==one-shot)
(void *) (int) i, // init timer id = button index
TimerCallback // timer callback handler (all buttons use
Expand Down Expand Up @@ -90,7 +90,7 @@ 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 = 0;
uint8_t buttonevent = 1;
timerId = (uint32_t) pvTimerGetTimerID(xTimer);

switch (timerId)
Expand All @@ -106,7 +106,7 @@ void ButtonHandler::TimerCallback(TimerHandle_t xTimer)
break;
}

if (buttonevent)
if (!buttonevent)
{
GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_PRESSED);
}
Expand Down
12 changes: 10 additions & 2 deletions examples/chip-tool/config/PersistentStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@ constexpr LogCategory kDefaultLoggingLevel = kLogCategory_Automation;

std::string GetFilename(const char * name)
{
const char * tmpdir = getenv("TMPDIR");

if (tmpdir == nullptr)
{
tmpdir = "/tmp";
}

if (name == nullptr)
{
return "/tmp/chip_tool_config.ini";
return std::string(tmpdir) + "/chip_tool_config.ini";
}
return "/tmp/chip_tool_config." + std::string(name) + ".ini";

return std::string(tmpdir) + "/chip_tool_config." + std::string(name) + ".ini";
}

CHIP_ERROR PersistentStorage::Init(const char * name)
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/infineon/psoc6/include/AppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#define APP_LIGHT_BUTTON CYBSP_USER_BTN1
#define APP_FUNCTION_BUTTON CYBSP_USER_BTN2
#define APP_BUTTON_DEBOUNCE_PERIOD_MS 200
#define APP_BUTTON_MIN_ASSERT_TIME_MS 30

#define APP_BUTTON_PRESSED 0
#define APP_BUTTON_RELEASED 1
Expand Down
8 changes: 4 additions & 4 deletions examples/lighting-app/infineon/psoc6/src/ButtonHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void ButtonHandler::Init(void)
for (uint8_t i = 0; i < kButtonCount; i++)
{
buttonTimers[i] = xTimerCreate("BtnTmr", // Just a text name, not used by the RTOS kernel
APP_BUTTON_DEBOUNCE_PERIOD_MS, // timer period
APP_BUTTON_MIN_ASSERT_TIME_MS, // timer period
false, // no timer reload (==one-shot)
(void *) (int) i, // init timer id = button index
TimerCallback // timer callback handler (all buttons use
Expand Down Expand Up @@ -88,8 +88,8 @@ 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 = 0;
timerId = (uint32_t) pvTimerGetTimerID(xTimer);
uint8_t buttonevent;
timerId = (uint32_t) pvTimerGetTimerID(xTimer);
if (timerId)
{
buttonevent = cyhal_gpio_read(APP_FUNCTION_BUTTON);
Expand All @@ -98,7 +98,7 @@ void ButtonHandler::TimerCallback(TimerHandle_t xTimer)
{
buttonevent = cyhal_gpio_read(APP_LIGHT_BUTTON);
}
if (buttonevent)
if (!buttonevent)
{
GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_PRESSED);
}
Expand Down
4 changes: 4 additions & 0 deletions examples/lighting-app/qpg/include/CHIPProjectConfig.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@
*
* 0xFFF1: Test Vendor.
*/
#ifndef CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID
#define CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID 0xFFF1
#endif // CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID

/**
* CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID
*
* 0x8005: example lighting app
*/
#ifndef CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID
#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID 0x8005
#endif // CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID

/**
* CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION
Expand Down
12 changes: 7 additions & 5 deletions examples/lighting-app/telink/include/LightingManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -45,25 +45,27 @@ class LightingManager

using LightingCallback_fn = void (*)(Action_t, int32_t);

CHIP_ERROR Init(const device * pwmDevice, uint32_t pwmChannel);
CHIP_ERROR Init(const device * pwmDevice, uint32_t pwmChannel, uint8_t aMinLevel, uint8_t aMaxLevel, uint8_t aDefaultLevel = 0);
void Set(bool aOn);
bool IsTurnedOn() const { return mState == kState_On; }
uint8_t GetLevel() const { return mLevel; }
uint8_t GetMinLevel() const { return mMinLevel; }
uint8_t GetMaxLevel() const { return mMaxLevel; }
bool InitiateAction(Action_t aAction, int32_t aActor, uint8_t size, uint8_t * value);
void SetCallbacks(LightingCallback_fn aActionInitiated_CB, LightingCallback_fn aActionCompleted_CB);

private:
static constexpr uint8_t kMaxLevel = 254;

friend LightingManager & LightingMgr();
State_t mState;
uint8_t mMinLevel;
uint8_t mMaxLevel;
uint8_t mLevel;
const device * mPwmDevice;
uint32_t mPwmChannel;

LightingCallback_fn mActionInitiated_CB;
LightingCallback_fn mActionCompleted_CB;

void Set(bool aOn);
void SetLevel(uint8_t aLevel);
void UpdateLight();

Expand Down
27 changes: 16 additions & 11 deletions examples/lighting-app/telink/src/AppTask.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -32,6 +32,7 @@

#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-id.h>
#include <app/clusters/identify-server/identify-server.h>
#include <app/util/attribute-storage.h>
Expand Down Expand Up @@ -62,6 +63,8 @@ namespace {
constexpr int kAppEventQueueSize = 10;
constexpr uint8_t kButtonPushEvent = 1;
constexpr uint8_t kButtonReleaseEvent = 0;
constexpr uint8_t kDefaultMinLevel = 0;
constexpr uint8_t kDefaultMaxLevel = 254;

K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent));

Expand Down Expand Up @@ -112,6 +115,8 @@ Identify sIdentify = {

} // namespace

using namespace ::chip;
using namespace ::chip::app;
using namespace ::chip::Credentials;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Internal;
Expand All @@ -134,13 +139,18 @@ CHIP_ERROR AppTask::Init()
InitButtons();

// Init lighting manager
ret = LightingMgr().Init(LIGHTING_PWM_DEVICE, LIGHTING_PWM_CHANNEL);
uint8_t minLightLevel = kDefaultMinLevel;
Clusters::LevelControl::Attributes::MinLevel::Get(1, &minLightLevel);

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

ret = LightingMgr().Init(LIGHTING_PWM_DEVICE, LIGHTING_PWM_CHANNEL, minLightLevel, maxLightLevel, maxLightLevel);
if (ret != CHIP_NO_ERROR)
{
LOG_ERR("Failed to int lighting manager");
return ret;
}

LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted);

// Init ZCL Data Model and start server
Expand Down Expand Up @@ -422,20 +432,15 @@ void AppTask::DispatchEvent(AppEvent * aEvent)

void AppTask::UpdateClusterState()
{
uint8_t onoff = LightingMgr().IsTurnedOn();

// write the new on/off value
EmberAfStatus status =
emberAfWriteAttribute(1, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, &onoff, ZCL_BOOLEAN_ATTRIBUTE_TYPE);
EmberAfStatus status = Clusters::OnOff::Attributes::OnOff::Set(1, LightingMgr().IsTurnedOn());

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

uint8_t level = LightingMgr().GetLevel();

status =
emberAfWriteAttribute(1, ZCL_LEVEL_CONTROL_CLUSTER_ID, ZCL_CURRENT_LEVEL_ATTRIBUTE_ID, &level, ZCL_INT8U_ATTRIBUTE_TYPE);
status = Clusters::LevelControl::Attributes::CurrentLevel::Set(1, LightingMgr().GetLevel());

if (status != EMBER_ZCL_STATUS_SUCCESS)
{
Expand Down
19 changes: 13 additions & 6 deletions examples/lighting-app/telink/src/LightingManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,6 +20,8 @@

#include "AppConfig.h"

#include <lib/support/CodeUtils.h>

#include <drivers/pwm.h>
#include <logging/log.h>
#include <zephyr.h>
Expand All @@ -28,13 +30,16 @@ LOG_MODULE_DECLARE(app);

LightingManager LightingManager::sLight;

CHIP_ERROR LightingManager::Init(const device * pwmDevice, uint32_t pwmChannel)
CHIP_ERROR LightingManager::Init(const device * pwmDevice, uint32_t pwmChannel, 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;
mLevel = kMaxLevel;
mMinLevel = aMinLevel;
mMaxLevel = aMaxLevel;
mLevel = aDefaultLevel;
mPwmDevice = pwmDevice;
mPwmChannel = pwmChannel;

Expand Down Expand Up @@ -123,7 +128,9 @@ void LightingManager::Set(bool aOn)

void LightingManager::UpdateLight()
{
constexpr uint32_t kPwmWidthUs = 20000u;
const uint8_t level = mState == kState_On ? mLevel : 0;
pwm_pin_set_usec(mPwmDevice, mPwmChannel, kPwmWidthUs, kPwmWidthUs * level / kMaxLevel, 0);
constexpr uint32_t kPwmWidthUs = 20000u;
const uint8_t maxEffectiveLevel = mMaxLevel - mMinLevel;
const uint8_t effectiveLevel = mState == kState_On ? chip::min<uint8_t>(mLevel - mMinLevel, maxEffectiveLevel) : 0;

pwm_pin_set_usec(mPwmDevice, mPwmChannel, kPwmWidthUs, kPwmWidthUs * effectiveLevel / maxEffectiveLevel, 0);
}
24 changes: 19 additions & 5 deletions examples/lighting-app/telink/src/ZclCallbacks.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,36 +19,39 @@
#include "AppTask.h"
#include "LightingManager.h"

#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/ConcreteAttributePath.h>
#include <lib/support/logging/CHIPLogging.h>

using namespace chip;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::OnOff;

void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
uint8_t * value)
{
ClusterId clusterId = attributePath.mClusterId;
AttributeId attributeId = attributePath.mAttributeId;
ChipLogProgress(Zcl, "Cluster callback: " ChipLogFormatMEI, ChipLogValueMEI(clusterId));

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);
}
else if (clusterId == LevelControl::Id && attributeId == LevelControl::Attributes::CurrentLevel::Id)
{
ChipLogProgress(Zcl, "Value: %u, length %u", *value, size);
if (size == 1)
ChipLogProgress(Zcl, "Cluster LevelControl: attribute CurrentLevel set to %u", *value);

if (LightingMgr().IsTurnedOn())
{
LightingMgr().InitiateAction(LightingManager::LEVEL_ACTION, AppEvent::kEventType_Lighting, size, value);
}
else
{
ChipLogError(Zcl, "wrong length for level: %d", size);
ChipLogDetail(Zcl, "LED is off. Try to use move-to-level-with-on-off instead of move-to-level");
}
}
}
Expand All @@ -64,5 +67,16 @@ void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath &
*/
void emberAfOnOffClusterInitCallback(EndpointId endpoint)
{
EmberAfStatus status;
bool storedValue;

// Read storedValue on/off value
status = Attributes::OnOff::Get(1, &storedValue);
if (status == EMBER_ZCL_STATUS_SUCCESS)
{
// Set actual state to stored before reboot
LightingMgr().Set(storedValue);
}

GetAppTask().UpdateClusterState();
}
2 changes: 1 addition & 1 deletion examples/lock-app/infineon/psoc6/include/AppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#define APP_LOCK_BUTTON CYBSP_USER_BTN1
#define APP_FUNCTION_BUTTON CYBSP_USER_BTN2
#define APP_BUTTON_DEBOUNCE_PERIOD_MS 200
#define APP_BUTTON_MIN_ASSERT_TIME_MS 30

#define APP_BUTTON_PRESSED 0
#define APP_BUTTON_RELEASED 1
Expand Down
Loading

0 comments on commit 7ae2127

Please sign in to comment.