Skip to content

Commit

Permalink
Fix build after Power Source update
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian-Nordic committed Jul 15, 2022
1 parent 57a72e2 commit 9c0f6bf
Showing 1 changed file with 34 additions and 57 deletions.
91 changes: 34 additions & 57 deletions examples/weather_station/src/app_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ constexpr uint32_t kBatteryCapacityUaH = 1350000;
/* Average device current consumption in uA */
constexpr uint32_t kDeviceAverageCurrentConsumptionUa = CONFIG_AVERAGE_CURRENT_CONSUMPTION;
/* Fully charged battery operation time in seconds */
constexpr uint32_t kFullBatteryOperationTime =
kBatteryCapacityUaH / kDeviceAverageCurrentConsumptionUa * 3600;
constexpr uint32_t kFullBatteryOperationTime = kBatteryCapacityUaH / kDeviceAverageCurrentConsumptionUa * 3600;
/* It is recommended to toggle the signalled state with 0.5 s interval. */
constexpr size_t kIdentifyTimerIntervalMs = 500;

Expand All @@ -99,12 +98,12 @@ bool sHaveBLEConnections;
LedState sLedState = LedState::kAlive;

/* Add identify for all endpoints */
Identify sIdentifyTemperature = { chip::EndpointId{ kTemperatureMeasurementEndpointId }, AppTask::OnIdentifyStart, AppTask::OnIdentifyStop,
EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_AUDIBLE_BEEP };
Identify sIdentifyHumidity = { chip::EndpointId{ kHumidityMeasurementEndpointId }, AppTask::OnIdentifyStart, AppTask::OnIdentifyStop,
EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_AUDIBLE_BEEP };
Identify sIdentifyPressure = { chip::EndpointId{ kPressureMeasurementEndpointId }, AppTask::OnIdentifyStart, AppTask::OnIdentifyStop,
EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_AUDIBLE_BEEP };
Identify sIdentifyTemperature = { chip::EndpointId{ kTemperatureMeasurementEndpointId }, AppTask::OnIdentifyStart,
AppTask::OnIdentifyStop, EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_AUDIBLE_BEEP };
Identify sIdentifyHumidity = { chip::EndpointId{ kHumidityMeasurementEndpointId }, AppTask::OnIdentifyStart,
AppTask::OnIdentifyStop, EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_AUDIBLE_BEEP };
Identify sIdentifyPressure = { chip::EndpointId{ kPressureMeasurementEndpointId }, AppTask::OnIdentifyStart,
AppTask::OnIdentifyStop, EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_AUDIBLE_BEEP };

const device *sBme688SensorDev = DEVICE_DT_GET_ONE(bosch_bme680);
} /* namespace */
Expand Down Expand Up @@ -135,11 +134,9 @@ CHIP_ERROR AppTask::Init()
}

#ifdef CONFIG_OPENTHREAD_MTD_SED
err = ConnectivityMgr().SetThreadDeviceType(
ConnectivityManager::kThreadDeviceType_SleepyEndDevice);
err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice);
#else
err = ConnectivityMgr().SetThreadDeviceType(
ConnectivityManager::kThreadDeviceType_MinimalEndDevice);
err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice);
#endif
if (err != CHIP_NO_ERROR) {
LOG_ERR("ConnectivityMgr().SetThreadDeviceType() failed");
Expand Down Expand Up @@ -204,19 +201,13 @@ CHIP_ERROR AppTask::Init()

/* Initialize timers */
k_timer_init(
&sFunctionTimer,
[](k_timer *) { sAppTask.PostEvent(AppEvent{ AppEvent::FunctionTimer }); },
nullptr);
&sFunctionTimer, [](k_timer *) { sAppTask.PostEvent(AppEvent{ AppEvent::FunctionTimer }); }, nullptr);
k_timer_init(
&sMeasurementsTimer,
[](k_timer *) { sAppTask.PostEvent(AppEvent{ AppEvent::MeasurementsTimer }); },
&sMeasurementsTimer, [](k_timer *) { sAppTask.PostEvent(AppEvent{ AppEvent::MeasurementsTimer }); },
nullptr);
k_timer_start(&sMeasurementsTimer, K_MSEC(kMeasurementsIntervalMs),
K_MSEC(kMeasurementsIntervalMs));
k_timer_start(&sMeasurementsTimer, K_MSEC(kMeasurementsIntervalMs), K_MSEC(kMeasurementsIntervalMs));
k_timer_init(
&sIdentifyTimer,
[](k_timer *) { sAppTask.PostEvent(AppEvent{ AppEvent::IdentifyTimer }); },
nullptr);
&sIdentifyTimer, [](k_timer *) { sAppTask.PostEvent(AppEvent{ AppEvent::IdentifyTimer }); }, nullptr);

