|
| 1 | +/* |
| 2 | + This file is part of the Arduino_MKRIoTCarrier 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 | + board_revision = getRevision; |
| 26 | +} |
| 27 | + |
| 28 | +AirQualityClass::~AirQualityClass() |
| 29 | +{ |
| 30 | +} |
| 31 | + |
| 32 | +int AirQualityClass::begin() |
| 33 | +{ |
| 34 | + _revision = board_revision(); |
| 35 | + if (_revision == BOARD_REVISION_2) { |
| 36 | + if (mkr_iot_carrier_rev2::iaqSensor == nullptr) { |
| 37 | + iaqSensor = new Bsec(); |
| 38 | + iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire); |
| 39 | + if (checkIaqSensorStatus() == STATUS_ERROR){ |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + bsec_virtual_sensor_t sensorList[10] = { |
| 44 | + BSEC_OUTPUT_RAW_TEMPERATURE, |
| 45 | + BSEC_OUTPUT_RAW_PRESSURE, |
| 46 | + BSEC_OUTPUT_RAW_HUMIDITY, |
| 47 | + BSEC_OUTPUT_RAW_GAS, |
| 48 | + BSEC_OUTPUT_IAQ, |
| 49 | + BSEC_OUTPUT_STATIC_IAQ, |
| 50 | + BSEC_OUTPUT_CO2_EQUIVALENT, |
| 51 | + BSEC_OUTPUT_BREATH_VOC_EQUIVALENT, |
| 52 | + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE, |
| 53 | + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, |
| 54 | + }; |
| 55 | + |
| 56 | + iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS); |
| 57 | + if (checkIaqSensorStatus() == STATUS_ERROR){ |
| 58 | + return 0; |
| 59 | + } |
| 60 | + mkr_iot_carrier_rev2::iaqSensor = iaqSensor; |
| 61 | + } else { |
| 62 | + iaqSensor = mkr_iot_carrier_rev2::iaqSensor; |
| 63 | + } |
| 64 | + return 1; |
| 65 | + } |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +int AirQualityClass::checkIaqSensorStatus(void) |
| 70 | +{ |
| 71 | + if (iaqSensor->status != BSEC_OK) { |
| 72 | + if (iaqSensor->status < BSEC_OK) { |
| 73 | + return STATUS_ERROR; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (iaqSensor->bme680Status != BME680_OK) { |
| 78 | + if (iaqSensor->bme680Status < BME680_OK) { |
| 79 | + return STATUS_ERROR; |
| 80 | + } |
| 81 | + } |
| 82 | + return STATUS_OK; |
| 83 | +} |
| 84 | + |
| 85 | +void AirQualityClass::end() |
| 86 | +{ |
| 87 | + if (_revision == BOARD_REVISION_2) { |
| 88 | + delete iaqSensor; |
| 89 | + iaqSensor = nullptr; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +float AirQualityClass::readVOC() |
| 94 | +{ |
| 95 | + if (_revision == BOARD_REVISION_2) { |
| 96 | + while(!iaqSensor->run()){ } |
| 97 | + float reading = iaqSensor->breathVocEquivalent; |
| 98 | + return reading; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +float AirQualityClass::readGasResistor() |
| 103 | +{ |
| 104 | + if (_revision == BOARD_REVISION_2) { |
| 105 | + while(!iaqSensor->run()){ } |
| 106 | + float reading = iaqSensor->gasResistance; |
| 107 | + return reading; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +float AirQualityClass::readIAQ() |
| 112 | +{ |
| 113 | + if (_revision == BOARD_REVISION_2) { |
| 114 | + while(!iaqSensor->run()){ } |
| 115 | + float reading = iaqSensor->iaq; |
| 116 | + return reading; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +float AirQualityClass::readIAQAccuracy() |
| 121 | +{ |
| 122 | + if (_revision == BOARD_REVISION_2) { |
| 123 | + while(!iaqSensor->run()){ } |
| 124 | + float reading = iaqSensor->iaqAccuracy; |
| 125 | + return reading; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +float AirQualityClass::readStaticIAQ() |
| 130 | +{ |
| 131 | + if (_revision == BOARD_REVISION_2) { |
| 132 | + while(!iaqSensor->run()){ } |
| 133 | + float reading = iaqSensor->staticIaq; |
| 134 | + return reading; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +float AirQualityClass::readCO2() |
| 140 | +{ |
| 141 | + if (_revision == BOARD_REVISION_2) { |
| 142 | + while(!iaqSensor->run()){ } |
| 143 | + float reading = iaqSensor->co2Equivalent; |
| 144 | + return reading; |
| 145 | + } |
| 146 | +} |
| 147 | + |
0 commit comments