-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdpsensor.h
150 lines (126 loc) · 4.74 KB
/
sdpsensor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* sdpsensor.h
*
* Created on: Sep 7, 2021
* Author: Danylo Ulianych
*/
#ifndef SDPSENSOR_H_
#define SDPSENSOR_H_
#include "esp_err.h"
#include "driver/i2c.h"
class SDPSensor {
private:
uint8_t i2c_addr; /* I2C address */
i2c_port_t i2c_port; /* I2C master port */
uint16_t pressureScale; /* Diff pressure scale */
bool initialized;
/* Information parameters */
uint32_t modelNumber;
uint32_t rangePa;
uint32_t productId;
uint64_t serialNumber;
public:
/**
* Constructor
*
* @param i2c_addr - I2C address.
* @param i2c_port - I2C port.
* ESP boards have two I2C peripherals.
* Defaults to 0.
*/
SDPSensor(uint8_t i2c_addr, i2c_port_t i2c_port = 0);
/**
* Initialize I2C. Same as `Wire.begin(SDA, SCL)`.
*
* @param pinSDA - SDA GPIO
* @param pinSCL - SCL GPIO
*
* @returns the error code (defined in esp_err.h)
*/
esp_err_t initI2C(int pinSDA, int pinSCL);
/**
* Initialize an SDP sensor.
*
* You may need to call `stopContinuous()` or `reset()` function
* prior to initializing the sensor, if previously it was working in
* a continuous mode.
* The product ID and the diff pressure scale are read and saved here.
* The returned error code other than ESP_OK (0) means that the sensor
* has not been properly initialized.
*
* @returns the error code (defined in esp_err.h)
*/
esp_err_t begin();
/**
* Get sensor info parameters. Arguments can be NULL.
*
* @param modelNumber - a pointer to save the model number (3x or 8xx)
* @param rangePa - a pointer to save the measurement range (in Pa)
* @param productId - a pointer to save the product ID (combination of two above)
* @param serialNumber - a pointer to save the unique serial number
*/
void getInfo(uint32_t *modelNumber, uint32_t *rangePa, uint32_t *productId, uint64_t *serialNumber);
/**
* Return the diff pressure scale, saved in the `initSensor()` call.
*
* @returns diff pressure scale
*/
uint16_t getPressureScale();
/**
* Start the sensor in the continuous mode.
*
* @returns the error code (defined in esp_err.h)
*/
esp_err_t startContinuous();
/**
* Stop the sensor continuous mode.
*
* @returns the error code (defined in esp_err.h)
*/
esp_err_t stopContinuous();
/**
* Reset the SDP sensor.
* All other sensors connected to the same I2C line
* (same port) will also receive the reset command.
*
* @returns the error code (defined in esp_err.h)
*/
esp_err_t reset();
/**
* Attach the IRQ handler callback to a dedicated GPIO sensor pin.
* Only the SDP3x sensor series have an IRQ pin.
*
* @param irqGPIO - GPIO interrupt pin number
* @param irqHandler - interrupt handler callback function
* @returns the error code (defined in esp_err.h)
*/
esp_err_t attachIRQHandler(int irqGPIO, void (*irqHandler)() );
/**
* Read the raw (unnormalized) differential pressure value and
* save the result in `diffPressureRaw`. To convert it to a
* physical value in Pa, one should divide it by the pressure
* scale (see the `getPressureScale()` function).
*
* This call is non-blocking (zero I2C timeout).
*
* @param diffPressureRaw - a pointer to save the result
* @returns the error code (defined in esp_err.h):
* ESP_OK - success
* ESP_FAIL - failure
* ESP_ERR_TIMEOUT - timed out
* ESP_ERR_INVALID_CRC - CRC mismatch
* ESP_ERR_INVALID_STATE - not initialized
*/
esp_err_t readDiffPressure(int16_t *diffPressureRaw);
/**
* Read the raw differential pressure value AND the temperature.
*
* This call is non-blocking (zero I2C timeout).
*
* @param diffPressureRaw - a pointer to save the diff pressure
* @param temperature - a pointer to save the temperature in Celsius
* @returns the error code (defined in esp_err.h)
*/
esp_err_t readDiffPressureTemperature(int16_t *diffPressureRaw, float *temperature);
};
#endif /* SDPSENSOR_H_ */