/* Initialize CHIP server */
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());
Expand All @@ -228,8 +219,7 @@ CHIP_ERROR AppTask::Init()
InitBasicOTARequestor();
#endif
ConfigurationMgr().LogDeviceConfig();
PrintOnboardingCodes(
chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));

/*
* Add CHIP event handler and start CHIP thread.
Expand Down Expand Up @@ -259,9 +249,8 @@ void AppTask::OpenPairingWindow()
return;
}

if (chip::Server::GetInstance()
.GetCommissioningWindowManager()
.OpenBasicCommissioningWindow() != CHIP_NO_ERROR) {
if (chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow() !=
CHIP_NO_ERROR) {
LOG_ERR("OpenBasicCommissioningWindow() failed");
}
}
Expand All @@ -275,7 +264,7 @@ CHIP_ERROR AppTask::StartApp()
while (true) {
k_msgq_get(&sAppEventQueue, &event, K_FOREVER);
DispatchEvent(event);
}
}

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -378,8 +367,7 @@ void AppTask::MeasurementsTimerHandler()

void AppTask::OnIdentifyStart(Identify *)
{
k_timer_start(&sIdentifyTimer, K_MSEC(kIdentifyTimerIntervalMs),
K_MSEC(kIdentifyTimerIntervalMs));
k_timer_start(&sIdentifyTimer, K_MSEC(kIdentifyTimerIntervalMs), K_MSEC(kIdentifyTimerIntervalMs));
}

void AppTask::OnIdentifyStop(Identify *)
Expand All @@ -402,8 +390,7 @@ void AppTask::UpdateTemperatureClusterState()
/* Defined by cluster temperature measured value = 100 x temperature in degC with resolution of
* 0.01 degC. val1 is an integer part of the value and val2 is fractional part in one-millionth
* parts. To achieve resolution of 0.01 degC val2 needs to be divided by 10000. */
int16_t newValue =
static_cast<int16_t>(sTemperature.val1 * 100 + sTemperature.val2 / 10000);
int16_t newValue = static_cast<int16_t>(sTemperature.val1 * 100 + sTemperature.val2 / 10000);

