]> sigrok.org Git - libsigrok.git/blobdiff - hardware/fluke-dmm/fluke.c
All drivers: Drop unneeded comments.
[libsigrok.git] / hardware / fluke-dmm / fluke.c
index 809509755a8357e4e2b22b0a28b11c74aae606d3..33f7bb651e5c1e7668cd65fe4b11361efb75c00a 100644 (file)
@@ -20,7 +20,6 @@
 #include <glib.h>
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
-#include "config.h"
 #include "fluke-dmm.h"
 #include <stdlib.h>
 #include <math.h>
 #include <errno.h>
 
 
+static struct sr_datafeed_analog *handle_qm_v1(const struct sr_dev_inst *sdi,
+               char **tokens)
+{
+       struct sr_datafeed_analog *analog;
+       float fvalue;
+       char *e, *u;
+       gboolean is_oor;
+
+       (void)sdi;
+
+       if (strcmp(tokens[0], "QM"))
+               return NULL;
+
+       if ((e = strstr(tokens[1], "Out of range"))) {
+               is_oor = TRUE;
+               fvalue = -1;
+       } else {
+               is_oor = FALSE;
+               fvalue = strtof(tokens[1], &e);
+               if (fvalue == 0.0 && e == tokens[1]) {
+                       /* Happens all the time, when switching modes. */
+                       sr_dbg("Invalid float.");
+                       return NULL;
+               }
+       }
+       while(*e && *e == ' ')
+               e++;
+
+       /* TODO: Check malloc return value. */
+       analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
+       analog->num_samples = 1;
+       /* TODO: Check malloc return value. */
+       analog->data = g_try_malloc(sizeof(float));
+       if (is_oor)
+               *analog->data = NAN;
+       else
+               *analog->data = fvalue;
+       analog->mq = -1;
+
+       if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
+               analog->mq = SR_MQ_VOLTAGE;
+               analog->unit = SR_UNIT_VOLT;
+               if (!is_oor && e[0] == 'm')
+                       *analog->data /= 1000;
+               /* This catches "V AC", "V DC" and "V AC+DC". */
+               if (strstr(u, "AC"))
+                       analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
+               if (strstr(u, "DC"))
+                       analog->mqflags |= SR_MQFLAG_DC;
+       } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
+               analog->mq = SR_MQ_VOLTAGE;
+               if (u[2] == 'm')
+                       analog->unit = SR_UNIT_DECIBEL_MW;
+               else
+                       analog->unit = SR_UNIT_DECIBEL_VOLT;
+               analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
+       } else if ((u = strstr(e, "Ohms"))) {
+               analog->mq = SR_MQ_RESISTANCE;
+               analog->unit = SR_UNIT_OHM;
+               if (is_oor)
+                       *analog->data = INFINITY;
+               else if (e[0] == 'k')
+                       *analog->data *= 1000;
+               else if (e[0] == 'M')
+                       *analog->data *= 1000000;
+       } else if (!strcmp(e, "nS")) {
+               analog->mq = SR_MQ_CONDUCTANCE;
+               analog->unit = SR_UNIT_SIEMENS;
+               *analog->data /= 1e+9;
+       } else if ((u = strstr(e, "Farads"))) {
+               analog->mq = SR_MQ_CAPACITANCE;
+               analog->unit = SR_UNIT_FARAD;
+               if (!is_oor) {
+                       if (e[0] == 'm')
+                               *analog->data /= 1e+3;
+                       else if (e[0] == 'u')
+                               *analog->data /= 1e+6;
+                       else if (e[0] == 'n')
+                               *analog->data /= 1e+9;
+               }
+       } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
+               analog->mq = SR_MQ_TEMPERATURE;
+               if (u[4] == 'C')
+                       analog->unit = SR_UNIT_CELSIUS;
+               else
+                       analog->unit = SR_UNIT_FAHRENHEIT;
+       } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
+               analog->mq = SR_MQ_CURRENT;
+               analog->unit = SR_UNIT_AMPERE;
+               /* This catches "A AC", "A DC" and "A AC+DC". */
+               if (strstr(u, "AC"))
+                       analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
+               if (strstr(u, "DC"))
+                       analog->mqflags |= SR_MQFLAG_DC;
+               if (!is_oor) {
+                       if (e[0] == 'm')
+                               *analog->data /= 1e+3;
+                       else if (e[0] == 'u')
+                               *analog->data /= 1e+6;
+               }
+       } else if ((u = strstr(e, "Hz"))) {
+               analog->mq = SR_MQ_FREQUENCY;
+               analog->unit = SR_UNIT_HERTZ;
+               if (e[0] == 'k')
+                       *analog->data *= 1e+3;
+       } else if (!strcmp(e, "%")) {
+               analog->mq = SR_MQ_DUTY_CYCLE;
+               analog->unit = SR_UNIT_PERCENTAGE;
+       } else if ((u = strstr(e, "ms"))) {
+               analog->mq = SR_MQ_PULSE_WIDTH;
+               analog->unit = SR_UNIT_SECOND;
+               *analog->data /= 1e+3;
+       }
+
+       if (analog->mq == -1) {
+               /* Not a valid measurement. */
+               g_free(analog->data);
+               g_free(analog);
+               analog = NULL;
+       }
+
+       return analog;
+}
+
 static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
                char **tokens)
 {
@@ -36,14 +159,17 @@ static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
        char *eptr;
 
        (void)sdi;
+
        fvalue = strtof(tokens[0], &eptr);
        if (fvalue == 0.0 && eptr == tokens[0]) {
-               sr_err("fluke-dmm: invalid float");
+               sr_err("Invalid float.");
                return NULL;
        }
 
+       /* TODO: Check malloc return value. */
        analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
        analog->num_samples = 1;
+       /* TODO: Check malloc return value. */
        analog->data = g_try_malloc(sizeof(float));
        *analog->data = fvalue;
        analog->mq = -1;
@@ -61,15 +187,22 @@ static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
                        *analog->data = NAN;
                } else
                        analog->mq = -1;
