SPS30 library

Dependents:   IZU2021_SPS30 Hybrid_IZU2021_MISSION_v2 Hybrid_IZU2021_MISSION

Committer:
ziqiyap
Date:
Tue Mar 05 07:01:55 2019 +0000
Revision:
2:549bee9a4cd0
Parent:
1:0078efb8903a
Child:
3:adeb61dd9ceb
added auto clean and manual clean class methods

Who changed what in which revision?

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