if (newValue > kTemperatureMeasurementAttributeMaxValue ||
newValue < kTemperatureMeasurementAttributeMinValue) {
Expand All @@ -430,17 +417,16 @@ void AppTask::UpdatePressureClusterState()
/* Defined by cluster pressure measured value = 10 x pressure in kPa with resolution of 0.1 kPa.
* val1 is an integer part of the value and val2 is fractional part in one-millionth parts.
* To achieve resolution of 0.1 kPa val2 needs to be divided by 100000. */
int16_t newValue =
static_cast<int16_t>(sPressure.val1 * 10 + sPressure.val2 / 100000);
int16_t newValue = static_cast<int16_t>(sPressure.val1 * 10 + sPressure.val2 / 100000);

if (newValue > kPressureMeasurementAttributeMaxValue ||
newValue < kPressureMeasurementAttributeMinValue) {
/* Read value exceeds permitted limits, so assign invalid value code to it. */
newValue = kPressureMeasurementAttributeInvalidValue;
}

status = Clusters::PressureMeasurement::Attributes::MeasuredValue::Set(
kPressureMeasurementEndpointId, newValue);
status = Clusters::PressureMeasurement::Attributes::MeasuredValue::Set(kPressureMeasurementEndpointId,
newValue);
if (status != EMBER_ZCL_STATUS_SUCCESS) {
LOG_ERR("Updating pressure measurement %x", status);
}
Expand All @@ -458,8 +444,7 @@ void AppTask::UpdateRelativeHumidityClusterState()
/* Defined by cluster humidity measured value = 100 x humidity in %RH with resolution of 0.01 %.
* val1 is an integer part of the value and val2 is fractional part in one-millionth parts.
* To achieve resolution of 0.01 % val2 needs to be divided by 10000. */
uint16_t newValue =
static_cast<int16_t>(sHumidity.val1 * 100 + sHumidity.val2 / 10000);
uint16_t newValue = static_cast<int16_t>(sHumidity.val1 * 100 + sHumidity.val2 / 10000);

if (newValue > kHumidityMeasurementAttributeMaxValue ||
newValue < kHumidityMeasurementAttributeMinValue) {
Expand Down Expand Up @@ -510,8 +495,7 @@ void AppTask::UpdatePowerSourceClusterState()
(kMaximalOperatingVoltageMv - kMinimalOperatingVoltageMv);
}

batteryTimeRemaining =
kFullBatteryOperationTime * batteryPercentage / kMaxBatteryPercentage;
batteryTimeRemaining = kFullBatteryOperationTime * batteryPercentage / kMaxBatteryPercentage;

if (voltage < kCriticalThresholdVoltageMv) {
batteryChargeLevel = Clusters::PowerSource::BatChargeLevel::kCritical;
Expand All @@ -527,44 +511,37 @@ void AppTask::UpdatePowerSourceClusterState()
batteryCharged = Clusters::PowerSource::BatChargeState::kIsNotCharging;
}

status = Clusters::PowerSource::Attributes::BatteryVoltage::Set(kPowerSourceEndpointId,
voltage);
status = Clusters::PowerSource::Attributes::BatVoltage::Set(kPowerSourceEndpointId, voltage);
if (status != EMBER_ZCL_STATUS_SUCCESS) {
LOG_ERR("Updating battery voltage failed %x", status);
}

status = Clusters::PowerSource::Attributes::BatteryPercentRemaining::Set(
kPowerSourceEndpointId, batteryPercentage);
status = Clusters::PowerSource::Attributes::BatPercentRemaining::Set(kPowerSourceEndpointId, batteryPercentage);
if (status != EMBER_ZCL_STATUS_SUCCESS) {
LOG_ERR("Updating battery percentage failed %x", status);
}

status = Clusters::PowerSource::Attributes::BatteryTimeRemaining::Set(
kPowerSourceEndpointId, batteryTimeRemaining);
status = Clusters::PowerSource::Attributes::BatTimeRemaining::Set(kPowerSourceEndpointId, batteryTimeRemaining);
if (status != EMBER_ZCL_STATUS_SUCCESS) {
LOG_ERR("Updating battery time remaining failed %x", status);
}

status = Clusters::PowerSource::Attributes::BatteryChargeLevel::Set(
kPowerSourceEndpointId, chip::to_underlying(batteryChargeLevel));
status = Clusters::PowerSource::Attributes::BatChargeLevel::Set(kPowerSourceEndpointId, batteryChargeLevel);
if (status != EMBER_ZCL_STATUS_SUCCESS) {
LOG_ERR("Updating battery charge level failed %x", status);
}

status = Clusters::PowerSource::Attributes::Status::Set(kPowerSourceEndpointId,
chip::to_underlying(batteryStatus));
status = Clusters::PowerSource::Attributes::Status::Set(kPowerSourceEndpointId, batteryStatus);
if (status != EMBER_ZCL_STATUS_SUCCESS) {
LOG_ERR("Updating battery status failed %x", status);
}

status = Clusters::PowerSource::Attributes::BatteryPresent::Set(kPowerSourceEndpointId,
batteryPresent);
status = Clusters::PowerSource::Attributes::BatPresent::Set(kPowerSourceEndpointId, batteryPresent);
if (status != EMBER_ZCL_STATUS_SUCCESS) {
LOG_ERR("Updating battery present failed %x", status);
}

status = Clusters::PowerSource::Attributes::BatteryChargeState::Set(
kPowerSourceEndpointId, chip::to_underlying(batteryCharged));
status = Clusters::PowerSource::Attributes::BatChargeState::Set(kPowerSourceEndpointId, batteryCharged);
if (status != EMBER_ZCL_STATUS_SUCCESS) {
LOG_ERR("Updating battery charge failed %x", status);
}
Expand Down Expand Up @@ -624,7 +601,7 @@ void AppTask::UpdateStatusLED()
default:
break;
}
}
}

void AppTask::LEDStateUpdateHandler(LEDWidget &ledWidget)
{
Expand All @@ -640,8 +617,8 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent *event, intptr_t /* arg */)
if (NFCMgr().IsTagEmulationStarted()) {
LOG_INF("NFC Tag emulation is already started");
} else {
ShareQRCodeOverNFC(chip::RendezvousInformationFlags(
chip::RendezvousInformationFlag::kBLE));
ShareQRCodeOverNFC(
chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
}
} else if (event->CHIPoBLEAdvertisingChange.Result == kActivity_Stopped) {
NFCMgr().StopTagEmulation();
Expand Down

0 comments on commit 9c0f6bf

Please sign in to comment.