Skip to content

Commit

Permalink
[Telink] Lighting app build with disabled CONFIG_PWM (project-chip#35621
Browse files Browse the repository at this point in the history
)

* soc: riscv: telink: Lighting app run without CONFIG_PWM

Add Dummy backend to the PWM Manager

Add option to execute Lightining app with LED Manager

Signed-off-by: Borys Nykytiuk <[email protected]>

* Restyled by whitespace

* Restyled by clang-format

* Override method linkLed inside lighting app

* Restyled by clang-format

---------

Signed-off-by: Borys Nykytiuk <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Alex Tsitsiura <[email protected]>
  • Loading branch information
3 people authored Sep 20, 2024
1 parent 3166e3a commit 4bdafa2
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 6 deletions.
6 changes: 5 additions & 1 deletion examples/lighting-app/telink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ target_sources(app PRIVATE
${TELINK_COMMON}/zephyr_ext/zephyr_key_matrix.c
${TELINK_COMMON}/zephyr_ext/zephyr_key_pool.c
${TELINK_COMMON}/zephyr_ext/zephyr_led_pool.c
${TELINK_COMMON}/zephyr_ext/zephyr_pwm_pool.c
${TELINK_COMMON}/zephyr_ext/zephyr_ws2812.c)

chip_configure_data_model(app
ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../lighting-common/lighting-app.zap
)

if(CONFIG_PWM)
target_sources(app PRIVATE
${TELINK_COMMON}/zephyr_ext/zephyr_pwm_pool.c)
endif()

if(CONFIG_BOOTLOADER_MCUBOOT)
target_sources(app PRIVATE ${TELINK_COMMON}/util/src/OTAUtil.cpp)
endif()
Expand Down
1 change: 1 addition & 0 deletions examples/lighting-app/telink/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class AppTask : public AppTaskCommon
friend class AppTaskCommon;

CHIP_ERROR Init(void);
void LinkLeds(LedManager & ledManager);

static void LightingActionEventHandler(AppEvent * aEvent);
#ifdef CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET
Expand Down
19 changes: 19 additions & 0 deletions examples/lighting-app/telink/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <app/server/Server.h>

#include "ColorFormat.h"
#include "LEDManager.h"
#include "PWMManager.h"

#include <app-common/zap-generated/attributes/Accessors.h>
Expand Down Expand Up @@ -130,16 +131,24 @@ void AppTask::SetInitiateAction(Fixture_Action aAction, int32_t aActor, uint8_t
if (aAction == ON_ACTION)
{
sfixture_on = true;
#ifdef CONFIG_PWM
PwmManager::getInstance().setPwm(PwmManager::EAppPwm_Red, (((uint32_t) sLedRgb.r * 1000) / UINT8_MAX));
PwmManager::getInstance().setPwm(PwmManager::EAppPwm_Green, (((uint32_t) sLedRgb.g * 1000) / UINT8_MAX));
PwmManager::getInstance().setPwm(PwmManager::EAppPwm_Blue, (((uint32_t) sLedRgb.b * 1000) / UINT8_MAX));
#else
LedManager::getInstance().setLed(LedManager::EAppLed_App0, true);
#endif
}
else
{
sfixture_on = false;
#ifdef CONFIG_PWM
PwmManager::getInstance().setPwm(PwmManager::EAppPwm_Red, false);
PwmManager::getInstance().setPwm(PwmManager::EAppPwm_Green, false);
PwmManager::getInstance().setPwm(PwmManager::EAppPwm_Blue, false);
#else
LedManager::getInstance().setLed(LedManager::EAppLed_App0, false);
#endif
}
}
else if (aAction == LEVEL_ACTION)
Expand Down Expand Up @@ -217,6 +226,9 @@ void AppTask::PowerOnFactoryResetEventHandler(AppEvent * aEvent)
PwmManager::getInstance().setPwm(PwmManager::EAppPwm_Red, (bool) (sPowerOnFactoryResetTimerCnt % 2));
PwmManager::getInstance().setPwm(PwmManager::EAppPwm_Green, (bool) (sPowerOnFactoryResetTimerCnt % 2));
PwmManager::getInstance().setPwm(PwmManager::EAppPwm_Blue, (bool) (sPowerOnFactoryResetTimerCnt % 2));
#if !CONFIG_PWM
LedManager::getInstance().setLed(LedManager::EAppLed_App0, (bool) (sPowerOnFactoryResetTimerCnt % 2));
#endif
k_timer_init(&sPowerOnFactoryResetTimer, PowerOnFactoryResetTimerEvent, nullptr);
k_timer_start(&sPowerOnFactoryResetTimer, K_MSEC(kPowerOnFactoryResetIndicationTimeMs),
K_MSEC(kPowerOnFactoryResetIndicationTimeMs));
Expand All @@ -237,3 +249,10 @@ void AppTask::PowerOnFactoryResetTimerEvent(struct k_timer * timer)
}
}
#endif /* CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET */

