]> git.gag.com Git - fw/altos/blob - src/telelco-v0.1/ao_lco.c
altos/telebt-v3.0: Turn red LED off after initialization
[fw/altos] / src / telelco-v0.1 / ao_lco.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_lco.h>
20 #include <ao_event.h>
21 #include <ao_seven_segment.h>
22 #include <ao_quadrature.h>
23 #include <ao_lco_func.h>
24 #include <ao_radio_cmac.h>
25
26 #define DEBUG   1
27
28 #if DEBUG
29 static uint8_t  ao_lco_debug;
30 #define PRINTD(...) do { if (!ao_lco_debug) break; printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
31 #else
32 #define PRINTD(...) 
33 #endif
34
35 #define AO_LCO_PAD_DIGIT        0
36 #define AO_LCO_BOX_DIGIT_1      1
37 #define AO_LCO_BOX_DIGIT_10     2
38
39 static uint8_t  ao_lco_min_box, ao_lco_max_box;
40 static uint8_t  ao_lco_mutex;
41 static uint8_t  ao_lco_pad;
42 static uint8_t  ao_lco_box;
43 static uint8_t  ao_lco_armed;
44 static uint8_t  ao_lco_firing;
45 static uint8_t  ao_lco_valid;
46 static uint8_t  ao_lco_got_channels;
47 static uint16_t ao_lco_tick_offset;
48
49 static struct ao_pad_query      ao_pad_query;
50
51 static void
52 ao_lco_set_pad(void)
53 {
54         ao_seven_segment_set(AO_LCO_PAD_DIGIT, ao_lco_pad + 1);
55 }
56
57 static void
58 ao_lco_set_box(void)
59 {
60         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ao_lco_box % 10);
61         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, ao_lco_box / 10);
62 }
63
64 #define MASK_SIZE(n)    (((n) + 7) >> 3)
65 #define MASK_ID(n)      ((n) >> 3)
66 #define MASK_SHIFT(n)   ((n) & 7)
67
68 static uint8_t  ao_lco_box_mask[MASK_SIZE(AO_PAD_MAX_BOXES)];
69
70 static uint8_t
71 ao_lco_box_present(uint8_t box)
72 {
73         if (box >= AO_PAD_MAX_BOXES)
74                 return 0;
75         return (ao_lco_box_mask[MASK_ID(box)] >> MASK_SHIFT(box)) & 1;
76 }
77
78 static uint8_t
79 ao_lco_pad_present(uint8_t pad)
80 {
81         if (!ao_lco_got_channels || !ao_pad_query.channels)
82                 return pad == 0;
83         if (pad >= AO_PAD_MAX_CHANNELS)
84                 return 0;
85         return (ao_pad_query.channels >> pad) & 1;
86 }
87
88 static uint8_t
89 ao_lco_pad_first(void)
90 {
91         uint8_t pad;
92
93         for (pad = 0; pad < AO_PAD_MAX_CHANNELS; pad++)
94                 if (ao_lco_pad_present(pad))
95                         return pad;
96         return 0;
97 }
98
99 static void
100 ao_lco_input(void)
101 {
102         static struct ao_event  event;
103         int8_t  dir, new_box, new_pad;
104
105         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
106         ao_lco_set_pad();
107         ao_lco_set_box();
108         for (;;) {
109                 ao_event_get(&event);
110                 PRINTD("event type %d unit %d value %d\n",
111                        event.type, event.unit, event.value);
112                 switch (event.type) {
113                 case AO_EVENT_QUADRATURE:
114                         switch (event.unit) {
115                         case AO_QUADRATURE_PAD:
116                                 if (!ao_lco_armed) {
117                                         if (event.value == ao_lco_pad)
118                                                 break;
119                                         dir = ((int8_t) event.value - (int8_t) ao_lco_pad) > 0 ? 1 : -1;
120                                         new_pad = event.value;
121                                         while (!ao_lco_pad_present(new_pad)) {
122                                                 new_pad += dir;
123                                                 if (new_pad > AO_PAD_MAX_CHANNELS)
124                                                         new_pad = 0;
125                                                 else if (new_pad < 0)
126                                                         new_pad = AO_PAD_MAX_CHANNELS - 1;
127                                                 if (new_pad == ao_lco_pad)
128                                                         break;
129                                         }
130                                         if (new_pad != ao_lco_pad) {
131                                                 ao_lco_pad = new_pad;
132                                                 ao_quadrature_count[AO_QUADRATURE_PAD] = ao_lco_pad;
133                                                 ao_lco_set_pad();
134                                         }
135                                 }
136                                 break;
137                         case AO_QUADRATURE_BOX:
138                                 if (!ao_lco_armed) {
139                                         if (event.value == ao_lco_box)
140                                                 break;
141                                         dir = ((int8_t) event.value - (int8_t) ao_lco_box) > 0 ? 1 : -1;
142                                         new_box = event.value;
143                                         while (!ao_lco_box_present(new_box)) {
144                                                 new_box += dir;
145                                                 if (new_box > ao_lco_max_box)
146                                                         new_box = ao_lco_min_box;
147                                                 else if (new_box < ao_lco_min_box)
148                                                         new_box = ao_lco_min_box;
149                                                 if (new_box == ao_lco_box)
150                                                         break;
151                                         }
152                                         ao_quadrature_count[AO_QUADRATURE_PAD] = new_box;
153                                         if (ao_lco_box != new_box) {
154                                                 ao_lco_box = new_box;
155                                                 ao_lco_got_channels = 0;
156                                                 ao_lco_set_box();
157                                         }
158                                 }
159                                 break;
160                         }
161                         break;
162                 case AO_EVENT_BUTTON:
163                         switch (event.unit) {
164                         case AO_BUTTON_ARM:
165                                 ao_lco_armed = event.value;
166                                 PRINTD("Armed %d\n", ao_lco_armed);
167                                 ao_wakeup(&ao_lco_armed);
168                                 break;
169                         case AO_BUTTON_FIRE:
170                                 if (ao_lco_armed) {
171                                         ao_lco_firing = event.value;
172                                         PRINTD("Firing %d\n", ao_lco_firing);
173                                         ao_wakeup(&ao_lco_armed);
174                                 }
175                                 break;
176                         }
177                         break;
178                 }
179         }
180 }
181
182 static AO_LED_TYPE      continuity_led[AO_LED_CONTINUITY_NUM] = {
183 #ifdef AO_LED_CONTINUITY_0
184         AO_LED_CONTINUITY_0,
185 #endif
186 #ifdef AO_LED_CONTINUITY_1
187         AO_LED_CONTINUITY_1,
188 #endif
189 #ifdef AO_LED_CONTINUITY_2
190         AO_LED_CONTINUITY_2,
191 #endif
192 #ifdef AO_LED_CONTINUITY_3
193         AO_LED_CONTINUITY_3,
194 #endif
195 #ifdef AO_LED_CONTINUITY_4
196         AO_LED_CONTINUITY_4,
197 #endif
198 #ifdef AO_LED_CONTINUITY_5
199         AO_LED_CONTINUITY_5,
200 #endif
201 #ifdef AO_LED_CONTINUITY_6
202         AO_LED_CONTINUITY_6,
203 #endif
204 #ifdef AO_LED_CONTINUITY_7
205         AO_LED_CONTINUITY_7,
206 #endif
207 };
208
209 static void
210 ao_lco_update(void)
211 {
212         int8_t                  r;
213         uint8_t                 c;
214
215         r = ao_lco_query(ao_lco_box, &ao_pad_query, &ao_lco_tick_offset);
216         if (r == AO_RADIO_CMAC_OK) {
217                 c = ao_lco_got_channels;
218                 ao_lco_got_channels = 1;
219                 ao_lco_valid = 1;
220                 if (!c) {
221                         ao_lco_pad = ao_lco_pad_first();
222                         ao_lco_set_pad();
223                 }
224         } else
225                 ao_lco_valid = 0;
226
227 #if 0
228         PRINTD("lco_query success arm_status %d i0 %d i1 %d i2 %d i3 %d\n",
229                query.arm_status,
230                query.igniter_status[0],
231                query.igniter_status[1],
232                query.igniter_status[2],
233                query.igniter_status[3]);
234 #endif
235
236         ao_wakeup(&ao_pad_query);
237 }
238
239 static void
240 ao_lco_box_set_present(uint8_t box)
241 {
242         if (box >= AO_PAD_MAX_BOXES)
243                 return;
244         ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
245 }
246
247 static void
248 ao_lco_search(void)
249 {
250         uint16_t        tick_offset;
251         int8_t          r;
252
253         ao_lco_min_box = 0xff;
254         ao_lco_max_box = 0x00;
255         for (ao_lco_box = 0; ao_lco_box < AO_PAD_MAX_BOXES; ao_lco_box++) {
256                 if ((ao_lco_box % 10) == 0)
257                         ao_lco_set_box();
258                 r = ao_lco_query(ao_lco_box, &ao_pad_query, &ao_lco_tick_offset);
259                 if (r == AO_RADIO_CMAC_OK) {
260                         if (ao_lco_box < ao_lco_min_box)
261                                 ao_lco_min_box = ao_lco_box;
262                         if (ao_lco_box > ao_lco_max_box)
263                                 ao_lco_max_box = ao_lco_box;
264                         ao_lco_box_set_present(ao_lco_box);
265                 }
266         }
267         if (ao_lco_min_box <= ao_lco_max_box)
268                 ao_lco_box = ao_lco_min_box;
269         else
270                 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
271         ao_lco_valid = 0;
272         ao_lco_got_channels = 0;
273         ao_lco_pad = 0;
274 }
275
276 static void
277 ao_lco_igniter_status(void)
278 {
279         uint8_t         c;
280         uint16_t        delay;
281
282         for (;;) {
283 //              ao_alarm(delay);
284                 ao_sleep(&ao_pad_query);
285 //              ao_clear_alarm();
286                 if (!ao_lco_valid) {
287                         ao_led_on(AO_LED_RED);
288                         ao_led_off(AO_LED_GREEN);
289                         continue;
290                 }
291                 PRINTD("RSSI %d\n", ao_radio_cmac_rssi);
292                 if (ao_radio_cmac_rssi < -90)
293                         ao_led_on(AO_LED_RED|AO_LED_GREEN);
294                 else {
295                         ao_led_on(AO_LED_GREEN);
296                         ao_led_off(AO_LED_RED);
297                 }
298                 if (ao_pad_query.arm_status)
299                         ao_led_on(AO_LED_REMOTE_ARM);
300                 else
301                         ao_led_off(AO_LED_REMOTE_ARM);
302                 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
303                         uint8_t status;
304
305                         if (ao_pad_query.channels & (1 << c))
306                                 status = ao_pad_query.igniter_status[c];
307                         else
308                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
309                         if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
310                                 ao_led_on(continuity_led[c]);
311                         else
312                                 ao_led_off(continuity_led[c]);
313                 }
314         }
315 }
316
317 static void
318 ao_lco_arm_warn(void)
319 {
320         for (;;) {
321                 while (!ao_lco_armed)
322                         ao_sleep(&ao_lco_armed);
323                 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
324                 ao_delay(AO_MS_TO_TICKS(200));
325         }
326 }
327
328 static struct ao_task ao_lco_input_task;
329 static struct ao_task ao_lco_monitor_task;
330 static struct ao_task ao_lco_arm_warn_task;
331 static struct ao_task ao_lco_igniter_status_task;
332
333 static void
334 ao_lco_monitor(void)
335 {
336         uint16_t                delay;
337
338         ao_lco_search();
339         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
340         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
341         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
342         for (;;) {
343                 PRINTD("monitor armed %d firing %d offset %d\n",
344                        ao_lco_armed, ao_lco_firing, ao_lco_tick_offset);
345
346                 if (ao_lco_armed && ao_lco_firing) {
347                         PRINTD("Firing box %d pad %d: valid %d\n",
348                                ao_lco_box, ao_lco_pad, ao_lco_valid);
349                         if (!ao_lco_valid)
350                                 ao_lco_update();
351                         if (ao_lco_valid)
352                                 ao_lco_ignite(ao_lco_box, 1 << ao_lco_pad, ao_lco_tick_offset);
353                 } else if (ao_lco_armed) {
354                         PRINTD("Arming box %d pad %d\n",
355                                ao_lco_box, ao_lco_pad);
356                         if (!ao_lco_valid)
357                                 ao_lco_update();
358                         ao_lco_arm(ao_lco_box, 1 << ao_lco_pad, ao_lco_tick_offset);
359                         ao_lco_update();
360                 } else {
361                         ao_lco_update();
362                 }
363                 if (ao_lco_armed && ao_lco_firing)
364                         delay = AO_MS_TO_TICKS(100);
365                 else
366                         delay = AO_SEC_TO_TICKS(1);
367                 ao_alarm(delay);
368                 ao_sleep(&ao_lco_armed);
369                 ao_clear_alarm();
370         }
371 }
372
373 #if DEBUG
374 void
375 ao_lco_set_debug(void)
376 {
377         ao_cmd_decimal();
378         if (ao_cmd_status == ao_cmd_success)
379                 ao_lco_debug = ao_cmd_lex_i != 0;
380 }
381
382 __code struct ao_cmds ao_lco_cmds[] = {
383         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
384         { 0, NULL }
385 };
386 #endif
387
388 void
389 ao_lco_init(void)
390 {
391         ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
392 #if DEBUG
393         ao_cmd_register(&ao_lco_cmds[0]);
394 #endif
395 }