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