@@ -48,7 +48,7 @@ struct ctr_x4_data {
48
48
int16_t adc_buf [1 ];
49
49
ctr_x4_user_cb user_cb ;
50
50
void * user_data ;
51
- bool is_line_present ;
51
+ enum ctr_x4_line_state line_state ;
52
52
};
53
53
54
54
static inline const struct ctr_x4_config * get_config (const struct device * dev )
@@ -138,10 +138,10 @@ static int ctr_x4_get_line_voltage_(const struct device *dev, int *line_voltage_
138
138
return 0 ;
139
139
}
140
140
141
- static int ctr_x4_get_line_present_ (const struct device * dev , bool * is_line_present )
141
+ static int ctr_x4_get_line_state_ (const struct device * dev , enum ctr_x4_line_state * line_state )
142
142
{
143
143
k_mutex_lock (& get_data (dev )-> lock , K_FOREVER );
144
- * is_line_present = get_data (dev )-> is_line_present ;
144
+ * line_state = get_data (dev )-> line_state ;
145
145
k_mutex_unlock (& get_data (dev )-> lock );
146
146
147
147
return 0 ;
@@ -168,19 +168,23 @@ static void work_handler(struct k_work *work)
168
168
}
169
169
170
170
k_mutex_lock (& get_data (dev )-> lock , K_FOREVER );
171
+ enum ctr_x4_line_state line_state = get_data (dev )-> line_state ;
171
172
172
- if (get_data ( dev ) -> is_line_present ) {
173
+ if (line_state == CTR_X4_LINE_STATE_CONNECTED || line_state == CTR_X4_LINE_STATE_UNKNOWN ) {
173
174
if (voltage_mv < get_config (dev )-> line_threshold_min ) {
174
- get_data (dev )-> is_line_present = false ;
175
+ get_data (dev )-> line_state = CTR_X4_LINE_STATE_DISCONNECTED ;
175
176
176
177
if (get_data (dev )-> user_cb ) {
177
178
get_data (dev )-> user_cb (dev , CTR_X4_EVENT_LINE_DISCONNECTED ,
178
179
get_data (dev )-> user_data );
179
180
}
180
181
}
181
- } else {
182
+ }
183
+
184
+ if (line_state == CTR_X4_LINE_STATE_DISCONNECTED ||
185
+ line_state == CTR_X4_LINE_STATE_UNKNOWN ) {
182
186
if (voltage_mv > get_config (dev )-> line_threshold_max ) {
183
- get_data (dev )-> is_line_present = true ;
187
+ get_data (dev )-> line_state = CTR_X4_LINE_STATE_CONNECTED ;
184
188
185
189
if (get_data (dev )-> user_cb ) {
186
190
get_data (dev )-> user_cb (dev , CTR_X4_EVENT_LINE_CONNECTED ,
@@ -248,23 +252,14 @@ static int ctr_x4_init(const struct device *dev)
248
252
k_timer_start (& get_data (dev )-> timer , K_MSEC (get_config (dev )-> line_measurement_interval ),
249
253
K_MSEC (get_config (dev )-> line_measurement_interval ));
250
254
251
- int line_voltage_mv ;
252
- ret = ctr_x4_get_line_voltage (dev , & line_voltage_mv );
253
- if (ret ) {
254
- LOG_ERR ("Call `ctr_x4_get_line_voltage` failed: %d" , ret );
255
- return ret ;
256
- }
257
-
258
- get_data (dev )-> is_line_present = line_voltage_mv > get_config (dev )-> line_threshold_min ;
259
-
260
255
return 0 ;
261
256
}
262
257
263
258
static const struct ctr_x4_driver_api ctr_x4_driver_api = {
264
259
.set_handler = ctr_x4_set_handler_ ,
265
260
.set_output = ctr_x4_set_output_ ,
266
261
.get_line_voltage = ctr_x4_get_line_voltage_ ,
267
- .get_line_present = ctr_x4_get_line_present_ ,
262
+ .get_line_state = ctr_x4_get_line_state_ ,
268
263
};
269
264
270
265
#define CTR_X4_INIT (n ) \
0 commit comments