Skip to content

Commit

Permalink
[Silabs] Update the efr32 Thermostat app (#23408)
Browse files Browse the repository at this point in the history
* Update the efr32 Thermostat app, Use the temperature sensor when available, add a LCD UI for thermostat

* Address PR comments, Cleanup rebase and regen
  • Loading branch information
jmartinez-silabs authored Nov 15, 2022
1 parent 66dcdce commit 9a41c9c
Show file tree
Hide file tree
Showing 23 changed files with 1,435 additions and 152 deletions.
49 changes: 49 additions & 0 deletions examples/platform/efr32/TemperatureSensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2019 Google LLC.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "TemperatureSensor.h"

#ifdef __cplusplus
extern "C" {
#endif
// This is a C implementation. Need the ifdef __cplusplus else we get linking issues
#include "sl_sensor_rht.h"

#ifdef __cplusplus
}
#endif

namespace TemperatureSensor {
constexpr uint16_t kSensorTemperatureOffset = 800;

sl_status_t Init()
{
return sl_sensor_rht_init();
}

sl_status_t GetTemp(uint32_t * relativeHumidity, int16_t * temperature)
{
// Sensor resolution 0.001 C
// DataModel resolution 0.01 C
int32_t temp;
sl_status_t status = sl_sensor_rht_get(relativeHumidity, &temp);
*temperature = static_cast<int16_t>(temp / 10) - kSensorTemperatureOffset;
return status;
}
}; // namespace TemperatureSensor
28 changes: 28 additions & 0 deletions examples/platform/efr32/TemperatureSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2019 Google LLC.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "sl_status.h"
#include <stdint.h>

namespace TemperatureSensor {
sl_status_t Init();
sl_status_t GetTemp(uint32_t * relativeHumidity, int16_t * temperature);
}; // namespace TemperatureSensor
16 changes: 14 additions & 2 deletions examples/platform/efr32/display/lcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,20 @@ void SilabsLCD::WriteDemoUI(bool state)
void SilabsLCD::WriteDemoUI()
{
Clear();
demoUIClearMainScreen(mName);
demoUIDisplayApp(dState.mainState);
if (customUI != nullptr)
{
customUI(&glibContext);
}
else
{
demoUIClearMainScreen(mName);
demoUIDisplayApp(dState.mainState);
}
}

void SilabsLCD::SetCustomUI(customUICB cb)
{
customUI = cb;
}

#ifdef QR_CODE_ENABLED
Expand Down
7 changes: 5 additions & 2 deletions examples/platform/efr32/display/lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ class SilabsLCD
{

public:
typedef void (*customUICB)(GLIB_Context_t * context);
CHIP_ERROR Init(uint8_t * name = nullptr, bool initialState = false);
void * Context();
int Clear(void);
int DrawPixel(void * pContext, int32_t x, int32_t y);
int Update(void);
void WriteDemoUI(bool state);
void SetCustomUI(customUICB cb);

#ifdef QR_CODE_ENABLED
void SetQRCode(uint8_t * str, uint32_t size);
void ShowQRCode(bool show, bool forceRefresh = false);
Expand Down Expand Up @@ -66,6 +69,6 @@ class SilabsLCD
#else
uint8_t mName[APP_NAME_MAX_LENGTH + 1];
#endif

DemoState_t dState;
customUICB customUI = nullptr;
DemoState_t dState;
};
34 changes: 34 additions & 0 deletions examples/thermostat/efr32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ assert(current_os == "freertos")

efr32_project_dir = "${chip_root}/examples/thermostat/efr32"
examples_plat_dir = "${chip_root}/examples/platform/efr32"
efr32_sdk_root = "${chip_root}/third_party/silabs/gecko_sdk"

declare_args() {
# Dump memory usage at link time.
Expand All @@ -59,6 +60,10 @@ declare_args() {

# Argument to Disable IPv4 for wifi(rs911)
chip_enable_wifi_ipv4 = false

# Enable the temperature sensor
# Some boards do not have a temperature sensor
use_temp_sensor = silabs_board != "BRD2703A" && silabs_board != "BRD4319A"
}

declare_args() {
Expand Down Expand Up @@ -133,6 +138,7 @@ efr32_sdk("sdk") {
"${efr32_project_dir}/include",
"${examples_plat_dir}",
"${chip_root}/src/lib",
"${efr32_sdk_root}/app/common/util/app_assert",
]

defines = [
Expand Down Expand Up @@ -172,6 +178,18 @@ efr32_sdk("sdk") {
defines += [ "SL_WFX_CONFIG_SCAN" ]
}
}
if (use_temp_sensor) {
include_dirs += [
"${efr32_sdk_root}/platform/driver/i2cspm/inc",
"${efr32_sdk_root}/app/bluetooth/common/sensor_rht",
"${efr32_sdk_root}/app/bluetooth/common/sensor_rht/config",
"${efr32_sdk_root}/hardware/driver/si70xx/inc",
"${efr32_sdk_root}/app/bluetooth/common/sensor_select",
"${efr32_sdk_root}/platform/common/config",
]

defines += [ "USE_TEMP_SENSOR" ]
}
}

efr32_executable("thermostat_app") {
Expand All @@ -186,6 +204,8 @@ efr32_executable("thermostat_app") {
"${examples_plat_dir}/init_efrPlatform.cpp",
"${examples_plat_dir}/matter_config.cpp",
"src/AppTask.cpp",
"src/SensorManager.cpp",
"src/TemperatureManager.cpp",
"src/ZclCallbacks.cpp",
"src/main.cpp",
]
Expand All @@ -194,6 +214,19 @@ efr32_executable("thermostat_app") {
sources += [ "${examples_plat_dir}/LEDWidget.cpp" ]
}

if (use_temp_sensor) {
sources += [
"${efr32_sdk_root}/app/bluetooth/common/sensor_rht/sl_sensor_rht.c",
"${efr32_sdk_root}/app/bluetooth/common/sensor_select/sl_sensor_select.c",
"${efr32_sdk_root}/hardware/driver/si70xx/src/sl_si70xx.c",
"${efr32_sdk_root}/platform/common/src/sl_status.c",
"${efr32_sdk_root}/platform/driver/i2cspm/src/sl_i2cspm.c",
"${efr32_sdk_root}/platform/emlib/src/em_i2c.c",
"${examples_plat_dir}/TemperatureSensor.cpp",
"${sdk_support_root}/matter/efr32/${silabs_family}/${silabs_board}/autogen/sl_i2cspm_init.c",
]
}

if (chip_enable_pw_rpc || chip_build_libshell || enable_openthread_cli ||
use_wf200 || use_rs911x) {
sources += [ "${examples_plat_dir}/uart.cpp" ]
Expand Down Expand Up @@ -268,6 +301,7 @@ efr32_executable("thermostat_app") {
sources += [
"${examples_plat_dir}/display/demo-ui.c",
"${examples_plat_dir}/display/lcd.cpp",
"src/ThermostatUI.cpp",
]
include_dirs += [ "${examples_plat_dir}/display" ]
defines += [
Expand Down
6 changes: 0 additions & 6 deletions examples/thermostat/efr32/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ struct AppEvent
{
kEventType_Button = 0,
kEventType_Timer,
kEventType_Light,
kEventType_Install,
};

Expand All @@ -44,11 +43,6 @@ struct AppEvent
{
void * Context;
} TimerEvent;
struct
{
uint8_t Action;
int32_t Actor;
} LightEvent;
};

EventHandler Handler;
Expand Down
17 changes: 11 additions & 6 deletions examples/thermostat/efr32/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@
#include <stdbool.h>
#include <stdint.h>

#ifdef DISPLAY_ENABLED
#include "ThermostatUI.h"
#endif

#include "AppEvent.h"
#include "BaseApplication.h"
#include "FreeRTOS.h"
#include "SensorManager.h"
#include "TemperatureManager.h"
#include "sl_simple_button_instances.h"
#include "timers.h" // provides FreeRTOS timer support
#include <app/clusters/identify-server/identify-server.h>
Expand Down Expand Up @@ -69,6 +75,11 @@ class AppTask : public BaseApplication

CHIP_ERROR StartAppTask();

/**
* @brief Request an update of the Thermostat LCD UI
*/
void UpdateThermoStatUI();

/**
* @brief Event handler when a button is pressed
* Function posts an event for button processing
Expand Down Expand Up @@ -112,11 +123,5 @@ class AppTask : public BaseApplication
*/
static void ButtonHandler(AppEvent * aEvent);

/**
* @brief PB1 Button event processing function
* Function triggers a thermostat action sent to the CHIP task
*
* @param aEvent button event being processed
*/
static void ThermostatActionEventHandler(AppEvent * aEvent);
};
10 changes: 9 additions & 1 deletion examples/thermostat/efr32/include/CHIPProjectConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
*
* 0x8005: example lighting app
*/
#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID 0x8004
#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID 0x800E

/**
* CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION
Expand Down Expand Up @@ -97,6 +97,14 @@
*/
#define CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE 1

/**
* CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC
*
* Enables synchronizing the device's real time clock with a remote Chip Time service
* using the Chip Time Sync protocol.
*/
#define CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC 0

/**
* CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER
*
Expand Down
48 changes: 48 additions & 0 deletions examples/thermostat/efr32/include/SensorManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*
* Copyright (c) 2019 Google LLC.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <stdbool.h>
#include <stdint.h>

#include "AppEvent.h"

#include "FreeRTOS.h"
#include "timers.h" // provides FreeRTOS timer support
#include <app-common/zap-generated/attributes/Accessors.h>
#include <lib/core/CHIPError.h>

class SensorManager
{
public:
CHIP_ERROR Init();

private:
friend SensorManager & SensorMgr();

// Reads new generated sensor value, stores it, and updates local temperature attribute
static void SensorTimerEventHandler(TimerHandle_t xTimer);

static SensorManager sSensorManager;
};

inline SensorManager & SensorMgr()
{
return SensorManager::sSensorManager;
}
Loading

0 comments on commit 9a41c9c

Please sign in to comment.