src-avr: Finish up telescience logging
[fw/altos] / src-avr / ao_log_telescience.c
index df3ccb45cc3a708c9fc92310f702d16171c64fd2..f092027a6db88bcc78424ea13ab1e677025856b4 100644 (file)
 
 static struct ao_task ao_log_task;
 
-uint8_t ao_log_running;
-uint8_t        ao_log_mutex;
-uint32_t       ao_log_begin_pos, ao_log_end_pos;
+uint8_t        ao_log_running;
+uint8_t                ao_log_mutex;
+uint32_t       ao_log_start_pos;
+uint32_t       ao_log_end_pos;
 uint32_t       ao_log_current_pos;
 
-#define AO_LOG_TELESCIENCE_START       'b'
-#define AO_LOG_TELESCIENCE_STOP                'e'
-#define AO_LOG_TELESCIENCE_DATA                'd'
+#define AO_LOG_TELESCIENCE_START       ((uint8_t) 's')
+#define AO_LOG_TELESCIENCE_DATA                ((uint8_t) 'd')
 
 struct ao_log_telescience {
        uint8_t         type;
@@ -73,12 +73,31 @@ ao_log_telescience_data(struct ao_log_telescience *log)
        return wrote;
 }
 
+static uint8_t
+ao_log_valid(struct ao_log_telescience *log)
+{
+       uint8_t *d;
+       uint8_t i;
+       d = (uint8_t *) log;
+       for (i = 0; i < sizeof (struct ao_log_telescience); i++)
+               if (d[i] != 0xff)
+                       return 1;
+       return 0;
+}
+
 void
 ao_log_telescience(void)
 {
        ao_storage_setup();
 
-       ao_log_current_pos = 0;
+       /* Find end of data */
+       while (ao_log_start_pos < ao_log_end_pos) {
+               if (!(ao_storage_read(ao_log_start_pos, &log, sizeof (struct ao_log_telescience))))
+                       break;
+               if (!ao_log_valid(&log))
+                       break;
+       }
+       ao_log_current_pos = ao_log_start_pos;
        ao_log_end_pos = ao_storage_config;
        for (;;) {
                while (!ao_log_running)
@@ -105,17 +124,13 @@ ao_log_telescience(void)
                        /* Wait for more ADC data to arrive */
                        ao_sleep((void *) &ao_adc_head);
                }
-               memset(&log, '\0', sizeof (struct ao_log_telescience));
-               log.type = AO_LOG_TELESCIENCE_STOP;
-               log.tick = ao_time();
-               ao_log_telescience_data(&log);
-               ao_storage_flush();
        }
 }
 
 void
 ao_log_start(void)
 {
+       printf("Log goes from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
        ao_log_running = 1;
        ao_wakeup(&ao_log_running);
 }
@@ -123,6 +138,7 @@ ao_log_start(void)
 void
 ao_log_stop(void)
 {
+       printf ("Log stopped at %ld\n", ao_log_current_pos);
        ao_log_running = 0;
        ao_wakeup((void *) &ao_adc_head);
 }
@@ -139,8 +155,62 @@ ao_log_set(void)
        }
 }
 
+void
+ao_log_list(void)
+{
+       uint32_t        pos;
+       uint32_t        start = 0;
+       uint8_t         flight = 0;
+
+       for (pos = 0; pos < ao_storage_config; pos += sizeof (struct ao_log_telescience)) {
+               if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
+                       break;
+               if (!ao_log_valid(&log) || log.type == AO_LOG_TELESCIENCE_START) {
+                       if (pos != start) {
+                               printf("flight %d start %x end %x\n",
+                                      flight,
+                                      (uint16_t) (start >> 8),
+                                      (uint16_t) ((pos + 0xff) >> 8));
+                       }
+                       if (!ao_log_valid(&log))
+                               break;
+                       start = pos;
+                       flight++;
+               }
+       }
+       printf ("done\n");
+}
+
+void
+ao_log_delete(void)
+{
+       uint32_t        pos;
+
+       ao_cmd_hex();
+       if (ao_cmd_status != ao_cmd_success)
+               return;
+       if (ao_cmd_lex_i != 1) {
+               ao_cmd_status = ao_cmd_syntax_error;
+               printf("No such flight: %d\n", ao_cmd_lex_i);
+               return;
+       }
+       for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
+               if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
+                       break;
+               if (!ao_log_valid(&log))
+                       break;
+               ao_storage_erase(pos);
+       }
+       if (pos == 0)
+               printf("No such flight: %d\n", ao_cmd_lex_i);
+       else
+               printf ("Erased\n");
+}
+
 const struct ao_cmds ao_log_cmds[] = {
        { ao_log_set,   "L <0 off, 1 on>\0Set logging mode" },
+       { ao_log_list,  "l\0List stored flight logs" },
+       { ao_log_delete, "d 1\0Delete all stored flights" },
        { 0,    NULL },
 };