Switch from GPLv2 to GPLv2+
[fw/altos] / src / drivers / ao_lco_func.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_pad.h>
21 #include <ao_radio_cmac.h>
22 #include <ao_lco_func.h>
23
24 static __xdata struct ao_pad_command    command;
25 static __xdata uint8_t                  ao_lco_mutex;
26
27 int8_t
28 ao_lco_query(uint16_t box, struct ao_pad_query *query, uint16_t *tick_offset)
29 {
30         int8_t          r;
31         uint16_t        sent_time;
32         uint16_t        timeout = AO_MS_TO_TICKS(10);
33
34 #if HAS_RADIO_RATE
35         switch (ao_config.radio_rate) {
36         case AO_RADIO_RATE_38400:
37         default:
38                 break;
39         case AO_RADIO_RATE_9600:
40                 timeout = AO_MS_TO_TICKS(20);
41                 break;
42         case AO_RADIO_RATE_2400:
43                 timeout = AO_MS_TO_TICKS(80);
44                 break;
45         }
46 #endif
47         ao_mutex_get(&ao_lco_mutex);
48         command.tick = ao_time();
49         command.box = box;
50         command.cmd = AO_LAUNCH_QUERY;
51         command.channels = 0;
52         ao_radio_cmac_send(&command, sizeof (command));
53         sent_time = ao_time();
54         r = ao_radio_cmac_recv(query, sizeof (*query), timeout);
55         if (r == AO_RADIO_CMAC_OK)
56                 *tick_offset = sent_time - query->tick;
57         ao_mutex_put(&ao_lco_mutex);
58         return r;
59 }
60
61 void
62 ao_lco_arm(uint16_t box, uint8_t channels, uint16_t tick_offset)
63 {
64         ao_mutex_get(&ao_lco_mutex);
65         command.tick = ao_time() - tick_offset;
66         command.box = box;
67         command.cmd = AO_LAUNCH_ARM;
68         command.channels = channels;
69         ao_radio_cmac_send(&command, sizeof (command));
70         ao_mutex_put(&ao_lco_mutex);
71 }
72
73 void
74 ao_lco_ignite(void)
75 {
76         ao_mutex_get(&ao_lco_mutex);
77         command.tick = 0;
78         command.box = 0;
79         command.cmd = AO_LAUNCH_FIRE;
80         command.channels = 0;
81         ao_radio_cmac_send(&command, sizeof (command));
82         ao_mutex_put(&ao_lco_mutex);
83 }