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_74hc165.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;
26 static __pdata uint8_t ao_pad_armed;
27 static __pdata uint16_t ao_pad_arm_time;
28 static __pdata uint8_t ao_pad_box;
29 static __xdata uint8_t ao_pad_disabled;
30 static __pdata uint16_t ao_pad_packet_time;
32 #ifndef AO_PAD_RSSI_MINIMUM
33 #define AO_PAD_RSSI_MINIMUM -90
39 static __pdata uint8_t ao_pad_debug;
40 #define PRINTD(...) (ao_pad_debug ? (printf(__VA_ARGS__), 0) : 0)
41 #define FLUSHD() (ao_pad_debug ? (flush(), 0) : 0)
51 ao_gpio_set(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, v);
54 ao_beep(v ? AO_BEEP_MID : 0);
65 ao_gpio_set(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, v);
77 while (!ao_pad_ignite)
78 ao_sleep(&ao_pad_ignite);
80 * Actually set the pad bits
84 if (ao_pad_ignite & (1 << 0))
85 pins |= (1 << AO_PAD_PIN_0);
88 if (ao_pad_ignite & (1 << 1))
89 pins |= (1 << AO_PAD_PIN_1);
92 if (ao_pad_ignite & (1 << 2))
93 pins |= (1 << AO_PAD_PIN_2);
96 if (ao_pad_ignite & (1 << 3))
97 pins |= (1 << AO_PAD_PIN_3);
99 PRINTD("ignite pins 0x%x\n", pins);
100 ao_gpio_set_bits(AO_PAD_PORT, pins);
101 while (ao_pad_ignite) {
104 ao_delay(AO_PAD_FIRE_TIME);
106 ao_gpio_clr_bits(AO_PAD_PORT, pins);
107 PRINTD("turn off pins 0x%x\n", pins);
111 #define AO_PAD_ARM_SIREN_INTERVAL 200
113 #ifndef AO_PYRO_R_PYRO_SENSE
114 #define AO_PYRO_R_PYRO_SENSE 100
115 #define AO_PYRO_R_SENSE_GND 27
116 #define AO_FIRE_R_POWER_FET 100
117 #define AO_FIRE_R_FET_SENSE 100
118 #define AO_FIRE_R_SENSE_GND 27
126 __pdata uint8_t prev = 0, cur = 0;
127 __pdata uint8_t beeping = 0;
128 __xdata volatile struct ao_data *packet;
129 __pdata uint16_t arm_beep_time = 0;
131 sample = ao_data_head;
133 __pdata int16_t pyro;
135 while (sample == ao_data_head)
136 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
140 packet = &ao_data_ring[sample];
141 sample = ao_data_ring_next(sample);
143 pyro = packet->adc.pyro;
145 #define VOLTS_TO_PYRO(x) ((int16_t) ((x) * ((1.0 * AO_PYRO_R_SENSE_GND) / \
146 (1.0 * (AO_PYRO_R_SENSE_GND + AO_PYRO_R_PYRO_SENSE)) / 3.3 * AO_ADC_MAX)))
149 #define VOLTS_TO_FIRE(x) ((int16_t) ((x) * ((1.0 * AO_FIRE_R_SENSE_GND) / \
150 (1.0 * (AO_FIRE_R_SENSE_GND + AO_FIRE_R_FET_SENSE)) / 3.3 * AO_ADC_MAX)))
152 /* convert ADC value to voltage in tenths, then add .2 for the diode drop */
153 query.battery = (packet->adc.batt + 96) / 192 + 2;
155 if (pyro > VOLTS_TO_PYRO(10)) {
156 query.arm_status = AO_PAD_ARM_STATUS_ARMED;
158 } else if (pyro < VOLTS_TO_PYRO(5)) {
159 query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
162 if ((ao_time() % 100) < 50)
164 query.arm_status = AO_PAD_ARM_STATUS_UNKNOWN;
167 if ((ao_time() - ao_pad_packet_time) > AO_SEC_TO_TICKS(2))
169 else if (ao_radio_cmac_rssi < AO_PAD_RSSI_MINIMUM)
174 for (c = 0; c < AO_PAD_NUM; c++) {
175 int16_t sense = packet->adc.sense[c];
176 uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
179 * pyro is run through a divider, so pyro = v_pyro * 27 / 127 ~= v_pyro / 20
180 * v_pyro = pyro * 127 / 27
198 * If the relay is closed, then sense will be 0
199 * If no igniter is present, then sense will be v_pyro * 27k/227k = pyro * 127 / 227 ~= pyro/2
200 * If igniter is present, then sense will be v_pyro * 27k/127k ~= v_pyro / 20 = pyro
203 #if AO_FIRE_R_POWER_FET
204 if (sense <= pyro / 8) {
205 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
206 if ((ao_time() % 100) < 50)
207 cur |= AO_LED_CONTINUITY(c);
209 if (pyro / 8 * 3 <= sense && sense <= pyro / 8 * 5)
210 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
211 else if (pyro / 8 * 7 <= sense) {
212 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
213 cur |= AO_LED_CONTINUITY(c);
216 if (sense >= pyro / 8 * 5) {
217 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
218 cur |= AO_LED_CONTINUITY(c);
220 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
223 query.igniter_status[c] = status;
226 PRINTD("change leds from %02x to %02x\n",
233 if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
240 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
241 if (arm_beep_time == 0) {
242 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
247 } else if (beeping) {
258 if (!ao_pad_disabled) {
260 ao_radio_recv_abort();
268 ao_wakeup (&ao_pad_disabled);
273 ao_pad_read_box(void)
275 uint8_t byte = ao_74hc165_read();
283 #define ao_pad_read_box() 0
287 #define ao_pad_read_box() PAD_BOX
293 int16_t time_difference;
300 while (ao_pad_disabled)
301 ao_sleep(&ao_pad_disabled);
302 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
303 PRINTD ("cmac_recv %d %d\n", ret, ao_radio_cmac_rssi);
304 if (ret != AO_RADIO_CMAC_OK)
306 ao_pad_packet_time = ao_time();
308 ao_pad_box = ao_pad_read_box();
310 PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
311 command.tick, command.box, ao_pad_box, command.cmd, command.channels);
313 switch (command.cmd) {
315 if (command.box != ao_pad_box) {
316 PRINTD ("box number mismatch\n");
320 if (command.channels & ~(AO_PAD_ALL_CHANNELS))
323 time_difference = command.tick - ao_time();
324 PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
325 if (time_difference < 0)
326 time_difference = -time_difference;
327 if (time_difference > 10) {
328 PRINTD ("time difference too large %d\n", time_difference);
332 ao_pad_armed = command.channels;
333 ao_pad_arm_time = ao_time();
336 case AO_LAUNCH_QUERY:
337 if (command.box != ao_pad_box) {
338 PRINTD ("box number mismatch\n");
342 query.tick = ao_time();
343 query.box = ao_pad_box;
344 query.channels = AO_PAD_ALL_CHANNELS;
345 query.armed = ao_pad_armed;
346 PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
347 query.tick, query.box, query.channels, query.armed,
349 query.igniter_status[0],
350 query.igniter_status[1],
351 query.igniter_status[2],
352 query.igniter_status[3]);
353 ao_radio_cmac_send(&query, sizeof (query));
357 PRINTD ("not armed\n");
360 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
361 PRINTD ("late pad arm_time %d time %d\n",
362 ao_pad_arm_time, ao_time());
366 ao_pad_ignite = ao_pad_armed;
367 ao_pad_arm_time = ao_time();
368 ao_wakeup(&ao_pad_ignite);
379 printf ("Arm switch: ");
380 switch (query.arm_status) {
381 case AO_PAD_ARM_STATUS_ARMED:
384 case AO_PAD_ARM_STATUS_DISARMED:
385 printf ("Disarmed\n");
387 case AO_PAD_ARM_STATUS_UNKNOWN:
388 printf ("Unknown\n");
392 for (c = 0; c < AO_PAD_NUM; c++) {
393 printf ("Pad %d: ", c);
394 switch (query.igniter_status[c]) {
395 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED: printf ("No igniter. Relay closed\n"); break;
396 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN: printf ("No igniter. Relay open\n"); break;
397 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN: printf ("Good igniter. Relay open\n"); break;
398 case AO_PAD_IGNITER_STATUS_UNKNOWN: printf ("Unknown\n"); break;
409 if (!ao_match_word("DoIt"))
412 if (ao_cmd_status != ao_cmd_success)
414 ignite = 1 << ao_cmd_lex_i;
416 if (ao_cmd_status != ao_cmd_success) {
418 ao_cmd_status = ao_cmd_success;
420 repeat = ao_cmd_lex_i;
421 while (repeat-- > 0) {
422 ao_pad_ignite = ignite;
423 ao_wakeup(&ao_pad_ignite);
424 ao_delay(AO_PAD_FIRE_TIME>>1);
428 static __xdata struct ao_task ao_pad_task;
429 static __xdata struct ao_task ao_pad_ignite_task;
430 static __xdata struct ao_task ao_pad_monitor_task;
434 ao_pad_set_debug(void)
437 if (ao_cmd_status == ao_cmd_success)
438 ao_pad_debug = ao_cmd_lex_i != 0;
443 ao_pad_alarm_debug(void)
445 uint8_t which, value;
447 if (ao_cmd_status != ao_cmd_success)
449 which = ao_cmd_lex_i;
451 if (ao_cmd_status != ao_cmd_success)
453 value = ao_cmd_lex_i;
454 printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
462 __code struct ao_cmds ao_pad_cmds[] = {
463 { ao_pad_test, "t\0Test pad continuity" },
464 { ao_pad_manual, "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
466 { ao_pad_set_debug, "D <0 off, 1 on>\0Debug" },
467 { ao_pad_alarm_debug, "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
476 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
479 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
482 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
485 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
488 ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
491 ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
493 ao_cmd_register(&ao_pad_cmds[0]);
494 ao_add_task(&ao_pad_task, ao_pad, "pad listener");
495 ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
496 ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");