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;
35 static __pdata uint8_t ao_pad_debug;
36 #define PRINTD(...) (ao_pad_debug ? (printf(__VA_ARGS__), 0) : 0)
37 #define FLUSHD() (ao_pad_debug ? (flush(), 0) : 0)
47 ao_gpio_set(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, v);
49 ao_beep(v ? AO_BEEP_MID : 0);
57 ao_gpio_set(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, v);
67 while (!ao_pad_ignite)
68 ao_sleep(&ao_pad_ignite);
70 * Actually set the pad bits
74 if (ao_pad_ignite & (1 << 0))
75 pins |= (1 << AO_PAD_PIN_0);
78 if (ao_pad_ignite & (1 << 1))
79 pins |= (1 << AO_PAD_PIN_1);
82 if (ao_pad_ignite & (1 << 2))
83 pins |= (1 << AO_PAD_PIN_2);
86 if (ao_pad_ignite & (1 << 3))
87 pins |= (1 << AO_PAD_PIN_3);
89 AO_PAD_PORT = (AO_PAD_PORT & (~AO_PAD_ALL_PINS)) | pins;
90 while (ao_pad_ignite) {
93 ao_delay(AO_PAD_FIRE_TIME);
95 AO_PAD_PORT &= ~(AO_PAD_ALL_PINS);
99 #define AO_PAD_ARM_SIREN_INTERVAL 200
106 __pdata uint8_t prev = 0, cur = 0;
107 __pdata uint8_t beeping = 0;
108 __xdata struct ao_data *packet;
109 __pdata uint16_t arm_beep_time = 0;
111 sample = ao_data_head;
113 __pdata int16_t pyro;
115 while (sample == ao_data_head)
116 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
119 packet = &ao_data_ring[sample];
120 sample = ao_data_ring_next(sample);
122 pyro = packet->adc.pyro;
124 #define VOLTS_TO_PYRO(x) ((int16_t) ((x) * 27.0 / 127.0 / 3.3 * 32767.0))
127 if (pyro > VOLTS_TO_PYRO(10)) {
128 query.arm_status = AO_PAD_ARM_STATUS_ARMED;
130 } else if (pyro < VOLTS_TO_PYRO(5)) {
131 query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
134 if ((ao_time() % 100) < 50)
136 query.arm_status = AO_PAD_ARM_STATUS_UNKNOWN;
139 if ((ao_time() - ao_pad_packet_time) > AO_SEC_TO_TICKS(2))
141 else if (ao_radio_cmac_rssi < -90)
146 for (c = 0; c < AO_PAD_NUM; c++) {
147 int16_t sense = packet->adc.sense[c];
148 uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
151 * pyro is run through a divider, so pyro = v_pyro * 27 / 127 ~= v_pyro / 20
152 * v_pyro = pyro * 127 / 27
162 * If the relay is closed, then sense will be 0
163 * If no igniter is present, then sense will be v_pyro * 27k/227k = pyro * 127 / 227 ~= pyro/2
164 * If igniter is present, then sense will be v_pyro * 27k/127k ~= v_pyro / 20 = pyro
167 if (sense <= pyro / 8) {
168 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
169 if ((ao_time() % 100) < 50)
170 cur |= AO_LED_CONTINUITY(c);
172 else if (pyro / 8 * 3 <= sense && sense <= pyro / 8 * 5)
173 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
174 else if (pyro / 8 * 7 <= sense) {
175 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
176 cur |= AO_LED_CONTINUITY(c);
178 query.igniter_status[c] = status;
181 PRINTD("change leds from %02x to %02x\n",
188 if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
195 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
196 if (arm_beep_time == 0) {
197 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
202 } else if (beeping) {
213 if (!ao_pad_disabled) {
215 ao_radio_recv_abort();
223 ao_wakeup (&ao_pad_disabled);
228 ao_pad_read_box(void)
230 uint8_t byte = ao_74hc165_read();
238 #define ao_pad_read_box() 0
244 int16_t time_difference;
251 while (ao_pad_disabled)
252 ao_sleep(&ao_pad_disabled);
253 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
254 PRINTD ("cmac_recv %d %d\n", ret, ao_radio_cmac_rssi);
255 if (ret != AO_RADIO_CMAC_OK)
257 ao_pad_packet_time = ao_time();
259 ao_pad_box = ao_pad_read_box();
261 PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
262 command.tick, command.box, ao_pad_box, command.cmd, command.channels);
264 switch (command.cmd) {
266 if (command.box != ao_pad_box) {
267 PRINTD ("box number mismatch\n");
271 if (command.channels & ~(AO_PAD_ALL_CHANNELS))
274 time_difference = command.tick - ao_time();
275 PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
276 if (time_difference < 0)
277 time_difference = -time_difference;
278 if (time_difference > 10) {
279 PRINTD ("time difference too large %d\n", time_difference);
283 ao_pad_armed = command.channels;
284 ao_pad_arm_time = ao_time();
286 /* fall through ... */
288 case AO_LAUNCH_QUERY:
289 if (command.box != ao_pad_box) {
290 PRINTD ("box number mismatch\n");
294 query.tick = ao_time();
295 query.box = ao_pad_box;
296 query.channels = AO_PAD_ALL_CHANNELS;
297 query.armed = ao_pad_armed;
298 PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
299 query.tick, query.box, query.channels, query.armed,
301 query.igniter_status[0],
302 query.igniter_status[1],
303 query.igniter_status[2],
304 query.igniter_status[3]);
305 ao_radio_cmac_send(&query, sizeof (query));
309 PRINTD ("not armed\n");
312 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
313 PRINTD ("late pad arm_time %d time %d\n",
314 ao_pad_arm_time, ao_time());
317 time_difference = command.tick - ao_time();
318 if (time_difference < 0)
319 time_difference = -time_difference;
320 if (time_difference > 10) {
321 PRINTD ("time different too large %d\n", time_difference);
325 ao_pad_ignite = ao_pad_armed;
326 ao_pad_arm_time = ao_time();
327 ao_wakeup(&ao_pad_ignite);
338 printf ("Arm switch: ");
339 switch (query.arm_status) {
340 case AO_PAD_ARM_STATUS_ARMED:
343 case AO_PAD_ARM_STATUS_DISARMED:
344 printf ("Disarmed\n");
346 case AO_PAD_ARM_STATUS_UNKNOWN:
347 printf ("Unknown\n");
351 for (c = 0; c < AO_PAD_NUM; c++) {
352 printf ("Pad %d: ", c);
353 switch (query.igniter_status[c]) {
354 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED: printf ("No igniter. Relay closed\n"); break;
355 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN: printf ("No igniter. Relay open\n"); break;
356 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN: printf ("Good igniter. Relay open\n"); break;
357 case AO_PAD_IGNITER_STATUS_UNKNOWN: printf ("Unknown\n"); break;
366 if (!ao_match_word("DoIt"))
369 if (ao_cmd_status != ao_cmd_success)
371 ao_pad_ignite = 1 << ao_cmd_lex_i;
372 ao_wakeup(&ao_pad_ignite);
375 static __xdata struct ao_task ao_pad_task;
376 static __xdata struct ao_task ao_pad_ignite_task;
377 static __xdata struct ao_task ao_pad_monitor_task;
381 ao_pad_set_debug(void)
384 if (ao_cmd_status == ao_cmd_success)
385 ao_pad_debug = ao_cmd_lex_i != 0;
390 ao_pad_alarm_debug(void)
392 uint8_t which, value;
394 if (ao_cmd_status != ao_cmd_success)
396 which = ao_cmd_lex_i;
398 if (ao_cmd_status != ao_cmd_success)
400 value = ao_cmd_lex_i;
401 printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
409 __code struct ao_cmds ao_pad_cmds[] = {
410 { ao_pad_test, "t\0Test pad continuity" },
411 { ao_pad_manual, "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
413 { ao_pad_set_debug, "D <0 off, 1 on>\0Debug" },
414 { ao_pad_alarm_debug, "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
423 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
426 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
429 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
432 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
435 ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
438 ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
440 ao_cmd_register(&ao_pad_cmds[0]);
441 ao_add_task(&ao_pad_task, ao_pad, "pad listener");
442 ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
443 ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");