]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/motech-lps-30x/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / motech-lps-30x / protocol.c
index 86959ba5c3a71c1ac08241665130d7d080423bd4..b31f2ee1d33ad83bcf48f33e8881a32f55290f4b 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/** @file
- *  <em>Motech LPS-30x series</em> power supply driver
- *  @internal
- */
-
+#include <config.h>
 #include <errno.h>
 #include <string.h>
-
 #include "protocol.h"
 
 /** Send data packets for current measurements. */
@@ -34,32 +29,43 @@ static void send_data(struct sr_dev_inst *sdi)
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
        int i;
        float data[MAX_CHANNELS];
 
        devc = sdi->priv;
        packet.type = SR_DF_ANALOG;
        packet.payload = &analog;
-       analog.channels = sdi->channels;
-       analog.num_samples = 1;
 
-       analog.mq = SR_MQ_VOLTAGE;
-       analog.unit = SR_UNIT_VOLT;
-       analog.mqflags = SR_MQFLAG_DC;
+       /* Note: digits/spec_digits will be overridden later. */
+       sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
+
+       analog.meaning->channels = sdi->channels;
+       analog.num_samples = 1;
+       analog.meaning->mq = SR_MQ_VOLTAGE;
+       analog.meaning->unit = SR_UNIT_VOLT;
+       analog.meaning->mqflags = SR_MQFLAG_DC;
+       analog.encoding->digits = 3;
+       analog.spec->spec_digits = 2;
        analog.data = data;
+
        for (i = 0; i < devc->model->num_channels; i++)
-               analog.data[i] = devc->channel_status[i].output_voltage_last; /* Value always 3.3 or 5 for channel 3, if present! */
+               ((float *)analog.data)[i] = devc->channel_status[i].output_voltage_last; /* Value always 3.3 or 5 for channel 3, if present! */
        sr_session_send(sdi, &packet);
 
-       analog.mq = SR_MQ_CURRENT;
-       analog.unit = SR_UNIT_AMPERE;
-       analog.mqflags = 0;
+       analog.meaning->mq = SR_MQ_CURRENT;
+       analog.meaning->unit = SR_UNIT_AMPERE;
+       analog.meaning->mqflags = 0;
+       analog.encoding->digits = 4;
+       analog.spec->spec_digits = 3;
        analog.data = data;
        for (i = 0; i < devc->model->num_channels; i++)
-               analog.data[i] = devc->channel_status[i].output_current_last; /* Value always 0 for channel 3, if present! */
+               ((float *)analog.data)[i] = devc->channel_status[i].output_current_last; /* Value always 0 for channel 3, if present! */
        sr_session_send(sdi, &packet);
 
-       devc->num_samples++;
+       sr_sw_limits_update_samples_read(&devc->limits, 1);
 }
 
 /** Process a complete line (without CR/LF) in buf. */
@@ -70,6 +76,8 @@ static void process_line(struct sr_dev_inst *sdi)
        int auxint;
 
        devc = sdi->priv;
+       if (!devc)
+               return;
 
        switch (devc->acq_req_pending) {
        case 0: /* Should not happen... */
@@ -77,17 +85,22 @@ static void process_line(struct sr_dev_inst *sdi)
        case 1: /* Waiting for data reply to request */
                /* Convert numbers */
                switch (devc->acq_req) {
-               case AQ_U1:  case AQ_U2:  case AQ_I1:  case AQ_I2:
-                       if (sr_atod(devc->buf, &dbl) != SR_OK) {
+               case AQ_U1:
+               case AQ_U2:
+               case AQ_I1:
+               case AQ_I2:
+                       dbl = 0.0;
+                       if (sr_atod_ascii(devc->buf, &dbl) != SR_OK) {
                                sr_err("Failed to convert '%s' to double, errno=%d %s",
-                                       devc->buf, errno, strerror(errno));
+                                       devc->buf, errno, g_strerror(errno));
                                dbl = 0.0;
                        }
                        break;
                case AQ_STATUS:
+                       auxint = 0;
                        if (sr_atoi(devc->buf, &auxint) != SR_OK) {
                                sr_err("Failed to convert '%s' to int, errno=%d %s",
-                                       devc->buf, errno, strerror(errno));
+                                       devc->buf, errno, g_strerror(errno));
                                auxint = 0;
                        }
                        break;
@@ -137,7 +150,6 @@ SR_PRIV int motech_lps_30x_receive_data(int fd, int revents, void *cb_data)
        struct dev_context *devc;
        struct sr_serial_dev_inst *serial;
        int len;
-       gdouble elapsed_s;
 
        (void)fd;
 
@@ -176,60 +188,56 @@ SR_PRIV int motech_lps_30x_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))
-               sdi->driver->dev_acquisition_stop(sdi, cb_data);
+       if (sr_sw_limits_check(&devc->limits))
+               sr_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, cb_data);
-       }
+       /* Only request the next packet if required. */
+       if (!((sdi->status == SR_ST_ACTIVE) && (devc->acq_running)))
+               return TRUE;
 
