Update the oscilloscope state with new settings only after
they have been successfully stored in the device to avoid
an inconsistent state in case of SCPI SET command failure.

Signed-off-by: Guido Trentalancia <gu...@trentalancia.com>
---
 src/hardware/hameg-hmo/api.c |   18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff -pru libsigrok-git-orig/src/hardware/hameg-hmo/api.c 
libsigrok-git-hameg-hmo-update-state-after-scpi-cmd/src/hardware/hameg-hmo/api.c
--- libsigrok-git-orig/src/hardware/hameg-hmo/api.c     2018-11-17 
22:26:51.408234178 +0100
+++ 
libsigrok-git-hameg-hmo-update-state-after-scpi-cmd/src/hardware/hameg-hmo/api.c
    2018-11-18 15:01:39.643786638 +0100
@@ -257,7 +257,7 @@ static int config_set(uint32_t key, GVar
        struct dev_context *devc;
        const struct scope_config *model;
        struct scope_state *state;
-       double tmp_d;
+       double tmp_d, tmp_d2;
        gboolean update_sample_rate;
 
        if (!sdi)
@@ -284,13 +284,13 @@ static int config_set(uint32_t key, GVar
        case SR_CONF_TRIGGER_SOURCE:
                if ((idx = std_str_idx(data, *model->trigger_sources, 
model->num_trigger_sources)) < 0)
                        return SR_ERR_ARG;
-               state->trigger_source = idx;
                g_snprintf(command, sizeof(command),
                           (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
                           (*model->trigger_sources)[idx]);
                if (sr_scpi_send(sdi->conn, command) != SR_OK ||
                    sr_scpi_get_opc(sdi->conn) != SR_OK)
                        return SR_ERR;
+               state->trigger_source = idx;
                ret = SR_OK;
                break;
        case SR_CONF_VDIV:
@@ -300,7 +300,6 @@ static int config_set(uint32_t key, GVar
                        return SR_ERR_ARG;
                if ((j = std_cg_idx(cg, devc->analog_groups, 
model->analog_channels)) < 0)
                        return SR_ERR_ARG;
-               state->analog_channels[j].vdiv = idx;
                g_ascii_formatd(float_str, sizeof(float_str), "%E",
                        (float) (*model->vdivs)[idx][0] / 
(*model->vdivs)[idx][1]);
                g_snprintf(command, sizeof(command),
@@ -309,12 +308,12 @@ static int config_set(uint32_t key, GVar
                if (sr_scpi_send(sdi->conn, command) != SR_OK ||
                    sr_scpi_get_opc(sdi->conn) != SR_OK)
                        return SR_ERR;
+               state->analog_channels[j].vdiv = idx;
                ret = SR_OK;
                break;
        case SR_CONF_TIMEBASE:
                if ((idx = std_u64_tuple_idx(data, *model->timebases, 
model->num_timebases)) < 0)
                        return SR_ERR_ARG;
-               state->timebase = idx;
                g_ascii_formatd(float_str, sizeof(float_str), "%E",
                        (float) (*model->timebases)[idx][0] / 
(*model->timebases)[idx][1]);
                g_snprintf(command, sizeof(command),
@@ -323,6 +322,7 @@ static int config_set(uint32_t key, GVar
                if (sr_scpi_send(sdi->conn, command) != SR_OK ||
                    sr_scpi_get_opc(sdi->conn) != SR_OK)
                        return SR_ERR;
+               state->timebase = idx;
                ret = SR_OK;
                update_sample_rate = TRUE;
                break;
@@ -330,30 +330,30 @@ static int config_set(uint32_t key, GVar
                tmp_d = g_variant_get_double(data);
                if (tmp_d < 0.0 || tmp_d > 1.0)
                        return SR_ERR;
-               state->horiz_triggerpos = tmp_d;
-               tmp_d = -(tmp_d - 0.5) *
+               tmp_d2 = -(tmp_d - 0.5) *
                        ((double) (*model->timebases)[state->timebase][0] /
                        (*model->timebases)[state->timebase][1])
                         * model->num_xdivs;
-               g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
+               g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d2);
                g_snprintf(command, sizeof(command),
                           
(*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
                           float_str);
                if (sr_scpi_send(sdi->conn, command) != SR_OK ||
                    sr_scpi_get_opc(sdi->conn) != SR_OK)
                        return SR_ERR;
+               state->horiz_triggerpos = tmp_d;
                ret = SR_OK;
                break;
        case SR_CONF_TRIGGER_SLOPE:
                if ((idx = std_str_idx(data, *model->trigger_slopes, 
model->num_trigger_slopes)) < 0)
                        return SR_ERR_ARG;
-               state->trigger_slope = idx;
                g_snprintf(command, sizeof(command),
                           (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
                           (*model->trigger_slopes)[idx]);
                if (sr_scpi_send(sdi->conn, command) != SR_OK ||
                    sr_scpi_get_opc(sdi->conn) != SR_OK)
                        return SR_ERR;
+               state->trigger_slope = idx;
                ret = SR_OK;
                break;
        case SR_CONF_TRIGGER_PATTERN:
@@ -378,13 +378,13 @@ static int config_set(uint32_t key, GVar
                        return SR_ERR_ARG;
                if ((j = std_cg_idx(cg, devc->analog_groups, 
model->analog_channels)) < 0)
                        return SR_ERR_ARG;
-               state->analog_channels[j].coupling = idx;
                g_snprintf(command, sizeof(command),
                           (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
                           j + 1, (*model->coupling_options)[idx]);
                if (sr_scpi_send(sdi->conn, command) != SR_OK ||
                    sr_scpi_get_opc(sdi->conn) != SR_OK)
                        return SR_ERR;
+               state->analog_channels[j].coupling = idx;
                ret = SR_OK;
                break;
        case SR_CONF_LOGIC_THRESHOLD:


_______________________________________________
sigrok-devel mailing list
sigrok-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to