Skip to content

Commit

Permalink
[Silabs] Refactor Sensor code (project-chip#35979)
Browse files Browse the repository at this point in the history
* Refactor Sensor and LCD code

* Address review comments
  • Loading branch information
mkardous-silabs authored Oct 9, 2024
1 parent e02f0f2 commit 5224cec
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 141 deletions.
67 changes: 67 additions & 0 deletions examples/platform/silabs/Si70xxSensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
*
* 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 "sl_board_control.h"
#include "sl_i2cspm_instances.h"
#include "sl_si70xx.h"
#include <Si70xxSensor.h>
#include <lib/support/CodeUtils.h>

namespace {

constexpr uint16_t kSensorTemperatureOffset = 475;
bool initialized = false;

} // namespace

namespace Si70xxSensor {

sl_status_t Init()
{
sl_status_t status = SL_STATUS_OK;

status = sl_board_enable_sensor(SL_BOARD_SENSOR_RHT);
VerifyOrReturnError(status == SL_STATUS_OK, status);

status = sl_si70xx_init(sl_i2cspm_sensor, SI7021_ADDR);
VerifyOrReturnError(status == SL_STATUS_OK, status);

initialized = true;
return status;
}

sl_status_t GetSensorData(uint16_t & relativeHumidity, int16_t & temperature)
{
VerifyOrReturnError(initialized, SL_STATUS_NOT_INITIALIZED);

sl_status_t status = SL_STATUS_OK;
int32_t tempTemperature = 0;
uint32_t tempHumidity = 0;

status = sl_si70xx_measure_rh_and_temp(sl_i2cspm_sensor, SI7021_ADDR, &tempHumidity, &tempTemperature);
VerifyOrReturnError(status == SL_STATUS_OK, status);

// Sensor precision is milliX. We need to reduce to change the precision to centiX to fit with the cluster attributes presicion.
temperature = static_cast<int16_t>(tempTemperature / 10) - kSensorTemperatureOffset;
relativeHumidity = static_cast<uint16_t>(tempHumidity / 10);

return status;
}

}; // namespace Si70xxSensor
48 changes: 48 additions & 0 deletions examples/platform/silabs/Si70xxSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*
* 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 Si70xxSensor {

/**
* @brief Initialises the Si70xx Sensor.
*
* @return sl_status_t SL_STATUS_OK if there were no errors occured during initialisation.
* Error if an underlying platform error occured
*/
sl_status_t Init();

/**
* @brief Reads Humidity and temperature values from the Si70xx sensor.
* The init function must be called before calling the GetSensorData.
*
* @param[out] relativeHumidity Relative humidity percentage in centi-pourcentage (1000 == 10.00%)
* @param[out] temperature Ambiant temperature in centi-celsium (1000 == 10.00C)
*
* @return sl_status_t SL_STATUS_OK if there were no errors occured during initialisation.
* SL_STATUS_NOT_INITIALIZED if the sensor was not initialised
* Error if an underlying platform error occured
*/
sl_status_t GetSensorData(uint16_t & relativeHumidity, int16_t & temperature);

}; // namespace Si70xxSensor
7 changes: 7 additions & 0 deletions examples/platform/silabs/SiWx917/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ source_set("siwx917-common") {
public_deps += [ ":test-event-trigger" ]
}

if (sl_enable_si70xx_sensor) {
sources += [
"${silabs_common_plat_dir}/Si70xxSensor.cpp",
"${silabs_common_plat_dir}/Si70xxSensor.h",
]
}

if (app_data_model != "") {
public_deps += [ app_data_model ]
}
Expand Down
62 changes: 0 additions & 62 deletions examples/platform/silabs/TemperatureSensor.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions examples/platform/silabs/TemperatureSensor.h

This file was deleted.

1 change: 0 additions & 1 deletion examples/platform/silabs/display/demo-ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,5 @@ void demoUIClearMainScreen(uint8_t * name)
{
GLIB_clear(&glibContext);
demoUIDisplayHeader((char *) name);
demoUIDisplayApp(false);
demoUIDisplayProtocols();
}
3 changes: 1 addition & 2 deletions examples/platform/silabs/display/lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class SilabsLCD
int DrawPixel(void * pContext, int32_t x, int32_t y);
int Update(void);
void WriteDemoUI(bool state);
void WriteDemoUI();
void SetCustomUI(customUICB cb);

void GetScreen(Screen_e & screen);
Expand All @@ -85,8 +86,6 @@ class SilabsLCD
bool protocol1 = false; /* data */
} DemoState_t;

void WriteDemoUI();

#ifdef QR_CODE_ENABLED
void WriteQRCode();
void LCDFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h);
Expand Down
7 changes: 7 additions & 0 deletions examples/platform/silabs/efr32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ source_set("efr32-common") {
public_deps += [ ":test-event-trigger" ]
}

if (sl_enable_si70xx_sensor) {
sources += [
"${silabs_common_plat_dir}/Si70xxSensor.cpp",
"${silabs_common_plat_dir}/Si70xxSensor.h",
]
}

if (app_data_model != "") {
public_deps += [ app_data_model ]
}
Expand Down
28 changes: 0 additions & 28 deletions examples/thermostat/silabs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ import("${examples_common_plat_dir}/args.gni")
declare_args() {
# Dump memory usage at link time.
chip_print_memory_usage = false

# Enable the temperature sensor
# Some boards do not have a temperature sensor
use_temp_sensor = false
}

if (wifi_soc) {
Expand Down Expand Up @@ -112,19 +108,6 @@ if (wifi_soc) {
"PW_RPC_ENABLED",
]
}

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" ]
}
}
}

Expand All @@ -141,17 +124,6 @@ silabs_executable("thermostat_app") {
"src/ZclCallbacks.cpp",
]

if (use_temp_sensor) {
sources += [
"${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_common_plat_dir}/TemperatureSensor.cpp",
"${sdk_support_root}/matter/efr32/${silabs_family}/${silabs_board}/autogen/sl_i2cspm_init.c",
]
}

if (!disable_lcd) {
sources += [ "src/ThermostatUI.cpp" ]
}
Expand Down
Loading

0 comments on commit 5224cec

Please sign in to comment.