altos: Merge common config code in ao_config.c
authorKeith Packard <keithp@keithp.com>
Sat, 20 Aug 2011 18:28:53 +0000 (11:28 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 20 Aug 2011 18:32:43 +0000 (11:32 -0700)
This shuffles code around in ao_config.c to share some common code
segments for starting and committing config changes.

This also stops printing out changes as they are made which aren't
needed by the UI code.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/ao_config.c

index 331509e..e0eae78 100644 (file)
@@ -105,11 +105,24 @@ _ao_config_get(void)
        ao_config_loaded = 1;
 }
 
-void
-ao_config_get(void)
+static void
+_ao_config_edit_start(void)
 {
        ao_mutex_get(&ao_config_mutex);
        _ao_config_get();
+}
+
+static void
+_ao_config_edit_finish(void)
+{
+       ao_config_dirty = 1;
+       ao_mutex_put(&ao_config_mutex);
+}
+
+void
+ao_config_get(void)
+{
+       _ao_config_edit_start();
        ao_mutex_put(&ao_config_mutex);
 }
 
@@ -137,13 +150,10 @@ ao_config_callsign_set(void) __reentrant
        }
        if (ao_cmd_status != ao_cmd_success)
                return;
-       ao_mutex_get(&ao_config_mutex);
-       _ao_config_get();
+       _ao_config_edit_start();
        memcpy(&ao_config.callsign, &callsign,
               AO_MAX_CALLSIGN + 1);
-       ao_config_dirty = 1;
-       ao_mutex_put(&ao_config_mutex);
-       ao_config_callsign_show();
+       _ao_config_edit_finish();
 }
 
 void
@@ -159,12 +169,9 @@ ao_config_radio_channel_set(void) __reentrant
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
                return;
-       ao_mutex_get(&ao_config_mutex);
-       _ao_config_get();
+       _ao_config_edit_start();
        ao_config.radio_channel = ao_cmd_lex_i;
-       ao_config_dirty = 1;
-       ao_mutex_put(&ao_config_mutex);
-       ao_config_radio_channel_show();
+       _ao_config_edit_finish();
        ao_radio_recv_abort();
 }
 
@@ -183,12 +190,9 @@ ao_config_main_deploy_set(void) __reentrant
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
                return;
-       ao_mutex_get(&ao_config_mutex);
-       _ao_config_get();
+       _ao_config_edit_start();
        ao_config.main_deploy = ao_cmd_lex_i;
-       ao_config_dirty = 1;
-       ao_mutex_put(&ao_config_mutex);
-       ao_config_main_deploy_show();
+       _ao_config_edit_finish();
 }
 
 #if HAS_ACCEL
@@ -250,13 +254,10 @@ ao_config_accel_calibrate_set(void) __reentrant
                       up, down);
                return;
        }
-       ao_mutex_get(&ao_config_mutex);
-       _ao_config_get();
+       _ao_config_edit_start();
        ao_config.accel_plus_g = up;
        ao_config.accel_minus_g = down;
-       ao_config_dirty = 1;
-       ao_mutex_put(&ao_config_mutex);
-       ao_config_accel_calibrate_show();
+       _ao_config_edit_finish();
 }
 #endif /* HAS_ACCEL */
 
@@ -273,12 +274,9 @@ ao_config_apogee_delay_set(void) __reentrant
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
                return;
-       ao_mutex_get(&ao_config_mutex);
-       _ao_config_get();
+       _ao_config_edit_start();
        ao_config.apogee_delay = ao_cmd_lex_i;
-       ao_config_dirty = 1;
-       ao_mutex_put(&ao_config_mutex);
-       ao_config_apogee_delay_show();
+       _ao_config_edit_finish();
 }
 
 #endif /* HAS_ADC */
@@ -295,12 +293,9 @@ ao_config_radio_cal_set(void) __reentrant
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
                return;
-       ao_mutex_get(&ao_config_mutex);
-       _ao_config_get();
+       _ao_config_edit_start();
        ao_config.radio_setting = ao_config.radio_cal = ao_cmd_lex_u32;