-       /* Request next packet, if required. */
-       if ((sdi->status == SR_ST_ACTIVE) && (devc->acq_running)){
-               if (devc->acq_req_pending) {
-                       gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
-                       if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) {
-                               sr_spew("Request timeout: req=%d t=%lldus", (int)devc->acq_req, elapsed_us);
-                               devc->acq_req_pending = 0;
-                       }
+       if (devc->acq_req_pending) {
+               int64_t elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
+               if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) {
+                       sr_spew("Request timeout: req=%d t=%" PRIi64 "us",
+                               (int)devc->acq_req, elapsed_us);
+                       devc->acq_req_pending = 0;
                }
-               if (devc->acq_req_pending == 0) {
-                       switch (devc->acq_req) {
-                       case AQ_NONE: /* Fall through */
-                       case AQ_STATUS:
-                               devc->acq_req = AQ_U1;
-                               lps_send_req(serial, "VOUT1");
-                               break;
-                       case AQ_U1:
-                               devc->acq_req = AQ_I1;
-                               lps_send_req(serial, "IOUT1");
-                               break;
-                       case AQ_I1:
-                               if (devc->model->num_channels == 1) {
-                                       devc->acq_req = AQ_STATUS;
-                                       lps_send_req(serial, "STATUS");
-                               } else {
-                                       devc->acq_req = AQ_U2;
-                                       lps_send_req(serial, "VOUT2");
-                               }
-                               break;
-                       case AQ_U2:
-                               devc->acq_req = AQ_I2;
-                               lps_send_req(serial, "IOUT2");
-                               break;
-                       case AQ_I2:
+       }
+
+       if (devc->acq_req_pending == 0) {
+               switch (devc->acq_req) {
+               case AQ_NONE: /* Fall through */
+               case AQ_STATUS:
+                       devc->acq_req = AQ_U1;
+                       lps_send_req(serial, "VOUT1");
+                       break;
+               case AQ_U1:
+                       devc->acq_req = AQ_I1;
+                       lps_send_req(serial, "IOUT1");
+                       break;
+               case AQ_I1:
+                       if (devc->model->num_channels == 1) {
                                devc->acq_req = AQ_STATUS;
                                lps_send_req(serial, "STATUS");
-                               break;
-                       default:
-                               sr_err("Illegal devc->acq_req=%d", devc->acq_req);
-                               return SR_ERR;
+                       } else {
+                               devc->acq_req = AQ_U2;
+                               lps_send_req(serial, "VOUT2");
                        }
-                       devc->req_sent_at = g_get_real_time();
-                       devc->acq_req_pending = 1;
+                       break;
+               case AQ_U2:
+                       devc->acq_req = AQ_I2;
+                       lps_send_req(serial, "IOUT2");
+                       break;
+               case AQ_I2:
+                       devc->acq_req = AQ_STATUS;
+                       lps_send_req(serial, "STATUS");
+                       break;
+               default:
+                       sr_err("Illegal devc->acq_req=%d", devc->acq_req);
+                       return SR_ERR;
                }
+               devc->req_sent_at = g_get_real_time();
+               devc->acq_req_pending = 1;
        }
 
        return TRUE;