altos: Make telescience reliably log only when told to
[fw/altos] / src-avr / ao_log_telescience.c
index f092027a6db88bcc78424ea13ab1e677025856b4..ef5a1c97d3e03c386cea94968918e5f126c64028 100644 (file)
@@ -85,6 +85,29 @@ ao_log_valid(struct ao_log_telescience *log)
        return 0;
 }
 
+void
+ao_log_start(void)
+{
+       ao_log_running = 1;
+       ao_wakeup(&ao_log_running);
+}
+
+void
+ao_log_stop(void)
+{
+       ao_log_running = 0;
+       ao_wakeup((void *) &ao_adc_head);
+}
+
+void
+ao_log_check_pin(void)
+{
+       if (PINB & (1 << PINB0))
+               ao_log_stop();
+       else
+               ao_log_start();
+}
+
 void
 ao_log_telescience(void)
 {
@@ -97,12 +120,21 @@ ao_log_telescience(void)
                if (!ao_log_valid(&log))
                        break;
        }
+
+       /*
+        * Wait for the other side to settle down
+        */
+       ao_delay(AO_SEC_TO_TICKS(5));
+
+       ao_log_check_pin();
+
        ao_log_current_pos = ao_log_start_pos;
        ao_log_end_pos = ao_storage_config;
        for (;;) {
                while (!ao_log_running)
                        ao_sleep(&ao_log_running);
 
+               flush();
                memset(&log, '\0', sizeof (struct ao_log_telescience));
                log.type = AO_LOG_TELESCIENCE_START;
                log.tick = ao_time();
@@ -124,35 +156,25 @@ ao_log_telescience(void)
                        /* Wait for more ADC data to arrive */
                        ao_sleep((void *) &ao_adc_head);
                }
+               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);
-}
-
-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);
-}
-
 void
 ao_log_set(void)
 {
+       printf("Logging currently %s\n", ao_log_running ? "on" : "off");
        ao_cmd_hex();
        if (ao_cmd_status == ao_cmd_success) {
-               if (ao_cmd_lex_i)
+               if (ao_cmd_lex_i) {
+                       printf("Logging from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
                        ao_log_start();
-               else
+               } else {
+                       printf ("Log stopped at %ld\n", ao_log_current_pos);
                        ao_log_stop();
+               }
        }
+       ao_cmd_status = ao_cmd_success;
 }
 
 void
@@ -162,7 +184,7 @@ ao_log_list(void)
        uint32_t        start = 0;
        uint8_t         flight = 0;
 
-       for (pos = 0; pos < ao_storage_config; pos += sizeof (struct ao_log_telescience)) {
+       for (pos = 0; ; 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) {
@@ -194,6 +216,7 @@ ao_log_delete(void)
                printf("No such flight: %d\n", ao_cmd_lex_i);
                return;
        }
+       ao_log_stop();
        for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
                if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
                        break;
@@ -201,6 +224,7 @@ ao_log_delete(void)
                        break;
                ao_storage_erase(pos);
        }
+       ao_log_current_pos = ao_log_start_pos = 0;
        if (pos == 0)
                printf("No such flight: %d\n", ao_cmd_lex_i);
        else
@@ -214,11 +238,24 @@ const struct ao_cmds ao_log_cmds[] = {
        { 0,    NULL },
 };
 
+ISR(PCINT0_vect)
+{
+       ao_log_check_pin();
+}
+
 void
 ao_log_init(void)
 {
        ao_log_running = 0;
 
+       DDRB &= ~(1 << DDB0);
+
+       PORTB |= (1 << PORTB0);         /* Pull input up; require input to log */
+
+       PCMSK0 |= (1 << PCINT0);        /* Enable PCINT0 pin change */
+
+       PCICR |= (1 << PCIE0);          /* Enable pin change interrupt */
+
        ao_cmd_register(&ao_log_cmds[0]);
 
        ao_add_task(&ao_log_task, ao_log_telescience, "log");