Skip to content

Commit 8804364

Browse files
committed
display return code for reporting/ change prefix: ap->baro
1 parent dc38399 commit 8804364

File tree

4 files changed

+287
-277
lines changed

4 files changed

+287
-277
lines changed

examples/baro-ar/baro-ar.ino

Lines changed: 78 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@
2626
#include <Wire.h>
2727

2828
/* defines */
29-
#define AP_2SMPB02E_CHIP_ID 0x5C
29+
#define BARO_2SMPB02E_CHIP_ID 0x5C
3030

3131
#define GPIO_LED_R_PIN 4
3232
#define GPIO_LED_G_PIN 5
3333
#define GPIO_LED_B_PIN 6
3434

3535
/* values */
36-
ap_2smpb02e_setting_t ap_2smpb02e_setting;
36+
baro_2smpb02e_setting_t baro_2smpb02e_setting;
3737

3838
/* macros */
3939
#define conv8s_s24_be(a, b, c) \
4040
(int32_t)((((uint32_t)a << 16) & 0x00FF0000) | \
4141
(((uint32_t)b << 8) & 0x0000FF00) | \
4242
((uint32_t)c & 0x000000FF))
4343

44-
#define ap_halt(a) {Serial.println(a); while (1) {}}
44+
#define baro_halt(a) {Serial.println(a); while (1) {}}
4545

4646

4747
/* I2C functions */
@@ -78,80 +78,82 @@ bool i2c_read_reg8(uint8_t slave_addr, uint8_t register_addr,
7878
return false;
7979
}
8080

