Skip to content

Commit

Permalink
Merge 1c51d39 into 8a56f40
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost committed Nov 12, 2021
2 parents 8a56f40 + 1c51d39 commit 4435196
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 44 deletions.
1 change: 1 addition & 0 deletions examples/weather_station/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ target_include_directories(app PRIVATE
target_sources(app PRIVATE
src/app_task.cpp
src/main.cpp
src/buzzer.cpp
src/zap-generated/attribute-size.cpp
src/zap-generated/IMClusterCommandHandler.cpp
src/zap-generated/callback-stub.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ CONFIG_OBERON_MBEDTLS_AES_C=n
# Add support for LEDs and buttons on Nordic development kits
CONFIG_DK_LIBRARY=y
CONFIG_DK_LIBRARY_INVERT_LEDS=n
CONFIG_PWM=y

# Configure UART logging and shell
CONFIG_LOG=y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ CONFIG_OBERON_MBEDTLS_AES_C=n
# Add support for LEDs and buttons on Nordic development kits
CONFIG_DK_LIBRARY=y
CONFIG_DK_LIBRARY_INVERT_LEDS=n
CONFIG_PWM=y

# Disable all debug features
CONFIG_SHELL=n
Expand Down
3 changes: 2 additions & 1 deletion examples/weather_station/src/app_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ struct AppEvent {
FunctionRelease,
FunctionTimer,
MeasurementsTimer,
IdentifyTimer,
};

enum UpdateLedStateEventType : uint8_t { UpdateLedState = MeasurementsTimer + 1 };
enum UpdateLedStateEventType : uint8_t { UpdateLedState = IdentifyTimer + 1 };

enum OtherEventType : uint8_t {
#ifdef CONFIG_MCUMGR_SMP_BT
Expand Down
66 changes: 65 additions & 1 deletion examples/weather_station/src/app_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "app_task.h"

#include "buzzer.h"
#include "LEDWidget.h"
#include <platform/CHIPDeviceLayer.h>

Expand All @@ -18,7 +19,6 @@
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>


#include <dk_buttons_and_leds.h>
#include <drivers/sensor.h>
#include <logging/log.h>
Expand Down Expand Up @@ -50,10 +50,16 @@ constexpr uint8_t kPressureMeasurementEndpointId = 3;
constexpr int16_t kPressureMeasurementAttributeMaxValue = 0x7fff;
constexpr int16_t kPressureMeasurementAttributeMinValue = 0x8001;
constexpr int16_t kPressureMeasurementAttributeInvalidValue = 0x8000;
constexpr uint8_t kIdentifyEndpointId = 0;
/* It is recommended to toggle the signalled state with 0.5 s interval. */
constexpr size_t kIdentifyTimerIntervalMs = 500;
/* The identify iime attribute SHALL be decremented every second. */
constexpr size_t kIdentifyDecrementIntervalMs = 1000;

K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent));
k_timer sFunctionTimer;
k_timer sMeasurementsTimer;
k_timer sIdentifyTimer;
FunctionTimerMode sFunctionTimerMode = FunctionTimerMode::kDisabled;

LEDWidget sRedLED;
Expand All @@ -67,6 +73,12 @@ bool sHaveBLEConnections;

LedState sLedState = LedState::kAlive;

static uint16_t sIdentifyDurationMs;
static uint16_t sIdentifyFullDurationTimeMs;
static Identify sIdentify = {
chip::EndpointId{ kIdentifyEndpointId }, AppTask::OnIdentifyStart, AppTask::OnIdentifyStop, EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_AUDIBLE_BEEP
};

const device *kBme688SensorDev = device_get_binding(DT_LABEL(DT_INST(0, bosch_bme680)));
} /* namespace */

Expand Down Expand Up @@ -96,6 +108,12 @@ int AppTask::Init()
return -1;
}

if (BuzzerInit())
{
LOG_ERR("Buzzer init failed");
return -1;
}

#ifdef CONFIG_MCUMGR_SMP_BT
GetDFUOverSMP().Init(RequestSMPAdvertisingStart);
GetDFUOverSMP().ConfirmNewImage();
Expand All @@ -109,6 +127,8 @@ int AppTask::Init()
&sMeasurementsTimer, [](k_timer *) { sAppTask.PostEvent(AppEvent{ AppEvent::MeasurementsTimer }); },
nullptr);
k_timer_start(&sMeasurementsTimer, K_MSEC(kMeasurementsIntervalMs), K_MSEC(kMeasurementsIntervalMs));
k_timer_init(&sIdentifyTimer, [](k_timer *) { sAppTask.PostEvent(AppEvent{ AppEvent::IdentifyTimer }); },
nullptr);

/* Init ZCL Data Model and start server */
chip::Server::GetInstance().Init();
Expand Down Expand Up @@ -190,6 +210,9 @@ void AppTask::DispatchEvent(AppEvent &event)
case AppEvent::MeasurementsTimer:
MeasurementsTimerHandler();
break;
case AppEvent::IdentifyTimer:
IdentifyTimerHandler();
break;
case AppEvent::UpdateLedState:
event.UpdateLedStateEvent.LedWidget->UpdateState();
break;
Expand Down Expand Up @@ -254,6 +277,47 @@ void AppTask::MeasurementsTimerHandler()
sAppTask.UpdateClusterState();
}

