altos/test: Adjust CRC error rate after FEC fix
[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_pad(int8_t pad)
50 {
51         (void) pad;
52 }
53
54 void
55 ao_lco_show_box(int16_t box)
56 {
57         (void) box;
58 }
59
60 void
61 ao_lco_show(void)
62 {
63 }
64
65 static void
66 ao_lco_input(void)
67 {
68         static struct ao_event  event;
69         uint8_t timeout;
70
71         for (;;) {
72                 if (ao_config.pad_idle && !ao_lco_suspended) {
73                         timeout = ao_event_get_for(&event, AO_SEC_TO_TICKS(ao_config.pad_idle));
74                         if (timeout) {
75                                 ao_lco_suspend();
76                                 continue;
77                         }
78                 } else {
79                         ao_event_get(&event);
80                 }
81                 ao_lco_wakeup();
82                 PRINTD("event type %d unit %d value %ld\n",
83                        event.type, event.unit, (long) event.value);
84                 switch (event.type) {
85                 case AO_EVENT_BUTTON:
86                         switch (event.unit) {
87                         case AO_BUTTON_BOX:
88                                 ao_lco_set_box((int16_t) event.value);
89                                 ao_lco_set_armed(0);
90                                 break;
91                         case AO_BUTTON_ARM:
92                                 ao_lco_set_armed((uint8_t) event.value);
93                                 break;
94                         case AO_BUTTON_FIRE:
95                                 if (ao_lco_armed)
96                                         ao_lco_set_firing((uint8_t) event.value);
97                                 break;
98                         }
99                         break;
100                 }
101         }
102 }
103
104 static struct ao_task ao_lco_input_task;
105 static struct ao_task ao_lco_monitor_task;
106 static struct ao_task ao_lco_arm_warn_task;
107 static struct ao_task ao_lco_igniter_status_task;
108
109 static void
110 ao_lco_main(void)
111 {
112         ao_config_get();
113         ao_lco_set_box(ao_button_get(AO_BUTTON_BOX));
114         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
115         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
116         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
117         ao_led_on((uint16_t) ~0);
118         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
119         ao_led_off((uint16_t) ~0);
120         ao_lco_monitor();
121 }
122
123 #if DEBUG
124 static void
125 ao_lco_set_debug(void)
126 {
127         uint32_t r = ao_cmd_decimal();
128         if (ao_cmd_status == ao_cmd_success)
129                 ao_lco_debug = r != 0;
130 }
131
132 const struct ao_cmds ao_lco_cmds[] = {
133         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
134         { 0, NULL }
135 };
136 #endif
137
138 void
139 ao_lco_init(void)
140 {
141         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
142 #if DEBUG
143         ao_cmd_register(&ao_lco_cmds[0]);
144 #endif
145 }