altos/telefire: Report valid channels instead of valid pins
[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_74hc497.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                         if (sample & 2)
186                                 ao_siren(1);
187                         else
188                                 ao_siren(0);
189                         beeping = 1;
190                 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
191                         if (arm_beep_time == 0) {
192                                 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
193                                 beeping = 1;
194                                 ao_siren(1);
195                         }
196                         --arm_beep_time;
197                 } else if (beeping) {
198                         beeping = 0;
199                         ao_siren(0);
200                         ao_strobe(0);
201                 }
202         }
203 }
204
205 void
206 ao_pad_disable(void)
207 {
208         if (!ao_pad_disabled) {
209                 ao_pad_disabled = 1;
210                 ao_radio_recv_abort();
211         }
212 }
213
214 void
215 ao_pad_enable(void)
216 {
217         ao_pad_disabled = 0;
218         ao_wakeup (&ao_pad_disabled);
219 }
220
221 static void
222 ao_pad(void)
223 {
224         int16_t time_difference;
225         int8_t  ret;
226
227         ao_pad_box = 0;
228         ao_led_set(0);
229         ao_led_on(AO_LED_POWER);
230         for (;;) {
231                 FLUSHD();
232                 while (ao_pad_disabled)
233                         ao_sleep(&ao_pad_disabled);
234                 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
235                 PRINTD ("cmac_recv %d\n", ret);
236                 if (ret != AO_RADIO_CMAC_OK)
237                         continue;
238                 
239                 PRINTD ("tick %d box %d cmd %d channels %02x\n",
240                         command.tick, command.box, command.cmd, command.channels);
241
242                 switch (command.cmd) {
243                 case AO_LAUNCH_ARM:
244                         if (command.box != ao_pad_box) {
245                                 PRINTD ("box number mismatch\n");
246                                 break;
247                         }
248
249                         if (command.channels & ~(AO_PAD_ALL_CHANNELS))
250                                 break;
251
252                         time_difference = command.tick - ao_time();
253                         PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
254                         if (time_difference < 0)
255                                 time_difference = -time_difference;
256                         if (time_difference > 10) {
257                                 PRINTD ("time difference too large %d\n", time_difference);
258                                 break;
259                         }
260                         PRINTD ("armed\n");
261                         ao_pad_armed = command.channels;
262                         ao_pad_arm_time = ao_time();
263
264                         /* fall through ... */
265
266                 case AO_LAUNCH_QUERY:
267                         if (command.box != ao_pad_box) {
268                                 PRINTD ("box number mismatch\n");
269                                 break;
270                         }
271
272                         query.tick = ao_time();
273                         query.box = ao_pad_box;
274                         query.channels = AO_PAD_ALL_CHANNELS;
275                         query.armed = ao_pad_armed;
276                         PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
277                                 query.tick, query.box, query.channels, query.armed,
278                                 query.arm_status,
279                                 query.igniter_status[0],
280                                 query.igniter_status[1],
281                                 query.igniter_status[2],
282                                 query.igniter_status[3]);
283                         ao_radio_cmac_send(&query, sizeof (query));
284                         break;
285                 case AO_LAUNCH_FIRE:
286                         if (!ao_pad_armed) {
287                                 PRINTD ("not armed\n");
288                                 break;
289                         }
290                         if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
291                                 PRINTD ("late pad arm_time %d time %d\n",
292                                         ao_pad_arm_time, ao_time());
293                                 break;
294                         }
295                         time_difference = command.tick - ao_time();
296                         if (time_difference < 0)
297                                 time_difference = -time_difference;
298                         if (time_difference > 10) {
299                                 PRINTD ("time different too large %d\n", time_difference);
300                                 break;
301                         }
302                         PRINTD ("ignite\n");
303                         ao_pad_ignite = ao_pad_armed;
304                         ao_pad_arm_time = ao_time();
305                         ao_wakeup(&ao_pad_ignite);
306                         break;
307                 }
308         }
309 }
310
311 void
312 ao_pad_test(void)
313 {
314         uint8_t c;
315
316         printf ("Arm switch: ");
317         switch (query.arm_status) {
318         case AO_PAD_ARM_STATUS_ARMED:
319                 printf ("Armed\n");
320                 break;
321         case AO_PAD_ARM_STATUS_DISARMED:
322                 printf ("Disarmed\n");
323                 break;
324         case AO_PAD_ARM_STATUS_UNKNOWN:
325                 printf ("Unknown\n");
326                 break;
327         }
328
329         for (c = 0; c < AO_PAD_NUM; c++) {
330                 printf ("Pad %d: ");
331                 switch (query.igniter_status[c]) {
332                 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED:     printf ("No igniter. Relay closed\n"); break;
333                 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN:       printf ("No igniter. Relay open\n"); break;
334                 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN:     printf ("Good igniter. Relay open\n"); break;
335                 case AO_PAD_IGNITER_STATUS_UNKNOWN:                     printf ("Unknown\n"); break;
336                 }
337         }
338 }
339
340 void
341 ao_pad_manual(void)
342 {
343         ao_cmd_white();
344         if (!ao_match_word("DoIt"))
345                 return;
346         ao_cmd_decimal();
347         if (ao_cmd_status != ao_cmd_success)
348                 return;
349         ao_pad_ignite = 1 << ao_cmd_lex_i;
350         ao_wakeup(&ao_pad_ignite);
351 }
352
353 static __xdata struct ao_task ao_pad_task;
354 static __xdata struct ao_task ao_pad_ignite_task;
355 static __xdata struct ao_task ao_pad_monitor_task;
356
357 #if DEBUG
358 void
359 ao_pad_set_debug(void)
360 {
361         ao_cmd_decimal();
362         if (ao_cmd_status == ao_cmd_success)
363                 ao_pad_debug = ao_cmd_lex_i != 0;
364 }
365 #endif
366
367 __code struct ao_cmds ao_pad_cmds[] = {
368         { ao_pad_test,  "t\0Test pad continuity" },
369         { ao_pad_manual,        "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
370 #if DEBUG
371         { ao_pad_set_debug,     "D <0 off, 1 on>\0Debug" },
372 #endif
373         { 0, NULL }
374 };
375
376 void
377 ao_pad_init(void)
378 {
379 #if AO_PAD_NUM > 0
380         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
381 #endif
382 #if AO_PAD_NUM > 1
383         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
384 #endif
385 #if AO_PAD_NUM > 2
386         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
387 #endif
388 #if AO_PAD_NUM > 3
389         ao_enable_output(AO_PAD_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
390 #endif
391 #ifdef AO_STROBE
392         ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
393 #endif
394 #ifdef AO_SIREN
395         ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
396 #endif
397         ao_cmd_register(&ao_pad_cmds[0]);
398         ao_add_task(&ao_pad_task, ao_pad, "pad listener");
399         ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
400         ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");
401 }