telefire: Report telefire battery voltage over telemetry link
[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
292                         /* fall through ... */
293
294                 case AO_LAUNCH_QUERY:
295                         if (command.box != ao_pad_box) {
296                                 PRINTD ("box number mismatch\n");
297                                 break;
298                         }
299
300                         query.tick = ao_time();
301                         query.box = ao_pad_box;
302                         query.channels = AO_PAD_ALL_CHANNELS;
303                         query.armed = ao_pad_armed;
304                         PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
305                                 query.tick, query.box, query.channels, query.armed,
306                                 query.arm_status,
307                                 query.igniter_status[0],
308                                 query.igniter_status[1],
309                                 query.igniter_status[2],
310                                 query.igniter_status[3]);
311                         ao_radio_cmac_send(&query, sizeof (query));
312                         break;
313                 case AO_LAUNCH_FIRE:
314                         if (!ao_pad_armed) {
315                                 PRINTD ("not armed\n");
316                                 break;
317                         }
318                         if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
319                                 PRINTD ("late pad arm_time %d time %d\n",
320                                         ao_pad_arm_time, ao_time());
321                                 break;
322                         }
323                         time_difference = command.tick - ao_time();
324                         if (time_difference < 0)
325                                 time_difference = -time_difference;
326                         if (time_difference > 10) {
327                                 PRINTD ("time different too large %d\n", time_difference);
328                                 break;
329                         }
330                         PRINTD ("ignite\n");
331                         ao_pad_ignite = ao_pad_armed;
332                         ao_pad_arm_time = ao_time();
333                         ao_wakeup(&ao_pad_ignite);
334                         break;
335                 }
336         }
337 }
338
339 void
340 ao_pad_test(void)
341 {
342         uint8_t c;
343
344         printf ("Arm switch: ");
345         switch (query.arm_status) {
346         case AO_PAD_ARM_STATUS_ARMED:
347                 printf ("Armed\n");
348                 break;
349         case AO_PAD_ARM_STATUS_DISARMED:
350                 printf ("Disarmed\n");
351                 break;
352         case AO_PAD_ARM_STATUS_UNKNOWN:
353                 printf ("Unknown\n");
354                 break;
355         }
356
357         for (c = 0; c < AO_PAD_NUM; c++) {
358                 printf ("Pad %d: ", c);
359                 switch (query.igniter_status[c]) {
360                 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED:     printf ("No igniter. Relay closed\n"); break;
361                 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN:       printf ("No igniter. Relay open\n"); break;
362                 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN:     printf ("Good igniter. Relay open\n"); break;
363                 case AO_PAD_IGNITER_STATUS_UNKNOWN:                     printf ("Unknown\n"); break;
364                 }
365         }
366 }
367
368 void
369 ao_pad_manual(void)
370 {
371         uint8_t ignite;
372         int     repeat;
373         ao_cmd_white();
374         if (!ao_match_word("DoIt"))
375                 return;
376         ao_cmd_decimal();
377         if (ao_cmd_status != ao_cmd_success)
378                 return;
379         ignite = 1 << ao_cmd_lex_i;
380         ao_cmd_decimal();
381         if (ao_cmd_status != ao_cmd_success) {
382                 repeat = 1;
383                 ao_cmd_status = ao_cmd_success;
384         } else
385                 repeat = ao_cmd_lex_i;
386         while (repeat-- > 0) {
387                 ao_pad_ignite = ignite;
388                 ao_wakeup(&ao_pad_ignite);
389                 ao_delay(AO_PAD_FIRE_TIME>>1);
390         }
391 }
392
393 static __xdata struct ao_task ao_pad_task;
394 static __xdata struct ao_task ao_pad_ignite_task;
395 static __xdata struct ao_task ao_pad_monitor_task;
396
397 #if DEBUG
398 void
399 ao_pad_set_debug(void)
400 {
401         ao_cmd_decimal();
402         if (ao_cmd_status == ao_cmd_success)
403                 ao_pad_debug = ao_cmd_lex_i != 0;
404 }
405
406
407 static void
408 ao_pad_alarm_debug(void)
409 {
410         uint8_t which, value;
411         ao_cmd_decimal();
412         if (ao_cmd_status != ao_cmd_success)
413                 return;
414         which = ao_cmd_lex_i;
415         ao_cmd_decimal();
416         if (ao_cmd_status != ao_cmd_success)
417                 return;
418         value = ao_cmd_lex_i;
419         printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
420         if (which)
421                 ao_siren(value);
422         else
423                 ao_strobe(value);
424 }
425 #endif
426
427 __code struct ao_cmds ao_pad_cmds[] = {
428         { ao_pad_test,  "t\0Test pad continuity" },
429         { ao_pad_manual,        "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
430 #if DEBUG
431         { ao_pad_set_debug,     "D <0 off, 1 on>\0Debug" },
432         { ao_pad_alarm_debug,   "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
433 #endif
434         { 0, NULL }
435 };
436
437 void
438 ao_pad_init(void)
439 {
440 #if AO_PAD_NUM > 0
441         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
442 #endif
443 #if AO_PAD_NUM > 1
444         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
445 #endif
446 #if AO_PAD_NUM > 2
447         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
448 #endif
449 #if AO_PAD_NUM > 3
450         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
451 #endif
452 #ifdef AO_STROBE
453         ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
454 #endif
455 #ifdef AO_SIREN
456         ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
457 #endif
458         ao_cmd_register(&ao_pad_cmds[0]);
459         ao_add_task(&ao_pad_task, ao_pad, "pad listener");
460         ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
461         ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");
462 }