Merge remote-tracking branch 'origin/master'
[fw/altos] / src / ao_companion.c
index b424dd896078c468e09759311546181647771ac8..4c8f4269588769054a541535b5e939934f82aac7 100644 (file)
@@ -43,6 +43,8 @@ ao_companion_send_command(uint8_t command)
        ao_companion_command.command = command;
        ao_companion_command.flight_state = ao_flight_state;
        ao_companion_command.tick = ao_time();
+       ao_companion_command.serial = ao_serial_number;
+       ao_companion_command.flight = ao_flight_number;
        ao_spi_send(&ao_companion_command, sizeof (ao_companion_command));
 }
 
@@ -53,9 +55,8 @@ ao_companion_get_setup(void)
        ao_companion_send_command(AO_COMPANION_SETUP);
        ao_spi_recv(&ao_companion_setup, sizeof (ao_companion_setup));
        COMPANION_DESELECT();
-       if (ao_companion_setup.board_id != ~ao_companion_setup.board_id)
-               return 0;
-       return 1;
+       return (ao_companion_setup.board_id ==
+               ~ao_companion_setup.board_id_inverse);
 }
 
 static void
@@ -69,15 +70,31 @@ ao_companion_get_data(void)
        COMPANION_DESELECT();
 }
 
+static void
+ao_companion_notify(void)
+{
+       COMPANION_SELECT();
+       ao_companion_send_command(AO_COMPANION_NOTIFY);
+       COMPANION_DESELECT();
+}
+
 void
 ao_companion(void)
 {
-       if (!ao_companion_get_setup())
-               ao_exit();
-       ao_companion_running = 1;
-       for (;;) {
-               ao_delay(ao_companion_setup.update_period);
-               ao_companion_get_data();
+       uint8_t i;
+       while (!ao_flight_number)
+               ao_sleep(&ao_flight_number);
+       for (i = 0; i < 10; i++) {
+               ao_delay(AO_SEC_TO_TICKS(1));
+               if ((ao_companion_running = ao_companion_get_setup()))
+                   break;
+       }
+       while (ao_companion_running) {
+               ao_alarm(ao_companion_setup.update_period);
+               if (ao_sleep(DATA_TO_XDATA(&ao_flight_state)))
+                       ao_companion_get_data();
+               else
+                       ao_companion_notify();
        }
        ao_exit();
 }