Switch from GPLv2 to GPLv2+
[fw/altos] / src / drivers / ao_lco_two.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_lco.h>
21 #include <ao_event.h>
22 #include <ao_lco_func.h>
23 #include <ao_radio_cmac.h>
24
25 #define DEBUG   1
26
27 #if DEBUG
28 static uint8_t  ao_lco_debug;
29 #define DEBUG_EVENT     1
30 #define DEBUG_STATUS    2
31 #define PRINTD(l, ...) do { if (!(ao_lco_debug & l)) break; printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
32 #else
33 #define PRINTD(l,...)
34 #endif
35
36 #define AO_LCO_VALID_LAST       1
37 #define AO_LCO_VALID_EVER       2
38
39 static uint8_t  ao_lco_suspended;
40 static uint8_t  ao_lco_selected;
41 static uint8_t  ao_lco_valid;
42 static uint8_t  ao_lco_channels;
43 static uint16_t ao_lco_tick_offset;
44
45 /* UI values */
46 static uint8_t  ao_lco_armed;
47 static uint8_t  ao_lco_firing;
48
49 #define ao_lco_box      (ao_config.pad_box)
50
51 static struct ao_pad_query      ao_pad_query;
52
53 #define MASK_SIZE(n)    (((n) + 7) >> 3)
54 #define MASK_ID(n)      ((n) >> 3)
55 #define MASK_SHIFT(n)   ((n) & 7)
56
57 static void
58 ao_lco_set_armed(int pad, int armed)
59 {
60         uint8_t bit = (1 << pad);
61
62         if (armed) {
63                 ao_lco_selected |= bit;
64                 ao_lco_armed |= bit;
65         } else {
66                 ao_lco_selected &= ~bit;
67                 ao_lco_armed &= ~bit;
68         }
69         PRINTD(DEBUG_EVENT, "pad %d bit 0x%x armed %d ao_lco_selected 0x%x ao_lco_armed 0x%x\n",
70                pad, bit, armed, ao_lco_selected, ao_lco_armed);
71         ao_wakeup(&ao_lco_armed);
72 }
73
74 static void
75 ao_lco_suspend(void)
76 {
77         if (!ao_lco_suspended) {
78                 PRINTD(DEBUG_EVENT, "suspend\n");
79                 ao_lco_suspended = 1;
80                 ao_lco_selected = 0;
81                 ao_lco_armed = 0;
82                 ao_wakeup(&ao_pad_query);
83         }
84 }
85
86 static void
87 ao_lco_wakeup(void)
88 {
89         if (ao_lco_suspended) {
90                 ao_lco_suspended = 0;
91                 ao_wakeup(&ao_lco_suspended);
92         }
93 }
94
95 static void
96 ao_lco_input(void)
97 {
98         static struct ao_event  event;
99         uint8_t timeout;
100
101         ao_config_get();
102         for (;;) {
103                 if (ao_config.pad_idle && !ao_lco_suspended) {
104                         timeout = ao_event_get_for(&event, AO_SEC_TO_TICKS(ao_config.pad_idle));
105                         if (timeout) {
106                                 ao_lco_suspend();
107                                 continue;
108                         }
109                 } else {
110                         ao_event_get(&event);
111                 }
112                 ao_lco_wakeup();
113                 PRINTD(DEBUG_EVENT, "event type %d unit %d value %d\n",
114                        event.type, event.unit, event.value);
115                 switch (event.type) {
116                 case AO_EVENT_BUTTON:
117                         switch (event.unit) {
118                         case AO_BUTTON_ARM_0:
119                                 ao_lco_set_armed(0, event.value);
120                                 break;
121 #if AO_BUTTON_ARM_NUM > 1
122                         case AO_BUTTON_ARM_1:
123                                 ao_lco_set_armed(1, event.value);
124                                 break;
125 #endif
126                         case AO_BUTTON_FIRE:
127                                 if (ao_lco_armed) {
128                                         ao_lco_firing = event.value;
129                                         PRINTD(DEBUG_EVENT, "Firing %d\n", ao_lco_firing);
130                                         ao_wakeup(&ao_lco_armed);
131                                 }
132                                 break;
133                         }
134                         break;
135                 }
136         }
137 }
138
139 static AO_LED_TYPE      continuity_led[AO_LED_CONTINUITY_NUM] = {
140 #ifdef AO_LED_CONTINUITY_0
141         AO_LED_CONTINUITY_0,
142 #endif
143 #ifdef AO_LED_CONTINUITY_1
144         AO_LED_CONTINUITY_1,
145 #endif
146 #ifdef AO_LED_CONTINUITY_2
147         AO_LED_CONTINUITY_2,
148 #endif
149 #ifdef AO_LED_CONTINUITY_3
150         AO_LED_CONTINUITY_3,
151 #endif
152 #ifdef AO_LED_CONTINUITY_4
153         AO_LED_CONTINUITY_4,
154 #endif
155 #ifdef AO_LED_CONTINUITY_5
156         AO_LED_CONTINUITY_5,
157 #endif
158 #ifdef AO_LED_CONTINUITY_6
159         AO_LED_CONTINUITY_6,
160 #endif
161 #ifdef AO_LED_CONTINUITY_7
162         AO_LED_CONTINUITY_7,
163 #endif
164 };
165
166 static uint8_t
167 ao_lco_get_channels(void)
168 {
169         int8_t                  r;
170
171         r = ao_lco_query(ao_lco_box, &ao_pad_query, &ao_lco_tick_offset);
172         if (r == AO_RADIO_CMAC_OK) {
173                 ao_lco_channels = ao_pad_query.channels;
174                 ao_lco_valid = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
175         } else
176                 ao_lco_valid &= ~AO_LCO_VALID_LAST;
177         PRINTD(DEBUG_STATUS, "ao_lco_get_channels() rssi %d valid %d ret %d offset %d\n", ao_radio_cmac_rssi, ao_lco_valid, r, ao_lco_tick_offset);
178         ao_wakeup(&ao_pad_query);
179         return ao_lco_valid;
180 }
181
182 static void
183 ao_lco_igniter_status(void)
184 {
185         uint8_t         c;
186         uint8_t         t = 0;
187
188         for (;;) {
189                 ao_sleep(&ao_pad_query);
190                 while (ao_lco_suspended) {
191                         ao_led_off(AO_LED_GREEN|AO_LED_AMBER|AO_LED_RED|AO_LED_REMOTE_ARM);
192                         for (c = 0; c < AO_LED_CONTINUITY_NUM; c++)
193                                 ao_led_off(continuity_led[c]);
194                         ao_sleep(&ao_lco_suspended);
195                 }
196                 PRINTD(DEBUG_STATUS, "RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid);
197                 if (!(ao_lco_valid & AO_LCO_VALID_LAST)) {
198                         ao_led_on(AO_LED_RED);
199                         ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
200                         continue;
201                 }
202                 if (ao_radio_cmac_rssi < -90) {
203                         ao_led_on(AO_LED_AMBER);
204                         ao_led_off(AO_LED_RED|AO_LED_GREEN);
205                 } else {
206                         ao_led_on(AO_LED_GREEN);
207                         ao_led_off(AO_LED_RED|AO_LED_AMBER);
208                 }
209                 if (ao_pad_query.arm_status)
210                         ao_led_on(AO_LED_REMOTE_ARM);
211                 else
212                         ao_led_off(AO_LED_REMOTE_ARM);
213
214                 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
215                         uint8_t status;
216
217                         if (ao_pad_query.channels & (1 << c))
218                                 status = ao_pad_query.igniter_status[c];
219                         else
220                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
221                         PRINTD(DEBUG_STATUS, "\tchannel %d status %d\n", c, status);
222                         if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
223                                 ao_led_on(continuity_led[c]);
224                         else
225                                 ao_led_off(continuity_led[c]);
226                 }
227                 t = 1-t;
228         }
229 }
230
231 static void
232 ao_lco_arm_warn(void)
233 {
234         int     i;
235         for (;;) {
236                 while (ao_lco_suspended)
237                         ao_sleep(&ao_lco_suspended);
238                 while (!ao_lco_armed)
239                         ao_sleep(&ao_lco_armed);
240                 for (i = 0; i < ao_lco_armed; i++) {
241                         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(100));
242                         ao_delay(AO_MS_TO_TICKS(100));
243                 }
244                 ao_delay(AO_MS_TO_TICKS(300));
245         }
246 }
247
248 static struct ao_task ao_lco_input_task;
249 static struct ao_task ao_lco_monitor_task;
250 static struct ao_task ao_lco_arm_warn_task;
251 static struct ao_task ao_lco_igniter_status_task;
252
253 static void
254 ao_lco_monitor(void)
255 {
256         uint16_t                delay;
257
258         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
259         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
260         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
261         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
262         for (;;) {
263                 while (ao_lco_suspended)
264                         ao_sleep(&ao_lco_suspended);
265
266                 PRINTD(DEBUG_STATUS, "monitor armed %d firing %d\n",
267                        ao_lco_armed, ao_lco_firing);
268
269                 if (ao_lco_armed && ao_lco_firing) {
270                         ao_lco_ignite();
271                 } else {
272                         ao_lco_get_channels();
273                         if (ao_lco_armed) {
274                                 if (ao_lco_selected) {
275                                         PRINTD(DEBUG_STATUS, "Arming pads %x\n",
276                                                ao_lco_selected);
277                                         if (ao_lco_valid & AO_LCO_VALID_EVER) {
278                                                 ao_lco_arm(ao_lco_box, ao_lco_selected, ao_lco_tick_offset);
279                                                 ao_delay(AO_MS_TO_TICKS(10));
280                                         }
281                                 }
282                         }
283                 }
284                 if (ao_lco_armed && ao_lco_firing)
285                         delay = AO_MS_TO_TICKS(100);
286                 else {
287                         delay = AO_SEC_TO_TICKS(1);
288                 }
289                 ao_sleep_for(&ao_lco_armed, delay);
290         }
291 }
292
293 #if DEBUG
294 void
295 ao_lco_set_debug(void)
296 {
297         ao_cmd_decimal();
298         if (ao_cmd_status == ao_cmd_success)
299                 ao_lco_debug = ao_cmd_lex_i;
300 }
301
302 __code struct ao_cmds ao_lco_cmds[] = {
303         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
304         { 0, NULL }
305 };
306 #endif
307
308 void
309 ao_lco_init(void)
310 {
311         ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
312 #if DEBUG
313         ao_cmd_register(&ao_lco_cmds[0]);
314 #endif
315 }