altos: No space for pyro help on TP v0.1
[fw/altos] / src / core / ao_pyro.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_pyro.h>
20 #include <ao_sample.h>
21 #include <ao_flight.h>
22
23 #define ao_lowbit(x)    ((x) & (-x))
24
25 /*
26  * Given a pyro structure, figure out
27  * if the current flight state satisfies all
28  * of the requirements
29  */
30 static uint8_t
31 ao_pyro_ready(struct ao_pyro *pyro)
32 {
33         enum ao_pyro_flag flag, flags;
34
35         flags = pyro->flags;
36         while (flags != ao_pyro_none) {
37                 flag = ao_lowbit(flags);
38                 flags &= ~flag;
39                 switch (flag) {
40
41                 case ao_pyro_accel_less:
42                         if (ao_accel <= pyro->accel_less)
43                                 continue;
44                         break;
45                 case ao_pyro_accel_greater:
46                         if (ao_accel >= pyro->accel_greater)
47                                 continue;
48                         break;
49
50
51                 case ao_pyro_speed_less:
52                         if (ao_speed <= pyro->speed_less)
53                                 continue;
54                         break;
55                 case ao_pyro_speed_greater:
56                         if (ao_speed >= pyro->speed_greater)
57                                 continue;
58                         break;
59
60                 case ao_pyro_height_less:
61                         if (ao_height <= pyro->height_less)
62                                 continue;
63                         break;
64                 case ao_pyro_height_greater:
65                         if (ao_height >= pyro->height_greater)
66                                 continue;
67                         break;
68
69                 case ao_pyro_orient_less:
70 //                      if (ao_orient <= pyro->orient_less)
71                                 continue;
72                         break;
73                 case ao_pyro_orient_greater:
74 //                      if (ao_orient >= pyro->orient_greater)
75                                 continue;
76                         break;
77
78                 case ao_pyro_time_less:
79                         if ((int16_t) (ao_time() - ao_boost_tick) <= pyro->time_less)
80                                 continue;
81                         break;
82                 case ao_pyro_time_greater:
83                         if ((int16_t) (ao_time() - ao_boost_tick) >= pyro->time_greater)
84                                 continue;
85                         break;
86
87                 case ao_pyro_ascending:
88                         if (ao_speed > 0)
89                                 continue;
90                         break;
91                 case ao_pyro_descending:
92                         if (ao_speed < 0)
93                                 continue;
94                         break;
95
96                 case ao_pyro_after_motor:
97                         if (ao_motor_number == pyro->motor)
98                                 continue;
99                         break;
100
101                 case ao_pyro_delay:
102                         /* handled separately */
103                         continue;
104                         
105                 case ao_pyro_none:
106                         break;
107                 }
108                 return FALSE;
109         }
110         return TRUE;
111 }
112
113 #define ao_pyro_fire_port(port, bit, pin) do {  \
114                 ao_gpio_set(port, bit, pin, 1); \
115                 ao_delay(AO_MS_TO_TICKS(50));   \
116                 ao_gpio_set(port, bit, pin, 0); \
117         } while (0)
118         
119
120 static void
121 ao_pyro_fire(uint8_t p)
122 {
123         switch (p) {
124 #if AO_PYRO_NUM > 0
125         case 0: ao_pyro_fire_port(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0); break;
126 #endif
127 #if AO_PYRO_NUM > 1
128         case 1: ao_pyro_fire_port(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1); break;
129 #endif
130 #if AO_PYRO_NUM > 2
131         case 2: ao_pyro_fire_port(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2); break;
132 #endif
133 #if AO_PYRO_NUM > 3
134         case 3: ao_pyro_fire_port(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3); break;
135 #endif
136 #if AO_PYRO_NUM > 4
137         case 4: ao_pyro_fire_port(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4); break;
138 #endif
139 #if AO_PYRO_NUM > 5
140         case 5: ao_pyro_fire_port(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5); break;
141 #endif
142 #if AO_PYRO_NUM > 6
143         case 6: ao_pyro_fire_port(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6); break;
144 #endif
145 #if AO_PYRO_NUM > 7
146         case 7: ao_pyro_fire_port(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7); break;
147 #endif
148         default: break;
149         }
150         ao_delay(AO_MS_TO_TICKS(50));
151 }
152
153 static void
154 ao_pyro(void)
155 {
156         uint8_t         p;
157         struct ao_pyro  *pyro;
158
159         ao_config_get();
160         while (ao_flight_state < ao_flight_boost)
161                 ao_sleep(&ao_flight_state);
162
163         for (;;) {
164                 ao_delay(AO_MS_TO_TICKS(100));
165                 for (p = 0; p < AO_PYRO_NUM; p++) {
166                         pyro = &ao_config.pyro[p];
167
168                         /* Ignore igniters which have already fired
169                          */
170                         if (pyro->fired)
171                                 continue;
172
173                         /* Ignore disabled igniters
174                          */
175                         if (!pyro->flags)
176                                 continue;
177
178                         /* Check pyro state to see if it shoule fire
179                          */
180                         if (!pyro->delay_done) {
181                                 if (!ao_pyro_ready(pyro))
182                                         continue;
183
184                                 /* If there's a delay set, then remember when
185                                  * it expires
186                                  */
187                                 if (pyro->flags & ao_pyro_delay)
188                                         pyro->delay_done = ao_time() + pyro->delay;
189                         }
190
191                         /* Check to see if we're just waiting for
192                          * the delay to expire
193                          */
194                         if (pyro->delay_done) {
195                                 if ((int16_t) (ao_time() - pyro->delay_done) < 0)
196                                         continue;
197                         }
198
199                         ao_pyro_fire(p);
200                 }
201         }
202 }
203
204 __xdata struct ao_task ao_pyro_task;
205
206 #define NO_VALUE        0xff
207
208 #define AO_PYRO_NAME_LEN        3
209
210 #if !DISABLE_HELP
211 #define ENABLE_HELP 1
212 #endif
213
214 #if ENABLE_HELP
215 #define HELP(s) (s)
216 #else
217 #define HELP(s)
218 #endif
219
220 const struct {
221         char                    name[AO_PYRO_NAME_LEN];
222         enum ao_pyro_flag       flag;
223         uint8_t                 offset;
224 #if ENABLE_HELP
225         char                    *help;
226 #endif
227 } ao_pyro_values[] = {
228         { "a<", ao_pyro_accel_less,     offsetof(struct ao_pyro, accel_less), HELP("accel less (m/ss * 16)") },
229         { "a>", ao_pyro_accel_greater,  offsetof(struct ao_pyro, accel_greater), HELP("accel greater (m/ss * 16)") },
230
231         { "s<", ao_pyro_speed_less,     offsetof(struct ao_pyro, speed_less), HELP("speed less (m/s * 16)") },
232         { "s>", ao_pyro_speed_greater,  offsetof(struct ao_pyro, speed_greater), HELP("speed greater (m/s * 16)") },
233
234         { "h<", ao_pyro_height_less,    offsetof(struct ao_pyro, height_less), HELP("height less (m)") },
235         { "h>", ao_pyro_height_greater, offsetof(struct ao_pyro, height_greater), HELP("height greater (m)") },
236
237 #if 0
238         { "o<", ao_pyro_orient_less,    offsetof(struct ao_pyro, orient_less), HELP("orient less (deg)") },
239         { "o>", ao_pyro_orient_greater, offsetof(struct ao_pyro, orient_greater), HELP("orient greater (deg)")  },
240 #endif
241
242         { "t<", ao_pyro_time_less,      offsetof(struct ao_pyro, time_less), HELP("time less (s * 100)") },
243         { "t>", ao_pyro_time_greater,   offsetof(struct ao_pyro, time_greater), HELP("time greater (s * 100)")  },
244
245         { "A", ao_pyro_ascending,       NO_VALUE, HELP("ascending") },
246         { "D", ao_pyro_descending,      NO_VALUE, HELP("descending") },
247         
248         { "m", ao_pyro_after_motor,     offsetof(struct ao_pyro, motor), HELP("after motor") },
249
250         { "d", ao_pyro_delay,           offsetof(struct ao_pyro, delay), HELP("delay before firing (s * 100)") },
251         { "", ao_pyro_none,             NO_VALUE, HELP(NULL) },
252 };
253
254 static void
255 ao_pyro_print_name(uint8_t v)
256 {
257         const char *s = ao_pyro_values[v].name;
258         printf ("%s%s", s, "   " + strlen(s));
259 }
260
261 #if ENABLE_HELP
262 static void
263 ao_pyro_help(void)
264 {
265         uint8_t v;
266         for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
267                 ao_pyro_print_name(v);
268                 if (ao_pyro_values[v].offset != NO_VALUE)
269                         printf ("<n> ");
270                 else
271                         printf ("    ");
272                 printf ("%s\n", ao_pyro_values[v].help);
273         }
274 }
275 #endif
276               
277 void
278 ao_pyro_show(void)
279 {
280         uint8_t         p;
281         uint8_t         v;
282         struct ao_pyro  *pyro;
283
284         for (p = 0; p < AO_PYRO_NUM; p++) {
285                 printf ("Pyro %2d: ", p);
286                 pyro = &ao_config.pyro[p];
287                 if (!pyro->flags) {
288                         printf ("<disabled>\n");
289                         continue;
290                 }
291                 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
292                         if (!(pyro->flags & ao_pyro_values[v].flag))
293                                 continue;
294                         ao_pyro_print_name(v);
295                         if (ao_pyro_values[v].offset != NO_VALUE) {
296                                 int16_t value;
297
298                                 value = *((int16_t *) ((char *) pyro + ao_pyro_values[v].offset));
299                                 printf ("%6d ", value);
300                         } else {
301                                 printf ("       ");
302                         }
303                 }
304                 printf ("\n");
305         }
306 }
307
308 void
309 ao_pyro_set(void)
310 {
311         uint8_t p;
312         struct ao_pyro pyro_tmp;
313         char    name[AO_PYRO_NAME_LEN];
314         uint8_t c;
315         uint8_t v;
316
317         ao_cmd_white();
318
319 #if ENABLE_HELP
320         switch (ao_cmd_lex_c) {
321         case '?':
322                 ao_pyro_help();
323                 return;
324         }
325 #endif
326
327         ao_cmd_decimal();
328         if (ao_cmd_status != ao_cmd_success)
329                 return;
330         p = ao_cmd_lex_i;
331         if (p < 0 || AO_PYRO_NUM <= p) {
332                 printf ("invalid pyro channel %d\n", p);
333                 return;
334         }
335         pyro_tmp.flags = 0;
336         for (;;) {
337                 ao_cmd_white();
338                 if (ao_cmd_lex_c == '\n')
339                         break;
340                 
341                 for (c = 0; c < AO_PYRO_NAME_LEN - 1; c++) {
342                         if (ao_cmd_is_white())
343                                 break;
344                         name[c] = ao_cmd_lex_c;
345                         ao_cmd_lex();
346                 }
347                 name[c] = '\0';
348                 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
349                         if (!strcmp (ao_pyro_values[v].name, name))
350                                 break;
351                 }
352                 if (ao_pyro_values[v].flag == ao_pyro_none) {
353                         printf ("invalid pyro field %s\n", name);
354                         ao_cmd_status = ao_cmd_syntax_error;
355                         return;
356                 }
357                 pyro_tmp.flags |= ao_pyro_values[v].flag;
358                 if (ao_pyro_values[v].offset != NO_VALUE) {
359                         ao_cmd_decimal();
360                         if (ao_cmd_status != ao_cmd_success)
361                                 return;
362                         *((int16_t *) ((char *) &pyro_tmp + ao_pyro_values[v].offset)) = ao_cmd_lex_i;
363                 }
364         }
365         _ao_config_edit_start();
366         ao_config.pyro[p] = pyro_tmp;
367         _ao_config_edit_finish();
368 }
369
370 void
371 ao_pyro_init(void)
372 {
373 #if AO_PYRO_NUM > 0
374         ao_enable_output(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, 0);
375 #endif
376 #if AO_PYRO_NUM > 1
377         ao_enable_output(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, 0);
378 #endif
379 #if AO_PYRO_NUM > 2
380         ao_enable_output(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, 0);
381 #endif
382 #if AO_PYRO_NUM > 3
383         ao_enable_output(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, 0);
384 #endif
385 #if AO_PYRO_NUM > 4
386         ao_enable_output(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, 0);
387 #endif
388 #if AO_PYRO_NUM > 5
389         ao_enable_output(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, 0);
390 #endif
391 #if AO_PYRO_NUM > 6
392         ao_enable_output(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, 0);
393 #endif
394 #if AO_PYRO_NUM > 7
395         ao_enable_output(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, 0);
396 #endif
397         ao_add_task(&ao_pyro_task, ao_pyro, "pyro");
398 }