SPS30 library
Dependents: IZU2021_SPS30 Hybrid_IZU2021_MISSION_v2 Hybrid_IZU2021_MISSION
sps30.h@3:adeb61dd9ceb, 2019-03-05 (annotated)
- Committer:
- ziqiyap
- Date:
- Tue Mar 05 07:04:37 2019 +0000
- Revision:
- 3:adeb61dd9ceb
- Parent:
- 2:549bee9a4cd0
- Child:
- 4:7558ddc3c7d6
added author header in sps30.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ziqiyap | 3:adeb61dd9ceb | 1 | /******************************************************************************* |
ziqiyap | 3:adeb61dd9ceb | 2 | * Copyright (c) 2018-2019, Sensors and IoT Capability Centre (SIOT) at GovTech. |
ziqiyap | 3:adeb61dd9ceb | 3 | * |
ziqiyap | 3:adeb61dd9ceb | 4 | * Contributor(s): |
ziqiyap | 3:adeb61dd9ceb | 5 | * Yap Zi Qi yap_zi_qi@tech.gov.sg |
ziqiyap | 3:adeb61dd9ceb | 6 | *******************************************************************************/ |
ziqiyap | 3:adeb61dd9ceb | 7 | |
ziqiyap | 0:9221dac25d3b | 8 | #ifndef SPS30_H |
ziqiyap | 0:9221dac25d3b | 9 | #define SPS30_H |
ziqiyap | 0:9221dac25d3b | 10 | |
ziqiyap | 2:549bee9a4cd0 | 11 | #define SPS30_I2C_ADDR 0xd2 // left shifted by 1 bit from 0x69 |
ziqiyap | 0:9221dac25d3b | 12 | |
ziqiyap | 0:9221dac25d3b | 13 | #define SPS30_CMMD_STRT_MEAS 0x0010 |
ziqiyap | 0:9221dac25d3b | 14 | #define SPS30_CMMD_STOP_MEAS 0x0104 |
ziqiyap | 0:9221dac25d3b | 15 | #define SPS30_CMMD_GET_READY_STAT 0x0202 |
ziqiyap | 0:9221dac25d3b | 16 | #define SPS30_CMMD_READ_MEAS 0x0300 |
ziqiyap | 0:9221dac25d3b | 17 | |
ziqiyap | 0:9221dac25d3b | 18 | #define SPS30_CMMD_AUTO_CLEAN_INTV 0x8004 |
ziqiyap | 0:9221dac25d3b | 19 | #define SPS30_CMMD_START_FAN_CLEAN 0x5607 |
ziqiyap | 0:9221dac25d3b | 20 | |
ziqiyap | 0:9221dac25d3b | 21 | #define SPS30_CMMD_SOFT_RESET 0xD304 |
ziqiyap | 0:9221dac25d3b | 22 | |
ziqiyap | 0:9221dac25d3b | 23 | #define SPS30_CMMD_READ_SERIALNBR 0xD033 |
ziqiyap | 0:9221dac25d3b | 24 | #define SPS30_CMMD_READ_ARTICLECODE 0xD025 |
ziqiyap | 0:9221dac25d3b | 25 | |
ziqiyap | 2:549bee9a4cd0 | 26 | #define SPS30_POLYNOMIAL 0x31 // P(x) = x^8 + x^5 + x^4 + 1 = 100110p01 |
ziqiyap | 0:9221dac25d3b | 27 | #define SPS30_CRC_INIT 0xff |
ziqiyap | 0:9221dac25d3b | 28 | |
ziqiyap | 2:549bee9a4cd0 | 29 | #define SPS30_SN_SIZE 33 // size of the s/n ascii string + CRC values |
ziqiyap | 2:549bee9a4cd0 | 30 | #define SPS30_ACODE_SIZE 33 // size of the article code ascii string + CRC values |
ziqiyap | 0:9221dac25d3b | 31 | |
ziqiyap | 2:549bee9a4cd0 | 32 | /** Create SPS30 controller class |
ziqiyap | 2:549bee9a4cd0 | 33 | * |
ziqiyap | 2:549bee9a4cd0 | 34 | * @param sps30 class |
ziqiyap | 2:549bee9a4cd0 | 35 | * |
ziqiyap | 2:549bee9a4cd0 | 36 | */ |
ziqiyap | 0:9221dac25d3b | 37 | class sps30 { |
ziqiyap | 0:9221dac25d3b | 38 | |
ziqiyap | 0:9221dac25d3b | 39 | public: |
ziqiyap | 0:9221dac25d3b | 40 | enum SPSerror { |
ziqiyap | 0:9221dac25d3b | 41 | SPSnoERROR, //all ok |
ziqiyap | 0:9221dac25d3b | 42 | SPSisReady, //ready ststus register |
ziqiyap | 0:9221dac25d3b | 43 | SPSnoAckERROR, //no I2C ACK error |
ziqiyap | 0:9221dac25d3b | 44 | SPStimeoutERROR, //I2C timeout error |
ziqiyap | 0:9221dac25d3b | 45 | SPScrcERROR, //CRC error, any |
ziqiyap | 0:9221dac25d3b | 46 | }; |
ziqiyap | 0:9221dac25d3b | 47 | |
ziqiyap | 2:549bee9a4cd0 | 48 | uint16_t ready; /**< 1 = ready, 0 = busy */ |
ziqiyap | 2:549bee9a4cd0 | 49 | uint32_t clean_interval_i; /** 32 unsigned bit in seconds */ |
ziqiyap | 0:9221dac25d3b | 50 | |
ziqiyap | 2:549bee9a4cd0 | 51 | float mass_1p0_f; /**< float of Mass Conc of PM1.0 */ |
ziqiyap | 2:549bee9a4cd0 | 52 | float mass_2p5_f; /**< float of Mass Conc of PM2.5 */ |
ziqiyap | 2:549bee9a4cd0 | 53 | float mass_4p0_f; /**< float of Mass Conc of PM4.0 */ |
ziqiyap | 2:549bee9a4cd0 | 54 | float mass_10p0_f; /**< float of Mass Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 55 | |
ziqiyap | 2:549bee9a4cd0 | 56 | float num_0p5_f; /**< float of Number Conc of PM0.5 */ |
ziqiyap | 2:549bee9a4cd0 | 57 | float num_1p0_f; /**< float of Number Conc of PM1.0 */ |
ziqiyap | 2:549bee9a4cd0 | 58 | float num_2p5_f; /**< float of Number Conc of PM2.5 */ |
ziqiyap | 2:549bee9a4cd0 | 59 | float num_4p0_f; /**< float of Number Conc of PM4.0 */ |
ziqiyap | 2:549bee9a4cd0 | 60 | float num_10p0_f; /**< float of Number Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 61 | |
ziqiyap | 2:549bee9a4cd0 | 62 | float typ_pm_size_f; /**< float of Typical Particle Size */ |
ziqiyap | 0:9221dac25d3b | 63 | |
ziqiyap | 2:549bee9a4cd0 | 64 | uint8_t acode[33]; /**< Article code number */ |
ziqiyap | 2:549bee9a4cd0 | 65 | uint8_t sn[33]; /**< ASCII Serial Number */ |
ziqiyap | 0:9221dac25d3b | 66 | |
ziqiyap | 0:9221dac25d3b | 67 | |
ziqiyap | 0:9221dac25d3b | 68 | |
ziqiyap | 0:9221dac25d3b | 69 | /** Create a SPS30 object using the specified I2C object |
ziqiyap | 0:9221dac25d3b | 70 | * @param sda - mbed I2C interface pin |
ziqiyap | 0:9221dac25d3b | 71 | * @param scl - mbed I2C interface pin |
ziqiyap | 0:9221dac25d3b | 72 | * @param I2C Frequency (in Hz) |
ziqiyap | 0:9221dac25d3b | 73 | * |
ziqiyap | 0:9221dac25d3b | 74 | * @return none |
ziqiyap | 0:9221dac25d3b | 75 | */ |
ziqiyap | 0:9221dac25d3b | 76 | sps30(PinName sda, PinName scl, int i2cFrequency); |
ziqiyap | 0:9221dac25d3b | 77 | |
ziqiyap | 0:9221dac25d3b | 78 | /** Destructor |
ziqiyap | 0:9221dac25d3b | 79 | * |
ziqiyap | 0:9221dac25d3b | 80 | * @param --none-- |
ziqiyap | 0:9221dac25d3b | 81 | * |
ziqiyap | 0:9221dac25d3b | 82 | * @return none |
ziqiyap | 0:9221dac25d3b | 83 | */ |
ziqiyap | 0:9221dac25d3b | 84 | ~sps30(); |
ziqiyap | 0:9221dac25d3b | 85 | |
ziqiyap | 0:9221dac25d3b | 86 | /** Start Auto-Measurement |
ziqiyap | 0:9221dac25d3b | 87 | * |
ziqiyap | 0:9221dac25d3b | 88 | * @param --none-- |
ziqiyap | 0:9221dac25d3b | 89 | * |
ziqiyap | 0:9221dac25d3b | 90 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 91 | */ |
ziqiyap | 0:9221dac25d3b | 92 | uint8_t startMeasurement(); |
ziqiyap | 0:9221dac25d3b | 93 | |
ziqiyap | 0:9221dac25d3b | 94 | /** Stop Auto-Measurement |
ziqiyap | 0:9221dac25d3b | 95 | * |
ziqiyap | 0:9221dac25d3b | 96 | * @param --none-- |
ziqiyap | 0:9221dac25d3b | 97 | * |
ziqiyap | 0:9221dac25d3b | 98 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 99 | */ |
ziqiyap | 0:9221dac25d3b | 100 | uint8_t stopMeasurement(); |
ziqiyap | 0:9221dac25d3b | 101 | |
ziqiyap | 0:9221dac25d3b | 102 | /** Get Ready Status register |
ziqiyap | 0:9221dac25d3b | 103 | * |
ziqiyap | 0:9221dac25d3b | 104 | * @param --none-- |
ziqiyap | 0:9221dac25d3b | 105 | * @see Ready Status result |
ziqiyap | 0:9221dac25d3b | 106 | * |
ziqiyap | 0:9221dac25d3b | 107 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 108 | */ |
ziqiyap | 0:9221dac25d3b | 109 | uint8_t getReadyStatus(); |
ziqiyap | 0:9221dac25d3b | 110 | |
ziqiyap | 0:9221dac25d3b | 111 | /** Get all particulate matter parameters |
ziqiyap | 0:9221dac25d3b | 112 | * |
ziqiyap | 0:9221dac25d3b | 113 | * @param --none- |
ziqiyap | 0:9221dac25d3b | 114 | * @see Results in Public member variables |
ziqiyap | 0:9221dac25d3b | 115 | * |
ziqiyap | 0:9221dac25d3b | 116 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 117 | */ |
ziqiyap | 0:9221dac25d3b | 118 | uint8_t readMeasurement(); |
ziqiyap | 0:9221dac25d3b | 119 | |
ziqiyap | 0:9221dac25d3b | 120 | /** Calculate the SPS30 CRC value |
ziqiyap | 0:9221dac25d3b | 121 | * |
ziqiyap | 0:9221dac25d3b | 122 | * @param 16 bit value to perform a CRC check on |
ziqiyap | 0:9221dac25d3b | 123 | * |
ziqiyap | 0:9221dac25d3b | 124 | * @return 8 bit CRC value |
ziqiyap | 0:9221dac25d3b | 125 | */ |
ziqiyap | 0:9221dac25d3b | 126 | uint8_t calcCrc2b(uint16_t seed); |
ziqiyap | 0:9221dac25d3b | 127 | |
ziqiyap | 0:9221dac25d3b | 128 | /** Compare received CRC value with calculated CRC value |
ziqiyap | 0:9221dac25d3b | 129 | * |
ziqiyap | 0:9221dac25d3b | 130 | * @param 16 bit value to perform a CRC check on |
ziqiyap | 0:9221dac25d3b | 131 | * @param 8 bit value to compare CRC values |
ziqiyap | 0:9221dac25d3b | 132 | * |
ziqiyap | 0:9221dac25d3b | 133 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 134 | */ |
ziqiyap | 0:9221dac25d3b | 135 | uint8_t checkCrc2b(uint16_t seed, uint8_t crcIn); |
ziqiyap | 0:9221dac25d3b | 136 | |
ziqiyap | 0:9221dac25d3b | 137 | /** Get Article Code |
ziqiyap | 0:9221dac25d3b | 138 | * |
ziqiyap | 0:9221dac25d3b | 139 | * @param --none-- |
ziqiyap | 2:549bee9a4cd0 | 140 | * @see ASCII Article Code as acode[33] (returns all 0) |
ziqiyap | 0:9221dac25d3b | 141 | * |
ziqiyap | 0:9221dac25d3b | 142 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 143 | */ |
ziqiyap | 0:9221dac25d3b | 144 | uint8_t getArticleCode(); |
ziqiyap | 0:9221dac25d3b | 145 | |
ziqiyap | 0:9221dac25d3b | 146 | /** Get Serial Number |
ziqiyap | 0:9221dac25d3b | 147 | * |
ziqiyap | 0:9221dac25d3b | 148 | * @param --none-- |
ziqiyap | 2:549bee9a4cd0 | 149 | * @see ASCII Serial Number as sn[33] |
ziqiyap | 0:9221dac25d3b | 150 | * |
ziqiyap | 0:9221dac25d3b | 151 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 152 | */ |
ziqiyap | 0:9221dac25d3b | 153 | uint8_t getSerialNumber(); |
ziqiyap | 2:549bee9a4cd0 | 154 | |
ziqiyap | 2:549bee9a4cd0 | 155 | /** Read Auto Cleaning Interval on the SPS30 |
ziqiyap | 2:549bee9a4cd0 | 156 | * |
ziqiyap | 2:549bee9a4cd0 | 157 | * @param --none-- |
ziqiyap | 2:549bee9a4cd0 | 158 | * |
ziqiyap | 2:549bee9a4cd0 | 159 | * @return enum SPSerror |
ziqiyap | 2:549bee9a4cd0 | 160 | */ |
ziqiyap | 2:549bee9a4cd0 | 161 | uint8_t readAutoCleanInterval(); |
ziqiyap | 2:549bee9a4cd0 | 162 | |
ziqiyap | 2:549bee9a4cd0 | 163 | /** Set Auto Cleaning Interval on the SPS30 |
ziqiyap | 2:549bee9a4cd0 | 164 | * |
ziqiyap | 2:549bee9a4cd0 | 165 | * @param Auto Cleaning Interval in seconds |
ziqiyap | 2:549bee9a4cd0 | 166 | * default is 604800s = 1 week, 0 to disable auto clean |
ziqiyap | 2:549bee9a4cd0 | 167 | * |
ziqiyap | 2:549bee9a4cd0 | 168 | * @return enum SPSerror |
ziqiyap | 2:549bee9a4cd0 | 169 | */ |
ziqiyap | 2:549bee9a4cd0 | 170 | uint8_t setAutoCleanInterval(uint32_t set_interval = 604800); |
ziqiyap | 2:549bee9a4cd0 | 171 | |
ziqiyap | 2:549bee9a4cd0 | 172 | |
ziqiyap | 2:549bee9a4cd0 | 173 | /** Perform manual Fan Cleaning |
ziqiyap | 2:549bee9a4cd0 | 174 | * |
ziqiyap | 2:549bee9a4cd0 | 175 | * @param --none-- |
ziqiyap | 2:549bee9a4cd0 | 176 | * |
ziqiyap | 2:549bee9a4cd0 | 177 | * @return enum SPSerror |
ziqiyap | 2:549bee9a4cd0 | 178 | */ |
ziqiyap | 2:549bee9a4cd0 | 179 | uint8_t startFanClean(); |
ziqiyap | 2:549bee9a4cd0 | 180 | |
ziqiyap | 0:9221dac25d3b | 181 | |
ziqiyap | 0:9221dac25d3b | 182 | /** Perform a soft reset |
ziqiyap | 0:9221dac25d3b | 183 | * |
ziqiyap | 0:9221dac25d3b | 184 | * @param --none-- |
ziqiyap | 0:9221dac25d3b | 185 | * |
ziqiyap | 0:9221dac25d3b | 186 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 187 | */ |
ziqiyap | 0:9221dac25d3b | 188 | uint8_t softReset(); |
ziqiyap | 0:9221dac25d3b | 189 | |
ziqiyap | 0:9221dac25d3b | 190 | private: |
ziqiyap | 0:9221dac25d3b | 191 | char i2cBuff[60]; |
ziqiyap | 2:549bee9a4cd0 | 192 | |
ziqiyap | 2:549bee9a4cd0 | 193 | uint16_t clean_interval_m; /**< High order 16 bit word of Auto Clean Interval */ |
ziqiyap | 2:549bee9a4cd0 | 194 | uint16_t clean_interval_l; /**< High order 16 bit word of Auto Clean Interval */ |
ziqiyap | 0:9221dac25d3b | 195 | |
ziqiyap | 0:9221dac25d3b | 196 | uint16_t mass_1p0_m; /**< High order 16 bit word of Mass Conc of PM1.0 */ |
ziqiyap | 0:9221dac25d3b | 197 | uint16_t mass_1p0_l; /**< Low order 16 bit word of Mass Conc of PM1.0 */ |
ziqiyap | 0:9221dac25d3b | 198 | uint16_t mass_2p5_m; /**< High order 16 bit word of Mass Conc of PM2.5 */ |
ziqiyap | 0:9221dac25d3b | 199 | uint16_t mass_2p5_l; /**< Low order 16 bit word of Mass Conc of PM2.5 */ |
ziqiyap | 0:9221dac25d3b | 200 | uint16_t mass_4p0_m; /**< High order 16 bit word of Mass Conc of PM4.0 */ |
ziqiyap | 0:9221dac25d3b | 201 | uint16_t mass_4p0_l; /**< Low order 16 bit word of Mass Conc of PM4.0 */ |
ziqiyap | 2:549bee9a4cd0 | 202 | uint16_t mass_10p0_m; /**< High order 16 bit word of Mass Conc of PM10 */ |
ziqiyap | 2:549bee9a4cd0 | 203 | uint16_t mass_10p0_l; /**< Low order 16 bit word of Mass Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 204 | |
ziqiyap | 2:549bee9a4cd0 | 205 | uint16_t num_0p5_m; /**< High order 16 bit word of Number Conc of PM0.5 */ |
ziqiyap | 2:549bee9a4cd0 | 206 | uint16_t num_0p5_l; /**< Low order 16 bit word of Number Conc of PM0.5 */ |
ziqiyap | 2:549bee9a4cd0 | 207 | uint16_t num_1p0_m; /**< High order 16 bit word of Number Conc of PM1.0 */ |
ziqiyap | 2:549bee9a4cd0 | 208 | uint16_t num_1p0_l; /**< Low order 16 bit word of Number Conc of PM1.0 */ |
ziqiyap | 2:549bee9a4cd0 | 209 | uint16_t num_2p5_m; /**< High order 16 bit word of Number Conc of PM2.5 */ |
ziqiyap | 2:549bee9a4cd0 | 210 | uint16_t num_2p5_l; /**< Low order 16 bit word of Number Conc of PM2.5 */ |
ziqiyap | 2:549bee9a4cd0 | 211 | uint16_t num_4p0_m; /**< High order 16 bit word of Number Conc of PM4.0 */ |
ziqiyap | 2:549bee9a4cd0 | 212 | uint16_t num_4p0_l; /**< Low order 16 bit word of Number Conc of PM4.0 */ |
ziqiyap | 0:9221dac25d3b | 213 | uint16_t num_10p0_m; /**< High order 16 bit word of Number Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 214 | uint16_t num_10p0_l; /**< Low order 16 bit word of Number Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 215 | |
ziqiyap | 2:549bee9a4cd0 | 216 | uint16_t typ_pm_size_m; /**< High order 16 bit word of Typical Particle Size */ |
ziqiyap | 2:549bee9a4cd0 | 217 | uint16_t typ_pm_size_l; /**< Low order 16 bit word of Typical Particle Size */ |
ziqiyap | 0:9221dac25d3b | 218 | |
ziqiyap | 0:9221dac25d3b | 219 | uint32_t mass_1p0_i; /**< 32 bit int of Mass Conc of PM1.0 */ |
ziqiyap | 0:9221dac25d3b | 220 | uint32_t mass_2p5_i; /**< 32 bit int of Mass Conc of PM2.5 */ |
ziqiyap | 0:9221dac25d3b | 221 | uint32_t mass_4p0_i; /**< 32 bit int of Mass Conc of PM4.0 */ |
ziqiyap | 2:549bee9a4cd0 | 222 | uint32_t mass_10p0_i; /**< 32 bit int of Mass Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 223 | |
ziqiyap | 2:549bee9a4cd0 | 224 | uint32_t num_0p5_i; /**< 32 bit int of Number Conc of PM0.5 */ |
ziqiyap | 2:549bee9a4cd0 | 225 | uint32_t num_1p0_i; /**< 32 bit int of Number Conc of PM1.0 */ |
ziqiyap | 2:549bee9a4cd0 | 226 | uint32_t num_2p5_i; /**< 32 bit int of Number Conc of PM2.5 */ |
ziqiyap | 2:549bee9a4cd0 | 227 | uint32_t num_4p0_i; /**< 32 bit int of Number Conc of PM4.0 */ |
ziqiyap | 0:9221dac25d3b | 228 | uint32_t num_10p0_i; /**< 32 bit int of Number Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 229 | |
ziqiyap | 2:549bee9a4cd0 | 230 | uint32_t typ_pm_size_i; /**< 32 bit int of Typical Particle Size */ |
ziqiyap | 0:9221dac25d3b | 231 | |
ziqiyap | 0:9221dac25d3b | 232 | protected: |
ziqiyap | 0:9221dac25d3b | 233 | I2C _i2c; |
ziqiyap | 0:9221dac25d3b | 234 | |
ziqiyap | 0:9221dac25d3b | 235 | }; |
ziqiyap | 0:9221dac25d3b | 236 | #endif |
ziqiyap | 0:9221dac25d3b | 237 |