]> sigrok.org Git - libsigrok.git/commitdiff
gmc-mh-1x-2x: Use software limits helpers
authorLars-Peter Clausen <redacted>
Wed, 4 May 2016 12:19:26 +0000 (14:19 +0200)
committerUwe Hermann <redacted>
Tue, 31 May 2016 13:54:52 +0000 (15:54 +0200)
Use the new software limit helper functions rather than open-coding their
functionality.

This also fixes the issue that driver the does not reset all the limit
statistics in acquisition_start().

Signed-off-by: Lars-Peter Clausen <redacted>
src/hardware/gmc-mh-1x-2x/api.c
src/hardware/gmc-mh-1x-2x/protocol.c
src/hardware/gmc-mh-1x-2x/protocol.h

index 7b8e632162d522e9aaa5e46cb223fa129a74fb0c..0ca9029fe355b8eadbff8cb379641bbe779e5727 100644 (file)
@@ -207,11 +207,8 @@ static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options)
                sdi->vendor = g_strdup(VENDOR_GMC);
                sdi->model = g_strdup(gmc_model_str(model));
                devc = g_malloc0(sizeof(struct dev_context));
+               sr_sw_limits_init(&devc->limits);
                devc->model = model;
-               devc->limit_samples = 0;
-               devc->limit_msec = 0;
-               devc->num_samples = 0;
-               devc->elapsed_msec = g_timer_new();
                devc->settings_ok = FALSE;
                sdi->conn = serial;
                sdi->priv = devc;
@@ -298,7 +295,7 @@ static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
 
                if (devc->model != METRAHIT_NONE) {
                        sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(devc->model));
-                       devc->elapsed_msec = g_timer_new();
+                       sr_sw_limits_init(&devc->limits);
                        sdi->model = g_strdup(gmc_model_str(devc->model));
                        sdi->version = g_strdup_printf("Firmware %d.%d", devc->fw_ver_maj, devc->fw_ver_min);
                        sdi->conn = serial;
@@ -339,13 +336,8 @@ static int dev_close(struct sr_dev_inst *sdi)
        std_serial_dev_close(sdi);
 
        sdi->status = SR_ST_INACTIVE;
-
-       /* Free dynamically allocated resources. */
-       if ((devc = sdi->priv) && devc->elapsed_msec) {
-               g_timer_destroy(devc->elapsed_msec);
-               devc->elapsed_msec = NULL;
+       if ((devc = sdi->priv))
                devc->model = METRAHIT_NONE;
-       }
 
        return SR_OK;
 }
@@ -366,11 +358,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
        ret = SR_OK;
        switch (key) {
        case SR_CONF_LIMIT_SAMPLES:
-               *data = g_variant_new_uint64(devc->limit_samples);
-               break;
        case SR_CONF_LIMIT_MSEC:
-               *data = g_variant_new_uint64(devc->limit_msec);
-               break;
+               return sr_sw_limits_config_get(&devc->limits, key, data);
        case SR_CONF_POWER_OFF:
                *data = g_variant_new_boolean(FALSE);
                break;
@@ -444,12 +433,9 @@ static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi)
        devc->settings_ok = FALSE;
        devc->buflen = 0;
 
+       sr_sw_limits_acquisition_start(&devc->limits);
        std_session_send_df_header(sdi, LOG_PREFIX);
 
-       /* Start timer, if required. */
-       if (devc->limit_msec)
-               g_timer_start(devc->elapsed_msec);
-
        /* Poll every 40ms, or whenever some data comes in. */
        serial = sdi->conn;
        serial_source_add(sdi->session, serial, G_IO_IN, 40,
@@ -470,12 +456,9 @@ static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi)
        devc->settings_ok = FALSE;
        devc->buflen = 0;
 
+       sr_sw_limits_acquisition_start(&devc->limits);
        std_session_send_df_header(sdi, LOG_PREFIX);
 
