Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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                 /* convert ADC value to voltage in tenths, then add .2 for the diode drop */
131                 query.battery = (packet->adc.batt + 96) / 192 + 2;
132                 cur = 0;
133                 if (pyro > VOLTS_TO_PYRO(10)) {
134                         query.arm_status = AO_PAD_ARM_STATUS_ARMED;
135                         cur |= AO_LED_ARMED;
136                 } else if (pyro < VOLTS_TO_PYRO(5)) {
137                         query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
138                         arm_beep_time = 0;
139                 } else {
140                         if ((ao_time() % 100) < 50)
141                                 cur |= AO_LED_ARMED;
142                         query.arm_status = AO_PAD_ARM_STATUS_UNKNOWN;
143                         arm_beep_time = 0;
144                 }
145                 if ((ao_time() - ao_pad_packet_time) > AO_SEC_TO_TICKS(2))
146                         cur |= AO_LED_RED;
147                 else if (ao_radio_cmac_rssi < AO_PAD_RSSI_MINIMUM)
148                         cur |= AO_LED_AMBER;
149                 else
150                         cur |= AO_LED_GREEN;
151
152                 for (c = 0; c < AO_PAD_NUM; c++) {
153                         int16_t         sense = packet->adc.sense[c];
154                         uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
155
156                         /*
157                          *      pyro is run through a divider, so pyro = v_pyro * 27 / 127 ~= v_pyro / 20
158                          *      v_pyro = pyro * 127 / 27
159                          *
160                          *              v_pyro \
161                          *      100k            igniter
162                          *              output /
163                          *      100k           \
164                          *              sense   relay
165                          *      27k            /
166                          *              gnd ---
167                          *
168                          *      If the relay is closed, then sense will be 0
169                          *      If no igniter is present, then sense will be v_pyro * 27k/227k = pyro * 127 / 227 ~= pyro/2
170                          *      If igniter is present, then sense will be v_pyro * 27k/127k ~= v_pyro / 20 = pyro
171                          */
172
173                         if (sense <= pyro / 8) {
174                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
175                                 if ((ao_time() % 100) < 50)
176                                         cur |= AO_LED_CONTINUITY(c);
177                         }
178                         else if (pyro / 8 * 3 <= sense && sense <= pyro / 8 * 5)
179                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
180                         else if (pyro / 8 * 7 <= sense) {
181                                 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
182                                 cur |= AO_LED_CONTINUITY(c);
183                         }
184                         query.igniter_status[c] = status;
185                 }
186                 if (cur != prev) {
187                         PRINTD("change leds from %02x to %02x\n",
188                                prev, cur);
189                         FLUSHD();
190                         ao_led_set(cur);
191                         prev = cur;
192                 }
193
194                 if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
195                         ao_pad_armed = 0;
196
197                 if (ao_pad_armed) {
198                         ao_strobe(1);
199                         ao_siren(1);
200                         beeping = 1;
201                 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
202                         if (arm_beep_time == 0) {
203                                 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
204                                 beeping = 1;
205                                 ao_siren(1);
206                         }
207                         --arm_beep_time;
208                 } else if (beeping) {
209                         beeping = 0;
210                         ao_siren(0);
211                         ao_strobe(0);
212                 }
213         }
214 }
215
216 void
217 ao_pad_disable(void)
218 {
219         if (!ao_pad_disabled) {
220                 ao_pad_disabled = 1;
221                 ao_radio_recv_abort();
222         }
223 }
224
225 void
226 ao_pad_enable(void)
227 {
228         ao_pad_disabled = 0;
229         ao_wakeup (&ao_pad_disabled);
230 }
231
232 #if HAS_74HC165
233 static uint8_t
234 ao_pad_read_box(void)
235 {
236         uint8_t         byte = ao_74hc165_read();
237         uint8_t         h, l;
238
239         h = byte >> 4;
240         l = byte & 0xf;
241         return h * 10 + l;
242 }
243 #else
244 #define ao_pad_read_box()       0
245 #endif
246
247 static void
248 ao_pad(void)
249 {
250         int16_t time_difference;
251         int8_t  ret;
252
253         ao_pad_box = 0;
254         ao_led_set(0);
255         for (;;) {
256                 FLUSHD();
257                 while (ao_pad_disabled)
258                         ao_sleep(&ao_pad_disabled);
259                 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
260                 PRINTD ("cmac_recv %d %d\n", ret, ao_radio_cmac_rssi);
261                 if (ret != AO_RADIO_CMAC_OK)
262                         continue;
263                 ao_pad_packet_time = ao_time();
264
265                 ao_pad_box = ao_pad_read_box();
266
267                 PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
268                         command.tick, command.box, ao_pad_box, command.cmd, command.channels);
269
270                 switch (command.cmd) {
271                 case AO_LAUNCH_ARM:
272                         if (command.box != ao_pad_box) {
273                                 PRINTD ("box number mismatch\n");
274                                 break;
275                         }
276
277                         if (command.channels & ~(AO_PAD_ALL_CHANNELS))
278                                 break;
279
280                         time_difference = command.tick - ao_time();
281                         PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
282                         if (time_difference < 0)
283                                 time_difference = -time_difference;
284                         if (time_difference > 10) {
285                                 PRINTD ("time difference too large %d\n", time_difference);
286                                 break;
287                         }
288                         PRINTD ("armed\n");
289                         ao_pad_armed = command.channels;
290                         ao_pad_arm_time = ao_time();
291                         break;
292
293                 case AO_LAUNCH_QUERY:
294                         if (command.box != ao_pad_box) {
295                                 PRINTD ("box number mismatch\n");
296                                 break;
297                         }
298
299                         query.tick = ao_time();
300                         query.box = ao_pad_box;
301                         query.channels = AO_PAD_ALL_CHANNELS;
302                         query.armed = ao_pad_armed;
303                         PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
304                                 query.tick, query.box, query.channels, query.armed,
305                                 query.arm_status,
306                                 query.igniter_status[0],
307                                 query.igniter_status[1],
308                                 query.igniter_status[2],
309                                 query.igniter_status[3]);
310                         ao_radio_cmac_send(&query, sizeof (query));
311                         break;
312                 case AO_LAUNCH_FIRE:
313                         if (!ao_pad_armed) {
314                                 PRINTD ("not armed\n");
315                                 break;
316                         }
317                         if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
318                                 PRINTD ("late pad arm_time %d time %d\n",
319                                         ao_pad_arm_time, ao_time());
320                                 break;
321                         }
322                         PRINTD ("ignite\n");
323                         ao_pad_ignite = ao_pad_armed;
324                         ao_pad_arm_time = ao_time();
325                         ao_wakeup(&ao_pad_ignite);
326                         break;
327                 }
328         }
329 }
330
331 void
332 ao_pad_test(void)
333 {
334         uint8_t c;
335
336         printf ("Arm switch: ");
337         switch (query.arm_status) {
338         case AO_PAD_ARM_STATUS_ARMED:
339                 printf ("Armed\n");
340                 break;
341         case AO_PAD_ARM_STATUS_DISARMED:
342                 printf ("Disarmed\n");
343                 break;
344         case AO_PAD_ARM_STATUS_UNKNOWN:
345                 printf ("Unknown\n");
346                 break;
347         }
348
349         for (c = 0; c < AO_PAD_NUM; c++) {
350                 printf ("Pad %d: ", c);
351                 switch (query.igniter_status[c]) {
352                 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED:     printf ("No igniter. Relay closed\n"); break;
353                 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN:       printf ("No igniter. Relay open\n"); break;
354                 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN:     printf ("Good igniter. Relay open\n"); break;
355                 case AO_PAD_IGNITER_STATUS_UNKNOWN:                     printf ("Unknown\n"); break;
356                 }
357         }
358 }
359
360 void
361 ao_pad_manual(void)
362 {
363         uint8_t ignite;
364         int     repeat;
365         ao_cmd_white();
366         if (!ao_match_word("DoIt"))
367                 return;
368         ao_cmd_decimal();
369         if (ao_cmd_status != ao_cmd_success)
370                 return;
371         ignite = 1 << ao_cmd_lex_i;
372         ao_cmd_decimal();
373         if (ao_cmd_status != ao_cmd_success) {
374                 repeat = 1;
375                 ao_cmd_status = ao_cmd_success;
376         } else
377                 repeat = ao_cmd_lex_i;
378         while (repeat-- > 0) {
379                 ao_pad_ignite = ignite;
380                 ao_wakeup(&ao_pad_ignite);
381                 ao_delay(AO_PAD_FIRE_TIME>>1);
382         }
383 }
384
385 static __xdata struct ao_task ao_pad_task;
386 static __xdata struct ao_task ao_pad_ignite_task;
387 static __xdata struct ao_task ao_pad_monitor_task;
388
389 #if DEBUG
390 void
391 ao_pad_set_debug(void)
392 {
393         ao_cmd_decimal();
394         if (ao_cmd_status == ao_cmd_success)
395                 ao_pad_debug = ao_cmd_lex_i != 0;
396 }
397
398
399 static void
400 ao_pad_alarm_debug(void)
401 {
402         uint8_t which, value;
403         ao_cmd_decimal();
404         if (ao_cmd_status != ao_cmd_success)
405                 return;
406         which = ao_cmd_lex_i;
407         ao_cmd_decimal();
408         if (ao_cmd_status != ao_cmd_success)
409                 return;
410         value = ao_cmd_lex_i;
411         printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
412         if (which)
413                 ao_siren(value);
414         else
415                 ao_strobe(value);
416 }
417 #endif
418
419 __code struct ao_cmds ao_pad_cmds[] = {
420         { ao_pad_test,  "t\0Test pad continuity" },
421         { ao_pad_manual,        "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
422 #if DEBUG
423         { ao_pad_set_debug,     "D <0 off, 1 on>\0Debug" },
424         { ao_pad_alarm_debug,   "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
425 #endif
426         { 0, NULL }
427 };
428
429 void
430 ao_pad_init(void)
431 {
432 #if AO_PAD_NUM > 0
433         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
434 #endif
435 #if AO_PAD_NUM > 1
436         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
437 #endif
438 #if AO_PAD_NUM > 2
439         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
440 #endif
441 #if AO_PAD_NUM > 3
442         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
443 #endif
444 #ifdef AO_STROBE
445         ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
446 #endif
447 #ifdef AO_SIREN
448         ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
449 #endif
450         ao_cmd_register(&ao_pad_cmds[0]);
451         ao_add_task(&ao_pad_task, ao_pad, "pad listener");
452         ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
453         ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");
454 }