3ad239cced3e3c62f07545ba51277982668face2
[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 static __pdata uint16_t ao_pad_packet_time;
31
32 #ifndef AO_PAD_RSSI_MINIMUM
33 #define AO_PAD_RSSI_MINIMUM     -90
34 #endif
35
36 #define DEBUG   1
37
38 #if DEBUG
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)
42 #else
43 #define PRINTD(...)
44 #define FLUSHD()
45 #endif
46
47 static void
48 ao_siren(uint8_t v)
49 {
50 #ifdef AO_SIREN
51         ao_gpio_set(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, v);
52 #else
53         ao_beep(v ? AO_BEEP_MID : 0);
54 #endif
55 }
56
57 static void
58 ao_strobe(uint8_t v)
59 {
60 #ifdef AO_STROBE
61         ao_gpio_set(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, v);
62 #endif
63 }
64
65 static void
66 ao_pad_run(void)
67 {
68         uint8_t pins;
69
70         for (;;) {
71                 while (!ao_pad_ignite)
72                         ao_sleep(&ao_pad_ignite);
73                 /*
74                  * Actually set the pad bits
75                  */
76                 pins = 0;
77 #if AO_PAD_NUM > 0
78                 if (ao_pad_ignite & (1 << 0))
79                         pins |= (1 << AO_PAD_PIN_0);
80 #endif
81 #if AO_PAD_NUM > 1
82                 if (ao_pad_ignite & (1 << 1))
83                         pins |= (1 << AO_PAD_PIN_1);
84 #endif
85 #if AO_PAD_NUM > 2
86                 if (ao_pad_ignite & (1 << 2))
87                         pins |= (1 << AO_PAD_PIN_2);
88 #endif
89 #if AO_PAD_NUM > 3
90                 if (ao_pad_ignite & (1 << 3))
91                         pins |= (1 << AO_PAD_PIN_3);
92 #endif
93                 AO_PAD_PORT = (AO_PAD_PORT & (~AO_PAD_ALL_PINS)) | pins;
94                 while (ao_pad_ignite) {
95                         ao_pad_ignite = 0;
96
97                         ao_delay(AO_PAD_FIRE_TIME);
98                 }
99                 AO_PAD_PORT &= ~(AO_PAD_ALL_PINS);
100         }
101 }
102
103 #define AO_PAD_ARM_SIREN_INTERVAL       200
104
105 static void
106 ao_pad_monitor(void)
107 {
108         uint8_t                 c;
109         uint8_t                 sample;
110         __pdata uint8_t         prev = 0, cur = 0;
111         __pdata uint8_t         beeping = 0;
112         __xdata struct ao_data  *packet;
113         __pdata uint16_t        arm_beep_time = 0;
114
115         sample = ao_data_head;
116         for (;;) {
117                 __pdata int16_t                 pyro;
118                 ao_arch_critical(
119                         while (sample == ao_data_head)
120                                 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
121                         );
122
123                 packet = &ao_data_ring[sample];
124                 sample = ao_data_ring_next(sample);
125
126                 pyro = packet->adc.pyro;
127
128 #define VOLTS_TO_PYRO(x) ((int16_t) ((x) * 27.0 / 127.0 / 3.3 * 32767.0))
129
130                 cur = 0;
131                 if (pyro > VOLTS_TO_PYRO(10)) {
132                         query.arm_status = AO_PAD_ARM_STATUS_ARMED;
133                         cur |= AO_LED_ARMED;
134                 } else if (pyro < VOLTS_TO_PYRO(5)) {
135                         query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
136                         arm_beep_time = 0;
137                 } else {
138                         if ((ao_time() % 100) < 50)
139                                 cur |= AO_LED_ARMED;
140                         query.arm_status = AO_PAD_ARM_STATUS_UNKNOWN;
141                         arm_beep_time = 0;
142                 }
143                 if ((ao_time() - ao_pad_packet_time) > AO_SEC_TO_TICKS(2))
144                         cur |= AO_LED_RED;
145                 else if (ao_radio_cmac_rssi < AO_PAD_RSSI_MINIMUM)
146                         cur |= AO_LED_AMBER;
147                 else
148                         cur |= AO_LED_GREEN;
149
150                 for (c = 0; c < AO_PAD_NUM; c++) {
151                         int16_t         sense = packet->adc.sense[c];
152                         uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
153
154                         /*
155                          *      pyro is run through a divider, so pyro = v_pyro * 27 / 127 ~= v_pyro / 20
156                          *      v_pyro = pyro * 127 / 27
157                          *
158                          *              v_pyro \
159                          *      100k            igniter
160                          *              output /
161                          *      100k           \
162                          *              sense   relay
163                          *      27k            /
164                          *              gnd ---
165                          *
166                          *      If the relay is closed, then sense will be 0
167                          *      If no igniter is present, then sense will be v_pyro * 27k/227k = pyro * 127 / 227 ~= pyro/2
168                          *      If igniter is present, then sense will be v_pyro * 27k/127k ~= v_pyro / 20 = pyro
169                          */
170
171                         if (sense <= pyro / 8) {
172                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
173                                 if ((ao_time() % 100) < 50)
174                                         cur |= AO_LED_CONTINUITY(c);
175                         }
176                         else if (pyro / 8 * 3 <= sense && sense <= pyro / 8 * 5)
177                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
178                         else if (pyro / 8 * 7 <= sense) {
179                                 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
180                                 cur |= AO_LED_CONTINUITY(c);
181                         }
182                         query.igniter_status[c] = status;
183                 }
184                 if (cur != prev) {
185                         PRINTD("change leds from %02x to %02x\n",
186                                prev, cur);
187                         FLUSHD();
188                         ao_led_set(cur);
189                         prev = cur;
190                 }
191
192                 if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
193                         ao_pad_armed = 0;
194
195                 if (ao_pad_armed) {
196                         ao_strobe(1);
197                         ao_siren(1);
198                         beeping = 1;
199                 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
200                         if (arm_beep_time == 0) {
201                                 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
202                                 beeping = 1;
203                                 ao_siren(1);
204                         }
205                         --arm_beep_time;
206                 } else if (beeping) {
207                         beeping = 0;
208                         ao_siren(0);
209                         ao_strobe(0);
210                 }
211         }
212 }
213
214 void
215 ao_pad_disable(void)
216 {
217         if (!ao_pad_disabled) {
218                 ao_pad_disabled = 1;
219                 ao_radio_recv_abort();
220         }
221 }
222
223 void
224 ao_pad_enable(void)
225 {
226         ao_pad_disabled = 0;
227         ao_wakeup (&ao_pad_disabled);
228 }
229
230 #if HAS_74HC165
231 static uint8_t
232 ao_pad_read_box(void)
233 {
234         uint8_t         byte = ao_74hc165_read();
235         uint8_t         h, l;
236
237         h = byte >> 4;
238         l = byte & 0xf;
239         return h * 10 + l;
240 }
241 #else
242 #define ao_pad_read_box()       0
243 #endif
244
245 static void
246 ao_pad(void)
247 {
248         int16_t time_difference;
249         int8_t  ret;
250
251         ao_pad_box = 0;
252         ao_led_set(0);
253         for (;;) {
254                 FLUSHD();
255                 while (ao_pad_disabled)
256                         ao_sleep(&ao_pad_disabled);
257                 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
258                 PRINTD ("cmac_recv %d %d\n", ret, ao_radio_cmac_rssi);
259                 if (ret != AO_RADIO_CMAC_OK)
260                         continue;
261                 ao_pad_packet_time = ao_time();
262
263                 ao_pad_box = ao_pad_read_box();
264
265                 PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
266                         command.tick, command.box, ao_pad_box, command.cmd, command.channels);
267
268                 switch (command.cmd) {
269                 case AO_LAUNCH_ARM:
270                         if (command.box != ao_pad_box) {
271                                 PRINTD ("box number mismatch\n");
272                                 break;
273                         }
274
275                         if (command.channels & ~(AO_PAD_ALL_CHANNELS))
276                                 break;
277
278                         time_difference = command.tick - ao_time();
279                         PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
280                         if (time_difference < 0)
281                                 time_difference = -time_difference;
282                         if (time_difference > 10) {
283                                 PRINTD ("time difference too large %d\n", time_difference);
284                                 break;
285                         }
286                         PRINTD ("armed\n");
287                         ao_pad_armed = command.channels;
288                         ao_pad_arm_time = ao_time();
289
290                         /* fall through ... */
291
292                 case AO_LAUNCH_QUERY:
293                         if (command.box != ao_pad_box) {
294                                 PRINTD ("box number mismatch\n");
295                                 break;
296                         }
297
298                         query.tick = ao_time();
299                         query.box = ao_pad_box;
300                         query.channels = AO_PAD_ALL_CHANNELS;
301                         query.armed = ao_pad_armed;
302                         PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
303                                 query.tick, query.box, query.channels, query.armed,
304                                 query.arm_status,
305                                 query.igniter_status[0],
306                                 query.igniter_status[1],
307                                 query.igniter_status[2],
308                                 query.igniter_status[3]);
309                         ao_radio_cmac_send(&query, sizeof (query));
310                         break;
311                 case AO_LAUNCH_FIRE:
312                         if (!ao_pad_armed) {
313                                 PRINTD ("not armed\n");
314                                 break;
315                         }
316                         if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
317                                 PRINTD ("late pad arm_time %d time %d\n",
318                                         ao_pad_arm_time, ao_time());
319                                 break;
320                         }
321                         time_difference = command.tick - ao_time();
322                         if (time_difference < 0)
323                                 time_difference = -time_difference;
324                         if (time_difference > 10) {
325                                 PRINTD ("time different too large %d\n", time_difference);
326                                 break;
327                         }
328                         PRINTD ("ignite\n");
329                         ao_pad_ignite = ao_pad_armed;
330                         ao_pad_arm_time = ao_time();
331                         ao_wakeup(&ao_pad_ignite);
332                         break;
333                 }
334         }
335 }
336
337 void
338 ao_pad_test(void)
339 {
340         uint8_t c;
341
342         printf ("Arm switch: ");
343         switch (query.arm_status) {
344         case AO_PAD_ARM_STATUS_ARMED:
345                 printf ("Armed\n");
346                 break;
347         case AO_PAD_ARM_STATUS_DISARMED:
348                 printf ("Disarmed\n");
349                 break;
350         case AO_PAD_ARM_STATUS_UNKNOWN:
351                 printf ("Unknown\n");
352                 break;
353         }
354
355         for (c = 0; c < AO_PAD_NUM; c++) {
356                 printf ("Pad %d: ", c);
357                 switch (query.igniter_status[c]) {
358                 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED:     printf ("No igniter. Relay closed\n"); break;
359                 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN:       printf ("No igniter. Relay open\n"); break;
360                 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN:     printf ("Good igniter. Relay open\n"); break;
361                 case AO_PAD_IGNITER_STATUS_UNKNOWN:                     printf ("Unknown\n"); break;
362                 }
363         }
364 }
365
366 void
367 ao_pad_manual(void)
368 {
369         uint8_t ignite;
370         int     repeat;
371         ao_cmd_white();
372         if (!ao_match_word("DoIt"))
373                 return;
374         ao_cmd_decimal();
375         if (ao_cmd_status != ao_cmd_success)
376                 return;
377         ignite = 1 << ao_cmd_lex_i;
378         ao_cmd_decimal();
379         if (ao_cmd_status != ao_cmd_success) {
380                 repeat = 1;
381                 ao_cmd_status = ao_cmd_success;
382         } else
383                 repeat = ao_cmd_lex_i;
384         while (repeat-- > 0) {
385                 ao_pad_ignite = ignite;
386                 ao_wakeup(&ao_pad_ignite);
387                 ao_delay(AO_PAD_FIRE_TIME>>1);
388         }
389 }
390
391 static __xdata struct ao_task ao_pad_task;
392 static __xdata struct ao_task ao_pad_ignite_task;
393 static __xdata struct ao_task ao_pad_monitor_task;
394
395 #if DEBUG
396 void
397 ao_pad_set_debug(void)
398 {
399         ao_cmd_decimal();
400         if (ao_cmd_status == ao_cmd_success)
401                 ao_pad_debug = ao_cmd_lex_i != 0;
402 }
403
404
405 static void
406 ao_pad_alarm_debug(void)
407 {
408         uint8_t which, value;
409         ao_cmd_decimal();
410         if (ao_cmd_status != ao_cmd_success)
411                 return;
412         which = ao_cmd_lex_i;
413         ao_cmd_decimal();
414         if (ao_cmd_status != ao_cmd_success)
415                 return;
416         value = ao_cmd_lex_i;
417         printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
418         if (which)
419                 ao_siren(value);
420         else
421                 ao_strobe(value);
422 }
423 #endif
424
425 __code struct ao_cmds ao_pad_cmds[] = {
426         { ao_pad_test,  "t\0Test pad continuity" },
427         { ao_pad_manual,        "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
428 #if DEBUG
429         { ao_pad_set_debug,     "D <0 off, 1 on>\0Debug" },
430         { ao_pad_alarm_debug,   "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
431 #endif
432         { 0, NULL }
433 };
434
435 void
436 ao_pad_init(void)
437 {
438 #if AO_PAD_NUM > 0
439         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
440 #endif
441 #if AO_PAD_NUM > 1
442         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
443 #endif
444 #if AO_PAD_NUM > 2
445         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
446 #endif
447 #if AO_PAD_NUM > 3
448         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
449 #endif
450 #ifdef AO_STROBE
451         ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
452 #endif
453 #ifdef AO_SIREN
454         ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
455 #endif
456         ao_cmd_register(&ao_pad_cmds[0]);
457         ao_add_task(&ao_pad_task, ao_pad, "pad listener");
458         ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
459         ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");
460 }