|
| 1 | +/* |
| 2 | + This file is part of the Arduino_LSM6DSOX library. |
| 3 | + Copyright (c) 2021 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include "AirQualityClass.h" |
| 21 | + |
| 22 | +// sets function called on slave write |
| 23 | +AirQualityClass::AirQualityClass( getRev_t getRevision) |
| 24 | +{ |
| 25 | + //If board_revision = 1, IMU module is LSM6DSOX, otherwise is LSM6DS3 |
| 26 | + board_revision = getRevision; |
| 27 | +} |
| 28 | + |
| 29 | +AirQualityClass::~AirQualityClass() |
| 30 | +{ |
| 31 | +} |
| 32 | + |
| 33 | +int AirQualityClass::begin() |
| 34 | +{ |
| 35 | + _revision = board_revision(); |
| 36 | + if (_revision == BOARD_REVISION_2) { |
| 37 | + if (mkr_iot_carrier_rev2::iaqSensor == nullptr) { |
| 38 | + iaqSensor = new Bsec(); |
| 39 | + iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire); |
| 40 | + if (checkIaqSensorStatus() == STATUS_ERROR){ |
| 41 | + return 0; |
| 42 | + } |
| 43 | + |
| 44 | + bsec_virtual_sensor_t sensorList[10] = { |
| 45 | + BSEC_OUTPUT_RAW_TEMPERATURE, |
| 46 | + BSEC_OUTPUT_RAW_PRESSURE, |
| 47 | + BSEC_OUTPUT_RAW_HUMIDITY, |
| 48 | + BSEC_OUTPUT_RAW_GAS, |
| 49 | + BSEC_OUTPUT_IAQ, |
| 50 | + BSEC_OUTPUT_STATIC_IAQ, |
| 51 | + BSEC_OUTPUT_CO2_EQUIVALENT, |
| 52 | + BSEC_OUTPUT_BREATH_VOC_EQUIVALENT, |
| 53 | + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE, |
| 54 | + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, |
| 55 | + }; |
| 56 | + |
| 57 | + iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS); |
| 58 | + if (checkIaqSensorStatus() == STATUS_ERROR){ |
| 59 | + return 0; |
| 60 | + } |
| 61 | + mkr_iot_carrier_rev2::iaqSensor = iaqSensor; |
| 62 | + } else { |
| 63 | + iaqSensor = mkr_iot_carrier_rev2::iaqSensor; |
| 64 | + } |
| 65 | + return 1; |
| 66 | + } |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +int AirQualityClass::checkIaqSensorStatus(void) |
| 71 | +{ |
| 72 | + if (iaqSensor->status != BSEC_OK) { |
| 73 | + if (iaqSensor->status < BSEC_OK) { |
| 74 | + return STATUS_ERROR; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (iaqSensor->bme680Status != BME680_OK) { |
| 79 | + if (iaqSensor->bme680Status < BME680_OK) { |
| 80 | + return STATUS_ERROR; |
| 81 | + } |
| 82 | + } |
| 83 | + return STATUS_OK; |
| 84 | +} |
| 85 | + |
| 86 | +void AirQualityClass::end() |
| 87 | +{ |
| 88 | + if (_revision == BOARD_REVISION_2) { |
| 89 | + delete iaqSensor; |
| 90 | + iaqSensor = nullptr; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +float AirQualityClass::readVOC() |
| 95 | +{ |
| 96 | + if (_revision == BOARD_REVISION_2) { |
| 97 | + while(!iaqSensor->run()){ } |
| 98 | + float reading = iaqSensor->breathVocEquivalent; |
| 99 | + return reading; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +float AirQualityClass::readGasResistor() |
| 104 | +{ |
| 105 | + if (_revision == BOARD_REVISION_2) { |
| 106 | + while(!iaqSensor->run()){ } |
| 107 | + float reading = iaqSensor->gasResistance; |
| 108 | + return reading; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +float AirQualityClass::readIAQ() |
| 113 | +{ |
| 114 | + if (_revision == BOARD_REVISION_2) { |
| 115 | + while(!iaqSensor->run()){ } |
| 116 | + float reading = iaqSensor->iaq; |
| 117 | + return reading; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +float AirQualityClass::readIAQAccuracy() |
| 122 | +{ |
| 123 | + if (_revision == BOARD_REVISION_2) { |
| 124 | + while(!iaqSensor->run()){ } |
| 125 | + float reading = iaqSensor->iaqAccuracy; |
| 126 | + return reading; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +float AirQualityClass::readStaticIAQ() |
| 131 | +{ |
| 132 | + if (_revision == BOARD_REVISION_2) { |
| 133 | + while(!iaqSensor->run()){ } |
| 134 | + float reading = iaqSensor->staticIaq; |
| 135 | + return reading; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | + |
| 140 | +float AirQualityClass::readCO2() |
| 141 | +{ |
| 142 | + if (_revision == BOARD_REVISION_2) { |
| 143 | + while(!iaqSensor->run()){ } |
| 144 | + float reading = iaqSensor->co2Equivalent; |
| 145 | + return reading; |
| 146 | + } |
| 147 | +} |
| 148 | + |
0 commit comments