|
| 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 |
0 commit comments