Switch from GPLv2 to GPLv2+
[fw/altos] / src / drivers / ao_science_slave.c
1 /*
2  * Copyright © 2011 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_product.h"
21 #include "ao_flight.h"
22 #include "ao_companion.h"
23
24 struct ao_companion_command     ao_companion_command;
25
26 static const struct ao_companion_setup  ao_telescience_setup = {
27         .board_id               = AO_idProduct_NUMBER,
28         .board_id_inverse       = ~AO_idProduct_NUMBER,
29         .update_period          = 50,
30         .channels               = AO_LOG_TELESCIENCE_NUM_ADC,
31 };
32
33 void ao_spi_slave(void)
34 {
35         if (!ao_spi_slave_recv((uint8_t *) &ao_companion_command,
36                                sizeof (ao_companion_command)))
37                 return;
38
39         /* Figure out the outbound data */
40         switch (ao_companion_command.command) {
41         case AO_COMPANION_SETUP:
42                 ao_spi_slave_send((uint8_t *) &ao_telescience_setup,
43                                   sizeof (ao_telescience_setup));
44                 break;
45         case AO_COMPANION_FETCH:
46                 ao_spi_slave_send((uint8_t *) &ao_data_ring[ao_data_ring_prev(ao_data_head)].adc,
47                                   AO_LOG_TELESCIENCE_NUM_ADC * sizeof (uint16_t));
48                 break;
49         case AO_COMPANION_NOTIFY:
50                 break;
51         default:
52                 return;
53         }
54
55 #if HAS_LOG
56         ao_log_single_write_data.telescience.tm_tick = ao_companion_command.tick;
57         if (ao_log_single_write_data.telescience.tm_state != ao_companion_command.flight_state) {
58                 ao_log_single_write_data.telescience.tm_state = ao_companion_command.flight_state;
59                 if (ao_flight_boost <= ao_log_single_write_data.telescience.tm_state) {
60                         if (ao_log_single_write_data.telescience.tm_state < ao_flight_landed)
61                                 ao_log_single_start();
62                         else
63                                 ao_log_single_stop();
64                 }
65         }
66 #endif
67 }