]> git.gag.com Git - fw/altos/blob - src/telelco-v0.1/ao_lco.c
update changelog for Debian build
[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_sleep(&ao_pad_query);
284                 if (!ao_lco_valid) {
285                         ao_led_on(AO_LED_RED);
286                         ao_led_off(AO_LED_GREEN);
287                         continue;
288                 }
289                 PRINTD("RSSI %d\n", ao_radio_cmac_rssi);
290                 if (ao_radio_cmac_rssi < -90)
291                         ao_led_on(AO_LED_RED|AO_LED_GREEN);
292                 else {
293                         ao_led_on(AO_LED_GREEN);
294                         ao_led_off(AO_LED_RED);
295                 }
296                 if (ao_pad_query.arm_status)
297                         ao_led_on(AO_LED_REMOTE_ARM);
298                 else
299                         ao_led_off(AO_LED_REMOTE_ARM);
300                 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
301                         uint8_t status;
302
303                         if (ao_pad_query.channels & (1 << c))
304                                 status = ao_pad_query.igniter_status[c];
305                         else
306                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
307                         if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
308                                 ao_led_on(continuity_led[c]);
309                         else
310                                 ao_led_off(continuity_led[c]);
311                 }
312         }
313 }
314
315 static void
316 ao_lco_arm_warn(void)
317 {
318         for (;;) {
319                 while (!ao_lco_armed)
320                         ao_sleep(&ao_lco_armed);
321                 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
322                 ao_delay(AO_MS_TO_TICKS(200));
323         }
324 }
325
326 static struct ao_task ao_lco_input_task;
327 static struct ao_task ao_lco_monitor_task;
328 static struct ao_task ao_lco_arm_warn_task;
329 static struct ao_task ao_lco_igniter_status_task;
330
331 static void
332 ao_lco_monitor(void)
333 {
334         uint16_t                delay;
335
336         ao_lco_search();
337         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
338         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
339         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
340         for (;;) {
341                 PRINTD("monitor armed %d firing %d offset %d\n",
342                        ao_lco_armed, ao_lco_firing, ao_lco_tick_offset);
343
344                 if (ao_lco_armed && ao_lco_firing) {
345                         PRINTD("Firing box %d pad %d: valid %d\n",
346                                ao_lco_box, ao_lco_pad, ao_lco_valid);
347                         if (!ao_lco_valid)
348                                 ao_lco_update();
349                         if (ao_lco_valid)
350                                 ao_lco_ignite(ao_lco_box, 1 << ao_lco_pad, ao_lco_tick_offset);
351                 } else if (ao_lco_armed) {
352                         PRINTD("Arming box %d pad %d\n",
353                                ao_lco_box, ao_lco_pad);
354                         if (!ao_lco_valid)
355                                 ao_lco_update();
356                         ao_lco_arm(ao_lco_box, 1 << ao_lco_pad, ao_lco_tick_offset);
357                         ao_lco_update();
358                 } else {
359                         ao_lco_update();
360                 }
361                 if (ao_lco_armed && ao_lco_firing)
362                         delay = AO_MS_TO_TICKS(100);
363                 else
364                         delay = AO_SEC_TO_TICKS(1);
365                 ao_sleep_for(&ao_lco_armed, delay);
366         }
367 }
368
369 #if DEBUG
370 void
371 ao_lco_set_debug(void)
372 {
373         ao_cmd_decimal();
374         if (ao_cmd_status == ao_cmd_success)
375                 ao_lco_debug = ao_cmd_lex_i != 0;
376 }
377
378 __code struct ao_cmds ao_lco_cmds[] = {
379         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
380         { 0, NULL }
381 };
382 #endif
383
384 void
385 ao_lco_init(void)
386 {
387         ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
388 #if DEBUG
389         ao_cmd_register(&ao_lco_cmds[0]);
390 #endif
391 }