Skip to content

Commit 5224cec

Browse files
[Silabs] Refactor Sensor code (project-chip#35979)
* Refactor Sensor and LCD code * Address review comments
1 parent e02f0f2 commit 5224cec

File tree

13 files changed

+196
-141
lines changed

13 files changed

+196
-141
lines changed
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
*
3+
* Copyright (c) 2020 Project CHIP Authors
4+
* Copyright (c) 2019 Google LLC.
5+
* All rights reserved.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#include "sl_board_control.h"
21+
#include "sl_i2cspm_instances.h"
22+
#include "sl_si70xx.h"
23+
#include <Si70xxSensor.h>
24+
#include <lib/support/CodeUtils.h>
25+
26+
namespace {
27+
28+
constexpr uint16_t kSensorTemperatureOffset = 475;
29+
bool initialized = false;
30+
31+
} // namespace
32+
33+
namespace Si70xxSensor {
34+
35+
sl_status_t Init()
36+
{
37+
sl_status_t status = SL_STATUS_OK;
38+
39+
status = sl_board_enable_sensor(SL_BOARD_SENSOR_RHT);
40+
VerifyOrReturnError(status == SL_STATUS_OK, status);
41+
42+
status = sl_si70xx_init(sl_i2cspm_sensor, SI7021_ADDR);
43+
VerifyOrReturnError(status == SL_STATUS_OK, status);
44+
45+
initialized = true;
46+
return status;
47+
}
48+
49+
sl_status_t GetSensorData(uint16_t & relativeHumidity, int16_t & temperature)
50+
{
51+
VerifyOrReturnError(initialized, SL_STATUS_NOT_INITIALIZED);
52+
53+
sl_status_t status = SL_STATUS_OK;
54+
int32_t tempTemperature = 0;
55+
uint32_t tempHumidity = 0;
56+
57+
status = sl_si70xx_measure_rh_and_temp(sl_i2cspm_sensor, SI7021_ADDR, &tempHumidity, &tempTemperature);
58+
VerifyOrReturnError(status == SL_STATUS_OK, status);
59+
60+
// Sensor precision is milliX. We need to reduce to change the precision to centiX to fit with the cluster attributes presicion.
61+
temperature = static_cast<int16_t>(tempTemperature / 10) - kSensorTemperatureOffset;
62+
relativeHumidity = static_cast<uint16_t>(tempHumidity / 10);
63+
64+
return status;
65+
}
66+
67+
}; // namespace Si70xxSensor
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
*
3+
* Copyright (c) 2020 Project CHIP Authors
4+
* Copyright (c) 2019 Google LLC.
5+
* All rights reserved.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#pragma once
21+
22+
#include "sl_status.h"
23+
#include <stdint.h>
24+
25+
namespace Si70xxSensor {
26+
27+
/**
28+
* @brief Initialises the Si70xx Sensor.
29+
*
30+
* @return sl_status_t SL_STATUS_OK if there were no errors occured during initialisation.
31+
* Error if an underlying platform error occured
32+
*/
33+
sl_status_t Init();
34+
35+
/**
36+
* @brief Reads Humidity and temperature values from the Si70xx sensor.
37+
* The init function must be called before calling the GetSensorData.
38+
*
39+
* @param[out] relativeHumidity Relative humidity percentage in centi-pourcentage (1000 == 10.00%)
40+
* @param[out] temperature Ambiant temperature in centi-celsium (1000 == 10.00C)
41+
*
42+
* @return sl_status_t SL_STATUS_OK if there were no errors occured during initialisation.
43+
* SL_STATUS_NOT_INITIALIZED if the sensor was not initialised
44+
* Error if an underlying platform error occured
45+
*/
46+
sl_status_t GetSensorData(uint16_t & relativeHumidity, int16_t & temperature);
47+
48+
}; // namespace Si70xxSensor

examples/platform/silabs/SiWx917/BUILD.gn

+7
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ source_set("siwx917-common") {
252252
public_deps += [ ":test-event-trigger" ]
253253
}
254254

255+
if (sl_enable_si70xx_sensor) {
256+
sources += [
257+
"${silabs_common_plat_dir}/Si70xxSensor.cpp",
258+
"${silabs_common_plat_dir}/Si70xxSensor.h",
259+
]
260+
}
261+
255262
if (app_data_model != "") {
256263
public_deps += [ app_data_model ]
257264
}

examples/platform/silabs/TemperatureSensor.cpp

-62
This file was deleted.

examples/platform/silabs/TemperatureSensor.h

-28
This file was deleted.

examples/platform/silabs/display/demo-ui.c

-1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,5 @@ void demoUIClearMainScreen(uint8_t * name)
167167
{
168168
GLIB_clear(&glibContext);
169169
demoUIDisplayHeader((char *) name);
170-
demoUIDisplayApp(false);
171170
demoUIDisplayProtocols();
172171
}

examples/platform/silabs/display/lcd.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class SilabsLCD
6565
int DrawPixel(void * pContext, int32_t x, int32_t y);
6666
int Update(void);
6767
void WriteDemoUI(bool state);
68+
void WriteDemoUI();
6869
void SetCustomUI(customUICB cb);
6970

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

88-
void WriteDemoUI();
89-
9089
#ifdef QR_CODE_ENABLED
9190
void WriteQRCode();
9291
void LCDFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h);

