2fa0a21f0ace94700ac10950e14c68dcee896a34
[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_button.h>
23 #include <ao_lco_func.h>
24 #include <ao_radio_cmac.h>
25
26 static uint8_t  ao_lco_suspended;
27
28 static void
29 ao_lco_suspend(void)
30 {
31         if (!ao_lco_suspended) {
32                 PRINTD("suspend\n");
33                 ao_lco_suspended = 1;
34                 ao_lco_armed = 0;
35                 ao_wakeup(&ao_pad_query);
36         }
37 }
38
39 static void
40 ao_lco_wakeup(void)
41 {
42         if (ao_lco_suspended) {
43                 ao_lco_suspended = 0;
44                 ao_wakeup(&ao_lco_suspended);
45         }
46 }
47
48 void
49 ao_lco_show_display(void)
50 {
51 }
52
53 static void
54 ao_lco_input(void)
55 {
56         static struct ao_event  event;
57         uint8_t timeout;
58
59         for (;;) {
60                 if (ao_config.pad_idle && !ao_lco_suspended) {
61                         timeout = ao_event_get_for(&event, AO_SEC_TO_TICKS(ao_config.pad_idle));
62                         if (timeout) {
63                                 ao_lco_suspend();
64                                 continue;
65                         }
66                 } else {
67                         ao_event_get(&event);
68                 }
69                 ao_lco_wakeup();
70                 PRINTD("event type %d unit %d value %d\n",
71                        event.type, event.unit, event.value);
72                 switch (event.type) {
73                 case AO_EVENT_BUTTON:
74                         switch (event.unit) {
75                         case AO_BUTTON_BOX:
76                                 ao_lco_set_box(event.value);
77                                 ao_lco_set_armed(0);
78                                 break;
79                         case AO_BUTTON_ARM:
80                                 ao_lco_set_armed(event.value);
81                                 break;
82                         case AO_BUTTON_FIRE:
83                                 if (ao_lco_armed)
84                                         ao_lco_set_firing(event.value);
85                                 break;
86                         }
87                         break;
88                 }
89         }
90 }
91
92 static struct ao_task ao_lco_input_task;
93 static struct ao_task ao_lco_monitor_task;
94 static struct ao_task ao_lco_arm_warn_task;
95 static struct ao_task ao_lco_igniter_status_task;
96
97 static void
98 ao_lco_main(void)
99 {
100         ao_config_get();
101         ao_lco_set_box(ao_button_get(AO_BUTTON_BOX));
102         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
103         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
104         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
105         ao_led_on(~0);
106         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
107         ao_led_off(~0);
108         ao_lco_monitor();
109 }
110
111 #if DEBUG
112 void
113 ao_lco_set_debug(void)
114 {
115         ao_cmd_decimal();
116         if (ao_cmd_status == ao_cmd_success)
117                 ao_lco_debug = ao_cmd_lex_i;
118 }
119
120 __code struct ao_cmds ao_lco_cmds[] = {
121         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
122         { 0, NULL }
123 };
124 #endif
125
126 void
127 ao_lco_init(void)
128 {
129         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
130 #if DEBUG
131         ao_cmd_register(&ao_lco_cmds[0]);
132 #endif
133 }