altos/telefire: Leave siren on all the time. Add siren/strobe debugging.
[fw/altos] / src / drivers / ao_pad.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19 #include <ao_pad.h>
20 #include <ao_74hc165.h>
21 #include <ao_radio_cmac.h>
22
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
31 #define DEBUG   1
32
33 #if DEBUG
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)
37 #else
38 #define PRINTD(...) 
39 #define FLUSHD()    
40 #endif
41
42 static void
43 ao_siren(uint8_t v)
44 {
45 #ifdef AO_SIREN
46         ao_gpio_set(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, v);
47 #else
48         ao_beep(v ? AO_BEEP_MID : 0);
49 #endif
50 }
51
52 static void
53 ao_strobe(uint8_t v)
54 {
55 #ifdef AO_STROBE
56         ao_gpio_set(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, v);
57 #endif
58 }
59
60 static void
61 ao_pad_run(void)
62 {
63         uint8_t pins;
64
65         for (;;) {
66                 while (!ao_pad_ignite)
67                         ao_sleep(&ao_pad_ignite);
68                 /*
69                  * Actually set the pad bits
70                  */
71                 pins = 0;
72 #if AO_PAD_NUM > 0
73                 if (ao_pad_ignite & (1 << 0))
74                         pins |= (1 << AO_PAD_PIN_0);
75 #endif
76 #if AO_PAD_NUM > 1
77                 if (ao_pad_ignite & (1 << 1))
78                         pins |= (1 << AO_PAD_PIN_1);
79 #endif
80 #if AO_PAD_NUM > 2
81                 if (ao_pad_ignite & (1 << 2))
82                         pins |= (1 << AO_PAD_PIN_2);
83 #endif
84 #if AO_PAD_NUM > 3
85                 if (ao_pad_ignite & (1 << 3))
86                         pins |= (1 << AO_PAD_PIN_3);
87 #endif
88                 AO_PAD_PORT = (AO_PAD_PORT & (~AO_PAD_ALL_PINS)) | pins;
89                 while (ao_pad_ignite) {
90                         ao_pad_ignite = 0;
91
92                         ao_delay(AO_PAD_FIRE_TIME);
93                 }
94                 AO_PAD_PORT &= ~(AO_PAD_ALL_PINS);
95         }
96 }
97
98 #define AO_PAD_ARM_SIREN_INTERVAL       200
99
100 static void
101 ao_pad_monitor(void)
102 {
103         uint8_t                 c;
104         uint8_t                 sample;
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;
109
110         sample = ao_data_head;
111         for (;;) {
112                 __pdata int16_t                 pyro;
113                 ao_arch_critical(
114                         while (sample == ao_data_head)
115                                 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
116                         );
117
118                 packet = &ao_data_ring[sample];
119                 sample = ao_data_ring_next(sample);
120
121                 pyro = packet->adc.pyro;
122
123 #define VOLTS_TO_PYRO(x) ((int16_t) ((x) * 27.0 / 127.0 / 3.3 * 32767.0))
124
125                 cur = 0;
126                 if (pyro > VOLTS_TO_PYRO(10)) {
127                         query.arm_status = AO_PAD_ARM_STATUS_ARMED;
128                         cur |= AO_LED_ARMED;
129                 } else if (pyro < VOLTS_TO_PYRO(5)) {
130                         query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
131                         arm_beep_time = 0;
132                 } else {
133                         if ((ao_time() % 100) < 50)
134                                 cur |= AO_LED_ARMED;
135                         query.arm_status = AO_PAD_ARM_STATUS_UNKNOWN;
136                         arm_beep_time = 0;
137                 }
138
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;
142
143                         /*
144                          *      pyro is run through a divider, so pyro = v_pyro * 27 / 127 ~= v_pyro / 20
145                          *      v_pyro = pyro * 127 / 27
146                          *
147                          *              v_pyro \
148                          *      100k            igniter
149                          *              output /        
150                          *      100k           \
151                          *              sense   relay
152                          *      27k            / 
153                          *              gnd ---   
154                          *
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
158                          */
159
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);
164                         }
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);
170                         }
171                         query.igniter_status[c] = status;
172                 }
173                 if (cur != prev) {
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);
177                         prev = cur;
178                 }
179
180                 if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
181                         ao_pad_armed = 0;
182
183                 if (ao_pad_armed) {
184                         ao_strobe(1);
185                         ao_siren(1);
186                         beeping = 1;
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;
190                                 beeping = 1;
191                                 ao_siren(1);
192                         }
193                         --arm_beep_time;
194                 } else if (beeping) {
195                         beeping = 0;
196                         ao_siren(0);
197                         ao_strobe(0);
198                 }
199         }
200 }
201
202 void
203 ao_pad_disable(void)
204 {
205         if (!ao_pad_disabled) {
206                 ao_pad_disabled = 1;
207                 ao_radio_recv_abort();
208         }
209 }
210
211 void
212 ao_pad_enable(void)
213 {
214         ao_pad_disabled = 0;
215         ao_wakeup (&ao_pad_disabled);
216 }
217
218 #if HAS_74HC165
219 static uint8_t
220 ao_pad_read_box(void)
221 {
222         uint8_t         byte = ao_74hc165_read();
223         uint8_t         h, l;
224
225         h = byte >> 4;
226         l = byte & 0xf;
227         return h * 10 + l;
228 }
229 #else
230 #define ao_pad_read_box()       0
231 #endif
232
233 static void
234 ao_pad(void)
235 {
236         int16_t time_difference;
237         int8_t  ret;
238
239         ao_pad_box = 0;
240         ao_led_set(0);
241         ao_led_on(AO_LED_POWER);
242         for (;;) {
243                 FLUSHD();
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)
249                         continue;
250                 
251                 ao_pad_box = ao_pad_read_box();
252
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);
255
256                 switch (command.cmd) {
257                 case AO_LAUNCH_ARM:
258                         if (command.box != ao_pad_box) {
259                                 PRINTD ("box number mismatch\n");
260                                 break;
261                         }
262
263                         if (command.channels & ~(AO_PAD_ALL_CHANNELS))
264                                 break;
265
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);
272                                 break;
273                         }
274                         PRINTD ("armed\n");
275                         ao_pad_armed = command.channels;
276                         ao_pad_arm_time = ao_time();
277
278                         /* fall through ... */
279
280                 case AO_LAUNCH_QUERY:
281                         if (command.box != ao_pad_box) {
282                                 PRINTD ("box number mismatch\n");
283                                 break;
284                         }
285
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,
292                                 query.arm_status,
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));
298                         break;
299                 case AO_LAUNCH_FIRE:
300                         if (!ao_pad_armed) {
301                                 PRINTD ("not armed\n");
302                                 break;
303                         }
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());
307                                 break;
308                         }
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);
314                                 break;
315                         }
316                         PRINTD ("ignite\n");
317                         ao_pad_ignite = ao_pad_armed;
318                         ao_pad_arm_time = ao_time();
319                         ao_wakeup(&ao_pad_ignite);
320                         break;
321                 }
322         }
323 }
324
325 void
326 ao_pad_test(void)
327 {
328         uint8_t c;
329
330         printf ("Arm switch: ");
331         switch (query.arm_status) {
332         case AO_PAD_ARM_STATUS_ARMED:
333                 printf ("Armed\n");
334                 break;
335         case AO_PAD_ARM_STATUS_DISARMED:
336                 printf ("Disarmed\n");
337                 break;
338         case AO_PAD_ARM_STATUS_UNKNOWN:
339                 printf ("Unknown\n");
340                 break;
341         }
342
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;
350                 }
351         }
352 }
353
354 void
355 ao_pad_manual(void)
356 {
357         ao_cmd_white();
358         if (!ao_match_word("DoIt"))
359                 return;
360         ao_cmd_decimal();
361         if (ao_cmd_status != ao_cmd_success)
362                 return;
363         ao_pad_ignite = 1 << ao_cmd_lex_i;
364         ao_wakeup(&ao_pad_ignite);
365 }
366
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;
370
371 #if DEBUG
372 void
373 ao_pad_set_debug(void)
374 {
375         ao_cmd_decimal();
376         if (ao_cmd_status == ao_cmd_success)
377                 ao_pad_debug = ao_cmd_lex_i != 0;
378 }
379
380
381 static void
382 ao_pad_alarm_debug(void)
383 {
384         uint8_t which, value;
385         ao_cmd_decimal();
386         if (ao_cmd_status != ao_cmd_success)
387                 return;
388         which = ao_cmd_lex_i;
389         ao_cmd_decimal();
390         if (ao_cmd_status != ao_cmd_success)
391                 return;
392         value = ao_cmd_lex_i;
393         printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
394         if (which)
395                 ao_siren(value);
396         else
397                 ao_strobe(value);
398 }
399 #endif
400
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" },
404 #if DEBUG
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" },
407 #endif
408         { 0, NULL }
409 };
410
411 void
412 ao_pad_init(void)
413 {
414 #if AO_PAD_NUM > 0
415         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
416 #endif
417 #if AO_PAD_NUM > 1
418         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
419 #endif
420 #if AO_PAD_NUM > 2
421         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
422 #endif
423 #if AO_PAD_NUM > 3
424         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
425 #endif
426 #ifdef AO_STROBE
427         ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
428 #endif
429 #ifdef AO_SIREN
430         ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
431 #endif
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");
436 }