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