-       ao_config_dirty = 1;
-       ao_mutex_put(&ao_config_mutex);
-       ao_config_radio_cal_show();
+       _ao_config_edit_finish();
 }
 
 #if HAS_EEPROM
@@ -326,12 +321,9 @@ ao_config_log_set(void) __reentrant
        else if (ao_cmd_lex_i > config)
                printf("Flight log max %d kB\n", config);
        else {
-               ao_mutex_get(&ao_config_mutex);
-               _ao_config_get();
+               _ao_config_edit_start();
                ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
-               ao_config_dirty = 1;
-               ao_mutex_put(&ao_config_mutex);
-               ao_config_log_show();
+               _ao_config_edit_finish();
        }
 }
 #endif /* HAS_EEPROM */
@@ -349,12 +341,9 @@ ao_config_ignite_mode_set(void) __reentrant
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
                return;
-       ao_mutex_get(&ao_config_mutex);
-       _ao_config_get();
+       _ao_config_edit_start();
        ao_config.ignite_mode = ao_cmd_lex_i;
-       ao_config_dirty = 1;
-       ao_mutex_put(&ao_config_mutex);
-       ao_config_ignite_mode_show();
+       _ao_config_edit_finish();
 }
 #endif
 
@@ -371,8 +360,7 @@ ao_config_pad_orientation_set(void) __reentrant
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
                return;
-       ao_mutex_get(&ao_config_mutex);
-       _ao_config_get();
+       _ao_config_edit_start();
        ao_cmd_lex_i &= 1;
        if (ao_config.pad_orientation != ao_cmd_lex_i) {
                uint16_t t;
@@ -381,9 +369,7 @@ ao_config_pad_orientation_set(void) __reentrant
                ao_config.accel_minus_g = 0x7fff - t;
        }
        ao_config.pad_orientation = ao_cmd_lex_i;
-       ao_config_dirty = 1;
-       ao_mutex_put(&ao_config_mutex);
-       ao_config_pad_orientation_show();
+       _ao_config_edit_finish();
 }
 #endif
 
@@ -399,13 +385,10 @@ ao_config_radio_setting_set(void) __reentrant
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
                return;
-       ao_mutex_get(&ao_config_mutex);
-       _ao_config_get();
+       _ao_config_edit_start();
        ao_config.radio_setting = ao_cmd_lex_u32;
        ao_config.radio_channel = 0;
-       ao_config_dirty = 1;
-       ao_mutex_put(&ao_config_mutex);
-       ao_config_radio_setting_show();
+       _ao_config_edit_finish();
        ao_radio_recv_abort();
 }
 
@@ -456,13 +439,13 @@ __code struct ao_config_var ao_config_vars[] = {
        { "R <setting>\0Radio freq control (freq = 434.550 * setting/cal)",
          ao_config_radio_setting_set,  ao_config_radio_setting_show },
        { "s\0Show",
-         ao_config_show,               ao_config_show },
+         ao_config_show,               0 },
 #if HAS_EEPROM
        { "w\0Write to eeprom",
-         ao_config_write,              ao_config_write },
+         ao_config_write,              0 },
 #endif
        { "?\0Help",
-         ao_config_help,               ao_config_help },
+         ao_config_help,               0 },
        { 0, 0, 0 }
 };
 
@@ -479,13 +462,10 @@ ao_config_set(void)
        func = 0;
        for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
                if (ao_config_vars[cmd].str[0] == c) {
-                       func = ao_config_vars[cmd].set;
-                       break;
+                       (*ao_config_vars[cmd].set)();
+                       return;
                }
-       if (func)
-               (*func)();
-       else
-               ao_cmd_status = ao_cmd_syntax_error;
+       ao_cmd_status = ao_cmd_syntax_error;
 }
 
 static void
@@ -505,7 +485,7 @@ ao_config_show(void) __reentrant
        printf("Config version: %d.%d\n",
               ao_config.major, ao_config.minor);
        for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
-               if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
+               if (ao_config_vars[cmd].show)
                        (*ao_config_vars[cmd].show)();
 }