@@ -54,50 +54,8 @@ static int bme280_decoder_get_size_info(struct sensor_chan_spec chan_spec, size_
54
54
}
55
55
}
56
56
57
- #define BME280_HUM_SHIFT (22)
58
- #define BME280_PRESS_SHIFT (24)
59
- #define BME280_TEMP_SHIFT (24)
60
-
61
- static void bme280_convert_double_to_q31 (double reading , int32_t shift , q31_t * out )
62
- {
63
- reading = reading * pow (2 , 31 - shift );
64
-
65
- int64_t reading_round = (reading < 0 ) ? (reading - 0.5 ) : (reading + 0.5 );
66
- int32_t reading_q31 = CLAMP (reading_round , INT32_MIN , INT32_MAX );
67
-
68
- if (reading_q31 < 0 ) {
69
- reading_q31 = abs (reading_q31 );
70
- reading_q31 = ~reading_q31 ;
71
- reading_q31 ++ ;
72
- }
73
-
74
- * out = reading_q31 ;
75
- }
76
-
77
- /* Refer to bme280.c bme280_channel_get() */
78
- static void bme280_convert_signed_temp_raw_to_q31 (int32_t reading , q31_t * out )
79
- {
80
- double temp_double = reading / 100.0 ;
81
-
82
- bme280_convert_double_to_q31 (temp_double , BME280_TEMP_SHIFT , out );
83
- }
84
-
85
- static void bme280_convert_unsigned_pressure_raw_to_q31 (uint32_t reading , q31_t * out )
86
- {
87
- double press_double = (reading / 256.0 ) / 1000.0 ; /* Pa -> hPa */
88
-
89
- bme280_convert_double_to_q31 (press_double , BME280_PRESS_SHIFT , out );
90
- }
91
-
92
- static void bme280_convert_unsigned_humidity_raw_to_q31 (uint32_t reading , q31_t * out )
93
- {
94
- double hum_double = (reading / 1024.0 );
95
-
96
- bme280_convert_double_to_q31 (hum_double , BME280_HUM_SHIFT , out );
97
- }
98
-
99
57
static int bme280_decoder_decode (const uint8_t * buffer , struct sensor_chan_spec chan_spec ,
100
- uint32_t * fit , uint16_t max_count , void * data_out )
58
+ uint32_t * fit , uint16_t max_count , void * data_out )
101
59
{
102
60
const struct bme280_encoded_data * edata = (const struct bme280_encoded_data * )buffer ;
103
61
@@ -113,26 +71,33 @@ static int bme280_decoder_decode(const uint8_t *buffer, struct sensor_chan_spec
113
71
switch (chan_spec .chan_type ) {
114
72
case SENSOR_CHAN_AMBIENT_TEMP :
115
73
if (edata -> has_temp ) {
116
- bme280_convert_signed_temp_raw_to_q31 (edata -> reading .comp_temp ,
117
- & out -> readings [0 ].temperature );
74
+ int32_t readq = edata -> reading .comp_temp * pow (2 , 31 - BME280_TEMP_SHIFT );
75
+ int32_t convq = BME280_TEMP_CONV * pow (2 , 31 - BME280_TEMP_SHIFT );
76
+
77
+ out -> readings [0 ].temperature =
78
+ (int32_t )((((int64_t )readq ) << (31 - BME280_TEMP_SHIFT )) /
79
+ ((int64_t )convq ));
118
80
out -> shift = BME280_TEMP_SHIFT ;
119
81
} else {
120
82
return - ENODATA ;
121
83
}
122
84
break ;
123
85
case SENSOR_CHAN_PRESS :
124
86
if (edata -> has_press ) {
125
- bme280_convert_unsigned_pressure_raw_to_q31 (edata -> reading .comp_press ,
126
- & out -> readings [0 ].pressure );
87
+ int32_t readq = edata -> reading .comp_press ;
88
+ int32_t convq = BME280_PRESS_CONV_KPA * pow (2 , 31 - BME280_PRESS_SHIFT );
89
+
90
+ out -> readings [0 ].pressure =
91
+ (int32_t )((((int64_t )readq ) << (31 - BME280_PRESS_SHIFT )) /
92
+ ((int64_t )convq ));
127
93
out -> shift = BME280_PRESS_SHIFT ;
128
94
} else {
129
95
return - ENODATA ;
130
96
}
131
97
break ;
132
98
case SENSOR_CHAN_HUMIDITY :
133
99
if (edata -> has_humidity ) {
134
- bme280_convert_unsigned_humidity_raw_to_q31 (edata -> reading .comp_humidity ,
135
- & out -> readings [0 ].humidity );
100
+ out -> readings [0 ].humidity = edata -> reading .comp_humidity ;
136
101
out -> shift = BME280_HUM_SHIFT ;
137
102
} else {
138
103
return - ENODATA ;
@@ -147,7 +112,6 @@ static int bme280_decoder_decode(const uint8_t *buffer, struct sensor_chan_spec
147
112
return 1 ;
148
113
}
149
114
150
-
151
115
SENSOR_DECODER_API_DT_DEFINE () = {
152
116
.get_frame_count = bme280_decoder_get_frame_count ,
153
117
.get_size_info = bme280_decoder_get_size_info ,
0 commit comments