-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAirQualitySensor.cpp
104 lines (88 loc) · 3.91 KB
/
AirQualitySensor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
*
* Copyright (c) 2024 Project CHIP Authors
* Copyright (c) 2024 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 "AirQualitySensor.h"
#include "sl_board_control.h"
#include <platform/CHIPDeviceLayer.h>
#ifdef USE_SPARKFUN_AIR_QUALITY_SENSOR
#include "sl_i2cspm_instances.h"
#include <sparkfun_sgp40.h>
#endif // USE_SPARKFUN_AIR_QUALITY_SENSOR
namespace {
bool initialized = false;
}
namespace AirQualitySensor {
/**
* @brief Initializes the air quality sensor.
*
* This function initializes the air quality sensor, specifically supporting the SparkFun SGP40 air quality sensor.
* It uses the I2C protocol for communication and sets up the VOC (Volatile Organic Compounds) algorithm for air quality
* measurement.
*
* @note If the macro USE_SPARKFUN_AIR_QUALITY_SENSOR is defined, the SparkFun SGP40 sensor is initialized.
* If initialization is successful, the function returns SL_STATUS_OK, indicating the sensor is ready for use.
* If the initialization fails, it returns SL_STATUS_FAIL. If USE_SPARKFUN_AIR_QUALITY_SENSOR is not defined,
* it is expected that the user provides an implementation for their specific air quality sensor.
*
* @return sl_status_t Returns SL_STATUS_OK if the initialization is successful, otherwise returns SL_STATUS_FAIL.
*/
sl_status_t Init()
{
sl_status_t status = SL_STATUS_FAIL;
#ifdef USE_SPARKFUN_AIR_QUALITY_SENSOR
status = sparkfun_sgp40_init(sl_i2cspm_qwiic);
VerifyOrReturnError(status == SL_STATUS_OK, SL_STATUS_FAIL);
initialized = true;
sparkfun_sgp40_voc_algorithm_init();
#else
// User implementation of Init
#endif // USE_SPARKFUN_AIR_QUALITY_SENSOR
return status;
}
/**
* @brief Retrieves the air quality measurement.
*
* This function fetches the current air quality measurement from the air quality sensor. It is designed to work
* with the SparkFun SGP40 air quality sensor when the USE_SPARKFUN_AIR_QUALITY_SENSOR macro is defined. The function
* calculates the VOC (Volatile Organic Compounds) index, which is used as a measure of air quality.
*
* @note The function requires the sensor to be initialized before calling. If the sensor is not initialized,
* SL_STATUS_NOT_INITIALIZED is returned. For the SparkFun SGP40 sensor, the function uses hardcoded
* values for humidity and temperature (50% and 25°C, respectively) as part of the VOC index calculation.
* It is recommended to replace these magic numbers with actual sensor readings.
*
* @param air_quality A pointer to an integer where the air quality measurement will be stored.
*
* @return sl_status_t Returns SL_STATUS_OK if the air quality measurement is successfully retrieved,
* SL_STATUS_NOT_INITIALIZED if the sensor has not been initialized, or other error codes as defined.
*/
sl_status_t GetAirQuality(int32_t & air_quality)
{
sl_status_t status = SL_STATUS_FAIL;
VerifyOrReturnError(initialized, SL_STATUS_NOT_INITIALIZED);
#ifdef USE_SPARKFUN_AIR_QUALITY_SENSOR
constexpr float relativeHumidity = 50; // 50%
constexpr float temperature = 25; // 25°C
status = sparkfun_sgp40_get_voc_index(&air_quality, relativeHumidity, temperature);
VerifyOrReturnError(status == SL_STATUS_OK, status);
#else
// User implementation of GetAirQuality
#endif // USE_SPARKFUN_AIR_QUALITY_SENSOR
return status;
}
}; // namespace AirQualitySensor