@@ -39,8 +39,6 @@ BUILD_ASSERT(CONFIG_APP_ENVIRONMENTAL_WATCHDOG_TIMEOUT_SECONDS >
3939 CONFIG_APP_ENVIRONMENTAL_MSG_PROCESSING_TIMEOUT_SECONDS ,
4040 "Watchdog timeout must be greater than maximum message processing time" );
4141
42- static const struct device * const sensor_dev = DEVICE_DT_GET (DT_NODELABEL (bme680 ));
43-
4442/* State machine */
4543
4644/* Defininig the module states.
@@ -64,14 +62,15 @@ struct environmental_state {
6462 /* Buffer for last zbus message */
6563 uint8_t msg_buf [MAX_MSG_SIZE ];
6664
67- double temperature ;
65+ /* Pointer to the BME680 sensor device */
66+ const struct device * const bme680 ;
6867
68+ /* Sensor values */
69+ double temperature ;
6970 double pressure ;
70-
7171 double humidity ;
7272};
7373
74-
7574/* Forward declarations of state handlers */
7675static void state_running_run (void * o );
7776
@@ -80,21 +79,40 @@ static const struct smf_state states[] = {
8079 SMF_CREATE_STATE (NULL , state_running_run , NULL , NULL , NULL ),
8180};
8281
83- static void sample ( void )
82+ static void sample_sensors ( const struct device * const bme680 )
8483{
8584 int err ;
8685 struct sensor_value temp = { 0 };
8786 struct sensor_value press = { 0 };
8887 struct sensor_value humidity = { 0 };
8988
90- err = sensor_sample_fetch (sensor_dev );
91- __ASSERT_NO_MSG (err == 0 );
92- err = sensor_channel_get (sensor_dev , SENSOR_CHAN_AMBIENT_TEMP , & temp );
93- __ASSERT_NO_MSG (err == 0 );
94- err = sensor_channel_get (sensor_dev , SENSOR_CHAN_PRESS , & press );
95- __ASSERT_NO_MSG (err == 0 );
96- err = sensor_channel_get (sensor_dev , SENSOR_CHAN_HUMIDITY , & humidity );
97- __ASSERT_NO_MSG (err == 0 );
89+ err = sensor_sample_fetch (bme680 );
90+ if (err ) {
91+ LOG_ERR ("sensor_sample_fetch, error: %d" , err );
92+ SEND_FATAL_ERROR ();
93+ return ;
94+ }
95+
96+ err = sensor_channel_get (bme680 , SENSOR_CHAN_AMBIENT_TEMP , & temp );
97+ if (err ) {
98+ LOG_ERR ("sensor_channel_get, error: %d" , err );
99+ SEND_FATAL_ERROR ();
100+ return ;
101+ }
102+
103+ err = sensor_channel_get (bme680 , SENSOR_CHAN_PRESS , & press );
104+ if (err ) {
105+ LOG_ERR ("sensor_channel_get, error: %d" , err );
106+ SEND_FATAL_ERROR ();
107+ return ;
108+ }
109+
110+ err = sensor_channel_get (bme680 , SENSOR_CHAN_HUMIDITY , & humidity );
111+ if (err ) {
112+ LOG_ERR ("sensor_channel_get, error: %d" , err );
113+ SEND_FATAL_ERROR ();
114+ return ;
115+ }
98116
99117 struct environmental_msg msg = {
100118 .type = ENVIRONMENTAL_SENSOR_SAMPLE_RESPONSE ,
@@ -134,7 +152,7 @@ static void state_running_run(void *o)
134152
135153 if (msg .type == ENVIRONMENTAL_SENSOR_SAMPLE_REQUEST ) {
136154 LOG_DBG ("Environmental values sample request received, getting data" );
137- sample ( );
155+ sample_sensors ( state_object -> bme680 );
138156 }
139157 }
140158}
@@ -150,7 +168,9 @@ static void environmental_task(void)
150168 const uint32_t execution_time_ms =
151169 (CONFIG_APP_ENVIRONMENTAL_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC );
152170 const k_timeout_t zbus_wait_ms = K_MSEC (wdt_timeout_ms - execution_time_ms );
153- struct environmental_state environmental_state = { 0 };
171+ struct environmental_state environmental_state = {
172+ .bme680 = DEVICE_DT_GET (DT_NODELABEL (bme680 )),
173+ };
154174
155175 LOG_DBG ("Environmental module task started" );
156176
0 commit comments