Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ECE4180FinalProjectFall22
ens160_i2c.cpp@3:63ff52373e71, 2022-12-06 (annotated)
- Committer:
- krishnamvs
- Date:
- Tue Dec 06 21:21:28 2022 +0000
- Revision:
- 3:63ff52373e71
- Child:
- 4:cb50c2f7e2b2
V1
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| krishnamvs | 3:63ff52373e71 | 1 | #include "ens160_i2c.h" |
| krishnamvs | 3:63ff52373e71 | 2 | |
| krishnamvs | 3:63ff52373e71 | 3 | ENS160::ENS160(PinName sda, PinName scl, uint8_t i2c_device_address) |
| krishnamvs | 3:63ff52373e71 | 4 | :i2c(sda, scl) |
| krishnamvs | 3:63ff52373e71 | 5 | { |
| krishnamvs | 3:63ff52373e71 | 6 | this->i2c_address = i2c_device_address; |
| krishnamvs | 3:63ff52373e71 | 7 | } |
| krishnamvs | 3:63ff52373e71 | 8 | |
| krishnamvs | 3:63ff52373e71 | 9 | bool ENS160::ping(uint8_t address) |
| krishnamvs | 3:63ff52373e71 | 10 | { |
| krishnamvs | 3:63ff52373e71 | 11 | int ret; |
| krishnamvs | 3:63ff52373e71 | 12 | char rxdata; |
| krishnamvs | 3:63ff52373e71 | 13 | ret = i2c.read(this->i2c_address, &rxdata, 1); |
| krishnamvs | 3:63ff52373e71 | 14 | if (ret) |
| krishnamvs | 3:63ff52373e71 | 15 | return true; |
| krishnamvs | 3:63ff52373e71 | 16 | return false; |
| krishnamvs | 3:63ff52373e71 | 17 | } |
| krishnamvs | 3:63ff52373e71 | 18 | |
| krishnamvs | 3:63ff52373e71 | 19 | uint8_t ENS160::readRegisterRegion(uint8_t reg, uint8_t *data, uint8_t length) |
| krishnamvs | 3:63ff52373e71 | 20 | { |
| krishnamvs | 3:63ff52373e71 | 21 | int i; |
| krishnamvs | 3:63ff52373e71 | 22 | char temp_dest[length]; |
| krishnamvs | 3:63ff52373e71 | 23 | char temp[1] = {reg}; |
| krishnamvs | 3:63ff52373e71 | 24 | i2c.write(this->i2c_address, temp, 1); |
| krishnamvs | 3:63ff52373e71 | 25 | i2c.read(this->i2c_address, temp_dest, length); |
| krishnamvs | 3:63ff52373e71 | 26 | for (i=0; i < length; i++) |
| krishnamvs | 3:63ff52373e71 | 27 | { |
| krishnamvs | 3:63ff52373e71 | 28 | data[i] = temp_dest[i]; |
| krishnamvs | 3:63ff52373e71 | 29 | } |
| krishnamvs | 3:63ff52373e71 | 30 | return length; |
| krishnamvs | 3:63ff52373e71 | 31 | } |
| krishnamvs | 3:63ff52373e71 | 32 | |
| krishnamvs | 3:63ff52373e71 | 33 | int32_t ENS160::writeRegisterRegion(uint8_t *data, uint8_t length) |
| krishnamvs | 3:63ff52373e71 | 34 | { |
| krishnamvs | 3:63ff52373e71 | 35 | char temp_dest[length]; |
| krishnamvs | 3:63ff52373e71 | 36 | for (int i = 0; i < length; i++) |
| krishnamvs | 3:63ff52373e71 | 37 | { |
| krishnamvs | 3:63ff52373e71 | 38 | temp_dest[i] = *(data + i); |
| krishnamvs | 3:63ff52373e71 | 39 | } |
| krishnamvs | 3:63ff52373e71 | 40 | return i2c.write(this->i2c_address, temp_dest, length); |
| krishnamvs | 3:63ff52373e71 | 41 | } |
| krishnamvs | 3:63ff52373e71 | 42 | |
| krishnamvs | 3:63ff52373e71 | 43 | int32_t ENS160::writeRegisterRegion(uint8_t reg, uint8_t data) |
| krishnamvs | 3:63ff52373e71 | 44 | { |
| krishnamvs | 3:63ff52373e71 | 45 | char temp_data[2] = {reg, data}; |
| krishnamvs | 3:63ff52373e71 | 46 | return i2c.write(this->i2c_address, temp_data, 2); |
| krishnamvs | 3:63ff52373e71 | 47 | } |
| krishnamvs | 3:63ff52373e71 | 48 | |
| krishnamvs | 3:63ff52373e71 | 49 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 50 | // getUniqueID() |
| krishnamvs | 3:63ff52373e71 | 51 | // Gets the device's unique ID |
| krishnamvs | 3:63ff52373e71 | 52 | uint16_t ENS160::getUniqueID() |
| krishnamvs | 3:63ff52373e71 | 53 | { |
| krishnamvs | 3:63ff52373e71 | 54 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 55 | uint8_t tempVal[2] = {0}; |
| krishnamvs | 3:63ff52373e71 | 56 | uint16_t id; |
| krishnamvs | 3:63ff52373e71 | 57 | |
| krishnamvs | 3:63ff52373e71 | 58 | retVal = readRegisterRegion(SFE_ENS160_PART_ID, tempVal, 2); |
| krishnamvs | 3:63ff52373e71 | 59 | |
| krishnamvs | 3:63ff52373e71 | 60 | id = tempVal[0]; |
| krishnamvs | 3:63ff52373e71 | 61 | id |= tempVal[1] << 8; |
| krishnamvs | 3:63ff52373e71 | 62 | |
| krishnamvs | 3:63ff52373e71 | 63 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 64 | return 0; |
| krishnamvs | 3:63ff52373e71 | 65 | |
| krishnamvs | 3:63ff52373e71 | 66 | return id; |
| krishnamvs | 3:63ff52373e71 | 67 | } |
| krishnamvs | 3:63ff52373e71 | 68 | |
| krishnamvs | 3:63ff52373e71 | 69 | /////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 70 | // isConnected() |
| krishnamvs | 3:63ff52373e71 | 71 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 72 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 73 | // retVal true if device is connected, false if not connected |
| krishnamvs | 3:63ff52373e71 | 74 | |
| krishnamvs | 3:63ff52373e71 | 75 | bool ENS160::isConnected() |
| krishnamvs | 3:63ff52373e71 | 76 | { |
| krishnamvs | 3:63ff52373e71 | 77 | uint16_t uniqueID; |
| krishnamvs | 3:63ff52373e71 | 78 | uniqueID = getUniqueID(); |
| krishnamvs | 3:63ff52373e71 | 79 | if( uniqueID != ENS160_DEVICE_ID ) |
| krishnamvs | 3:63ff52373e71 | 80 | return false; |
| krishnamvs | 3:63ff52373e71 | 81 | return true; |
| krishnamvs | 3:63ff52373e71 | 82 | } |
| krishnamvs | 3:63ff52373e71 | 83 | |
| krishnamvs | 3:63ff52373e71 | 84 | bool ENS160::init() |
| krishnamvs | 3:63ff52373e71 | 85 | { |
| krishnamvs | 3:63ff52373e71 | 86 | if(!this->ping(this->i2c_address)) |
| krishnamvs | 3:63ff52373e71 | 87 | return false; |
| krishnamvs | 3:63ff52373e71 | 88 | return this->isConnected(); |
| krishnamvs | 3:63ff52373e71 | 89 | } |
| krishnamvs | 3:63ff52373e71 | 90 | |
| krishnamvs | 3:63ff52373e71 | 91 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 92 | // setOperatingMode() |
| krishnamvs | 3:63ff52373e71 | 93 | // Sets the operating mode: Deep Sleep (0x00), Idle (0x01), Standard (0x02), Reset (0xF0) |
| krishnamvs | 3:63ff52373e71 | 94 | // |
| krishnamvs | 3:63ff52373e71 | 95 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 96 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 97 | // val The desired operating mode to set. |
| krishnamvs | 3:63ff52373e71 | 98 | bool ENS160::setOperatingMode(uint8_t val) |
| krishnamvs | 3:63ff52373e71 | 99 | { |
| krishnamvs | 3:63ff52373e71 | 100 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 101 | |
| krishnamvs | 3:63ff52373e71 | 102 | if( val > SFE_ENS160_RESET ) |
| krishnamvs | 3:63ff52373e71 | 103 | return false; |
| krishnamvs | 3:63ff52373e71 | 104 | |
| krishnamvs | 3:63ff52373e71 | 105 | retVal = this->writeRegisterRegion(SFE_ENS160_OP_MODE, val); |
| krishnamvs | 3:63ff52373e71 | 106 | |
| krishnamvs | 3:63ff52373e71 | 107 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 108 | return false; |
| krishnamvs | 3:63ff52373e71 | 109 | |
| krishnamvs | 3:63ff52373e71 | 110 | return true; |
| krishnamvs | 3:63ff52373e71 | 111 | } |
| krishnamvs | 3:63ff52373e71 | 112 | |
| krishnamvs | 3:63ff52373e71 | 113 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 114 | // getOperatingMode() |
| krishnamvs | 3:63ff52373e71 | 115 | // |
| krishnamvs | 3:63ff52373e71 | 116 | // Gets the current operating mode: Deep Sleep (0x00), Idle (0x01), Standard (0x02), Reset (0xF0) |
| krishnamvs | 3:63ff52373e71 | 117 | |
| krishnamvs | 3:63ff52373e71 | 118 | int8_t ENS160::getOperatingMode() |
| krishnamvs | 3:63ff52373e71 | 119 | { |
| krishnamvs | 3:63ff52373e71 | 120 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 121 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 122 | |
| krishnamvs | 3:63ff52373e71 | 123 | retVal = readRegisterRegion(SFE_ENS160_OP_MODE, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 124 | |
| krishnamvs | 3:63ff52373e71 | 125 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 126 | return -1; |
| krishnamvs | 3:63ff52373e71 | 127 | |
| krishnamvs | 3:63ff52373e71 | 128 | return tempVal; |
| krishnamvs | 3:63ff52373e71 | 129 | } |
| krishnamvs | 3:63ff52373e71 | 130 | |
| krishnamvs | 3:63ff52373e71 | 131 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 132 | // configureInterrupt() |
| krishnamvs | 3:63ff52373e71 | 133 | // |
| krishnamvs | 3:63ff52373e71 | 134 | // Changes all of the settings within the interrupt configuration register. |
| krishnamvs | 3:63ff52373e71 | 135 | // |
| krishnamvs | 3:63ff52373e71 | 136 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 137 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 138 | // val The desired configuration settings. |
| krishnamvs | 3:63ff52373e71 | 139 | |
| krishnamvs | 3:63ff52373e71 | 140 | bool ENS160::configureInterrupt(uint8_t val) |
| krishnamvs | 3:63ff52373e71 | 141 | { |
| krishnamvs | 3:63ff52373e71 | 142 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 143 | |
| krishnamvs | 3:63ff52373e71 | 144 | retVal = writeRegisterRegion(SFE_ENS160_CONFIG, val); |
| krishnamvs | 3:63ff52373e71 | 145 | |
| krishnamvs | 3:63ff52373e71 | 146 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 147 | return false; |
| krishnamvs | 3:63ff52373e71 | 148 | |
| krishnamvs | 3:63ff52373e71 | 149 | return true; |
| krishnamvs | 3:63ff52373e71 | 150 | } |
| krishnamvs | 3:63ff52373e71 | 151 | |
| krishnamvs | 3:63ff52373e71 | 152 | |
| krishnamvs | 3:63ff52373e71 | 153 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 154 | // setInterrupt() |
| krishnamvs | 3:63ff52373e71 | 155 | // |
| krishnamvs | 3:63ff52373e71 | 156 | // Enables the interrupt. |
| krishnamvs | 3:63ff52373e71 | 157 | // |
| krishnamvs | 3:63ff52373e71 | 158 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 159 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 160 | // enable Turns on or off the interrupt. |
| krishnamvs | 3:63ff52373e71 | 161 | |
| krishnamvs | 3:63ff52373e71 | 162 | bool ENS160::enableInterrupt(bool enable) |
| krishnamvs | 3:63ff52373e71 | 163 | { |
| krishnamvs | 3:63ff52373e71 | 164 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 165 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 166 | |
| krishnamvs | 3:63ff52373e71 | 167 | retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 168 | |
| krishnamvs | 3:63ff52373e71 | 169 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 170 | return false; |
| krishnamvs | 3:63ff52373e71 | 171 | |
| krishnamvs | 3:63ff52373e71 | 172 | tempVal = (tempVal | (uint8_t)enable); |
| krishnamvs | 3:63ff52373e71 | 173 | |
| krishnamvs | 3:63ff52373e71 | 174 | retVal = writeRegisterRegion(SFE_ENS160_CONFIG, tempVal); |
| krishnamvs | 3:63ff52373e71 | 175 | |
| krishnamvs | 3:63ff52373e71 | 176 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 177 | return false; |
| krishnamvs | 3:63ff52373e71 | 178 | |
| krishnamvs | 3:63ff52373e71 | 179 | return true; |
| krishnamvs | 3:63ff52373e71 | 180 | } |
| krishnamvs | 3:63ff52373e71 | 181 | |
| krishnamvs | 3:63ff52373e71 | 182 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 183 | // setInterruptPolarity() |
| krishnamvs | 3:63ff52373e71 | 184 | // |
| krishnamvs | 3:63ff52373e71 | 185 | // Changes the polarity of the interrupt: active high or active low. By default |
| krishnamvs | 3:63ff52373e71 | 186 | // this value is set to zero or active low. |
| krishnamvs | 3:63ff52373e71 | 187 | // |
| krishnamvs | 3:63ff52373e71 | 188 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 189 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 190 | // activeHigh Changes active state of interrupt from high to low. |
| krishnamvs | 3:63ff52373e71 | 191 | |
| krishnamvs | 3:63ff52373e71 | 192 | bool ENS160::setInterruptPolarity(bool activeHigh) |
| krishnamvs | 3:63ff52373e71 | 193 | { |
| krishnamvs | 3:63ff52373e71 | 194 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 195 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 196 | |
| krishnamvs | 3:63ff52373e71 | 197 | retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 198 | |
| krishnamvs | 3:63ff52373e71 | 199 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 200 | return false; |
| krishnamvs | 3:63ff52373e71 | 201 | |
| krishnamvs | 3:63ff52373e71 | 202 | tempVal = (tempVal | (activeHigh << 6)); |
| krishnamvs | 3:63ff52373e71 | 203 | |
| krishnamvs | 3:63ff52373e71 | 204 | retVal = writeRegisterRegion(SFE_ENS160_CONFIG, tempVal); |
| krishnamvs | 3:63ff52373e71 | 205 | |
| krishnamvs | 3:63ff52373e71 | 206 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 207 | return false; |
| krishnamvs | 3:63ff52373e71 | 208 | |
| krishnamvs | 3:63ff52373e71 | 209 | return true; |
| krishnamvs | 3:63ff52373e71 | 210 | } |
| krishnamvs | 3:63ff52373e71 | 211 | |
| krishnamvs | 3:63ff52373e71 | 212 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 213 | // getInterruptPolarity() |
| krishnamvs | 3:63ff52373e71 | 214 | // |
| krishnamvs | 3:63ff52373e71 | 215 | // Retrieves the polarity of the physical interrupt. |
| krishnamvs | 3:63ff52373e71 | 216 | |
| krishnamvs | 3:63ff52373e71 | 217 | int8_t ENS160::getInterruptPolarity() |
| krishnamvs | 3:63ff52373e71 | 218 | { |
| krishnamvs | 3:63ff52373e71 | 219 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 220 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 221 | |
| krishnamvs | 3:63ff52373e71 | 222 | retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 223 | |
| krishnamvs | 3:63ff52373e71 | 224 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 225 | return -1; |
| krishnamvs | 3:63ff52373e71 | 226 | |
| krishnamvs | 3:63ff52373e71 | 227 | tempVal &= 0x40; |
| krishnamvs | 3:63ff52373e71 | 228 | |
| krishnamvs | 3:63ff52373e71 | 229 | return (tempVal >> 6); |
| krishnamvs | 3:63ff52373e71 | 230 | } |
| krishnamvs | 3:63ff52373e71 | 231 | |
| krishnamvs | 3:63ff52373e71 | 232 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 233 | // setInterruptDrive() |
| krishnamvs | 3:63ff52373e71 | 234 | // |
| krishnamvs | 3:63ff52373e71 | 235 | // Changes the pin drive of the interrupt: open drain (default) to push/pull |
| krishnamvs | 3:63ff52373e71 | 236 | // |
| krishnamvs | 3:63ff52373e71 | 237 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 238 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 239 | // pushPull Changes the drive of the pin. |
| krishnamvs | 3:63ff52373e71 | 240 | |
| krishnamvs | 3:63ff52373e71 | 241 | bool ENS160::setInterruptDrive(bool pushPull) |
| krishnamvs | 3:63ff52373e71 | 242 | { |
| krishnamvs | 3:63ff52373e71 | 243 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 244 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 245 | |
| krishnamvs | 3:63ff52373e71 | 246 | retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 247 | |
| krishnamvs | 3:63ff52373e71 | 248 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 249 | return false; |
| krishnamvs | 3:63ff52373e71 | 250 | |
| krishnamvs | 3:63ff52373e71 | 251 | tempVal = (tempVal | (uint8_t)(pushPull << 5)); |
| krishnamvs | 3:63ff52373e71 | 252 | |
| krishnamvs | 3:63ff52373e71 | 253 | retVal = writeRegisterRegion(SFE_ENS160_CONFIG, tempVal); |
| krishnamvs | 3:63ff52373e71 | 254 | |
| krishnamvs | 3:63ff52373e71 | 255 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 256 | return false; |
| krishnamvs | 3:63ff52373e71 | 257 | |
| krishnamvs | 3:63ff52373e71 | 258 | return true; |
| krishnamvs | 3:63ff52373e71 | 259 | } |
| krishnamvs | 3:63ff52373e71 | 260 | |
| krishnamvs | 3:63ff52373e71 | 261 | |
| krishnamvs | 3:63ff52373e71 | 262 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 263 | // setDataInterrupt() |
| krishnamvs | 3:63ff52373e71 | 264 | // |
| krishnamvs | 3:63ff52373e71 | 265 | // Routes the data ready signal to the interrupt pin. |
| krishnamvs | 3:63ff52373e71 | 266 | // |
| krishnamvs | 3:63ff52373e71 | 267 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 268 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 269 | // enable Self-explanatory: enables or disables data ready on interrupt. |
| krishnamvs | 3:63ff52373e71 | 270 | |
| krishnamvs | 3:63ff52373e71 | 271 | bool ENS160::setDataInterrupt(bool enable) |
| krishnamvs | 3:63ff52373e71 | 272 | { |
| krishnamvs | 3:63ff52373e71 | 273 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 274 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 275 | |
| krishnamvs | 3:63ff52373e71 | 276 | retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 277 | |
| krishnamvs | 3:63ff52373e71 | 278 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 279 | return false; |
| krishnamvs | 3:63ff52373e71 | 280 | |
| krishnamvs | 3:63ff52373e71 | 281 | tempVal = (tempVal | (uint8_t)(enable << 1)); |
| krishnamvs | 3:63ff52373e71 | 282 | |
| krishnamvs | 3:63ff52373e71 | 283 | retVal = writeRegisterRegion(SFE_ENS160_CONFIG, tempVal); |
| krishnamvs | 3:63ff52373e71 | 284 | |
| krishnamvs | 3:63ff52373e71 | 285 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 286 | return false; |
| krishnamvs | 3:63ff52373e71 | 287 | |
| krishnamvs | 3:63ff52373e71 | 288 | return true; |
| krishnamvs | 3:63ff52373e71 | 289 | } |
| krishnamvs | 3:63ff52373e71 | 290 | |
| krishnamvs | 3:63ff52373e71 | 291 | |
| krishnamvs | 3:63ff52373e71 | 292 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 293 | // setGPRInterrupt() |
| krishnamvs | 3:63ff52373e71 | 294 | // |
| krishnamvs | 3:63ff52373e71 | 295 | // Routes the general purporse read register signal to the interrupt pin. |
| krishnamvs | 3:63ff52373e71 | 296 | // |
| krishnamvs | 3:63ff52373e71 | 297 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 298 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 299 | // enable Self-explanatory: enables or disables general purpos read interrupt. |
| krishnamvs | 3:63ff52373e71 | 300 | |
| krishnamvs | 3:63ff52373e71 | 301 | bool ENS160::setGPRInterrupt(bool enable) |
| krishnamvs | 3:63ff52373e71 | 302 | { |
| krishnamvs | 3:63ff52373e71 | 303 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 304 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 305 | |
| krishnamvs | 3:63ff52373e71 | 306 | retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 307 | |
| krishnamvs | 3:63ff52373e71 | 308 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 309 | return false; |
| krishnamvs | 3:63ff52373e71 | 310 | |
| krishnamvs | 3:63ff52373e71 | 311 | tempVal = (tempVal | (uint8_t)(enable << 3)); |
| krishnamvs | 3:63ff52373e71 | 312 | |
| krishnamvs | 3:63ff52373e71 | 313 | retVal = writeRegisterRegion(SFE_ENS160_CONFIG, tempVal); |
| krishnamvs | 3:63ff52373e71 | 314 | |
| krishnamvs | 3:63ff52373e71 | 315 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 316 | return false; |
| krishnamvs | 3:63ff52373e71 | 317 | |
| krishnamvs | 3:63ff52373e71 | 318 | return true; |
| krishnamvs | 3:63ff52373e71 | 319 | } |
| krishnamvs | 3:63ff52373e71 | 320 | |
| krishnamvs | 3:63ff52373e71 | 321 | |
| krishnamvs | 3:63ff52373e71 | 322 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 323 | // getAppVer() |
| krishnamvs | 3:63ff52373e71 | 324 | // |
| krishnamvs | 3:63ff52373e71 | 325 | // Retrieves the 24 bit application version of the device. |
| krishnamvs | 3:63ff52373e71 | 326 | |
| krishnamvs | 3:63ff52373e71 | 327 | uint32_t ENS160::getAppVer() |
| krishnamvs | 3:63ff52373e71 | 328 | { |
| krishnamvs | 3:63ff52373e71 | 329 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 330 | uint8_t tempVal[3] = {0}; |
| krishnamvs | 3:63ff52373e71 | 331 | uint32_t version; |
| krishnamvs | 3:63ff52373e71 | 332 | |
| krishnamvs | 3:63ff52373e71 | 333 | retVal = readRegisterRegion(SFE_ENS160_GPR_READ4, tempVal, 3); |
| krishnamvs | 3:63ff52373e71 | 334 | |
| krishnamvs | 3:63ff52373e71 | 335 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 336 | return 0; |
| krishnamvs | 3:63ff52373e71 | 337 | |
| krishnamvs | 3:63ff52373e71 | 338 | version = tempVal[0]; |
| krishnamvs | 3:63ff52373e71 | 339 | version |= tempVal[1] << 8; |
| krishnamvs | 3:63ff52373e71 | 340 | version |= tempVal[2] << 16; |
| krishnamvs | 3:63ff52373e71 | 341 | |
| krishnamvs | 3:63ff52373e71 | 342 | return version; |
| krishnamvs | 3:63ff52373e71 | 343 | } |
| krishnamvs | 3:63ff52373e71 | 344 | |
| krishnamvs | 3:63ff52373e71 | 345 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 346 | // setTempCompensation() |
| krishnamvs | 3:63ff52373e71 | 347 | // |
| krishnamvs | 3:63ff52373e71 | 348 | // The ENS160 can use temperature data to help give more accurate sensor data. |
| krishnamvs | 3:63ff52373e71 | 349 | // |
| krishnamvs | 3:63ff52373e71 | 350 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 351 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 352 | // kelvinConversion The given temperature in Kelvin |
| krishnamvs | 3:63ff52373e71 | 353 | |
| krishnamvs | 3:63ff52373e71 | 354 | bool ENS160::setTempCompensation(float tempKelvin) |
| krishnamvs | 3:63ff52373e71 | 355 | { |
| krishnamvs | 3:63ff52373e71 | 356 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 357 | uint8_t tempVal[3] = {0}; |
| krishnamvs | 3:63ff52373e71 | 358 | uint16_t kelvinConversion = tempKelvin; |
| krishnamvs | 3:63ff52373e71 | 359 | |
| krishnamvs | 3:63ff52373e71 | 360 | kelvinConversion = kelvinConversion * 64; // convert value - fixed equation pg. 29 of datasheet |
| krishnamvs | 3:63ff52373e71 | 361 | tempVal[0] = SFE_ENS160_TEMP_IN; |
| krishnamvs | 3:63ff52373e71 | 362 | tempVal[1] = (kelvinConversion & 0x00FF); |
| krishnamvs | 3:63ff52373e71 | 363 | tempVal[2] = (kelvinConversion & 0xFF00) >> 8; |
| krishnamvs | 3:63ff52373e71 | 364 | |
| krishnamvs | 3:63ff52373e71 | 365 | retVal = writeRegisterRegion(tempVal, 2); |
| krishnamvs | 3:63ff52373e71 | 366 | |
| krishnamvs | 3:63ff52373e71 | 367 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 368 | return false; |
| krishnamvs | 3:63ff52373e71 | 369 | |
| krishnamvs | 3:63ff52373e71 | 370 | return true; |
| krishnamvs | 3:63ff52373e71 | 371 | } |
| krishnamvs | 3:63ff52373e71 | 372 | |
| krishnamvs | 3:63ff52373e71 | 373 | |
| krishnamvs | 3:63ff52373e71 | 374 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 375 | // setTempCompensationCelsius() |
| krishnamvs | 3:63ff52373e71 | 376 | // |
| krishnamvs | 3:63ff52373e71 | 377 | // The ENS160 can use temperature data to help give more accurate sensor data. |
| krishnamvs | 3:63ff52373e71 | 378 | // |
| krishnamvs | 3:63ff52373e71 | 379 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 380 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 381 | // tempCelsius The given temperature in Celsius |
| krishnamvs | 3:63ff52373e71 | 382 | |
| krishnamvs | 3:63ff52373e71 | 383 | bool ENS160::setTempCompensationCelsius(float tempCelsius) |
| krishnamvs | 3:63ff52373e71 | 384 | { |
| krishnamvs | 3:63ff52373e71 | 385 | float kelvinConversion = tempCelsius + 273.15; |
| krishnamvs | 3:63ff52373e71 | 386 | |
| krishnamvs | 3:63ff52373e71 | 387 | if( setTempCompensation(kelvinConversion) ) |
| krishnamvs | 3:63ff52373e71 | 388 | return true; |
| krishnamvs | 3:63ff52373e71 | 389 | |
| krishnamvs | 3:63ff52373e71 | 390 | return false; |
| krishnamvs | 3:63ff52373e71 | 391 | } |
| krishnamvs | 3:63ff52373e71 | 392 | |
| krishnamvs | 3:63ff52373e71 | 393 | |
| krishnamvs | 3:63ff52373e71 | 394 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 395 | // setRHCompensation() |
| krishnamvs | 3:63ff52373e71 | 396 | // |
| krishnamvs | 3:63ff52373e71 | 397 | // The ENS160 can use relative Humidiy data to help give more accurate sensor data. |
| krishnamvs | 3:63ff52373e71 | 398 | // |
| krishnamvs | 3:63ff52373e71 | 399 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 400 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 401 | // humidity The given relative humidity. |
| krishnamvs | 3:63ff52373e71 | 402 | |
| krishnamvs | 3:63ff52373e71 | 403 | bool ENS160::setRHCompensation(uint16_t humidity) |
| krishnamvs | 3:63ff52373e71 | 404 | { |
| krishnamvs | 3:63ff52373e71 | 405 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 406 | uint8_t tempVal[3] = {0}; |
| krishnamvs | 3:63ff52373e71 | 407 | |
| krishnamvs | 3:63ff52373e71 | 408 | humidity = humidity * 512; // convert value - fixed equation pg. 29 in datasheet. |
| krishnamvs | 3:63ff52373e71 | 409 | tempVal[0] = SFE_ENS160_RH_IN; |
| krishnamvs | 3:63ff52373e71 | 410 | tempVal[1] = (humidity & 0x00FF); |
| krishnamvs | 3:63ff52373e71 | 411 | tempVal[2] = (humidity & 0xFF00) >> 8; |
| krishnamvs | 3:63ff52373e71 | 412 | |
| krishnamvs | 3:63ff52373e71 | 413 | retVal = writeRegisterRegion(tempVal, 2); |
| krishnamvs | 3:63ff52373e71 | 414 | |
| krishnamvs | 3:63ff52373e71 | 415 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 416 | return false; |
| krishnamvs | 3:63ff52373e71 | 417 | |
| krishnamvs | 3:63ff52373e71 | 418 | return true; |
| krishnamvs | 3:63ff52373e71 | 419 | } |
| krishnamvs | 3:63ff52373e71 | 420 | |
| krishnamvs | 3:63ff52373e71 | 421 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 422 | // setRHCompensationFloat() |
| krishnamvs | 3:63ff52373e71 | 423 | // |
| krishnamvs | 3:63ff52373e71 | 424 | // The ENS160 can use relative Humidiy data to help give more accurate sensor data. |
| krishnamvs | 3:63ff52373e71 | 425 | // |
| krishnamvs | 3:63ff52373e71 | 426 | // Parameter Description |
| krishnamvs | 3:63ff52373e71 | 427 | // --------- ----------------------------- |
| krishnamvs | 3:63ff52373e71 | 428 | // humidity The given relative humidity. |
| krishnamvs | 3:63ff52373e71 | 429 | |
| krishnamvs | 3:63ff52373e71 | 430 | bool ENS160::setRHCompensationFloat(float humidity) |
| krishnamvs | 3:63ff52373e71 | 431 | { |
| krishnamvs | 3:63ff52373e71 | 432 | uint16_t humidityConversion = (uint16_t)humidity; |
| krishnamvs | 3:63ff52373e71 | 433 | |
| krishnamvs | 3:63ff52373e71 | 434 | if( setRHCompensation(humidityConversion) ) |
| krishnamvs | 3:63ff52373e71 | 435 | return false; |
| krishnamvs | 3:63ff52373e71 | 436 | |
| krishnamvs | 3:63ff52373e71 | 437 | return true; |
| krishnamvs | 3:63ff52373e71 | 438 | } |
| krishnamvs | 3:63ff52373e71 | 439 | |
| krishnamvs | 3:63ff52373e71 | 440 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 441 | // checkDataStatus() |
| krishnamvs | 3:63ff52373e71 | 442 | // |
| krishnamvs | 3:63ff52373e71 | 443 | // This checks the if the NEWDAT bit is high indicating that new data is ready to be read. |
| krishnamvs | 3:63ff52373e71 | 444 | // The bit is cleared when data has been read from their registers. |
| krishnamvs | 3:63ff52373e71 | 445 | |
| krishnamvs | 3:63ff52373e71 | 446 | bool ENS160::checkDataStatus() |
| krishnamvs | 3:63ff52373e71 | 447 | { |
| krishnamvs | 3:63ff52373e71 | 448 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 449 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 450 | |
| krishnamvs | 3:63ff52373e71 | 451 | retVal = readRegisterRegion(SFE_ENS160_DEVICE_STATUS, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 452 | |
| krishnamvs | 3:63ff52373e71 | 453 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 454 | return false; |
| krishnamvs | 3:63ff52373e71 | 455 | |
| krishnamvs | 3:63ff52373e71 | 456 | tempVal &= 0x02; |
| krishnamvs | 3:63ff52373e71 | 457 | |
| krishnamvs | 3:63ff52373e71 | 458 | if( tempVal == 0x02 ) |
| krishnamvs | 3:63ff52373e71 | 459 | return true; |
| krishnamvs | 3:63ff52373e71 | 460 | |
| krishnamvs | 3:63ff52373e71 | 461 | return false; |
| krishnamvs | 3:63ff52373e71 | 462 | } |
| krishnamvs | 3:63ff52373e71 | 463 | |
| krishnamvs | 3:63ff52373e71 | 464 | |
| krishnamvs | 3:63ff52373e71 | 465 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 466 | // checkGPRStatus() |
| krishnamvs | 3:63ff52373e71 | 467 | // |
| krishnamvs | 3:63ff52373e71 | 468 | // This checks the if the NEWGPR bit is high indicating that there is data in the |
| krishnamvs | 3:63ff52373e71 | 469 | // general purpose read registers. The bit is cleared the relevant registers have been |
| krishnamvs | 3:63ff52373e71 | 470 | // read. |
| krishnamvs | 3:63ff52373e71 | 471 | |
| krishnamvs | 3:63ff52373e71 | 472 | bool ENS160::checkGPRStatus() |
| krishnamvs | 3:63ff52373e71 | 473 | { |
| krishnamvs | 3:63ff52373e71 | 474 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 475 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 476 | |
| krishnamvs | 3:63ff52373e71 | 477 | retVal = readRegisterRegion(SFE_ENS160_DEVICE_STATUS, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 478 | |
| krishnamvs | 3:63ff52373e71 | 479 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 480 | return false; |
| krishnamvs | 3:63ff52373e71 | 481 | |
| krishnamvs | 3:63ff52373e71 | 482 | tempVal &= 0x01; |
| krishnamvs | 3:63ff52373e71 | 483 | |
| krishnamvs | 3:63ff52373e71 | 484 | if( tempVal == 0x01 ) |
| krishnamvs | 3:63ff52373e71 | 485 | return true; |
| krishnamvs | 3:63ff52373e71 | 486 | |
| krishnamvs | 3:63ff52373e71 | 487 | return false; |
| krishnamvs | 3:63ff52373e71 | 488 | } |
| krishnamvs | 3:63ff52373e71 | 489 | |
| krishnamvs | 3:63ff52373e71 | 490 | |
| krishnamvs | 3:63ff52373e71 | 491 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 492 | // getFlags() |
| krishnamvs | 3:63ff52373e71 | 493 | // |
| krishnamvs | 3:63ff52373e71 | 494 | // This checks the status "flags" of the device (0-3). |
| krishnamvs | 3:63ff52373e71 | 495 | |
| krishnamvs | 3:63ff52373e71 | 496 | uint8_t ENS160::getFlags() |
| krishnamvs | 3:63ff52373e71 | 497 | { |
| krishnamvs | 3:63ff52373e71 | 498 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 499 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 500 | |
| krishnamvs | 3:63ff52373e71 | 501 | retVal = readRegisterRegion(SFE_ENS160_DEVICE_STATUS, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 502 | |
| krishnamvs | 3:63ff52373e71 | 503 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 504 | return 0xFF; // Change to general error |
| krishnamvs | 3:63ff52373e71 | 505 | |
| krishnamvs | 3:63ff52373e71 | 506 | tempVal = (tempVal & 0x0C) >> 2; |
| krishnamvs | 3:63ff52373e71 | 507 | |
| krishnamvs | 3:63ff52373e71 | 508 | switch( tempVal ) |
| krishnamvs | 3:63ff52373e71 | 509 | { |
| krishnamvs | 3:63ff52373e71 | 510 | case 0: // Normal operation |
| krishnamvs | 3:63ff52373e71 | 511 | return 0; |
| krishnamvs | 3:63ff52373e71 | 512 | break; |
| krishnamvs | 3:63ff52373e71 | 513 | case 1: // Warm-up phase |
| krishnamvs | 3:63ff52373e71 | 514 | return 1; |
| krishnamvs | 3:63ff52373e71 | 515 | break; |
| krishnamvs | 3:63ff52373e71 | 516 | case 2: // Initial Start-Up Phase |
| krishnamvs | 3:63ff52373e71 | 517 | return 2; |
| krishnamvs | 3:63ff52373e71 | 518 | break; |
| krishnamvs | 3:63ff52373e71 | 519 | case 3: // Invalid Output |
| krishnamvs | 3:63ff52373e71 | 520 | return 3; |
| krishnamvs | 3:63ff52373e71 | 521 | break; |
| krishnamvs | 3:63ff52373e71 | 522 | default: |
| krishnamvs | 3:63ff52373e71 | 523 | return 0xFF; |
| krishnamvs | 3:63ff52373e71 | 524 | } |
| krishnamvs | 3:63ff52373e71 | 525 | } |
| krishnamvs | 3:63ff52373e71 | 526 | |
| krishnamvs | 3:63ff52373e71 | 527 | |
| krishnamvs | 3:63ff52373e71 | 528 | |
| krishnamvs | 3:63ff52373e71 | 529 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 530 | // checkOperationStatus() |
| krishnamvs | 3:63ff52373e71 | 531 | // |
| krishnamvs | 3:63ff52373e71 | 532 | // Checks the bit that indicates if an operation mode is running i.e. the device is not off. |
| krishnamvs | 3:63ff52373e71 | 533 | |
| krishnamvs | 3:63ff52373e71 | 534 | bool ENS160::checkOperationStatus() |
| krishnamvs | 3:63ff52373e71 | 535 | { |
| krishnamvs | 3:63ff52373e71 | 536 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 537 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 538 | |
| krishnamvs | 3:63ff52373e71 | 539 | retVal = readRegisterRegion(SFE_ENS160_DEVICE_STATUS, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 540 | |
| krishnamvs | 3:63ff52373e71 | 541 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 542 | return false; |
| krishnamvs | 3:63ff52373e71 | 543 | |
| krishnamvs | 3:63ff52373e71 | 544 | tempVal &= 0x80; |
| krishnamvs | 3:63ff52373e71 | 545 | |
| krishnamvs | 3:63ff52373e71 | 546 | if( tempVal == 0x80 ) |
| krishnamvs | 3:63ff52373e71 | 547 | return true; |
| krishnamvs | 3:63ff52373e71 | 548 | |
| krishnamvs | 3:63ff52373e71 | 549 | return false; |
| krishnamvs | 3:63ff52373e71 | 550 | } |
| krishnamvs | 3:63ff52373e71 | 551 | |
| krishnamvs | 3:63ff52373e71 | 552 | |
| krishnamvs | 3:63ff52373e71 | 553 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 554 | // getOperationError() |
| krishnamvs | 3:63ff52373e71 | 555 | // |
| krishnamvs | 3:63ff52373e71 | 556 | // Checks the bit that indicates if an invalid operating mode has been selected. |
| krishnamvs | 3:63ff52373e71 | 557 | |
| krishnamvs | 3:63ff52373e71 | 558 | bool ENS160::getOperationError() |
| krishnamvs | 3:63ff52373e71 | 559 | { |
| krishnamvs | 3:63ff52373e71 | 560 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 561 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 562 | |
| krishnamvs | 3:63ff52373e71 | 563 | retVal = readRegisterRegion(SFE_ENS160_DEVICE_STATUS, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 564 | |
| krishnamvs | 3:63ff52373e71 | 565 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 566 | return false; |
| krishnamvs | 3:63ff52373e71 | 567 | |
| krishnamvs | 3:63ff52373e71 | 568 | tempVal &= 0x40; |
| krishnamvs | 3:63ff52373e71 | 569 | |
| krishnamvs | 3:63ff52373e71 | 570 | if( tempVal == 0x40 ) |
| krishnamvs | 3:63ff52373e71 | 571 | return true; |
| krishnamvs | 3:63ff52373e71 | 572 | |
| krishnamvs | 3:63ff52373e71 | 573 | return false; |
| krishnamvs | 3:63ff52373e71 | 574 | } |
| krishnamvs | 3:63ff52373e71 | 575 | |
| krishnamvs | 3:63ff52373e71 | 576 | |
| krishnamvs | 3:63ff52373e71 | 577 | |
| krishnamvs | 3:63ff52373e71 | 578 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 579 | // getAQI() |
| krishnamvs | 3:63ff52373e71 | 580 | // |
| krishnamvs | 3:63ff52373e71 | 581 | // This reports the calculated Air Quality Index according to UBA which is a value between 1-5. |
| krishnamvs | 3:63ff52373e71 | 582 | // The AQI-UBA is a guideline developed by the German Federal Environmental Agency and is widely |
| krishnamvs | 3:63ff52373e71 | 583 | // referenced and adopted by many countries and organizations. |
| krishnamvs | 3:63ff52373e71 | 584 | // |
| krishnamvs | 3:63ff52373e71 | 585 | // 1 - Excellent, 2 - Good, 3 - Moderate, 4 - Poor, 5 - Unhealthy. |
| krishnamvs | 3:63ff52373e71 | 586 | |
| krishnamvs | 3:63ff52373e71 | 587 | uint8_t ENS160::getAQI() |
| krishnamvs | 3:63ff52373e71 | 588 | { |
| krishnamvs | 3:63ff52373e71 | 589 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 590 | uint8_t tempVal; |
| krishnamvs | 3:63ff52373e71 | 591 | |
| krishnamvs | 3:63ff52373e71 | 592 | retVal = readRegisterRegion(SFE_ENS160_DATA_AQI, &tempVal, 1); |
| krishnamvs | 3:63ff52373e71 | 593 | |
| krishnamvs | 3:63ff52373e71 | 594 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 595 | return 0; |
| krishnamvs | 3:63ff52373e71 | 596 | |
| krishnamvs | 3:63ff52373e71 | 597 | tempVal = (tempVal & 0x03); |
| krishnamvs | 3:63ff52373e71 | 598 | |
| krishnamvs | 3:63ff52373e71 | 599 | return tempVal; |
| krishnamvs | 3:63ff52373e71 | 600 | } |
| krishnamvs | 3:63ff52373e71 | 601 | |
| krishnamvs | 3:63ff52373e71 | 602 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 603 | // getTVOC() |
| krishnamvs | 3:63ff52373e71 | 604 | // |
| krishnamvs | 3:63ff52373e71 | 605 | // This reports the Total Volatile Organic Compounds in ppb (parts per billion) |
| krishnamvs | 3:63ff52373e71 | 606 | |
| krishnamvs | 3:63ff52373e71 | 607 | uint16_t ENS160::getTVOC() |
| krishnamvs | 3:63ff52373e71 | 608 | { |
| krishnamvs | 3:63ff52373e71 | 609 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 610 | uint16_t tvoc; |
| krishnamvs | 3:63ff52373e71 | 611 | uint8_t tempVal[2] = {0}; |
| krishnamvs | 3:63ff52373e71 | 612 | |
| krishnamvs | 3:63ff52373e71 | 613 | retVal = readRegisterRegion(SFE_ENS160_DATA_TVOC, tempVal, 2); |
| krishnamvs | 3:63ff52373e71 | 614 | |
| krishnamvs | 3:63ff52373e71 | 615 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 616 | return 0; |
| krishnamvs | 3:63ff52373e71 | 617 | |
| krishnamvs | 3:63ff52373e71 | 618 | tvoc = tempVal[0]; |
| krishnamvs | 3:63ff52373e71 | 619 | tvoc |= tempVal[1] << 8; |
| krishnamvs | 3:63ff52373e71 | 620 | |
| krishnamvs | 3:63ff52373e71 | 621 | return tvoc; |
| krishnamvs | 3:63ff52373e71 | 622 | } |
| krishnamvs | 3:63ff52373e71 | 623 | |
| krishnamvs | 3:63ff52373e71 | 624 | |
| krishnamvs | 3:63ff52373e71 | 625 | |
| krishnamvs | 3:63ff52373e71 | 626 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 627 | // getETOH() |
| krishnamvs | 3:63ff52373e71 | 628 | // |
| krishnamvs | 3:63ff52373e71 | 629 | // This reports the ehtanol concentration in ppb (parts per billion). According to |
| krishnamvs | 3:63ff52373e71 | 630 | // the datasheet this is a "virtual mirror" of the ethanol-calibrated TVOC register, |
| krishnamvs | 3:63ff52373e71 | 631 | // which is why they share the same register. |
| krishnamvs | 3:63ff52373e71 | 632 | |
| krishnamvs | 3:63ff52373e71 | 633 | uint16_t ENS160::getETOH() |
| krishnamvs | 3:63ff52373e71 | 634 | { |
| krishnamvs | 3:63ff52373e71 | 635 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 636 | uint16_t ethanol; |
| krishnamvs | 3:63ff52373e71 | 637 | uint8_t tempVal[2] = {0}; |
| krishnamvs | 3:63ff52373e71 | 638 | |
| krishnamvs | 3:63ff52373e71 | 639 | retVal = readRegisterRegion(SFE_ENS160_DATA_ETOH, tempVal, 2); |
| krishnamvs | 3:63ff52373e71 | 640 | |
| krishnamvs | 3:63ff52373e71 | 641 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 642 | return 0; |
| krishnamvs | 3:63ff52373e71 | 643 | |
| krishnamvs | 3:63ff52373e71 | 644 | ethanol = tempVal[0]; |
| krishnamvs | 3:63ff52373e71 | 645 | ethanol |= tempVal[1] << 8; |
| krishnamvs | 3:63ff52373e71 | 646 | |
| krishnamvs | 3:63ff52373e71 | 647 | return ethanol; |
| krishnamvs | 3:63ff52373e71 | 648 | } |
| krishnamvs | 3:63ff52373e71 | 649 | |
| krishnamvs | 3:63ff52373e71 | 650 | |
| krishnamvs | 3:63ff52373e71 | 651 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 652 | // getECO2() |
| krishnamvs | 3:63ff52373e71 | 653 | // |
| krishnamvs | 3:63ff52373e71 | 654 | // This reports the CO2 concentration in ppm (parts per million) based on the detected VOCs and hydrogen. |
| krishnamvs | 3:63ff52373e71 | 655 | |
| krishnamvs | 3:63ff52373e71 | 656 | uint16_t ENS160::getECO2() |
| krishnamvs | 3:63ff52373e71 | 657 | { |
| krishnamvs | 3:63ff52373e71 | 658 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 659 | uint16_t eco; |
| krishnamvs | 3:63ff52373e71 | 660 | uint8_t tempVal[2] = {0}; |
| krishnamvs | 3:63ff52373e71 | 661 | |
| krishnamvs | 3:63ff52373e71 | 662 | retVal = readRegisterRegion(SFE_ENS160_DATA_ECO2, tempVal, 2); |
| krishnamvs | 3:63ff52373e71 | 663 | |
| krishnamvs | 3:63ff52373e71 | 664 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 665 | return 0; |
| krishnamvs | 3:63ff52373e71 | 666 | |
| krishnamvs | 3:63ff52373e71 | 667 | eco = tempVal[0]; |
| krishnamvs | 3:63ff52373e71 | 668 | eco |= tempVal[1] << 8; |
| krishnamvs | 3:63ff52373e71 | 669 | |
| krishnamvs | 3:63ff52373e71 | 670 | return eco; |
| krishnamvs | 3:63ff52373e71 | 671 | } |
| krishnamvs | 3:63ff52373e71 | 672 | |
| krishnamvs | 3:63ff52373e71 | 673 | |
| krishnamvs | 3:63ff52373e71 | 674 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 675 | // getTempKelvin() |
| krishnamvs | 3:63ff52373e71 | 676 | // |
| krishnamvs | 3:63ff52373e71 | 677 | // This reports the temperature compensation value given to the sensor in Kelvin. |
| krishnamvs | 3:63ff52373e71 | 678 | |
| krishnamvs | 3:63ff52373e71 | 679 | float ENS160::getTempKelvin() |
| krishnamvs | 3:63ff52373e71 | 680 | { |
| krishnamvs | 3:63ff52373e71 | 681 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 682 | float temperature; |
| krishnamvs | 3:63ff52373e71 | 683 | int16_t tempConversion; |
| krishnamvs | 3:63ff52373e71 | 684 | uint8_t tempVal[2] = {0}; |
| krishnamvs | 3:63ff52373e71 | 685 | |
| krishnamvs | 3:63ff52373e71 | 686 | retVal = readRegisterRegion(SFE_ENS160_DATA_T, tempVal, 2); |
| krishnamvs | 3:63ff52373e71 | 687 | |
| krishnamvs | 3:63ff52373e71 | 688 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 689 | return 0; |
| krishnamvs | 3:63ff52373e71 | 690 | |
| krishnamvs | 3:63ff52373e71 | 691 | tempConversion = tempVal[0]; |
| krishnamvs | 3:63ff52373e71 | 692 | tempConversion |= (tempVal[1] << 8); |
| krishnamvs | 3:63ff52373e71 | 693 | temperature = (float)tempConversion; |
| krishnamvs | 3:63ff52373e71 | 694 | |
| krishnamvs | 3:63ff52373e71 | 695 | temperature = temperature/64; // Formula as described on pg. 32 of datasheet. |
| krishnamvs | 3:63ff52373e71 | 696 | |
| krishnamvs | 3:63ff52373e71 | 697 | return temperature; |
| krishnamvs | 3:63ff52373e71 | 698 | } |
| krishnamvs | 3:63ff52373e71 | 699 | |
| krishnamvs | 3:63ff52373e71 | 700 | |
| krishnamvs | 3:63ff52373e71 | 701 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 702 | // getTempCelsius() |
| krishnamvs | 3:63ff52373e71 | 703 | // |
| krishnamvs | 3:63ff52373e71 | 704 | // This reports the temperature compensation value given to the sensor in Celsius. |
| krishnamvs | 3:63ff52373e71 | 705 | |
| krishnamvs | 3:63ff52373e71 | 706 | float ENS160::getTempCelsius() |
| krishnamvs | 3:63ff52373e71 | 707 | { |
| krishnamvs | 3:63ff52373e71 | 708 | float temperature; |
| krishnamvs | 3:63ff52373e71 | 709 | |
| krishnamvs | 3:63ff52373e71 | 710 | temperature = getTempKelvin(); |
| krishnamvs | 3:63ff52373e71 | 711 | |
| krishnamvs | 3:63ff52373e71 | 712 | return (temperature - 273.15); |
| krishnamvs | 3:63ff52373e71 | 713 | } |
| krishnamvs | 3:63ff52373e71 | 714 | |
| krishnamvs | 3:63ff52373e71 | 715 | |
| krishnamvs | 3:63ff52373e71 | 716 | ////////////////////////////////////////////////////////////////////////////// |
| krishnamvs | 3:63ff52373e71 | 717 | // getRH() |
| krishnamvs | 3:63ff52373e71 | 718 | // |
| krishnamvs | 3:63ff52373e71 | 719 | // This reports the relative humidity compensation value given to the sensor. |
| krishnamvs | 3:63ff52373e71 | 720 | |
| krishnamvs | 3:63ff52373e71 | 721 | float ENS160::getRH() |
| krishnamvs | 3:63ff52373e71 | 722 | { |
| krishnamvs | 3:63ff52373e71 | 723 | int32_t retVal; |
| krishnamvs | 3:63ff52373e71 | 724 | uint16_t rh; |
| krishnamvs | 3:63ff52373e71 | 725 | uint8_t tempVal[2] = {0}; |
| krishnamvs | 3:63ff52373e71 | 726 | |
| krishnamvs | 3:63ff52373e71 | 727 | retVal = readRegisterRegion(SFE_ENS160_DATA_RH, tempVal, 2); |
| krishnamvs | 3:63ff52373e71 | 728 | |
| krishnamvs | 3:63ff52373e71 | 729 | if( retVal != 0 ) |
| krishnamvs | 3:63ff52373e71 | 730 | return 0; |
| krishnamvs | 3:63ff52373e71 | 731 | |
| krishnamvs | 3:63ff52373e71 | 732 | rh = tempVal[0]; |
| krishnamvs | 3:63ff52373e71 | 733 | rh |= tempVal[1] << 8; |
| krishnamvs | 3:63ff52373e71 | 734 | |
| krishnamvs | 3:63ff52373e71 | 735 | rh = rh/512; // Formula as described on pg. 33 of datasheet. |
| krishnamvs | 3:63ff52373e71 | 736 | |
| krishnamvs | 3:63ff52373e71 | 737 | return rh; |
| krishnamvs | 3:63ff52373e71 | 738 | } |
