Advertisement
le_lukasz

Untitled

Nov 4th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1.  
  2. #ifndef _PMS7003_H_
  3. #define _PMS7003_H_
  4.  
  5. #include <Arduino.h>
  6.  
  7. #define PMS7003_DATA_SIZE 32   // 0x42 + 0x4d  + 28 bytes data + 2 bytes checksum = 32 bytes
  8.  
  9. typedef struct pms7003_struct {
  10.   uint8_t start_1;          // Start char 1 0x42 (fixed)
  11.   uint8_t start_2;          // Start char 2 0x4d (fixed)
  12.   uint16_t frame_length;    // Frame length = 2x13 + 2 (data + parity)
  13.  
  14.   uint16_t pm_1_0;          // PM1.0 concentration (CF = 1, standard particles) Unit ug/m^3
  15.   uint16_t pm_2_5;          // PM2.5 concentration (CF = 1, standard particulates) Unit ug/m^3
  16.   uint16_t pm_10_0;         // PM10 concentration (CF = 1, standard particulate matter) Unit ug/m^3
  17.  
  18.   uint16_t pm_1_0_atmos;    // PM1.0 concentration (in the atmosphere) Unit ug/m^3
  19.   uint16_t pm_2_5_atmos;    // PM2.5 concentration (in the atmosphere) Unit ug/m^3
  20.   uint16_t pm_10_0_atmos;   // PM10 concentration (in the atmosphere) Unit ug/m^3
  21.  
  22.   uint16_t raw_gt_0_3;      // Particles in 0.1 liter of air > 0.3um
  23.   uint16_t raw_gt_0_5;      // Particles in 0.1 liter of air > 0.5um
  24.   uint16_t raw_gt_1_0;      // Particles in 0.1 liter of air > 1.0um
  25.   uint16_t raw_gt_2_5;      // Particles in 0.1 liter of air > 2.5um
  26.   uint16_t raw_gt_5_0;      // Particles in 0.1 liter of air > 5.0um
  27.   uint16_t raw_gt_10_0;     // Particles in 0.1 liter of air > 10um
  28.  
  29.   uint8_t version_number;   // Version number
  30.   uint8_t error_code;       // Error code
  31.  
  32.   uint16_t checksum;        // Sum of each byte from start_1 ... error_code
  33. } PMS7003_STRUCT;
  34.  
  35. typedef union pms7003_data {
  36.   unsigned char bytes[PMS7003_DATA_SIZE];
  37.   uint16_t words[PMS7003_DATA_SIZE/2];
  38.   PMS7003_STRUCT values;
  39. } PMS7003_DATABUF;
  40.  
  41.  
  42. class PMS7003 {
  43.   public:
  44.     bool debug;
  45.     PMS7003();
  46.     void init();
  47.     void init(Stream *serial);
  48.     void updateFrame();
  49.     bool hasNewData();
  50.  
  51.     uint16_t getPM_1_0();
  52.     uint16_t getPM_2_5();
  53.     uint16_t getPM_10_0();
  54.     uint16_t getPM_1_0_atmos();
  55.     uint16_t getPM_2_5_atmos();
  56.     uint16_t getPM_10_0_atmos();
  57.  
  58.     uint16_t getRawGreaterThan_0_3();
  59.     uint16_t getRawGreaterThan_0_5();
  60.     uint16_t getRawGreaterThan_1_0();
  61.     uint16_t getRawGreaterThan_2_5();
  62.     uint16_t getRawGreaterThan_5_0();
  63.     uint16_t getRawGreaterThan_10_0();
  64.    
  65.     uint8_t getHWVersion();
  66.     uint8_t getErrorCode();
  67.  
  68.   private:
  69.     PMS7003_DATABUF sensorData;
  70.     bool dataReady;
  71.     bool initialized;
  72.     Stream *serial;
  73.     unsigned char lastByte,
  74.                   nextByte;
  75.     int bufferIndex;
  76.  
  77.     void dumpBytes();
  78.     void convertSensorData();
  79.     bool isValidChecksum();
  80.     uint16_t uint16FromBufferData(unsigned char *buff, int loc);
  81. };
  82.  
  83. #endif /*_PMS7003_H_*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement