Skip to content

Commit

Permalink
[Bouffalolab] Update application lighting operation in lighting-app (#…
Browse files Browse the repository at this point in the history
…25640)

* update application lighting operation

* Fix restyle

* add more comments
  • Loading branch information
wy-hh authored and pull[bot] committed Nov 14, 2023
1 parent 37d1a70 commit 973b2d1
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 283 deletions.
286 changes: 74 additions & 212 deletions examples/lighting-app/bouffalolab/common/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/ErrorStr.h>
#include <system/SystemClock.h>
#ifdef OTA_ENABLED
#include "OTAConfig.h"
#endif // OTA_ENABLED

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include <NetworkCommissioningDriver.h>
#include <route_hook/bl_route_hook.h>
#endif

#if HEAP_MONITORING
#include "MemMonitoring.h"
Expand Down Expand Up @@ -146,21 +138,32 @@ void AppTask::PostEvent(app_event_t event)
void AppTask::AppTaskMain(void * pvParameter)
{
app_event_t appEvent;
bool isStateReady = false;
bool onoff = false;

sLightLED.Init();

#ifdef LED_BTN_RESET
ButtonInit();
#else

/** Without RESET PIN defined, factory reset will be executed if power cycle count(resetCnt) >= APP_REBOOT_RESET_COUNT */
uint32_t resetCnt = 0;
size_t saved_value_len = 0;
ef_get_env_blob(APP_REBOOT_RESET_COUNT_KEY, &resetCnt, sizeof(resetCnt), &saved_value_len);
resetCnt++;
ef_set_env_blob(APP_REBOOT_RESET_COUNT_KEY, &resetCnt, sizeof(resetCnt));

/** To share with RESET PIN logic, mButtonPressedTime is used to recorded resetCnt increased.
* +1 makes sure mButtonPressedTime is not zero */
GetAppTask().mButtonPressedTime = chip::System::SystemClock().GetMonotonicMilliseconds64().count() + 1;
#endif

GetAppTask().sTimer = xTimerCreate("lightTmr", pdMS_TO_TICKS(1000), false, NULL, AppTask::TimerCallback);
if (GetAppTask().sTimer == NULL)
{
ChipLogError(NotSpecified, "Failed to create timer task");
appError(APP_ERROR_EVENT_QUEUE_FAILED);
}

ChipLogProgress(NotSpecified, "Starting Platform Manager Event Loop");
CHIP_ERROR ret = PlatformMgr().StartEventLoopTask();
if (ret != CHIP_NO_ERROR)
Expand All @@ -172,27 +175,10 @@ void AppTask::AppTaskMain(void * pvParameter)
GetAppTask().PostEvent(AppTask::APP_EVENT_TIMER);
vTaskSuspend(NULL);

#ifndef LED_BTN_RESET
GetAppTask().mButtonPressedTime = chip::System::SystemClock().GetMonotonicMilliseconds64().count() + 1;
if (ConnectivityMgr().IsThreadProvisioned())
{
GetAppTask().PostEvent(APP_EVENT_SYS_PROVISIONED);
}
#endif

GetAppTask().mIsConnected = false;

GetAppTask().sTimer = xTimerCreate("lightTmr", pdMS_TO_TICKS(1000), false, NULL, AppTask::TimerCallback);
if (GetAppTask().sTimer == NULL)
{
ChipLogError(NotSpecified, "Failed to create timer task");
appError(APP_ERROR_EVENT_QUEUE_FAILED);
}

ChipLogProgress(NotSpecified, "App Task started, with heap %d left\r\n", xPortGetFreeHeapSize());

StartTimer();

while (true)
{
appEvent = APP_EVENT_NONE;
Expand All @@ -201,29 +187,21 @@ void AppTask::AppTaskMain(void * pvParameter)
if (eventReceived)
{
PlatformMgr().LockChipStack();
if (APP_EVENT_SYS_BLE_ADV & appEvent)
{
LightingSetStatus(APP_EVENT_SYS_BLE_ADV);
LightingUpdate(APP_EVENT_LIGHTING_GO_THROUGH);

isStateReady = false;
}

if (APP_EVENT_SYS_PROVISIONED & appEvent)
{
LightingSetStatus(APP_EVENT_SYS_PROVISIONED);
LightingUpdate(APP_EVENT_LIGHTING_GO_THROUGH);

isStateReady = true;
LightingUpdate(APP_EVENT_LIGHTING_MASK);
}

if (APP_EVENT_BTN_SHORT & appEvent)
{
LightingSetStatus(APP_EVENT_SYS_LIGHT_TOGGLE);
LightingUpdate(APP_EVENT_LIGHTING_GO_THROUGH);
Clusters::OnOff::Attributes::OnOff::Get(GetAppTask().GetEndpointId(), &onoff);
onoff = !onoff;
Clusters::OnOff::Attributes::OnOff::Set(GetAppTask().GetEndpointId(), onoff);
LightingUpdate((app_event_t)(APP_EVENT_LIGHTING_MASK & appEvent));
}

if ((APP_EVENT_LIGHTING_MASK & appEvent) && isStateReady)
if (APP_EVENT_LIGHTING_MASK & appEvent)
{
LightingUpdate((app_event_t)(APP_EVENT_LIGHTING_MASK & appEvent));
}
Expand All @@ -233,6 +211,11 @@ void AppTask::AppTaskMain(void * pvParameter)
IdentifyHandleOp(appEvent);
}

if (APP_EVENT_SYS_BLE_ADV & appEvent)
{
LightingUpdate(APP_EVENT_SYS_BLE_ADV);
}

if (APP_EVENT_FACTORY_RESET & appEvent)
{
DeviceLayer::ConfigurationMgr().InitiateFactoryReset();
Expand All @@ -245,195 +228,67 @@ void AppTask::AppTaskMain(void * pvParameter)
}
}

void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t arg)
{
switch (event->Type)
{
case DeviceEventType::kCHIPoBLEAdvertisingChange:

#ifndef LED_BTN_RESET
if (ConnectivityMgr().IsThreadProvisioned())
{
GetAppTask().PostEvent(APP_EVENT_SYS_PROVISIONED);
break;
}
#endif

if (ConnectivityMgr().NumBLEConnections())
{
GetAppTask().PostEvent(APP_EVENT_SYS_BLE_CONN);
}
else
{
GetAppTask().PostEvent(APP_EVENT_SYS_BLE_ADV);

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
GetAppTask().mIsConnected = ConnectivityMgr().IsWiFiStationConnected();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
GetAppTask().mIsConnected = ConnectivityMgr().IsThreadAttached();
#endif
}
ChipLogProgress(NotSpecified, "BLE adv changed, connection number: %d\r\n", ConnectivityMgr().NumBLEConnections());
break;
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
case DeviceEventType::kThreadStateChange:

ChipLogProgress(NotSpecified, "Thread state changed, IsThreadAttached: %d\r\n", ConnectivityMgr().IsThreadAttached());
if (!GetAppTask().mIsConnected && ConnectivityMgr().IsThreadAttached())
{
GetAppTask().PostEvent(APP_EVENT_SYS_PROVISIONED);
GetAppTask().mIsConnected = true;
}
break;
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
case DeviceEventType::kWiFiConnectivityChange:

ChipLogProgress(NotSpecified, "Wi-Fi state changed to %s.\r\n",
ConnectivityMgr().IsWiFiStationConnected() ? "connected" : "disconnected");

chip::app::DnssdServer::Instance().StartServer();
NetworkCommissioning::BLWiFiDriver::GetInstance().SaveConfiguration();
if (!GetAppTask().mIsConnected && ConnectivityMgr().IsWiFiStationConnected())
{
GetAppTask().PostEvent(APP_EVENT_SYS_PROVISIONED);
GetAppTask().mIsConnected = true;
}
break;

case DeviceEventType::kInterfaceIpAddressChanged:
if ((event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV4_Assigned) ||
(event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned))
{
// MDNS server restart on any ip assignment: if link local ipv6 is configured, that
// will not trigger a 'internet connectivity change' as there is no internet
// connectivity. MDNS still wants to refresh its listening interfaces to include the
// newly selected address.
chip::app::DnssdServer::Instance().StartServer();
}

if (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned)
{
ChipLogProgress(NotSpecified, "Initializing route hook...");
bl_route_hook_init();
}
break;
#endif
case DeviceEventType::kFailSafeTimerExpired:

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
GetAppTask().mIsConnected = ConnectivityMgr().IsWiFiStationConnected();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
GetAppTask().mIsConnected = ConnectivityMgr().IsThreadAttached();
#endif

break;
default:
break;
}
}

