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>
21 #include <ao_radio_cmac.h>
23 static __xdata uint8_t ao_pad_ignite;
24 static __xdata struct ao_pad_command command;
25 static __xdata struct ao_pad_query query;
28 #define PRINTD(...) printf(__VA_ARGS__)
29 #define FLUSHD() flush()
39 while (!ao_pad_ignite)
40 ao_sleep(&ao_pad_ignite);
42 * Actually set the pad bits
44 AO_PAD_PORT = (AO_PAD_PORT & (~AO_PAD_ALL_PINS)) | ao_pad_ignite;
45 while (ao_pad_ignite) {
47 ao_delay(AO_PAD_FIRE_TIME);
49 AO_PAD_PORT &= ~(AO_PAD_ALL_PINS);
58 __pdata uint8_t prev = 0, cur = 0;
59 __pdata uint8_t beeping = 0;
60 __xdata struct ao_data *packet;
62 sample = ao_data_head;
66 while (sample == ao_data_head)
67 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
70 packet = &ao_data_ring[sample];
71 sample = ao_data_ring_next(sample);
73 pyro = packet->adc.pyro;
75 #define VOLTS_TO_PYRO(x) ((int16_t) ((x) * 27.0 / 127.0 / 3.3 * 32767.0))
78 if (pyro > VOLTS_TO_PYRO(4))
79 query.arm_status = AO_PAD_ARM_STATUS_ARMED;
80 else if (pyro < VOLTS_TO_PYRO(1))
81 query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
83 query.arm_status = AO_PAD_ARM_STATUS_UNKNOWN;
85 for (c = 0; c < AO_PAD_NUM; c++) {
86 int16_t sense = packet->adc.sense[c];
87 uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
89 if (query.arm_status == AO_PAD_ARM_STATUS_ARMED) {
91 * pyro is run through a divider, so pyro = v_pyro * 27 / 127 ~= v_pyro / 20
92 * v_pyro = pyro * 127 / 27
102 * If the relay is closed, then sense will be 0
103 * If no igniter is present, then sense will be v_pyro * 27k/227k = pyro * 127 / 227 ~= pyro/2
104 * If igniter is present, then sense will be v_pyro * 27k/127k ~= v_pyro / 20 = pyro
107 if (sense <= pyro / 8)
108 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
109 else if (pyro / 8 * 3 <= sense && sense <= pyro / 8 * 5)
110 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
111 else if (pyro / 8 * 7 <= sense) {
112 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
113 cur |= AO_LED_CONTINUITY(c);
116 query.igniter_status[c] = status;
119 ao_led_set_mask(cur, AO_LED_CONTINUITY_MASK);
123 if (pyro > VOLTS_TO_PYRO(9) && sample == 0) {
125 ao_beep(AO_BEEP_HIGH);
126 } else if (beeping) {
133 static __pdata uint8_t ao_pad_armed;
134 static __pdata uint16_t ao_pad_arm_time;
135 static __pdata uint8_t ao_pad_box;
136 static __xdata uint8_t ao_pad_disabled;
141 if (!ao_pad_disabled) {
143 ao_radio_recv_abort();
151 ao_wakeup (&ao_pad_disabled);
157 int16_t time_difference;
159 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
160 ao_pad_box = ao_74hc497_read();
162 ao_led_on(AO_LED_POWER);
165 while (ao_pad_disabled)
166 ao_sleep(&ao_pad_disabled);
167 if (ao_radio_cmac_recv(&command, sizeof (command), 0) != AO_RADIO_CMAC_OK)
170 PRINTD ("tick %d serial %d cmd %d channel %d\n",
171 command.tick, command.serial, command.cmd, command.channel);
173 switch (command.cmd) {
175 if (command.box != ao_pad_box) {
176 PRINTD ("box number mismatch\n");
180 if (command.channels & ~(AO_PAD_ALL_PINS))
183 time_difference = command.tick - ao_time();
184 PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
185 if (time_difference < 0)
186 time_difference = -time_difference;
187 if (time_difference > 10) {
188 PRINTD ("time difference too large %d\n", time_difference);
192 ao_pad_armed = command.channels;
193 ao_pad_arm_time = ao_time();
195 /* fall through ... */
197 case AO_LAUNCH_QUERY:
198 if (command.box != ao_pad_box) {
199 PRINTD ("box number mismatch\n");
203 query.tick = ao_time();
204 query.box = ao_pad_box;
205 query.channels = AO_PAD_ALL_PINS;
206 query.armed = ao_pad_armed;
207 PRINTD ("query tick %d serial %d channel %d valid %d arm %d igniter %d\n",
208 query.tick, query.serial, query.channel, query.valid, query.arm_status,
209 query.igniter_status);
210 ao_radio_cmac_send(&query, sizeof (query));
214 PRINTD ("not armed\n");
217 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
218 PRINTD ("late pad arm_time %d time %d\n",
219 ao_pad_arm_time, ao_time());
222 time_difference = command.tick - ao_time();
223 if (time_difference < 0)
224 time_difference = -time_difference;
225 if (time_difference > 10) {
226 PRINTD ("time different too large %d\n", time_difference);
230 ao_pad_ignite = ao_pad_armed;
231 ao_wakeup(&ao_pad_ignite);
242 printf ("Arm switch: ");
243 switch (query.arm_status) {
244 case AO_PAD_ARM_STATUS_ARMED:
247 case AO_PAD_ARM_STATUS_DISARMED:
248 printf ("Disarmed\n");
250 case AO_PAD_ARM_STATUS_UNKNOWN:
251 printf ("Unknown\n");
255 for (c = 0; c < AO_PAD_NUM; c++) {
257 switch (query.igniter_status[c]) {
258 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED: printf ("No igniter. Relay closed\n"); break;
259 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN: printf ("No igniter. Relay open\n"); break;
260 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN: printf ("Good igniter. Relay open\n"); break;
261 case AO_PAD_IGNITER_STATUS_UNKNOWN: printf ("Unknown\n"); break;
270 if (!ao_match_word("DoIt"))
273 if (ao_cmd_status != ao_cmd_success)
275 ao_pad_ignite = 1 << ao_cmd_lex_i;
276 ao_wakeup(&ao_pad_ignite);
279 static __xdata struct ao_task ao_pad_task;
280 static __xdata struct ao_task ao_pad_ignite_task;
281 static __xdata struct ao_task ao_pad_monitor_task;
283 __code struct ao_cmds ao_pad_cmds[] = {
284 { ao_pad_test, "t\0Test pad continuity" },
285 { ao_pad_manual, "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
293 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
296 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
299 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
302 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
304 ao_cmd_register(&ao_pad_cmds[0]);
305 ao_add_task(&ao_pad_task, ao_pad, "pad listener");
306 ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
307 ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");