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