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;
34 static __pdata uint8_t ao_pad_debug;
35 #define PRINTD(...) (ao_pad_debug ? (printf(__VA_ARGS__), 0) : 0)
36 #define FLUSHD() (ao_pad_debug ? (flush(), 0) : 0)
46 ao_gpio_set(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, v);
48 ao_beep(v ? AO_BEEP_MID : 0);
56 ao_gpio_set(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, v);
66 while (!ao_pad_ignite)
67 ao_sleep(&ao_pad_ignite);
69 * Actually set the pad bits
73 if (ao_pad_ignite & (1 << 0))
74 pins |= (1 << AO_PAD_PIN_0);
77 if (ao_pad_ignite & (1 << 1))
78 pins |= (1 << AO_PAD_PIN_1);
81 if (ao_pad_ignite & (1 << 2))
82 pins |= (1 << AO_PAD_PIN_2);
85 if (ao_pad_ignite & (1 << 3))
86 pins |= (1 << AO_PAD_PIN_3);
88 AO_PAD_PORT = (AO_PAD_PORT & (~AO_PAD_ALL_PINS)) | pins;
89 while (ao_pad_ignite) {
92 ao_delay(AO_PAD_FIRE_TIME);
94 AO_PAD_PORT &= ~(AO_PAD_ALL_PINS);
98 #define AO_PAD_ARM_SIREN_INTERVAL 200
105 __pdata uint8_t prev = 0, cur = 0;
106 __pdata uint8_t beeping = 0;
107 __xdata struct ao_data *packet;
108 __pdata uint16_t arm_beep_time = 0;
110 sample = ao_data_head;
112 __pdata int16_t pyro;
114 while (sample == ao_data_head)
115 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
118 packet = &ao_data_ring[sample];
119 sample = ao_data_ring_next(sample);
121 pyro = packet->adc.pyro;
123 #define VOLTS_TO_PYRO(x) ((int16_t) ((x) * 27.0 / 127.0 / 3.3 * 32767.0))
126 if (pyro > VOLTS_TO_PYRO(10)) {
127 query.arm_status = AO_PAD_ARM_STATUS_ARMED;
129 } else if (pyro < VOLTS_TO_PYRO(5)) {
130 query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
133 if ((ao_time() % 100) < 50)
135 query.arm_status = AO_PAD_ARM_STATUS_UNKNOWN;
139 for (c = 0; c < AO_PAD_NUM; c++) {
140 int16_t sense = packet->adc.sense[c];
141 uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
144 * pyro is run through a divider, so pyro = v_pyro * 27 / 127 ~= v_pyro / 20
145 * v_pyro = pyro * 127 / 27
155 * If the relay is closed, then sense will be 0
156 * If no igniter is present, then sense will be v_pyro * 27k/227k = pyro * 127 / 227 ~= pyro/2
157 * If igniter is present, then sense will be v_pyro * 27k/127k ~= v_pyro / 20 = pyro
160 if (sense <= pyro / 8) {
161 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
162 if ((ao_time() % 100) < 50)
163 cur |= AO_LED_CONTINUITY(c);
165 else if (pyro / 8 * 3 <= sense && sense <= pyro / 8 * 5)
166 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
167 else if (pyro / 8 * 7 <= sense) {
168 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
169 cur |= AO_LED_CONTINUITY(c);
171 query.igniter_status[c] = status;
174 PRINTD("change leds from %02x to %02x mask %02x\n",
175 prev, cur, AO_LED_CONTINUITY_MASK|AO_LED_ARMED);
176 ao_led_set_mask(cur, AO_LED_CONTINUITY_MASK | AO_LED_ARMED);
180 if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
187 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
188 if (arm_beep_time == 0) {
189 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
194 } else if (beeping) {
205 if (!ao_pad_disabled) {
207 ao_radio_recv_abort();
215 ao_wakeup (&ao_pad_disabled);
220 ao_pad_read_box(void)
222 uint8_t byte = ao_74hc165_read();
230 #define ao_pad_read_box() 0
236 int16_t time_difference;
241 ao_led_on(AO_LED_POWER);
244 while (ao_pad_disabled)
245 ao_sleep(&ao_pad_disabled);
246 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
247 PRINTD ("cmac_recv %d\n", ret);
248 if (ret != AO_RADIO_CMAC_OK)
251 ao_pad_box = ao_pad_read_box();
253 PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
254 command.tick, command.box, ao_pad_box, command.cmd, command.channels);
256 switch (command.cmd) {
258 if (command.box != ao_pad_box) {
259 PRINTD ("box number mismatch\n");
263 if (command.channels & ~(AO_PAD_ALL_CHANNELS))
266 time_difference = command.tick - ao_time();
267 PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
268 if (time_difference < 0)
269 time_difference = -time_difference;
270 if (time_difference > 10) {
271 PRINTD ("time difference too large %d\n", time_difference);
275 ao_pad_armed = command.channels;
276 ao_pad_arm_time = ao_time();
278 /* fall through ... */
280 case AO_LAUNCH_QUERY:
281 if (command.box != ao_pad_box) {
282 PRINTD ("box number mismatch\n");
286 query.tick = ao_time();
287 query.box = ao_pad_box;
288 query.channels = AO_PAD_ALL_CHANNELS;
289 query.armed = ao_pad_armed;
290 PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
291 query.tick, query.box, query.channels, query.armed,
293 query.igniter_status[0],
294 query.igniter_status[1],
295 query.igniter_status[2],
296 query.igniter_status[3]);
297 ao_radio_cmac_send(&query, sizeof (query));
301 PRINTD ("not armed\n");
304 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
305 PRINTD ("late pad arm_time %d time %d\n",
306 ao_pad_arm_time, ao_time());
309 time_difference = command.tick - ao_time();
310 if (time_difference < 0)
311 time_difference = -time_difference;
312 if (time_difference > 10) {
313 PRINTD ("time different too large %d\n", time_difference);
317 ao_pad_ignite = ao_pad_armed;
318 ao_pad_arm_time = ao_time();
319 ao_wakeup(&ao_pad_ignite);
330 printf ("Arm switch: ");
331 switch (query.arm_status) {
332 case AO_PAD_ARM_STATUS_ARMED:
335 case AO_PAD_ARM_STATUS_DISARMED:
336 printf ("Disarmed\n");
338 case AO_PAD_ARM_STATUS_UNKNOWN:
339 printf ("Unknown\n");
343 for (c = 0; c < AO_PAD_NUM; c++) {
344 printf ("Pad %d: ", c);
345 switch (query.igniter_status[c]) {
346 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED: printf ("No igniter. Relay closed\n"); break;
347 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN: printf ("No igniter. Relay open\n"); break;
348 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN: printf ("Good igniter. Relay open\n"); break;
349 case AO_PAD_IGNITER_STATUS_UNKNOWN: printf ("Unknown\n"); break;
358 if (!ao_match_word("DoIt"))
361 if (ao_cmd_status != ao_cmd_success)
363 ao_pad_ignite = 1 << ao_cmd_lex_i;
364 ao_wakeup(&ao_pad_ignite);
367 static __xdata struct ao_task ao_pad_task;
368 static __xdata struct ao_task ao_pad_ignite_task;
369 static __xdata struct ao_task ao_pad_monitor_task;
373 ao_pad_set_debug(void)
376 if (ao_cmd_status == ao_cmd_success)
377 ao_pad_debug = ao_cmd_lex_i != 0;
382 ao_pad_alarm_debug(void)
384 uint8_t which, value;
386 if (ao_cmd_status != ao_cmd_success)
388 which = ao_cmd_lex_i;
390 if (ao_cmd_status != ao_cmd_success)
392 value = ao_cmd_lex_i;
393 printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
401 __code struct ao_cmds ao_pad_cmds[] = {
402 { ao_pad_test, "t\0Test pad continuity" },
403 { ao_pad_manual, "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
405 { ao_pad_set_debug, "D <0 off, 1 on>\0Debug" },
406 { ao_pad_alarm_debug, "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
415 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
418 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
421 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
424 ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
427 ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
430 ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
432 ao_cmd_register(&ao_pad_cmds[0]);
433 ao_add_task(&ao_pad_task, ao_pad, "pad listener");
434 ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
435 ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");