Firmware SDK
twr_flood_detector.c
1 #include <twr_flood_detector.h>
2 
3 #define _TWR_FLOOD_DETECTOR_DELAY_RUN 50
4 
5 static void _twr_flood_task_interval(void *param);
6 
7 static void _twr_flood_task_measure(void *param);
8 
10 {
11  memset(self, 0, sizeof(&self));
12  self->_type = type;
13  self->_task_id_interval = twr_scheduler_register(_twr_flood_task_interval, self, TWR_TICK_INFINITY);
14  self->_task_id_measure = twr_scheduler_register(_twr_flood_task_measure, self, _TWR_FLOOD_DETECTOR_DELAY_RUN);
15 }
16 
17 void twr_flood_detector_set_event_handler(twr_flood_detector_t *self, void (*event_handler)(twr_flood_detector_t *, twr_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 == TWR_TICK_INFINITY)
28  {
29  twr_scheduler_plan_absolute(self->_task_id_interval, TWR_TICK_INFINITY);
30  }
31  else
32  {
33  twr_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  twr_scheduler_plan_now(self->_task_id_measure);
47 
48  return true;
49 }
50 
52 {
53  return self->_alarm;
54 }
55 
56 static void _twr_flood_task_interval(void *param)
57 {
59 
61 
62  twr_scheduler_plan_current_relative(self->_update_interval);
63 }
64 
65 static void _twr_flood_task_measure(void *param)
66 {
68 
69 start:
70 
71  switch (self->_state)
72  {
73  case TWR_FLOOD_DETECTOR_STATE_ERROR:
74  {
75  self->_measurement_active = false;
76 
77  if (self->_event_handler != NULL)
78  {
79  self->_event_handler(self, TWR_FLOOD_DETECTOR_EVENT_ERROR, self->_event_param);
80  }
81 
82  self->_state = TWR_FLOOD_DETECTOR_STATE_INITIALIZE;
83 
84  return;
85  }
86  case TWR_FLOOD_DETECTOR_STATE_INITIALIZE:
87  {
88  self->_state = TWR_FLOOD_DETECTOR_STATE_ERROR;
89 
90  switch (self->_type)
91  {
92  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_A:
93  {
95  {
96  goto start;
97  }
100  break;
101  }
102  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_B:
103  {
104  if (!twr_module_sensor_init())
105  {
106  goto start;
107  }
110  break;
111  }
112  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_C:
113  {
114  if (!twr_module_sensor_init())
115  {
116  goto start;
117  }
121  break;
122  }
123  default:
124  {
125  goto start;
126  }
127  }
128 
129  self->_state = TWR_FLOOD_DETECTOR_STATE_READY;
130 
131  if (self->_measurement_active)
132  {
134  }
135  return;
136  }
137  case TWR_FLOOD_DETECTOR_STATE_READY:
138  {
139  self->_state = TWR_FLOOD_DETECTOR_STATE_ERROR;
140 
141  switch (self->_type)
142  {
143  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_A:
144  {
146  {
147  goto start;
148  }
150  break;
151  }
152  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_B:
153  {
155  {
156  goto start;
157  }
159  break;
160  }
161  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_C:
162  {
164  break;
165  }
166  default:
167  {
168  goto start;
169  }
170  }
171 
172  self->_state = TWR_FLOOD_DETECTOR_STATE_MEASURE;
173  return;
174  }
175  case TWR_FLOOD_DETECTOR_STATE_MEASURE:
176  {
177  self->_state = TWR_FLOOD_DETECTOR_STATE_ERROR;
178 
179  switch (self->_type)
180  {
181  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_A:
182  {
183  self->_alarm = twr_gpio_get_input(TWR_GPIO_P4) == 1;
184 
186  {
187  goto start;
188  }
189  break;
190  }
191  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_B:
192  {
193  self->_alarm = twr_gpio_get_input(TWR_GPIO_P5) == 1;
194 
196  {
197  goto start;
198  }
199  break;
200  }
201  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_C:
202  {
203  self->_alarm = twr_gpio_get_input(TWR_GPIO_P7) == 1;
204  break;
205  }
206  default:
207  {
208  goto start;
209  }
210  }
211  self->_measurement_active = false;
212 
213  if (self->_event_handler != NULL)
214  {
215  self->_event_handler(self, TWR_FLOOD_DETECTOR_EVENT_UPDATE, self->_event_param);
216  }
217 
218  self->_state = TWR_FLOOD_DETECTOR_STATE_READY;
219 
220  }
221  default:
222  break;
223  }
224 }
struct twr_flood_detector_t twr_flood_detector_t
Instance.
void twr_flood_detector_init(twr_flood_detector_t *self, twr_flood_detector_type_t type)
Initialize flood detector.
bool twr_flood_detector_is_alarm(twr_flood_detector_t *self)
Is alarm.
bool twr_flood_detector_measure(twr_flood_detector_t *self)
Start measurement manually.
twr_flood_detector_type_t
Type sensor.
twr_flood_detector_event_t
Callback events.
void twr_flood_detector_set_update_interval(twr_flood_detector_t *self, twr_tick_t interval)
Set measurement interval.
void twr_flood_detector_set_event_handler(twr_flood_detector_t *self, void(*event_handler)(twr_flood_detector_t *, twr_flood_detector_event_t, void *), void *event_param)
Set callback function.
void twr_gpio_init(twr_gpio_channel_t channel)
Initialize GPIO channel.
Definition: twr_gpio.c:325
int twr_gpio_get_input(twr_gpio_channel_t channel)
Get input state for GPIO channel.
Definition: twr_gpio.c:465
void twr_gpio_set_mode(twr_gpio_channel_t channel, twr_gpio_mode_t mode)
Set mode of operation for GPIO channel.
Definition: twr_gpio.c:367
@ TWR_GPIO_MODE_INPUT
GPIO channel operates as input.
Definition: twr_gpio.h:105
@ TWR_GPIO_P5
GPIO channel P5, A5, DAC1.
Definition: twr_gpio.h:30
@ TWR_GPIO_P4
GPIO channel P4, A4, DAC0.
Definition: twr_gpio.h:27
@ TWR_GPIO_P7
GPIO channel P7, A6.
Definition: twr_gpio.h:36
bool twr_module_sensor_set_vdd(bool on)
Set VDD On / Off.
bool twr_module_sensor_init(void)
Initialize Sensor Module.
bool twr_module_sensor_set_pull(twr_module_sensor_channel_t channel, twr_module_sensor_pull_t pull)
Set pull of Sensor Module channel.
@ TWR_MODULE_SENSOR_PULL_UP_4K7
Channel has pull-up 4k7.
@ TWR_MODULE_SENSOR_PULL_NONE
Channel has no pull.
@ TWR_MODULE_SENSOR_CHANNEL_B
Channel B.
@ TWR_MODULE_SENSOR_CHANNEL_A
Channel A.
void twr_scheduler_plan_current_from_now(twr_tick_t tick)
Schedule current task to tick relative from now.
void twr_scheduler_plan_current_relative(twr_tick_t tick)
Schedule current task to tick relative from current spin.
void twr_scheduler_plan_absolute(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to absolute tick.
void twr_scheduler_plan_now(twr_scheduler_task_id_t task_id)
Schedule specified task for immediate execution.
void twr_scheduler_plan_current_now(void)
Schedule current task for immediate execution.
void twr_scheduler_plan_relative(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to tick relative from current spin.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
Definition: twr_scheduler.c:53
#define TWR_TICK_INFINITY
Maximum timestamp value.
Definition: twr_tick.h:12
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16