-       /* Start timer, if required. */
-       if (devc->limit_msec)
-               g_timer_start(devc->elapsed_msec);
-
        /* Poll every 40ms, or whenever some data comes in. */
        serial = sdi->conn;
        serial_source_add(sdi->session, serial, G_IO_IN, 40,
@@ -487,12 +470,6 @@ static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi)
 
 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 {
-       struct dev_context *devc;
-
-       /* Stop timer, if required. */
-       if (sdi && (devc = sdi->priv) && devc->limit_msec)
-               g_timer_stop(devc->elapsed_msec);
-
        return std_serial_dev_acquisition_stop(sdi, dev_close,
                        sdi->conn, LOG_PREFIX);
 }
index e116244e0c448401ad53c42d05c10c12db942d24..4758593d5aa4a389f8652764651e3fa1c1012458 100644 (file)
@@ -670,7 +670,7 @@ static void send_value(struct sr_dev_inst *sdi)
        packet.payload = &analog;
        sr_session_send(sdi, &packet);
 
-       devc->num_samples++;
+       sr_sw_limits_update_samples_read(&devc->limits, 1);
 }
 
 /** Process 6-byte data message, Metrahit 1x/2x send mode. */
@@ -1097,7 +1097,6 @@ SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data)
        struct sr_serial_dev_inst *serial;
        uint8_t buf, msgt;
        int len;
-       gdouble elapsed_s;
 
        (void)fd;
 
@@ -1171,16 +1170,9 @@ SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data)
                }
        }
 
-       /* If number of samples or time limit reached, stop acquisition. */
-       if (devc->limit_samples && (devc->num_samples >= devc->limit_samples))
+       if (sr_sw_limits_check(&devc->limits))
                sdi->driver->dev_acquisition_stop(sdi);
 
-       if (devc->limit_msec) {
-               elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL);
-               if ((elapsed_s * 1000) >= devc->limit_msec)
-                       sdi->driver->dev_acquisition_stop(sdi);
-       }
-
        return TRUE;
 }
 
@@ -1191,7 +1183,6 @@ SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data)
        struct sr_serial_dev_inst *serial;
        uint8_t buf;
        int len;
-       gdouble elapsed_s;
 
        (void)fd;
 
@@ -1222,16 +1213,9 @@ SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data)
                }
        }
 
-       /* If number of samples or time limit reached, stop acquisition. */
-       if (devc->limit_samples && (devc->num_samples >= devc->limit_samples))
+       if (sr_sw_limits_check(&devc->limits))
                sdi->driver->dev_acquisition_stop(sdi);
 
-       if (devc->limit_msec) {
-               elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL);
-               if ((elapsed_s * 1000) >= devc->limit_msec)
-                       sdi->driver->dev_acquisition_stop(sdi);
-       }
-
        /* Request next data set, if required */
        if (sdi->status == SR_ST_ACTIVE) {
                if (devc->response_pending) {
@@ -1539,12 +1523,9 @@ SR_PRIV int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *s
                else
                        g_usleep(2 * 1000 * 1000); /* Wait to ensure transfer before interface switched off. */
                break;
-       case SR_CONF_LIMIT_MSEC:
-               devc->limit_msec = g_variant_get_uint64(data);
-               break;
        case SR_CONF_LIMIT_SAMPLES:
-               devc->limit_samples = g_variant_get_uint64(data);
-               break;
+       case SR_CONF_LIMIT_MSEC:
+               return sr_sw_limits_config_set(&devc->limits, key, data);
        default:
                return SR_ERR_NA;
        }
index b368576b92858395010a43e624d0279e931c5de9..ea2e064fed286096f9352c66b7d0ee860b821ab9 100644 (file)
@@ -87,8 +87,7 @@ struct dev_context {
        enum model model;       /**< Model code. */
 
        /* Acquisition settings */
-       uint64_t limit_samples; /**< Target number of samples */
-       uint64_t limit_msec;    /**< Target sampling time */
+       struct sr_sw_limits limits;
 
        /* Operational state */
        gboolean settings_ok;   /**< Settings msg received yet. */
@@ -111,8 +110,6 @@ struct dev_context {
        gboolean response_pending; /**< Request sent, response is pending. */
 
        /* Temporary state across callbacks */
-       uint64_t num_samples;   /**< Current #samples for limit_samples */
-       GTimer *elapsed_msec;   /**< Used for sampling with limit_msec  */
        uint8_t buf[GMC_BUFSIZE];       /**< Buffer for read callback */
        int buflen;                     /**< Data len in buf */
 };