Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_flood_detector.c
1 #include <bc_flood_detector.h>
2 
3 #define _BC_FLOOD_DETECTOR_DELAY_RUN 50
4 
5 static void _bc_flood_task_interval(void *param);
6 
7 static void _bc_flood_task_measure(void *param);
8 
10 {
11  memset(self, 0, sizeof(&self));
12  self->_type = type;
13  self->_task_id_interval = bc_scheduler_register(_bc_flood_task_interval, self, BC_TICK_INFINITY);
14  self->_task_id_measure = bc_scheduler_register(_bc_flood_task_measure, self, _BC_FLOOD_DETECTOR_DELAY_RUN);
15 }
16 
17 void bc_flood_detector_set_event_handler(bc_flood_detector_t *self, void (*event_handler)(bc_flood_detector_t *, bc_flood_detector_event_t, void*), void *event_param)
18 {
19  self->_event_handler = event_handler;
20  self->_event_param = event_param;
21 }
22 
24 {
25  self->_update_interval = interval;
26 
27  if (self->_update_interval == BC_TICK_INFINITY)
28  {
29  bc_scheduler_plan_absolute(self->_task_id_interval, BC_TICK_INFINITY);
30  }
31  else
32  {
33  bc_scheduler_plan_relative(self->_task_id_interval, self->_update_interval);
34  }
35 }
36 
38 {
39  if (self->_measurement_active)
40  {
41  return false;
42  }
43 
44  self->_measurement_active = true;
45 
46  bc_scheduler_plan_now(self->_task_id_measure);
47 
48  return true;
49 }
50 
52 {
53  return self->_alarm;
54 }
55 
56 static void _bc_flood_task_interval(void *param)
57 {
58  bc_flood_detector_t *self = (bc_flood_detector_t *) param;
59 
61 
62  bc_scheduler_plan_current_relative(self->_update_interval);
63 }
64 
65 static void _bc_flood_task_measure(void *param)
66 {
67  bc_flood_detector_t *self = (bc_flood_detector_t *) param;
68 
69 start:
70 
71  switch (self->_state)
72  {
73  case BC_FLOOD_DETECTOR_STATE_ERROR:
74  {
75  self->_measurement_active = false;
76 
77  if (self->_event_handler != NULL)
78  {
79  self->_event_handler(self, BC_FLOOD_DETECTOR_EVENT_ERROR, self->_event_param);
80  }
81 
82  self->_state = BC_FLOOD_DETECTOR_STATE_INITIALIZE;
83 
84  return;
85  }
86  case BC_FLOOD_DETECTOR_STATE_INITIALIZE:
87  {
88  self->_state = BC_FLOOD_DETECTOR_STATE_ERROR;
89 
90  switch (self->_type)
91  {
92  case BC_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_A:
93  {
94  if (!bc_module_sensor_init())
95  {
96  goto start;
97  }
100  break;
101  }
102  case BC_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_B:
103  {
104  if (!bc_module_sensor_init())
105  {
106  goto start;
107  }
110  break;
111  }
112  default:
113  {
114  goto start;
115  }
116  }
117 
118  self->_state = BC_FLOOD_DETECTOR_STATE_READY;
119 
120  if (self->_measurement_active)
121  {
123  }
124  return;
125  }
126  case BC_FLOOD_DETECTOR_STATE_READY:
127  {
128  self->_state = BC_FLOOD_DETECTOR_STATE_ERROR;
129 
130  switch (self->_type)
131  {
132  case BC_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_A:
133  {
135  {
136  goto start;
137  }
139  break;
140  }
141  case BC_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_B:
142  {
144  {
145  goto start;
146  }
148  break;
149  }
150  default:
151  {
152  goto start;
153  }
154  }
155 
156  self->_state = BC_FLOOD_DETECTOR_STATE_MEASURE;
157  return;
158  }
159  case BC_FLOOD_DETECTOR_STATE_MEASURE:
160  {
161  self->_state = BC_FLOOD_DETECTOR_STATE_ERROR;
162 
163  switch (self->_type)
164  {
165  case BC_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_A:
166  {
167  self->_alarm = bc_gpio_get_input(BC_GPIO_P4) == 1;
168 
170  {
171  goto start;
172  }
173  break;
174  }
175  case BC_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_B:
176  {
177  self->_alarm = bc_gpio_get_input(BC_GPIO_P5) == 1;
178 
180  {
181  goto start;
182  }
183  break;
184  }
185  default:
186  {
187  goto start;
188  }
189  }
190  self->_measurement_active = false;
191 
192  if (self->_event_handler != NULL)
193  {
194  self->_event_handler(self, BC_FLOOD_DETECTOR_EVENT_UPDATE, self->_event_param);
195  }
196 
197  self->_state = BC_FLOOD_DETECTOR_STATE_READY;
198 
199  }
200  default:
201  break;
202  }
203 }
uint64_t bc_tick_t
Timestamp data type.
Definition: bc_tick.h:16
GPIO channel P5.
Definition: bc_gpio.h:30
Channel has pull-up 4k7.
bool bc_module_sensor_set_pull(bc_module_sensor_channel_t channel, bc_module_sensor_pull_t pull)
Set pull of Sensor Module channel.
void bc_scheduler_plan_absolute(bc_scheduler_task_id_t task_id, bc_tick_t tick)
Schedule specified task to absolute tick.
Definition: bc_scheduler.c:124
bool bc_flood_detector_is_alarm(bc_flood_detector_t *self)
Is alarm.
Channel has no pull.
bc_flood_detector_event_t
Callback events.
bc_scheduler_task_id_t bc_scheduler_register(void(*task)(void *), void *param, bc_tick_t tick)
Register task in scheduler.
Definition: bc_scheduler.c:56
void bc_scheduler_plan_now(bc_scheduler_task_id_t task_id)
Schedule specified task for immediate execution.
Definition: bc_scheduler.c:119
void bc_gpio_set_mode(bc_gpio_channel_t channel, bc_gpio_mode_t mode)
Set mode of operation for GPIO channel.
Definition: bc_gpio.c:340
void bc_scheduler_plan_current_relative(bc_tick_t tick)
Schedule current task to tick relative from current spin.
Definition: bc_scheduler.c:149
void bc_flood_detector_set_update_interval(bc_flood_detector_t *self, bc_tick_t interval)
Set measurement interval.
int bc_gpio_get_input(bc_gpio_channel_t channel)
Get input state for GPIO channel.
Definition: bc_gpio.c:436
GPIO channel operates as input.
Definition: bc_gpio.h:99
void bc_scheduler_plan_relative(bc_scheduler_task_id_t task_id, bc_tick_t tick)
Schedule specified task to tick relative from current spin.
Definition: bc_scheduler.c:129
bc_flood_detector_type_t
Type sensor.
struct bc_flood_detector_t bc_flood_detector_t
Instance.
void bc_scheduler_plan_current_from_now(bc_tick_t tick)
Schedule current task to tick relative from now.
Definition: bc_scheduler.c:154
#define BC_TICK_INFINITY
Maximum timestamp value.
Definition: bc_tick.h:12
bool bc_flood_detector_measure(bc_flood_detector_t *self)
Start measurement manually.
void bc_flood_detector_set_event_handler(bc_flood_detector_t *self, void(*event_handler)(bc_flood_detector_t *, bc_flood_detector_event_t, void *), void *event_param)
Set callback function.
bool bc_module_sensor_init(void)
Initialize Sensor Module.
GPIO channel P4.
Definition: bc_gpio.h:27
void bc_flood_detector_init(bc_flood_detector_t *self, bc_flood_detector_type_t type)
Initialize flood detector.
void bc_scheduler_plan_current_now(void)
Schedule current task for immediate execution.
Definition: bc_scheduler.c:139
void bc_gpio_init(bc_gpio_channel_t channel)
Initialize GPIO channel.
Definition: bc_gpio.c:301