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