]> git.gag.com Git - fw/altos/blob - src/drivers/ao_lco_func.c
Publish firmware with keithp-fat
[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; 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_radio_cmac.h>
21 #include <ao_lco_func.h>
22
23 static __xdata struct ao_pad_command    command;
24 static __xdata uint8_t                  ao_lco_mutex;
25
26 int8_t
27 ao_lco_query(uint16_t box, struct ao_pad_query *query, uint16_t *tick_offset)
28 {
29         int8_t          r;
30         uint16_t        sent_time;
31         uint16_t        timeout = AO_MS_TO_TICKS(10);
32
33 #if HAS_RADIO_RATE
34         switch (ao_config.radio_rate) {
35         case AO_RADIO_RATE_38400:
36         default:
37                 break;
38         case AO_RADIO_RATE_9600:
39                 timeout = AO_MS_TO_TICKS(20);
40                 break;
41         case AO_RADIO_RATE_2400:
42                 timeout = AO_MS_TO_TICKS(80);
43                 break;
44         }
45 #endif
46         ao_mutex_get(&ao_lco_mutex);
47         command.tick = ao_time();
48         command.box = box;
49         command.cmd = AO_LAUNCH_QUERY;
50         command.channels = 0;
51         ao_radio_cmac_send(&command, sizeof (command));
52         sent_time = ao_time();
53         r = ao_radio_cmac_recv(query, sizeof (*query), timeout);
54         if (r == AO_RADIO_CMAC_OK)
55                 *tick_offset = sent_time - query->tick;
56         ao_mutex_put(&ao_lco_mutex);
57         return r;
58 }
59
60 void
61 ao_lco_arm(uint16_t box, uint8_t channels, uint16_t tick_offset)
62 {
63         ao_mutex_get(&ao_lco_mutex);
64         command.tick = ao_time() - tick_offset;
65         command.box = box;
66         command.cmd = AO_LAUNCH_ARM;
67         command.channels = channels;
68         ao_radio_cmac_send(&command, sizeof (command));
69         ao_mutex_put(&ao_lco_mutex);
70 }
71
72 void
73 ao_lco_ignite(void)
74 {
75         ao_mutex_get(&ao_lco_mutex);
76         command.tick = 0;
77         command.box = 0;
78         command.cmd = AO_LAUNCH_FIRE;
79         command.channels = 0;
80         ao_radio_cmac_send(&command, sizeof (command));
81         ao_mutex_put(&ao_lco_mutex);
82 }