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  default:
113  {
114  goto start;
115  }
116  }
117 
118  self->_state = TWR_FLOOD_DETECTOR_STATE_READY;
119 
120  if (self->_measurement_active)
121  {
123  }
124  return;
125  }
126  case TWR_FLOOD_DETECTOR_STATE_READY:
127  {
128  self->_state = TWR_FLOOD_DETECTOR_STATE_ERROR;
129 
130  switch (self->_type)
131  {
132  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_A:
133  {
135  {
136  goto start;
137  }
139  break;
140  }
141  case TWR_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 = TWR_FLOOD_DETECTOR_STATE_MEASURE;
157  return;
158  }
159  case TWR_FLOOD_DETECTOR_STATE_MEASURE:
160  {
161  self->_state = TWR_FLOOD_DETECTOR_STATE_ERROR;
162 
163  switch (self->_type)
164  {
165  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_A:
166  {
167  self->_alarm = twr_gpio_get_input(TWR_GPIO_P4) == 1;
168 
170  {
171  goto start;
172  }
173  break;
174  }
175  case TWR_FLOOD_DETECTOR_TYPE_LD_81_SENSOR_MODULE_CHANNEL_B:
176  {
177  self->_alarm = twr_gpio_get_input(TWR_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, TWR_FLOOD_DETECTOR_EVENT_UPDATE, self->_event_param);
195  }
196 
197  self->_state = TWR_FLOOD_DETECTOR_STATE_READY;
198 
199  }
200  default:
201  break;
202  }
203 }
void twr_flood_detector_set_update_interval(twr_flood_detector_t *self, twr_tick_t interval)
Set measurement interval.
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
Channel has pull-up 4k7.
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_flood_detector_type_t
Type sensor.
twr_flood_detector_event_t
Callback events.
void twr_scheduler_plan_current_relative(twr_tick_t tick)
Schedule current task to tick relative from current spin.
int twr_gpio_get_input(twr_gpio_channel_t channel)
Get input state for GPIO channel.
Definition: twr_gpio.c:465
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
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.
void twr_scheduler_plan_current_from_now(twr_tick_t tick)
Schedule current task to tick relative from now.
bool twr_flood_detector_measure(twr_flood_detector_t *self)
Start measurement manually.
void twr_gpio_init(twr_gpio_channel_t channel)
Initialize GPIO channel.
Definition: twr_gpio.c:325
void twr_scheduler_plan_current_now(void)
Schedule current task for immediate execution.
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16
GPIO channel P4, A4, DAC0.
Definition: twr_gpio.h:27
Channel has no pull.
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_flood_detector_init(twr_flood_detector_t *self, twr_flood_detector_type_t type)
Initialize flood detector.
struct twr_flood_detector_t twr_flood_detector_t
Instance.
GPIO channel operates as input.
Definition: twr_gpio.h:105
bool twr_flood_detector_is_alarm(twr_flood_detector_t *self)
Is alarm.
GPIO channel P5, A5, DAC1.
Definition: twr_gpio.h:30
#define TWR_TICK_INFINITY
Maximum timestamp value.
Definition: twr_tick.h:12
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.