void AppTask::LinkLeds(LedManager & ledManager)
{
#if (!CONFIG_PWM)
ledManager.linkLed(LedManager::EAppLed_App0, 0);
#endif // !CONFIG_PWM
}
8 changes: 5 additions & 3 deletions examples/platform/telink/common/src/AppTaskCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,11 @@ void AppTaskCommon::InitPwms()

#if CONFIG_WS2812_STRIP
pwmManager.linkBackend(Ws2812Strip::getInstance());
#else
#elif CONFIG_PWM
pwmManager.linkBackend(PwmPool::getInstance());
#endif // CONFIG_WS2812_STRIP
#else
pwmManager.linkBackend(PwmDummy::getInstance());
#endif
}

void AppTaskCommon::LinkPwms(PwmManager & pwmManager)
Expand All @@ -420,7 +422,7 @@ void AppTaskCommon::LinkPwms(PwmManager & pwmManager)
pwmManager.linkPwm(PwmManager::EAppPwm_Red, 0);
pwmManager.linkPwm(PwmManager::EAppPwm_Green, 1);
pwmManager.linkPwm(PwmManager::EAppPwm_Blue, 2);
#else
#elif CONFIG_PWM
pwmManager.linkPwm(PwmManager::EAppPwm_Indication, 0);
pwmManager.linkPwm(PwmManager::EAppPwm_Red, 1);
pwmManager.linkPwm(PwmManager::EAppPwm_Green, 2);
Expand Down
22 changes: 21 additions & 1 deletion examples/platform/telink/util/include/PWMManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class Ws2812Strip : public PwmBackend
Ws2812Strip(){};
};

#else
#elif CONFIG_PWM

class PwmPool : public PwmBackend
{
Expand All @@ -150,4 +150,24 @@ class PwmPool : public PwmBackend
PwmPool(){};
};

#else

class PwmDummy : public PwmBackend
{
public:
static PwmDummy & getInstance();
bool linkHW();

void setPwmHW(size_t pwm, bool state);
void setPwmHW(size_t pwm, uint32_t permille);
void setPwmHWBlink(size_t pwm, size_t onMs, size_t offMs);
void setPwmHWBreath(size_t pwm, size_t breathMs);

PwmDummy(PwmDummy const &) = delete;
void operator=(PwmDummy const &) = delete;

private:
PwmDummy(){};
};

#endif // CONFIG_WS2812_STRIP
38 changes: 37 additions & 1 deletion examples/platform/telink/util/src/PWMManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void Ws2812Strip::setPwmHWBreath(size_t pwm, size_t breathMs)
LOG_WRN("WS2812 LED setPwmHWBreath not supported");
}

#else
#elif CONFIG_PWM

#include <zephyr_pwm_pool.h>

Expand Down Expand Up @@ -261,4 +261,40 @@ void PwmPool::setPwmHWBreath(size_t pwm, size_t breathMs)
}
}

#else
// Dummy implementation
PwmDummy & PwmDummy::getInstance()
{
static PwmDummy instance;

return instance;
}

bool PwmDummy::linkHW()
{
LOG_INF("PWM Dummy inited");

return true;
}

void PwmDummy::setPwmHW(size_t pwm, bool state)
{
LOG_INF("PWM Dummy %u turn %s", pwm, state ? "on" : "off");
}

void PwmDummy::setPwmHW(size_t pwm, uint32_t permille)
{
LOG_INF("PWM Dummy %u set %u", pwm, permille);
}

void PwmDummy::setPwmHWBlink(size_t pwm, size_t onMs, size_t offMs)
{
LOG_WRN("PWM Dummy setPwmHWBlink not supported");
}

void PwmDummy::setPwmHWBreath(size_t pwm, size_t breathMs)
{
LOG_WRN("PWM Dummy setPwmHWBreath not supported");
}

#endif // CONFIG_WS2812_STRIP

0 comments on commit 4bdafa2

Please sign in to comment.