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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include <ao_74hc165.h>
22 #include <ao_radio_cmac.h>
24 static __xdata uint8_t ao_pad_ignite;
25 static __xdata struct ao_pad_command command;
26 static __xdata struct ao_pad_query query;
27 static __pdata uint8_t ao_pad_armed;
28 static __pdata uint16_t ao_pad_arm_time;
29 static __pdata uint8_t ao_pad_box;
30 static __xdata uint8_t ao_pad_disabled;
31 static __pdata uint16_t ao_pad_packet_time;
33 #ifndef AO_PAD_RSSI_MINIMUM
34 #define AO_PAD_RSSI_MINIMUM -90
40 static __pdata uint8_t ao_pad_debug;
41 #define PRINTD(...) (ao_pad_debug ? (printf(__VA_ARGS__), 0) : 0)
42 #define FLUSHD() (ao_pad_debug ? (flush(), 0) : 0)
52 ao_gpio_set(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, v);
55 ao_beep(v ? AO_BEEP_MID : 0);
66 ao_gpio_set(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, v);
78 while (!ao_pad_ignite)
79 ao_sleep(&ao_pad_ignite);
81 * Actually set the pad bits
85 if (ao_pad_ignite & (1 << 0))
86 pins |= (1 << AO_PAD_PIN_0);
89 if (ao_pad_ignite & (1 << 1))
90 pins |= (1 << AO_PAD_PIN_1);
93 if (ao_pad_ignite & (1 << 2))
94 pins |= (1 << AO_PAD_PIN_2);
97 if (ao_pad_ignite & (1 << 3))
98 pins |= (1 << AO_PAD_PIN_3);
100 PRINTD("ignite pins 0x%x\n", pins);
101 ao_gpio_set_bits(AO_PAD_PORT, pins);
102 while (ao_pad_ignite) {
105 ao_delay(AO_PAD_FIRE_TIME);
107 ao_gpio_clr_bits(AO_PAD_PORT, pins);
108 PRINTD("turn off pins 0x%x\n", pins);
112 #define AO_PAD_ARM_SIREN_INTERVAL 200
114 #ifndef AO_PYRO_R_PYRO_SENSE
115 #define AO_PYRO_R_PYRO_SENSE 100
116 #define AO_PYRO_R_SENSE_GND 27
117 #define AO_FIRE_R_POWER_FET 100
118 #define AO_FIRE_R_FET_SENSE 100
119 #define AO_FIRE_R_SENSE_GND 27
127 __pdata uint8_t prev = 0, cur = 0;
128 __pdata uint8_t beeping = 0;
129 __xdata volatile struct ao_data *packet;
130 __pdata uint16_t arm_beep_time = 0;
132 sample = ao_data_head;
134 __pdata int16_t pyro;
136 while (sample == ao_data_head)
137 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
141 packet = &ao_data_ring[sample];
142 sample = ao_data_ring_next(sample);
144 pyro = packet->adc.pyro;
146 #define VOLTS_TO_PYRO(x) ((int16_t) ((x) * ((1.0 * AO_PYRO_R_SENSE_GND) / \
147 (1.0 * (AO_PYRO_R_SENSE_GND + AO_PYRO_R_PYRO_SENSE)) / 3.3 * AO_ADC_MAX)))
150 #define VOLTS_TO_FIRE(x) ((int16_t) ((x) * ((1.0 * AO_FIRE_R_SENSE_GND) / \
151 (1.0 * (AO_FIRE_R_SENSE_GND + AO_FIRE_R_FET_SENSE)) / 3.3 * AO_ADC_MAX)))
153 /* convert ADC value to voltage in tenths, then add .2 for the diode drop */
154 query.battery = (packet->adc.batt + 96) / 192 + 2;
156 if (pyro > VOLTS_TO_PYRO(10)) {
157 query.arm_status = AO_PAD_ARM_STATUS_ARMED;
159 #if AO_FIRE_R_POWER_FET
160 } else if (pyro > VOLTS_TO_PYRO(5)) {
161 if ((ao_time() % 100) < 50)
163 query.arm_status = AO_PAD_ARM_STATUS_UNKNOWN;
167 query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
170 if ((ao_time() - ao_pad_packet_time) > AO_SEC_TO_TICKS(2))
172 else if (ao_radio_cmac_rssi < AO_PAD_RSSI_MINIMUM)
177 for (c = 0; c < AO_PAD_NUM; c++) {
178 int16_t sense = packet->adc.sense[c];
179 uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
182 * pyro is run through a divider, so pyro = v_pyro * 27 / 127 ~= v_pyro / 20
183 * v_pyro = pyro * 127 / 27
201 * If the relay is closed, then sense will be 0
202 * If no igniter is present, then sense will be v_pyro * 27k/227k = pyro * 127 / 227 ~= pyro/2
203 * If igniter is present, then sense will be v_pyro * 27k/127k ~= v_pyro / 20 = pyro
206 #if AO_FIRE_R_POWER_FET
207 if (sense <= pyro / 8) {
208 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
209 if ((ao_time() % 100) < 50)
210 cur |= AO_LED_CONTINUITY(c);
212 if (pyro / 8 * 3 <= sense && sense <= pyro / 8 * 5)
213 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
214 else if (pyro / 8 * 7 <= sense) {
215 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
216 cur |= AO_LED_CONTINUITY(c);
219 if (sense >= pyro / 8 * 5) {
220 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
221 cur |= AO_LED_CONTINUITY(c);
223 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
226 query.igniter_status[c] = status;
229 PRINTD("change leds from %02x to %02x\n",
236 if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
243 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
244 if (arm_beep_time == 0) {
245 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
250 } else if (beeping) {
261 if (!ao_pad_disabled) {
263 ao_radio_recv_abort();
271 ao_wakeup (&ao_pad_disabled);
276 ao_pad_read_box(void)
278 uint8_t byte = ao_74hc165_read();
287 #if HAS_FIXED_PAD_BOX
288 #define ao_pad_read_box() ao_config.pad_box
292 #define ao_pad_read_box() PAD_BOX
298 int16_t time_difference;
305 while (ao_pad_disabled)
306 ao_sleep(&ao_pad_disabled);
307 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
308 PRINTD ("cmac_recv %d %d\n", ret, ao_radio_cmac_rssi);
309 if (ret != AO_RADIO_CMAC_OK)
311 ao_pad_packet_time = ao_time();
313 ao_pad_box = ao_pad_read_box();
315 PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
316 command.tick, command.box, ao_pad_box, command.cmd, command.channels);
318 switch (command.cmd) {
320 if (command.box != ao_pad_box) {
321 PRINTD ("box number mismatch\n");
325 if (command.channels & ~(AO_PAD_ALL_CHANNELS))
328 time_difference = command.tick - ao_time();
329 PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
330 if (time_difference < 0)
331 time_difference = -time_difference;
332 if (time_difference > 10) {
333 PRINTD ("time difference too large %d\n", time_difference);
337 ao_pad_armed = command.channels;
338 ao_pad_arm_time = ao_time();
342 if (command.box != ao_pad_box) {
343 PRINTD ("box number mismatch\n");
347 query.tick = ao_time();
348 query.box = ao_pad_box;
349 query.channels = AO_PAD_ALL_CHANNELS;
350 query.armed = ao_pad_armed;
351 PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
352 query.tick, query.box, query.channels, query.armed,
354 query.igniter_status[0],
355 query.igniter_status[1],
356 query.igniter_status[2],
357 query.igniter_status[3]);
358 ao_radio_cmac_send(&query, sizeof (query));
362 PRINTD ("not armed\n");
365 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
366 PRINTD ("late pad arm_time %d time %d\n",
367 ao_pad_arm_time, ao_time());
371 ao_pad_ignite = ao_pad_armed;
372 ao_pad_arm_time = ao_time();
373 ao_wakeup(&ao_pad_ignite);
377 PRINTD ("not armed\n");
381 if (!ao_log_running) ao_log_start();
383 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
384 PRINTD ("late pad arm_time %d time %d\n",
385 ao_pad_arm_time, ao_time());
389 ao_pad_ignite = ao_pad_armed;
390 ao_pad_arm_time = ao_time();
391 ao_wakeup(&ao_pad_ignite);
393 case AO_PAD_ENDSTATIC:
407 printf ("Arm switch: ");
408 switch (query.arm_status) {
409 case AO_PAD_ARM_STATUS_ARMED:
412 case AO_PAD_ARM_STATUS_DISARMED:
413 printf ("Disarmed\n");
415 case AO_PAD_ARM_STATUS_UNKNOWN:
416 printf ("Unknown\n");
420 for (c = 0; c < AO_PAD_NUM; c++) {
421 printf ("Pad %d: ", c);
422 switch (query.igniter_status[c]) {
423 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED: printf ("No igniter. Relay closed\n"); break;
424 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN: printf ("No igniter. Relay open\n"); break;
425 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN: printf ("Good igniter. Relay open\n"); break;
426 case AO_PAD_IGNITER_STATUS_UNKNOWN: printf ("Unknown\n"); break;
437 if (!ao_match_word("DoIt"))
440 if (ao_cmd_status != ao_cmd_success)
442 ignite = 1 << ao_cmd_lex_i;
444 if (ao_cmd_status != ao_cmd_success) {
446 ao_cmd_status = ao_cmd_success;
448 repeat = ao_cmd_lex_i;
449 while (repeat-- > 0) {
450 ao_pad_ignite = ignite;
451 ao_wakeup(&ao_pad_ignite);
452 ao_delay(AO_PAD_FIRE_TIME>>1);
456 static __xdata struct ao_task ao_pad_task;
457 static __xdata struct ao_task ao_pad_ignite_task;
458 static __xdata struct ao_task ao_pad_monitor_task;
462 ao_pad_set_debug(void)
465 if (ao_cmd_status == ao_cmd_success)
466 ao_pad_debug = ao_cmd_lex_i != 0;
471 ao_pad_alarm_debug(void)
473 uint8_t which, value;
475 if (ao_cmd_status != ao_cmd_success)
477 which = ao_cmd_lex_i;
479 if (ao_cmd_status != ao_cmd_success)
481 value = ao_cmd_lex_i;
482 printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
490 __code struct ao_cmds ao_pad_cmds[] = {
491 { ao_pad_test, "t\0Test pad continuity" },
492 { ao_pad_manual, "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
494 { ao_pad_set_debug, "D <0 off, 1 on>\0Debug" },
495 { ao_pad_alarm_debug, "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
504 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
507 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
510 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
513 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
516 ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
519 ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
521 ao_cmd_register(&ao_pad_cmds[0]);
522 ao_add_task(&ao_pad_task, ao_pad, "pad listener");
523 ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
524 ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");