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