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_74hc497.h>
22 __xdata uint8_t ao_pad_ignite;
24 #define ao_pad_igniter_status(c) AO_PAD_IGNITER_STATUS_UNKNOWN
25 #define ao_pad_arm_status() AO_PAD_ARM_STATUS_UNKNOWN
28 #define PRINTD(...) printf(__VA_ARGS__)
37 while (!ao_pad_ignite)
38 ao_sleep(&ao_pad_ignite);
40 * Actually set the pad bits
42 AO_PAD_PORT = (AO_PAD_PORT & (~AO_PAD_ALL_PINS)) | ao_pad_ignite;
43 while (ao_pad_ignite) {
45 ao_delay(AO_PAD_FIRE_TIME);
47 AO_PAD_PORT &= ~(AO_PAD_ALL_PINS);
55 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));
64 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
71 static __pdata uint8_t ao_pad_armed;
72 static __pdata uint16_t ao_pad_arm_time;
73 static __pdata uint8_t ao_pad_box;
74 static __xdata uint8_t ao_pad_disabled;
79 if (!ao_pad_disabled) {
81 ao_radio_recv_abort();
89 ao_wakeup (&ao_pad_disabled);
95 static __xdata struct ao_pad_command command;
96 static __xdata struct ao_pad_query query;
97 int16_t time_difference;
100 ao_led_off(AO_LED_RED);
101 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
102 ao_pad_box = ao_74hc497_read();
105 while (ao_pad_disabled)
106 ao_sleep(&ao_pad_disabled);
107 if (ao_radio_cmac_recv(&command, sizeof (command), 0) != AO_RADIO_CMAC_OK)
110 PRINTD ("tick %d serial %d cmd %d channel %d\n",
111 command.tick, command.serial, command.cmd, command.channel);
113 switch (command.cmd) {
115 if (command.box != ao_pad_box) {
116 PRINTD ("box number mismatch\n");
120 if (command.channels & ~(AO_PAD_ALL_PINS))
123 time_difference = command.tick - ao_time();
124 PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
125 if (time_difference < 0)
126 time_difference = -time_difference;
127 if (time_difference > 10) {
128 PRINTD ("time difference too large %d\n", time_difference);
132 ao_pad_armed = command.channels;
133 ao_pad_arm_time = ao_time();
135 /* fall through ... */
137 case AO_LAUNCH_QUERY:
138 if (command.box != ao_pad_box) {
139 PRINTD ("box number mismatch\n");
143 query.tick = ao_time();
144 query.box = ao_pad_box;
145 query.channels = AO_PAD_ALL_PINS;
146 query.armed = ao_pad_armed;
147 query.arm_status = ao_pad_arm_status();
148 for (c = 0; c < AO_PAD_NUM; c++)
149 query.igniter_status[c] = ao_pad_igniter_status(c);
150 PRINTD ("query tick %d serial %d channel %d valid %d arm %d igniter %d\n",
151 query.tick, query.serial, query.channel, query.valid, query.arm_status,
152 query.igniter_status);
153 ao_radio_cmac_send(&query, sizeof (query));
157 PRINTD ("not armed\n");
160 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
161 PRINTD ("late pad arm_time %d time %d\n",
162 ao_pad_arm_time, ao_time());
165 time_difference = command.tick - ao_time();
166 if (time_difference < 0)
167 time_difference = -time_difference;
168 if (time_difference > 10) {
169 PRINTD ("time different too large %d\n", time_difference);
173 ao_pad_ignite = ao_pad_armed;
174 ao_wakeup(&ao_pad_ignite);
184 switch (ao_igniter_status(ao_igniter_drogue)) {
185 case ao_igniter_ready:
186 case ao_igniter_active:
188 switch (ao_igniter_status(ao_igniter_main)) {
190 printf("unknown status\n");
192 case ao_igniter_ready:
193 printf("igniter good\n");
195 case ao_igniter_open:
196 printf("igniter bad\n");
201 printf("Disarmed\n");
210 if (!ao_match_word("DoIt"))
214 ao_wakeup(&ao_pad_ignite);
217 static __xdata struct ao_task ao_pad_task;
218 static __xdata struct ao_task ao_pad_ignite_task;
219 static __xdata struct ao_task ao_pad_status_task;
221 __code struct ao_cmds ao_pad_cmds[] = {
222 { ao_pad_test, "t\0Test pad continuity" },
223 { ao_pad_manual, "i <key>\0Fire igniter. <key> is doit with D&I" },
231 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
234 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
237 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
240 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
242 ao_cmd_register(&ao_pad_cmds[0]);
243 ao_add_task(&ao_pad_task, ao_pad, "pad listener");
244 ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
245 ao_add_task(&ao_pad_status_task, ao_pad_status, "pad status");