examples/platform/silabs/efr32/BUILD.gn

+7
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,13 @@ source_set("efr32-common") {
308308
public_deps += [ ":test-event-trigger" ]
309309
}
310310

311+
if (sl_enable_si70xx_sensor) {
312+
sources += [
313+
"${silabs_common_plat_dir}/Si70xxSensor.cpp",
314+
"${silabs_common_plat_dir}/Si70xxSensor.h",
315+
]
316+
}
317+
311318
if (app_data_model != "") {
312319
public_deps += [ app_data_model ]
313320
}

examples/thermostat/silabs/BUILD.gn

-28
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ import("${examples_common_plat_dir}/args.gni")
4747
declare_args() {
4848
# Dump memory usage at link time.
4949
chip_print_memory_usage = false
50-
51-
# Enable the temperature sensor
52-
# Some boards do not have a temperature sensor
53-
use_temp_sensor = false
5450
}
5551

5652
if (wifi_soc) {
@@ -112,19 +108,6 @@ if (wifi_soc) {
112108
"PW_RPC_ENABLED",
113109
]
114110
}
115-
116-
if (use_temp_sensor) {
117-
include_dirs += [
118-
"${efr32_sdk_root}/platform/driver/i2cspm/inc",
119-
"${efr32_sdk_root}/app/bluetooth/common/sensor_rht",
120-
"${efr32_sdk_root}/app/bluetooth/common/sensor_rht/config",
121-
"${efr32_sdk_root}/hardware/driver/si70xx/inc",
122-
"${efr32_sdk_root}/app/bluetooth/common/sensor_select",
123-
"${efr32_sdk_root}/platform/common/config",
124-
]
125-
126-
defines += [ "USE_TEMP_SENSOR" ]
127-
}
128111
}
129112
}
130113

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

144-
if (use_temp_sensor) {
145-
sources += [
146-
"${efr32_sdk_root}/hardware/driver/si70xx/src/sl_si70xx.c",
147-
"${efr32_sdk_root}/platform/common/src/sl_status.c",
148-
"${efr32_sdk_root}/platform/driver/i2cspm/src/sl_i2cspm.c",
149-
"${efr32_sdk_root}/platform/emlib/src/em_i2c.c",
150-
"${examples_common_plat_dir}/TemperatureSensor.cpp",
151-
"${sdk_support_root}/matter/efr32/${silabs_family}/${silabs_board}/autogen/sl_i2cspm_init.c",
152-
]
153-
}
154-
155127
if (!disable_lcd) {
156128
sources += [ "src/ThermostatUI.cpp" ]
157129
}

0 commit comments

Comments
 (0)