altos/telegps: Create new flight if current flight is erased
authorKeith Packard <keithp@keithp.com>
Fri, 13 Jun 2014 06:59:37 +0000 (23:59 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 13 Jun 2014 06:59:37 +0000 (23:59 -0700)
telegps is unique in that USB may be connected while a flight is
active and sensible things should happen. If a flight is being
recorded and gets erased, then a new flight should be started.

This is done by hooking in the flight erase code and calling out to
the tracker code to figure out whether to switch to a new flight or not.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/kernel/ao_log.c
src/kernel/ao_tracker.c
src/kernel/ao_tracker.h

index d9c3e00f6ce2fb98a40016e161230aae88a02b5e..91617d93531842dfe50b8469d0304817c254eb04 100644 (file)
@@ -18,6 +18,9 @@
 #include "ao.h"
 #include <ao_log.h>
 #include <ao_config.h>
+#if HAS_TRACKER
+#include <ao_tracker.h>
+#endif
 
 __xdata uint8_t        ao_log_mutex;
 __pdata uint32_t ao_log_current_pos;
@@ -251,6 +254,7 @@ ao_log_delete(void) __reentrant
 {
        uint8_t slot;
        uint8_t slots;
+       uint32_t log_current_pos, log_end_pos;
 
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
@@ -261,10 +265,13 @@ ao_log_delete(void) __reentrant
        if (ao_cmd_lex_i) {
                for (slot = 0; slot < slots; slot++) {
                        if (ao_log_flight(slot) == ao_cmd_lex_i) {
+#if HAS_TRACKER
+                               ao_tracker_erase_start(ao_cmd_lex_i);
+#endif
                                ao_log_erase_mark();
-                               ao_log_current_pos = ao_log_pos(slot);
-                               ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
-                               while (ao_log_current_pos < ao_log_end_pos) {
+                               log_current_pos = ao_log_pos(slot);
+                               log_end_pos = log_current_pos + ao_config.flight_log_max;
+                               while (log_current_pos < log_end_pos) {
                                        uint8_t i;
                                        static __xdata uint8_t b;
 
@@ -274,15 +281,18 @@ ao_log_delete(void) __reentrant
                                         * memory over and over again
                                         */
                                        for (i = 0; i < 16; i++) {
-                                               if (ao_storage_read(ao_log_current_pos + i, &b, 1))
+                                               if (ao_storage_read(log_current_pos + i, &b, 1))
                                                        if (b != 0xff)
                                                                break;
                                        }
                                        if (i == 16)
                                                break;
-                                       ao_storage_erase(ao_log_current_pos);
-                                       ao_log_current_pos += ao_storage_block;
+                                       ao_storage_erase(log_current_pos);
+                                       log_current_pos += ao_storage_block;
                                }
+#if HAS_TRACKER
+                               ao_tracker_erase_end();
+#endif
                                puts("Erased");
                                return;
                        }
index a6ad87a3be2608773fcca68e21676bdcb8d3008a..9febc7f090cb6ac33197b40cc054b9ee216e8a39 100644 (file)
@@ -50,6 +50,7 @@ struct gps_position   gps_position[GPS_RING];
 
 static uint8_t gps_head;
 
+static uint8_t tracker_mutex;
 static uint8_t log_started;
 static struct ao_telemetry_location gps_data;
 static uint8_t tracker_running;
@@ -100,17 +101,19 @@ ao_tracker(void)
                        tracker_running = 0;
                }
 
-               if (new_tracker_running && !tracker_running) {
+               if (new_tracker_running && !tracker_running)
                        ao_telemetry_set_interval(AO_SEC_TO_TICKS(tracker_interval));
-                       ao_log_start();
-               } else if (!new_tracker_running && tracker_running) {
+               else if (!new_tracker_running && tracker_running)
                        ao_telemetry_set_interval(0);
-                       ao_log_stop();
-               }
 
                tracker_running = new_tracker_running;
 
-               if (!tracker_running)
+               if (new_tracker_running && !ao_log_running)
+                       ao_log_start();
+               else if (!new_tracker_running && ao_log_running)
+                       ao_log_stop();
+
+               if (!ao_log_running)
                        continue;
 
                if (new & AO_GPS_NEW_DATA) {
@@ -138,10 +141,11 @@ ao_tracker(void)
                                        }
                                }
                                if (ao_tracker_force_telem) {
-                                       printf ("moving %d\n", moving);
+                                       printf ("moving %d started %d\n", moving, log_started);
                                        flush();
                                }
                                if (moving) {
+                                       ao_mutex_get(&tracker_mutex);
                                        if (!log_started) {
                                                ao_log_gps_flight();
                                                log_started = 1;
@@ -151,12 +155,37 @@ ao_tracker(void)
                                        gps_position[gps_head].longitude = gps_data.longitude;
                                        gps_position[gps_head].altitude = gps_data.altitude;
                                        gps_head = ao_gps_ring_next(gps_head);
+                                       ao_mutex_put(&tracker_mutex);
                                }
                        }
                }
        }
 }
 
+static uint8_t erasing_current;
+
+void
+ao_tracker_erase_start(uint16_t flight)
+{
+       erasing_current = flight == ao_flight_number;
+       if (erasing_current) {
+               ao_mutex_get(&tracker_mutex);
+               ao_log_stop();
+               if (++ao_flight_number == 0)
+                       ao_flight_number = 1;
+       }
+}
+
+void
+ao_tracker_erase_end(void)
+{
+       if (erasing_current) {
+               ao_log_scan();
+               log_started = 0;
+               ao_mutex_put(&tracker_mutex);
+       }
+}
+
 static struct ao_task ao_tracker_task;
 
 static void
index a0fd2f49294ab9a8e0cb18d26c9a5afbc299f02f..78c40cb4cad949710f007776ca28de48fb50a703 100644 (file)
 void
 ao_tracker_init(void);
 
+void
+ao_tracker_erase_start(uint16_t flight);
+
+void
+ao_tracker_erase_end(void);
+
 #endif /* _AO_TRACKER_H_ */