Switch from GPLv2 to GPLv2+
[fw/altos] / src / cc1111 / ao_launch.c
1 /*
2  * Copyright © 2011 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_radio_cmac.h>
21
22 __xdata uint16_t ao_launch_ignite;
23
24 #if 0
25 #define PRINTD(...) printf(__VA_ARGS__)
26 #else
27 #define PRINTD(...) 
28 #endif
29
30 static void
31 ao_launch_run(void)
32 {
33         for (;;) {
34                 while (!ao_launch_ignite)
35                         ao_sleep(&ao_launch_ignite);
36                 ao_ignition[ao_igniter_drogue].firing = 1;
37                 ao_ignition[ao_igniter_main].firing = 1;
38                 AO_IGNITER_DIR |= AO_IGNITER_DROGUE_BIT | AO_IGNITER_MAIN_BIT;
39                 AO_IGNITER_DROGUE = 1;
40                 while (ao_launch_ignite) {
41                         ao_launch_ignite = 0;
42                         ao_delay(AO_MS_TO_TICKS(500));
43                 }
44                 AO_IGNITER_DROGUE = 0;
45                 ao_ignition[ao_igniter_drogue].firing = 0;
46                 ao_ignition[ao_igniter_main].firing = 0;
47         }
48 }
49
50 static void
51 ao_launch_status(void)
52 {
53         uint8_t i;
54         for (;;) {
55                 ao_delay(AO_SEC_TO_TICKS(1));
56                 if (ao_igniter_status(ao_igniter_drogue) == ao_igniter_ready) {
57                         if (ao_igniter_status(ao_igniter_main) == ao_igniter_ready) {
58                                 for (i = 0; i < 5; i++) {
59                                         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(50));
60                                         ao_delay(AO_MS_TO_TICKS(100));
61                                 }
62                         } else {
63                                 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
64                         }
65                 }
66         }
67 }
68
69 static __pdata uint8_t  ao_launch_armed;
70 static __pdata uint16_t ao_launch_arm_time;
71
72 static void
73 ao_launch(void)
74 {
75         static __xdata struct ao_launch_command command;
76         static __xdata struct ao_launch_query   query;
77         int16_t time_difference;
78
79         ao_led_off(AO_LED_RED);
80         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
81         for (;;) {
82                 flush();
83                 if (ao_radio_cmac_recv(&command, sizeof (command), 0) != AO_RADIO_CMAC_OK)
84                         continue;
85                 
86                 PRINTD ("tick %d serial %d cmd %d channel %d\n",
87                         command.tick, command.serial, command.cmd, command.channel);
88
89                 switch (command.cmd) {
90                 case AO_LAUNCH_QUERY:
91                         if (command.serial != ao_serial_number) {
92                                 PRINTD ("serial number mismatch\n");
93                                 break;
94                         }
95
96                         if (command.channel == 0) {
97                                 query.valid = 1;
98                                 query.arm_status = ao_igniter_status(ao_igniter_drogue);
99                                 query.igniter_status = ao_igniter_status(ao_igniter_main);
100                         } else {
101                                 query.valid = 0;
102                         }
103                         query.tick = ao_time();
104                         query.serial = ao_serial_number;
105                         query.channel = command.channel;
106                         PRINTD ("query tick %d serial %d channel %d valid %d arm %d igniter %d\n",
107                                 query.tick, query.serial, query.channel, query.valid, query.arm_status,
108                                 query.igniter_status);
109                         ao_radio_cmac_send(&query, sizeof (query));
110                         break;
111                 case AO_LAUNCH_ARM:
112                         if (command.serial != ao_serial_number) {
113                                 PRINTD ("serial number mismatch\n");
114                                 break;
115                         }
116
117                         if (command.channel != 0)
118                                 break;
119                         time_difference = command.tick - ao_time();
120                         PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
121                         if (time_difference < 0)
122                                 time_difference = -time_difference;
123                         if (time_difference > 10) {
124                                 PRINTD ("time difference too large %d\n", time_difference);
125                                 break;
126                         }
127                         PRINTD ("armed\n");
128                         ao_launch_armed = 1;
129                         ao_launch_arm_time = ao_time();
130                         break;
131                 case AO_LAUNCH_FIRE:
132                         if (!ao_launch_armed) {
133                                 PRINTD ("not armed\n");
134                                 break;
135                         }
136                         if ((uint16_t) (ao_time() - ao_launch_arm_time) > AO_SEC_TO_TICKS(20)) {
137                                 PRINTD ("late launch arm_time %d time %d\n",
138                                         ao_launch_arm_time, ao_time());
139                                 break;
140                         }
141                         time_difference = command.tick - ao_time();
142                         if (time_difference < 0)
143                                 time_difference = -time_difference;
144                         if (time_difference > 10) {
145                                 PRINTD ("time different too large %d\n", time_difference);
146                                 break;
147                         }
148                         PRINTD ("ignite\n");
149                         ao_launch_ignite = 1;
150                         ao_wakeup(&ao_launch_ignite);
151                         break;
152                 }
153         }
154 }
155
156 void
157 ao_launch_test(void)
158 {
159         switch (ao_igniter_status(ao_igniter_drogue)) {
160         case ao_igniter_ready:
161         case ao_igniter_active:
162                 printf ("Armed: ");
163                 switch (ao_igniter_status(ao_igniter_main)) {
164                 default:
165                         printf("unknown status\n");
166                         break;
167                 case ao_igniter_ready:
168                         printf("igniter good\n");
169                         break;
170                 case ao_igniter_open:
171                         printf("igniter bad\n");
172                         break;
173                 }
174                 break;
175         default:
176                 printf("Disarmed\n");
177         }
178 }
179
180 void
181 ao_launch_manual(void)
182 {
183         ao_cmd_white();
184         if (!ao_match_word("DoIt"))
185                 return;
186         ao_cmd_white();
187         ao_launch_ignite = 1;
188         ao_wakeup(&ao_launch_ignite);
189 }
190
191 static __xdata struct ao_task ao_launch_task;
192 static __xdata struct ao_task ao_launch_ignite_task;
193 static __xdata struct ao_task ao_launch_status_task;
194
195 __code struct ao_cmds ao_launch_cmds[] = {
196         { ao_launch_test,       "t\0Test launch continuity" },
197         { ao_launch_manual,     "i <key>\0Fire igniter. <key> is doit with D&I" },
198         { 0, NULL }
199 };
200
201 void
202 ao_launch_init(void)
203 {
204         AO_IGNITER_DROGUE = 0;
205         AO_IGNITER_MAIN = 0;
206         AO_IGNITER_DIR |= AO_IGNITER_DROGUE_BIT | AO_IGNITER_MAIN_BIT;
207         ao_cmd_register(&ao_launch_cmds[0]);
208         ao_add_task(&ao_launch_task, ao_launch, "launch listener");
209         ao_add_task(&ao_launch_ignite_task, ao_launch_run, "launch igniter");
210         ao_add_task(&ao_launch_status_task, ao_launch_status, "launch status");
211 }