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)
69 case ao_pyro_orient_less:
70 // if (ao_orient <= pyro->orient_less)
73 case ao_pyro_orient_greater:
74 // if (ao_orient >= pyro->orient_greater)
78 case ao_pyro_time_less:
79 if ((int16_t) (ao_time() - ao_boost_tick) <= pyro->time_less)
82 case ao_pyro_time_greater:
83 if ((int16_t) (ao_time() - ao_boost_tick) >= pyro->time_greater)
87 case ao_pyro_ascending:
91 case ao_pyro_descending:
96 case ao_pyro_after_motor:
97 if (ao_motor_number == pyro->motor)
102 /* handled separately */
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); \
121 ao_pyro_fire(uint8_t p)
125 case 0: ao_pyro_fire_port(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0); break;
128 case 1: ao_pyro_fire_port(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1); break;
131 case 2: ao_pyro_fire_port(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2); break;
134 case 3: ao_pyro_fire_port(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3); break;
137 case 4: ao_pyro_fire_port(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4); break;
140 case 5: ao_pyro_fire_port(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5); break;
143 case 6: ao_pyro_fire_port(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6); break;
146 case 7: ao_pyro_fire_port(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7); break;
150 ao_delay(AO_MS_TO_TICKS(50));
157 struct ao_pyro *pyro;
160 while (ao_flight_state < ao_flight_boost)
161 ao_sleep(&ao_flight_state);
164 ao_delay(AO_MS_TO_TICKS(100));
165 for (p = 0; p < AO_PYRO_NUM; p++) {
166 pyro = &ao_config.pyro[p];
168 /* Ignore igniters which have already fired
173 /* Ignore disabled igniters
178 /* Check pyro state to see if it shoule fire
180 if (!pyro->delay_done) {
181 if (!ao_pyro_ready(pyro))
184 /* If there's a delay set, then remember when
187 if (pyro->flags & ao_pyro_delay)
188 pyro->delay_done = ao_time() + pyro->delay;
191 /* Check to see if we're just waiting for
192 * the delay to expire
194 if (pyro->delay_done) {
195 if ((int16_t) (ao_time() - pyro->delay_done) < 0)
204 __xdata struct ao_task ao_pyro_task;
206 #define NO_VALUE 0xff
208 #define AO_PYRO_NAME_LEN 3
211 char name[AO_PYRO_NAME_LEN];
212 enum ao_pyro_flag flag;
215 } ao_pyro_values[] = {
216 { "a<", ao_pyro_accel_less, offsetof(struct ao_pyro, accel_less), "accel less (m/ss * 16)" },
217 { "a>", ao_pyro_accel_greater, offsetof(struct ao_pyro, accel_greater), "accel greater (m/ss * 16)" },
219 { "s<", ao_pyro_speed_less, offsetof(struct ao_pyro, speed_less), "speed less (m/s * 16)" },
220 { "s>", ao_pyro_speed_greater, offsetof(struct ao_pyro, speed_greater), "speed greater (m/s * 16)" },
222 { "h<", ao_pyro_height_less, offsetof(struct ao_pyro, height_less), "height less (m)" },
223 { "h>", ao_pyro_height_greater, offsetof(struct ao_pyro, height_greater), "height greater (m)" },
225 { "o<", ao_pyro_orient_less, offsetof(struct ao_pyro, orient_less), "orient less (deg)" },
226 { "o>", ao_pyro_orient_greater, offsetof(struct ao_pyro, orient_greater), "orient greater (deg)" },
228 { "t<", ao_pyro_time_less, offsetof(struct ao_pyro, time_less), "time less (s * 100)" },
229 { "t>", ao_pyro_time_greater, offsetof(struct ao_pyro, time_greater), "time greater (s * 100)" },
231 { "A", ao_pyro_ascending, NO_VALUE, "ascending" },
232 { "D", ao_pyro_descending, NO_VALUE, "descending" },
234 { "m", ao_pyro_after_motor, offsetof(struct ao_pyro, motor), "after motor" },
236 { "d", ao_pyro_delay, offsetof(struct ao_pyro, delay), "delay before firing (s * 100)" },
237 { "", ao_pyro_none, NO_VALUE, NULL },
241 ao_pyro_print_name(uint8_t v)
243 const char *s = ao_pyro_values[v].name;
244 printf ("%s%s", s, " " + strlen(s));
251 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
252 ao_pyro_print_name(v);
253 if (ao_pyro_values[v].offset != NO_VALUE)
257 printf ("%s\n", ao_pyro_values[v].help);
266 struct ao_pyro *pyro;
268 for (p = 0; p < AO_PYRO_NUM; p++) {
269 printf ("Pyro %2d: ", p);
270 pyro = &ao_config.pyro[p];
272 printf ("<disabled>\n");
275 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
276 if (!(pyro->flags & ao_pyro_values[v].flag))
278 ao_pyro_print_name(v);
279 if (ao_pyro_values[v].offset != NO_VALUE) {
282 value = *((int16_t *) ((char *) pyro + ao_pyro_values[v].offset));
283 printf ("%6d ", value);
296 struct ao_pyro pyro_tmp;
297 char name[AO_PYRO_NAME_LEN];
302 switch (ao_cmd_lex_c) {
309 if (ao_cmd_status != ao_cmd_success)
312 if (p < 0 || AO_PYRO_NUM <= p) {
313 printf ("invalid pyro channel %d\n", p);
319 if (ao_cmd_lex_c == '\n')
322 for (c = 0; c < AO_PYRO_NAME_LEN - 1; c++) {
323 if (ao_cmd_is_white())
325 name[c] = ao_cmd_lex_c;
329 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
330 if (!strcmp (ao_pyro_values[v].name, name))
333 if (ao_pyro_values[v].flag == ao_pyro_none) {
334 printf ("invalid pyro field %s\n", name);
335 ao_cmd_status = ao_cmd_syntax_error;
338 pyro_tmp.flags |= ao_pyro_values[v].flag;
339 if (ao_pyro_values[v].offset != NO_VALUE) {
341 if (ao_cmd_status != ao_cmd_success)
343 *((int16_t *) ((char *) &pyro_tmp + ao_pyro_values[v].offset)) = ao_cmd_lex_i;
346 _ao_config_edit_start();
347 ao_config.pyro[p] = pyro_tmp;
348 _ao_config_edit_finish();
355 ao_enable_output(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, 0);
358 ao_enable_output(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, 0);
361 ao_enable_output(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, 0);
364 ao_enable_output(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, 0);
367 ao_enable_output(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, 0);
370 ao_enable_output(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, 0);
373 ao_enable_output(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, 0);
376 ao_enable_output(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, 0);
378 ao_add_task(&ao_pyro_task, ao_pyro, "pyro");