void AppTask::OnIdentifyStart(Identify *)
{
uint16_t identifyTimeS;
EmberAfStatus status = emberAfReadAttribute(kIdentifyEndpointId, ZCL_IDENTIFY_CLUSTER_ID, ZCL_IDENTIFY_TIME_ATTRIBUTE_ID, CLUSTER_MASK_SERVER, reinterpret_cast<uint8_t *>(&identifyTimeS), sizeof(identifyTimeS), NULL);
if (status != EMBER_ZCL_STATUS_SUCCESS) {
LOG_ERR("Reading identify time failed %x", status);
return;
}
sIdentifyFullDurationTimeMs = identifyTimeS * 1000;
k_timer_start(&sIdentifyTimer, K_MSEC(kIdentifyIntervalMs), K_MSEC(kIdentifyIntervalMs));
}

void AppTask::OnIdentifyStop(Identify *)
{
// empty
}

void AppTask::IdentifyTimerHandler()
{
sIdentifyDurationMs += kIdentifyIntervalMs;
/* Every second decrement IdentifyTime attribute value and update cluster state */
if (sIdentifyDurationMs % kIdentifyDecrementIntervalMs == 0) {
EmberAfStatus status;
int timeLeft = sIdentifyFullDurationTimeMs - sIdentifyDurationMs;
uint16_t identifyTime = timeLeft > 0 ? timeLeft : 0;
status = emberAfWriteAttribute(kIdentifyEndpointId, ZCL_IDENTIFY_CLUSTER_ID,
ZCL_IDENTIFY_TIME_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
reinterpret_cast<uint8_t *>(&identifyTime), ZCL_INT16U_ATTRIBUTE_TYPE);

if (status != EMBER_ZCL_STATUS_SUCCESS) {
LOG_ERR("Updating identify time failed %x", status);
}

if (identifyTime == 0) {
k_timer_stop(&sIdentifyTimer);
} else {
BuzzerToggleState();
}
}
}

void AppTask::UpdateClusterState()
{
struct sensor_value sTemperature, sPressure, sHumidity;
Expand Down
5 changes: 5 additions & 0 deletions examples/weather_station/src/app_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "app_event.h"

#include <app/clusters/identify-server/identify-server.h>
#include <platform/CHIPDeviceLayer.h>

#ifdef CONFIG_MCUMGR_SMP_BT
Expand All @@ -22,6 +23,9 @@ class AppTask {

void PostEvent(const AppEvent &aEvent);
void UpdateClusterState();
// void UpdateIdentifyClusterState(uint16_t identifyTime);
static void OnIdentifyStart(Identify *);
static void OnIdentifyStop(Identify *);

private:
friend AppTask &GetAppTask();
Expand All @@ -38,6 +42,7 @@ class AppTask {
static void ButtonReleaseHandler();
static void FunctionTimerHandler();
static void MeasurementsTimerHandler();
static void IdentifyTimerHandler();
static void UpdateStatusLED();
static void LEDStateUpdateHandler(LEDWidget &ledWidget);
static void ChipEventHandler(const chip::DeviceLayer::ChipDeviceEvent *event, intptr_t arg);
Expand Down
41 changes: 41 additions & 0 deletions examples/weather_station/src/buzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include "buzzer.h"

#include <drivers/pwm.h>

#define BUZZER_PWM_NODE DT_ALIAS(buzzer_pwm)
#define BUZZER_PWM_NAME DT_LABEL(BUZZER_PWM_NODE)
#define BUZZER_PWM_PIN DT_PROP(BUZZER_PWM_NODE, ch0_pin)
#define BUZZER_PWM_FLAGS DT_PWMS_FLAGS(BUZZER_PWM_NODE)

constexpr uint16_t kBuzzerPwmPeriodUs = 10000;
constexpr uint16_t kBuzzerPwmDutyCycleUs = 5000;

const device *kBuzzerDevice;
static bool sBuzzerState;

int BuzzerInit() {
kBuzzerDevice = device_get_binding(BUZZER_PWM_NAME);
if (!kBuzzerDevice)
{
return -ENODEV;
}
return 0;
}

void BuzzerSetState(bool onOff) {
sBuzzerState = onOff;
pwm_pin_set_usec(kBuzzerDevice, BUZZER_PWM_PIN, kBuzzerPwmPeriodUs, sBuzzerState ? kBuzzerPwmDutyCycleUs : 0,
BUZZER_PWM_FLAGS);
}

void BuzzerToggleState() {
sBuzzerState = !sBuzzerState;
pwm_pin_set_usec(kBuzzerDevice, BUZZER_PWM_PIN, kBuzzerPwmPeriodUs, sBuzzerState ? kBuzzerPwmDutyCycleUs : 0,
BUZZER_PWM_FLAGS);
}
9 changes: 9 additions & 0 deletions examples/weather_station/src/buzzer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

int BuzzerInit();
void BuzzerSetState(bool onOff);
void BuzzerToggleState();
Loading

0 comments on commit 4435196

Please sign in to comment.