Skip to content

Commit 1160b23

Browse files
committed
allow the sensor.begin() to exit with a non-zero code
1 parent b7e1afc commit 1160b23

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

sdpsensor-esp-arduino.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void setup() {
2020
Serial.begin(115200);
2121
delay(1000); // let serial console settle
2222
sdp.initI2C(19, 23); // same as Wire.begin(19, 23)
23-
sdp.initSensor();
23+
sdp.begin();
2424
sdp.startContinuous();
2525
}
2626

sdpsensor.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void SDPSensor::initI2C(int pinSDA, int pinSCL) {
8787
}
8888

8989

90-
void SDPSensor::initSensor() {
90+
esp_err_t SDPSensor::begin() {
9191
SDPSensor::reset(); // stop continuous mode
9292

9393
// commands to read product id
@@ -100,20 +100,20 @@ void SDPSensor::initSensor() {
100100
uint8_t read_buffer[18] = { 0 };
101101

102102
const TickType_t ticks_to_wait_long = pdMS_TO_TICKS(100);
103-
ESP_ERROR_CHECK(
104-
i2c_master_write_to_device(i2c_port, i2c_addr, cmd0, SDPSENSOR_I2C_CMD_LEN, ticks_to_wait_long));
105-
ESP_ERROR_CHECK(
106-
i2c_master_write_to_device(i2c_port, i2c_addr, cmd1, SDPSENSOR_I2C_CMD_LEN, ticks_to_wait_long));
103+
esp_err_t err;
104+
err = i2c_master_write_to_device(i2c_port, i2c_addr, cmd0, SDPSENSOR_I2C_CMD_LEN, ticks_to_wait_long);
105+
if (err != ESP_OK) return err;
106+
err = i2c_master_write_to_device(i2c_port, i2c_addr, cmd1, SDPSENSOR_I2C_CMD_LEN, ticks_to_wait_long);
107+
if (err != ESP_OK) return err;
107108

108109
/*
109110
Read product id and serial number.
110111
Data Format:
111112
| Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6...18 |
112113
| Value | pid1 |CRC| pid2 |CRC| serial |
113114
*/
114-
ESP_ERROR_CHECK(
115-
i2c_master_read_from_device(i2c_port, i2c_addr, read_buffer, 18,
116-
ticks_to_wait_long));
115+
err = i2c_master_read_from_device(i2c_port, i2c_addr, read_buffer, 18, ticks_to_wait_long);
116+
if (err != ESP_OK) return err;
117117

118118
const uint32_t pid = (read_buffer[0] << 24) | (read_buffer[1] << 16)
119119
| (read_buffer[3] << 8) | (read_buffer[4] << 0);
@@ -156,17 +156,19 @@ void SDPSensor::initSensor() {
156156

157157
ESP_LOGI(TAG_SDPSENSOR, "Initialized SDP%d %dPa sensor (PID=0x%08X)", model_number, range_pa, pid);
158158

159-
ESP_ERROR_CHECK(
160-
i2c_master_write_to_device(i2c_port, i2c_addr, cmd_measure, SDPSENSOR_I2C_CMD_LEN, ticks_to_wait_long));
159+
err = i2c_master_write_to_device(i2c_port, i2c_addr, cmd_measure, SDPSENSOR_I2C_CMD_LEN, ticks_to_wait_long);
160+
if (err != ESP_OK) return err;
161161

162162
vTaskDelay(pdMS_TO_TICKS(90)); // theoretically 45 ms
163163

164-
ESP_ERROR_CHECK(
165-
i2c_master_read_from_device(i2c_port, i2c_addr, read_buffer, 9, ticks_to_wait_long));
164+
err = i2c_master_read_from_device(i2c_port, i2c_addr, read_buffer, 9, ticks_to_wait_long);
165+
if (err != ESP_OK) return err;
166166

167167
this->pressureScale = ((int16_t) read_buffer[6]) << 8 | read_buffer[7];
168168

169169
ESP_LOGI(TAG_SDPSENSOR, "SDP%d pressure scale: %d", model_number, this->pressureScale);
170+
171+
return err;
170172
}
171173

172174
uint16_t SDPSensor::getPressureScale() {

sdpsensor.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ class SDPSensor {
4141

4242

4343
/**
44-
* Initialize SDP sensor.
44+
* Initialize an SDP sensor.
4545
*
4646
* Firstly, it resets the sensor prior to executing any I2C commands.
4747
* Then it reads and saves the sensor serial number and diff pressure scale.
48+
* The returned error code other than ESP_OK (0) means that the sensor has
49+
* not been properly initialized.
50+
*
51+
* @returns the error code (defined in esp_err.h)
4852
*/
49-
void initSensor();
53+
esp_err_t begin();
5054

5155

5256
/**

0 commit comments

Comments
 (0)