void AppTask::LightingUpdate(app_event_t event)
void AppTask::LightingUpdate(app_event_t status)
{
uint8_t hue, sat;
bool onoff;
DataModel::Nullable<uint8_t> v;
DataModel::Nullable<uint8_t> v(0);
EndpointId endpoint = GetAppTask().GetEndpointId();

do
if (APP_EVENT_LIGHTING_MASK & status)
{
if (EMBER_ZCL_STATUS_SUCCESS != Clusters::OnOff::Attributes::OnOff::Get(endpoint, &onoff))
do
{
break;
}
if (EMBER_ZCL_STATUS_SUCCESS != Clusters::OnOff::Attributes::OnOff::Get(endpoint, &onoff))
{
break;
}

if (EMBER_ZCL_STATUS_SUCCESS != Clusters::LevelControl::Attributes::CurrentLevel::Get(endpoint, v))
{
break;
}
if (EMBER_ZCL_STATUS_SUCCESS != Clusters::LevelControl::Attributes::CurrentLevel::Get(endpoint, v))
{
break;
}

if (EMBER_ZCL_STATUS_SUCCESS != Clusters::ColorControl::Attributes::CurrentHue::Get(endpoint, &hue))
{
break;
}
if (EMBER_ZCL_STATUS_SUCCESS != Clusters::ColorControl::Attributes::CurrentHue::Get(endpoint, &hue))
{
break;
}

if (EMBER_ZCL_STATUS_SUCCESS != Clusters::ColorControl::Attributes::CurrentSaturation::Get(endpoint, &sat))
{
break;
}
if (EMBER_ZCL_STATUS_SUCCESS != Clusters::ColorControl::Attributes::CurrentSaturation::Get(endpoint, &sat))
{
break;
}

if (!onoff)
{
sLightLED.SetLevel(0);
}
else
{
if (v.IsNull())
if (!onoff)
{
// Just pick something.
v.SetNonNull(254);
sLightLED.SetLevel(0);
}
else
{
if (v.IsNull())
{
// Just pick something.
v.SetNonNull(254);
}
#if defined(BL706_NIGHT_LIGHT) || defined(BL602_NIGHT_LIGHT)
sLightLED.SetColor(v.Value(), hue, sat);
sLightLED.SetColor(v.Value(), hue, sat);
#else
sLightLED.SetLevel(v.Value());
sLightLED.SetLevel(v.Value());
#endif
}

} while (0);
}

void AppTask::LightingSetOnoff(uint8_t bonoff)
{
uint8_t newValue = bonoff;
EndpointId endpoint = GetAppTask().GetEndpointId();

// write the new on/off value
Clusters::OnOff::Attributes::OnOff::Set(endpoint, newValue);
newValue = 254;
Clusters::LevelControl::Attributes::CurrentLevel::Set(endpoint, newValue);
}

void AppTask::LightingSetStatus(app_event_t status)
{
uint8_t level, hue, sat;
bool onoff = true;
EndpointId endpoint = GetAppTask().GetEndpointId();
static bool isProvisioned = false;
}

if (APP_EVENT_SYS_LIGHT_TOGGLE == status)
{
Clusters::OnOff::Attributes::OnOff::Get(endpoint, &onoff);
onoff = !onoff;
}
else if (APP_EVENT_SYS_BLE_ADV == status)
{
hue = 35;
Clusters::ColorControl::Attributes::CurrentHue::Set(endpoint, hue);
sat = 254;
Clusters::ColorControl::Attributes::CurrentSaturation::Set(endpoint, sat);
level = 254;
Clusters::LevelControl::Attributes::CurrentLevel::Set(endpoint, level);

isProvisioned = false;
} while (0);
}
else if (APP_EVENT_SYS_PROVISIONED == status)
else if (APP_EVENT_SYS_BLE_ADV & status)
{
if (isProvisioned)
{
return;
}
isProvisioned = true;
sat = 0;
Clusters::ColorControl::Attributes::CurrentSaturation::Set(endpoint, sat);
level = 254;
Clusters::LevelControl::Attributes::CurrentLevel::Set(endpoint, level);
#if defined(BL706_NIGHT_LIGHT) || defined(BL602_NIGHT_LIGHT)
/** show yellow to indicate BLE advertisement */
sLightLED.SetColor(254, 35, 254);
#else
/** show 30% brightness to indicate BLE advertisement */
sLightLED.SetLevel(25);
#endif
}

Clusters::OnOff::Attributes::OnOff::Set(endpoint, onoff);
}

bool AppTask::StartTimer(void)
Expand Down Expand Up @@ -502,7 +357,14 @@ void AppTask::TimerEventHandler(app_event_t event)
StartTimer();
if (GetAppTask().mIsFactoryResetIndicat)
{
LightingUpdate(APP_EVENT_LIGHTING_GO_THROUGH);
if (GetAppTask().mIsConnected)
{
LightingUpdate(APP_EVENT_LIGHTING_MASK);
}
else
{
LightingUpdate(APP_EVENT_SYS_BLE_ADV);
}
}
GetAppTask().mIsFactoryResetIndicat = false;
GetAppTask().mButtonPressedTime = 0;
Expand Down Expand Up @@ -610,7 +472,7 @@ void AppTask::IdentifyHandleOp(app_event_t event)
if (APP_EVENT_IDENTIFY_STOP & event)
{
identifyState = 0;
LightingUpdate(APP_EVENT_LIGHTING_GO_THROUGH);
LightingUpdate(APP_EVENT_LIGHTING_MASK);
ChipLogProgress(NotSpecified, "identify stop");
}
}
Expand Down
Loading

0 comments on commit 973b2d1

Please sign in to comment.