2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 #include <ao_sample.h>
21 #include <ao_flight.h>
23 #define ao_lowbit(x) ((x) & (-x))
26 * Given a pyro structure, figure out
27 * if the current flight state satisfies all
31 ao_pyro_ready(struct ao_pyro *pyro)
33 enum ao_pyro_flag flag, flags;
36 while (flags != ao_pyro_none) {
37 flag = ao_lowbit(flags);
41 case ao_pyro_accel_less:
42 if (ao_accel <= pyro->accel_less)
45 case ao_pyro_accel_greater:
46 if (ao_accel >= pyro->accel_greater)
51 case ao_pyro_speed_less:
52 if (ao_speed <= pyro->speed_less)
55 case ao_pyro_speed_greater:
56 if (ao_speed >= pyro->speed_greater)
60 case ao_pyro_height_less:
61 if (ao_height <= pyro->height_less)
64 case ao_pyro_height_greater:
65 if (ao_height >= pyro->height_greater)
70 case ao_pyro_orient_less:
71 if (ao_orient <= pyro->orient_less)
74 case ao_pyro_orient_greater:
75 if (ao_orient >= pyro->orient_greater)
80 case ao_pyro_time_less:
81 if ((int16_t) (ao_time() - ao_boost_tick) <= pyro->time_less)
84 case ao_pyro_time_greater:
85 if ((int16_t) (ao_time() - ao_boost_tick) >= pyro->time_greater)
89 case ao_pyro_ascending:
93 case ao_pyro_descending:
98 case ao_pyro_after_motor:
99 if (ao_motor_number == pyro->motor)
104 /* handled separately */
115 #define ao_pyro_fire_port(port, bit, pin) do { \
116 ao_gpio_set(port, bit, pin, 1); \
117 ao_delay(AO_MS_TO_TICKS(50)); \
118 ao_gpio_set(port, bit, pin, 0); \
123 ao_pyro_fire(uint8_t p)
127 case 0: ao_pyro_fire_port(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0); break;
130 case 1: ao_pyro_fire_port(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1); break;
133 case 2: ao_pyro_fire_port(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2); break;
136 case 3: ao_pyro_fire_port(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3); break;
139 case 4: ao_pyro_fire_port(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4); break;
142 case 5: ao_pyro_fire_port(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5); break;
145 case 6: ao_pyro_fire_port(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6); break;
148 case 7: ao_pyro_fire_port(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7); break;
152 ao_delay(AO_MS_TO_TICKS(50));
159 struct ao_pyro *pyro;
162 while (ao_flight_state < ao_flight_boost)
163 ao_sleep(&ao_flight_state);
166 ao_delay(AO_MS_TO_TICKS(100));
167 for (p = 0; p < AO_PYRO_NUM; p++) {
168 pyro = &ao_config.pyro[p];
170 /* Ignore igniters which have already fired
175 /* Ignore disabled igniters
180 /* Check pyro state to see if it shoule fire
182 if (!pyro->delay_done) {
183 if (!ao_pyro_ready(pyro))
186 /* If there's a delay set, then remember when
189 if (pyro->flags & ao_pyro_delay)
190 pyro->delay_done = ao_time() + pyro->delay;
193 /* Check to see if we're just waiting for
194 * the delay to expire
196 if (pyro->delay_done) {
197 if ((int16_t) (ao_time() - pyro->delay_done) < 0)
206 __xdata struct ao_task ao_pyro_task;
208 #define NO_VALUE 0xff
210 #define AO_PYRO_NAME_LEN 3
213 #define ENABLE_HELP 1
223 char name[AO_PYRO_NAME_LEN];
224 enum ao_pyro_flag flag;
229 } ao_pyro_values[] = {
230 { "a<", ao_pyro_accel_less, offsetof(struct ao_pyro, accel_less), HELP("accel less (m/ss * 16)") },
231 { "a>", ao_pyro_accel_greater, offsetof(struct ao_pyro, accel_greater), HELP("accel greater (m/ss * 16)") },
233 { "s<", ao_pyro_speed_less, offsetof(struct ao_pyro, speed_less), HELP("speed less (m/s * 16)") },
234 { "s>", ao_pyro_speed_greater, offsetof(struct ao_pyro, speed_greater), HELP("speed greater (m/s * 16)") },
236 { "h<", ao_pyro_height_less, offsetof(struct ao_pyro, height_less), HELP("height less (m)") },
237 { "h>", ao_pyro_height_greater, offsetof(struct ao_pyro, height_greater), HELP("height greater (m)") },
240 { "o<", ao_pyro_orient_less, offsetof(struct ao_pyro, orient_less), HELP("orient less (deg)") },
241 { "o>", ao_pyro_orient_greater, offsetof(struct ao_pyro, orient_greater), HELP("orient greater (deg)") },
244 { "t<", ao_pyro_time_less, offsetof(struct ao_pyro, time_less), HELP("time less (s * 100)") },
245 { "t>", ao_pyro_time_greater, offsetof(struct ao_pyro, time_greater), HELP("time greater (s * 100)") },
247 { "A", ao_pyro_ascending, NO_VALUE, HELP("ascending") },
248 { "D", ao_pyro_descending, NO_VALUE, HELP("descending") },
250 { "m", ao_pyro_after_motor, offsetof(struct ao_pyro, motor), HELP("after motor") },
252 { "d", ao_pyro_delay, offsetof(struct ao_pyro, delay), HELP("delay before firing (s * 100)") },
253 { "", ao_pyro_none, NO_VALUE, HELP(NULL) },
257 ao_pyro_print_name(uint8_t v)
259 const char *s = ao_pyro_values[v].name;
260 printf ("%s%s", s, " " + strlen(s));
268 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
269 ao_pyro_print_name(v);
270 if (ao_pyro_values[v].offset != NO_VALUE)
274 printf ("%s\n", ao_pyro_values[v].help);
284 struct ao_pyro *pyro;
286 printf ("Pyro-count: %d\n", AO_PYRO_NUM);
287 for (p = 0; p < AO_PYRO_NUM; p++) {
288 printf ("Pyro %2d: ", p);
289 pyro = &ao_config.pyro[p];
291 printf ("<disabled>\n");
294 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
295 if (!(pyro->flags & ao_pyro_values[v].flag))
297 ao_pyro_print_name(v);
298 if (ao_pyro_values[v].offset != NO_VALUE) {
301 value = *((int16_t *) ((char *) pyro + ao_pyro_values[v].offset));
302 printf ("%6d ", value);
315 struct ao_pyro pyro_tmp;
316 char name[AO_PYRO_NAME_LEN];
323 switch (ao_cmd_lex_c) {
331 if (ao_cmd_status != ao_cmd_success)
334 if (p < 0 || AO_PYRO_NUM <= p) {
335 printf ("invalid pyro channel %d\n", p);
341 if (ao_cmd_lex_c == '\n')
344 for (c = 0; c < AO_PYRO_NAME_LEN - 1; c++) {
345 if (ao_cmd_is_white())
347 name[c] = ao_cmd_lex_c;
351 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
352 if (!strcmp (ao_pyro_values[v].name, name))
355 if (ao_pyro_values[v].flag == ao_pyro_none) {
356 printf ("invalid pyro field %s\n", name);
357 ao_cmd_status = ao_cmd_syntax_error;
360 pyro_tmp.flags |= ao_pyro_values[v].flag;
361 if (ao_pyro_values[v].offset != NO_VALUE) {
363 if (ao_cmd_status != ao_cmd_success)
365 *((int16_t *) ((char *) &pyro_tmp + ao_pyro_values[v].offset)) = ao_cmd_lex_i;
368 _ao_config_edit_start();
369 ao_config.pyro[p] = pyro_tmp;
370 _ao_config_edit_finish();
377 if (!ao_match_word("DoIt"))
381 if (ao_cmd_lex_i < 0 || AO_PYRO_NUM <= ao_cmd_lex_i)
383 ao_pyro_fire(ao_cmd_lex_i);
387 const struct ao_cmds ao_pyro_cmds[] = {
388 { ao_pyro_manual, "P DoIt <n>\0Fire igniter" },
396 ao_enable_output(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, 0);
399 ao_enable_output(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, 0);
402 ao_enable_output(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, 0);
405 ao_enable_output(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, 0);
408 ao_enable_output(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, 0);
411 ao_enable_output(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, 0);
414 ao_enable_output(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, 0);
417 ao_enable_output(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, 0);
419 ao_cmd_register(&ao_pyro_cmds[0]);
420 ao_add_task(&ao_pyro_task, ao_pyro, "pyro");