-       } if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
+       } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
+               analog->mq = SR_MQ_VOLTAGE;
+               if (tokens[1][2] == 'm')
+                       analog->unit = SR_UNIT_DECIBEL_MW;
+               else
+                       analog->unit = SR_UNIT_DECIBEL_VOLT;
+               analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
+       } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
                if (!strcmp(tokens[2], "NORMAL")) {
                        analog->mq = SR_MQ_TEMPERATURE;
                        if (tokens[1][0] == 'C')
-                          analog->unit = SR_UNIT_CELSIUS;
+                               analog->unit = SR_UNIT_CELSIUS;
                        else
                                analog->unit = SR_UNIT_FAHRENHEIT;
                }
-       } if (!strcmp(tokens[1], "OHM")) {
+       } else if (!strcmp(tokens[1], "OHM")) {
                if (!strcmp(tokens[3], "NONE")) {
                        analog->mq = SR_MQ_RESISTANCE;
                        analog->unit = SR_UNIT_OHM;
@@ -86,7 +219,7 @@ static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
                        analog->unit = SR_UNIT_BOOLEAN;
                        *analog->data = 1.0;
                }
-       } if (!strcmp(tokens[1], "F")
+       } else if (!strcmp(tokens[1], "F")
                        && !strcmp(tokens[2], "NORMAL")
                        && !strcmp(tokens[3], "NONE")) {
                analog->mq = SR_MQ_CAPACITANCE;
@@ -94,7 +227,6 @@ static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
        } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
                analog->mq = SR_MQ_CURRENT;
                analog->unit = SR_UNIT_AMPERE;
-               analog->mqflags = SR_MQFLAG_RMS;
                if (!strcmp(tokens[2], "NORMAL")) {
                        if (tokens[1][1] == 'A') {
                                analog->mqflags |= SR_MQFLAG_AC;
@@ -108,18 +240,17 @@ static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
        } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
                analog->mq = SR_MQ_FREQUENCY;
                analog->unit = SR_UNIT_HERTZ;
-       } if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
+       } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
                analog->mq = SR_MQ_DUTY_CYCLE;
                analog->unit = SR_UNIT_PERCENTAGE;
-       } if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
+       } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
                analog->mq = SR_MQ_PULSE_WIDTH;
                analog->unit = SR_UNIT_SECOND;
-       } if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
+       } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
                analog->mq = SR_MQ_CONDUCTANCE;
                analog->unit = SR_UNIT_SIEMENS;
        }
 
-
        if (analog->mq == -1) {
                /* Not a valid measurement. */
                g_free(analog->data);
@@ -138,12 +269,12 @@ static void handle_line(const struct sr_dev_inst *sdi)
        char **tokens;
 
        devc = sdi->priv;
-       sr_spew("fluke-dmm: received line '%s' (%d)", devc->buf, devc->buflen);
+       sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
 
        if (devc->buflen == 1) {
                if (devc->buf[0] != '0') {
                        /* Not just a CMD_ACK from the query command. */
-                       sr_dbg("fluke-dmm: got CMD_ACK '%c'", devc->buf[0]);
+                       sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
                        devc->expect_response = FALSE;
                }
                devc->buflen = 0;
@@ -152,24 +283,20 @@ static void handle_line(const struct sr_dev_inst *sdi)
 
        analog = NULL;
        tokens = g_strsplit(devc->buf, ",", 0);
-       if (devc->profile->model == FLUKE_187) {
-               if (!strcmp(tokens[0], "QM")) {
-                       /* Yes, this is the response to the query. */
+       if (tokens[0] && tokens[1]) {
+               if (devc->profile->model == FLUKE_187) {
+                       devc->expect_response = FALSE;
+                       analog = handle_qm_v1(sdi, tokens);
+               } else if (devc->profile->model == FLUKE_287) {
                        devc->expect_response = FALSE;
-                       sr_spew("187 line");
-                       /* TODO */
-                       devc->buflen = 0;
+                       analog = handle_qm_v2(sdi, tokens);
                }
-       } else if (devc->profile->model == FLUKE_287) {
-               devc->expect_response = FALSE;
-               analog = handle_qm_v2(sdi, tokens);
        }
        g_strfreev(tokens);
        devc->buflen = 0;
 
        if (analog) {
                /* Got a measurement. */
-               sr_dbg("val %f", *analog->data);
                packet.type = SR_DF_ANALOG;
                packet.payload = analog;
                sr_session_send(devc->cb_data, &packet);
@@ -182,7 +309,7 @@ static void handle_line(const struct sr_dev_inst *sdi)
 
 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
 {
-       const struct sr_dev_inst *sdi;
+       struct sr_dev_inst *sdi;
        struct dev_context *devc;
        int len;
        int64_t now, elapsed;
@@ -221,9 +348,9 @@ SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
         * or temporary disconnect issues. */
        if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
                        || elapsed > 1000) {
-               sr_spew("fluke-dmm: sending QM");
+               sr_spew("Sending QM.");
                if (serial_write(devc->serial->fd, "QM\r", 3) == -1)
-                       sr_err("fluke-dmm: unable to send QM: %s", strerror(errno));
+                       sr_err("Unable to send QM: %s.", strerror(errno));
                devc->cmd_sent_at = now;
                devc->expect_response = TRUE;
        }