81-
/** <!-- ap_2smpb02e_setup {{{1 --> setup for 2SMPB-02E
82-
* 1. check CHIP_ID to I2C connections.
81+
82+
/** <!-- baro_2smpb02e_setup {{{1 --> setup for 2SMPB-02E
83+
* 1. check CHIP_ID to confirm I2C connections.
8384
* 2. read coefficient values for compensations.
8485
* 3. sensor setup and start to measurements.
8586
*/
86-
void ap_2smpb02e_setup(void) {
87+
bool baro_2smpb02e_setup(void) {
8788
bool result;
8889
uint8_t rbuf[32] = {0};
8990
uint8_t ex;
9091

9192
// 1.
92-
result = i2c_read_reg8(AP_2SMPB02E_ADDRESS,
93-
AP_2SMPB02E_REGI2C_CHIP_ID, rbuf, 1);
94-
if (result || rbuf[0] != AP_2SMPB02E_CHIP_ID) {
95-
ap_halt("cannot find 2SMPB-02E sensor, halted...");
93+
result = i2c_read_reg8(BARO_2SMPB02E_ADDRESS,
94+
BARO_2SMPB02E_REGI2C_CHIP_ID, rbuf, 1);
95+
if (result || rbuf[0] != BARO_2SMPB02E_CHIP_ID) {
96+
baro_halt("cannot find 2SMPB-02E sensor, halted...");
9697
}
9798

9899
// 2.
99-
result = i2c_read_reg8(AP_2SMPB02E_ADDRESS,
100-
AP_2SMPB02E_REGI2C_COEFS, rbuf, 25);
100+
result = i2c_read_reg8(BARO_2SMPB02E_ADDRESS,
101+
BARO_2SMPB02E_REGI2C_COEFS, rbuf, 25);
101102
if (result) {
102-
ap_halt("failed to read 2SMPB-02E coeffients, halted...");
103+
baro_halt("failed to read 2SMPB-02E coeffients, halted...");
103104
}
104105

105106
// pressure parameters
106107
ex = (rbuf[24] & 0xf0) >> 4;
107-
ap_2smpb02e_setting._B00 = ap_2smpb02e_conv20q4_dbl(rbuf, ex, 0);
108-
ap_2smpb02e_setting._BT1 = ap_2smpb02e_conv16_dbl(
109-
AP_2SMPB02E_COEFF_A_BT1, AP_2SMPB02E_COEFF_S_BT1, rbuf, 2);
110-
ap_2smpb02e_setting._BT2 = ap_2smpb02e_conv16_dbl(
111-
AP_2SMPB02E_COEFF_A_BT2, AP_2SMPB02E_COEFF_S_BT2, rbuf, 4);
112-
ap_2smpb02e_setting._BP1 = ap_2smpb02e_conv16_dbl(
113-
AP_2SMPB02E_COEFF_A_BP1, AP_2SMPB02E_COEFF_S_BP1, rbuf, 6);
114-
ap_2smpb02e_setting._B11 = ap_2smpb02e_conv16_dbl(
115-
AP_2SMPB02E_COEFF_A_B11, AP_2SMPB02E_COEFF_S_B11, rbuf, 8);
116-
ap_2smpb02e_setting._BP2 = ap_2smpb02e_conv16_dbl(
117-
AP_2SMPB02E_COEFF_A_BP2, AP_2SMPB02E_COEFF_S_BP2, rbuf, 10);
118-
ap_2smpb02e_setting._B12 = ap_2smpb02e_conv16_dbl(
119-
AP_2SMPB02E_COEFF_A_B12, AP_2SMPB02E_COEFF_S_B12, rbuf, 12);
120-
ap_2smpb02e_setting._B21 = ap_2smpb02e_conv16_dbl(
121-
AP_2SMPB02E_COEFF_A_B21, AP_2SMPB02E_COEFF_S_B21, rbuf, 14);
122-
ap_2smpb02e_setting._BP3 = ap_2smpb02e_conv16_dbl(
123-
AP_2SMPB02E_COEFF_A_BP3, AP_2SMPB02E_COEFF_S_BP3, rbuf, 16);
108+
baro_2smpb02e_setting._B00 = baro_2smpb02e_conv20q4_dbl(rbuf, ex, 0);
109+
baro_2smpb02e_setting._BT1 = baro_2smpb02e_conv16_dbl(
110+
BARO_2SMPB02E_COEFF_A_BT1, BARO_2SMPB02E_COEFF_S_BT1, rbuf, 2);
111+
baro_2smpb02e_setting._BT2 = baro_2smpb02e_conv16_dbl(
112+
BARO_2SMPB02E_COEFF_A_BT2, BARO_2SMPB02E_COEFF_S_BT2, rbuf, 4);
113+
baro_2smpb02e_setting._BP1 = baro_2smpb02e_conv16_dbl(
114+
BARO_2SMPB02E_COEFF_A_BP1, BARO_2SMPB02E_COEFF_S_BP1, rbuf, 6);
115+
baro_2smpb02e_setting._B11 = baro_2smpb02e_conv16_dbl(
116+
BARO_2SMPB02E_COEFF_A_B11, BARO_2SMPB02E_COEFF_S_B11, rbuf, 8);
117+
baro_2smpb02e_setting._BP2 = baro_2smpb02e_conv16_dbl(
118+
BARO_2SMPB02E_COEFF_A_BP2, BARO_2SMPB02E_COEFF_S_BP2, rbuf, 10);
119+
baro_2smpb02e_setting._B12 = baro_2smpb02e_conv16_dbl(
120+
BARO_2SMPB02E_COEFF_A_B12, BARO_2SMPB02E_COEFF_S_B12, rbuf, 12);
121+
baro_2smpb02e_setting._B21 = baro_2smpb02e_conv16_dbl(
122+
BARO_2SMPB02E_COEFF_A_B21, BARO_2SMPB02E_COEFF_S_B21, rbuf, 14);
123+
baro_2smpb02e_setting._BP3 = baro_2smpb02e_conv16_dbl(
124+
BARO_2SMPB02E_COEFF_A_BP3, BARO_2SMPB02E_COEFF_S_BP3, rbuf, 16);
124125

125126
// temperature parameters
126127
ex = (rbuf[24] & 0x0f);
127-
ap_2smpb02e_setting._A0 = ap_2smpb02e_conv20q4_dbl(rbuf, ex, 18);
128-
ap_2smpb02e_setting._A1 = ap_2smpb02e_conv16_dbl(
129-
AP_2SMPB02E_COEFF_A_A1, AP_2SMPB02E_COEFF_S_A1, rbuf, 20);
130-
ap_2smpb02e_setting._A2 = ap_2smpb02e_conv16_dbl(
131-
AP_2SMPB02E_COEFF_A_A2, AP_2SMPB02E_COEFF_S_A2, rbuf, 22);
128+
baro_2smpb02e_setting._A0 = baro_2smpb02e_conv20q4_dbl(rbuf, ex, 18);
129+
baro_2smpb02e_setting._A1 = baro_2smpb02e_conv16_dbl(
130+
BARO_2SMPB02E_COEFF_A_A1, BARO_2SMPB02E_COEFF_S_A1, rbuf, 20);
131+
baro_2smpb02e_setting._A2 = baro_2smpb02e_conv16_dbl(
132+
BARO_2SMPB02E_COEFF_A_A2, BARO_2SMPB02E_COEFF_S_A2, rbuf, 22);
132133

133134
// 3. setup a sensor at 125msec sampling and 32-IIR filter.
134-
rbuf[0] = AP_2SMPB02E_VAL_IOSETUP_STANDBY_0125MS;
135-
i2c_write_reg8(AP_2SMPB02E_ADDRESS, AP_2SMPB02E_REGI2C_IO_SETUP,
135+
rbuf[0] = BARO_2SMPB02E_VAL_IOSETUP_STANDBY_0125MS;
136+
i2c_write_reg8(BARO_2SMPB02E_ADDRESS, BARO_2SMPB02E_REGI2C_IO_SETUP,
136137
rbuf, sizeof(rbuf));
137138

138-
rbuf[0] = AP_2SMPB02E_VAL_IIR_32TIMES;
139-
i2c_write_reg8(AP_2SMPB02E_ADDRESS, AP_2SMPB02E_REGI2C_IIR,
139+
rbuf[0] = BARO_2SMPB02E_VAL_IIR_32TIMES;
140+
i2c_write_reg8(BARO_2SMPB02E_ADDRESS, BARO_2SMPB02E_REGI2C_IIR,
140141
rbuf, sizeof(rbuf));
141142

142143
// then, start to measurements.
143-
result = ap_2smpb02e_trigger_measurement(
144-
AP_2SMPB02E_VAL_MEASMODE_ULTRAHIGH);
144+
result = baro_2smpb02e_trigger_measurement(
145+
BARO_2SMPB02E_VAL_MEASMODE_ULTRAHIGH);
145146
if (result) {
146-
ap_halt("failed to wake up 2SMPB-02E sensor, halted...");
147+
baro_halt("failed to wake up 2SMPB-02E sensor, halted...");
147148
}
149+
return false;
148150
}
149151

150-
/** <!-- ap_2smpb02e_conv16_dbl {{{1 --> convert bytes buffer to double.
152+
/** <!-- baro_2smpb02e_conv16_dbl {{{1 --> convert bytes buffer to double.
151153
* bytes buffer format is a signed-16bit Big-Endian.
152154
*/
153-
static double ap_2smpb02e_conv16_dbl(double a, double s,
154-
uint8_t* buf, int offset) {
155+
static double baro_2smpb02e_conv16_dbl(double a, double s,
156+
uint8_t* buf, int offset) {
155157
uint16_t val;
156158
int16_t ret;
157159

@@ -165,7 +167,7 @@ static double ap_2smpb02e_conv16_dbl(double a, double s,
165167
return a + (double)ret * s / 32767.0;
166168
}
167169

168-
/** <!-- ap_2smpb02e_conv20q4_dbl {{{1 --> convert bytes buffer to double.
170+
/** <!-- baro_2smpb02e_conv20q4_dbl {{{1 --> convert bytes buffer to double.
169171
* bytes buffer format is signed 20Q4, from -32768.0 to 32767.9375
170172
*
171173
* ### bit field of 20Q4
@@ -177,7 +179,8 @@ static double ap_2smpb02e_conv16_dbl(double a, double s,
177179
* +-- Decimal point
178180
* ```
179181
*/
180-
static double ap_2smpb02e_conv20q4_dbl(uint8_t* buf, uint8_t ex, int offset) {
182+
static double baro_2smpb02e_conv20q4_dbl(uint8_t* buf,
183+
uint8_t ex, int offset) {
181184
int32_t ret;
182185
uint32_t val;
183186

@@ -190,43 +193,44 @@ static double ap_2smpb02e_conv20q4_dbl(uint8_t* buf, uint8_t ex, int offset) {
190193
return (double)ret / 16.0;
191194
}
192195

193-
/** <!-- ap_2smpb02e_trigger_measurement {{{1 --> start the sensor
196+
/** <!-- baro_2smpb02e_trigger_measurement {{{1 --> start the sensor
194197
*/
195-
static bool ap_2smpb02e_trigger_measurement(uint8_t mode) {
196-
uint8_t wbuf[1] = {mode | AP_2SMPB02E_VAL_POWERMODE_NORMAL};
198+
static bool baro_2smpb02e_trigger_measurement(uint8_t mode) {
199+
uint8_t wbuf[1] = {
200+
(uint8_t)(mode | BARO_2SMPB02E_VAL_POWERMODE_NORMAL)};
197201

198-
i2c_write_reg8(AP_2SMPB02E_ADDRESS, AP_2SMPB02E_REGI2C_CTRL_MEAS,
202+
i2c_write_reg8(BARO_2SMPB02E_ADDRESS, BARO_2SMPB02E_REGI2C_CTRL_MEAS,
199203
wbuf, sizeof(wbuf));
200204
return false;
201205
}
202206

203-
/** <!-- ap_2smpb02e_read {{{1 --> read the sensor digit and convert to
207+
/** <!-- baro_2smpb02e_read {{{1 --> read the sensor digit and convert to
204208
* physical values.
205209
*/
206-
bool ap_2smpb02e_read(uint32_t* pres, int16_t* temp,
210+
int baro_2smpb02e_read(uint32_t* pres, int16_t* temp,
207211
uint32_t* dp, uint32_t* dt) {
208212
bool ret;
209213
uint8_t rbuf[6] = {0};
210214
uint32_t rawtemp, rawpres;
211215

212216
ret = i2c_read_reg8(
213-
AP_2SMPB02E_ADDRESS, AP_2SMPB02E_REGI2C_PRES_TXD2,
217+
BARO_2SMPB02E_ADDRESS, BARO_2SMPB02E_REGI2C_PRES_TXD2,
214218
rbuf, sizeof(rbuf));
215219
if (ret) {
216-
return true;
220+
return 1;
217221
}
218222

219223
*dp = rawpres = conv8s_s24_be(rbuf[0], rbuf[1], rbuf[2]);
220224
*dt = rawtemp = conv8s_s24_be(rbuf[3], rbuf[4], rbuf[5]);
221-
return ap_2smpb02e_output_compensation(rawtemp, rawpres, pres, temp);
225+
return baro_2smpb02e_output_compensation(rawtemp, rawpres, pres, temp);
222226
}
223227

224-
/** <!-- ap_2smpb02e_output_compensation {{{1 --> compensate sensors
228+
/** <!-- baro_2smpb02e_output_compensation {{{1 --> compensate sensors
225229
* raw output digits to [Pa] and [degC].
226230
*/
227-
bool ap_2smpb02e_output_compensation(uint32_t raw_temp_val,
228-
uint32_t raw_press_val,
229-
uint32_t* pres, int16_t* temp
231+
bool baro_2smpb02e_output_compensation(uint32_t raw_temp_val,
232+
uint32_t raw_press_val,
233+
uint32_t* pres, int16_t* temp
230234
) {
231235
double Tr, Po;
232236
double Dt, Dp;
@@ -235,7 +239,7 @@ bool ap_2smpb02e_output_compensation(uint32_t raw_temp_val,
235239
Dp = (int32_t)raw_press_val - 0x800000;
236240

237241
// temperature compensation
238-
ap_2smpb02e_setting_t* c = &ap_2smpb02e_setting;
242+
baro_2smpb02e_setting_t* c = &baro_2smpb02e_setting;
239243
Tr = c->_A0 + c->_A1 * Dt + c->_A2 * (Dt * Dt);
240244

241245
// barometer compensation
@@ -269,7 +273,7 @@ void setup() {
269273
Wire.begin(); // master
270274

271275
Serial.println("sensor: barometer");
272-
ap_2smpb02e_setup();
276+
baro_2smpb02e_setup();
273277
delay(32);
274278
}
275279

@@ -280,23 +284,24 @@ void setup() {
280284
*/
281285
void loop() {
282286
static bool blink = false;
283-
uint32_t pres, pres_dp, pres_dt;
284-
int16_t temp_pres;
287+
uint32_t pres, dp, dt;
288+
int16_t temp;
285289

286290
blink = !blink;
287291
digitalWrite(GPIO_LED_R_PIN, blink ? HIGH: LOW);
288292
digitalWrite(GPIO_LED_G_PIN, blink ? HIGH: LOW);
289293
digitalWrite(GPIO_LED_B_PIN, blink ? HIGH: LOW);
290294
delay(900);
291-
ap_2smpb02e_read(&pres, &temp_pres, &pres_dp, &pres_dt);
295+
int ret = baro_2smpb02e_read(&pres, &temp, &dp, &dt);
292296
Serial.print("sensor output:");
293-
Serial.print(pres);
294-
Serial.print(",");
295-
Serial.print(temp_pres);
296-
Serial.print(",");
297-
Serial.print(pres_dp);
298-
Serial.print(",");
299-
Serial.print(pres_dt);
300-
Serial.println("");
297+
Serial.print(pres / 10.0);
298+
Serial.print("[Pa], ");
299+
Serial.print(temp / 100.0);
300+
Serial.print("[degC], ");
301+
Serial.print(dp);
302+
Serial.print("[],");
303+
Serial.print(dt);
304+
Serial.print("[], retun code:");
305+
Serial.println(ret);
301306
}
302307
// vi: ft=arduino:fdm=marker:et:sw=4:tw=80

0 commit comments

Comments
 (0)