]> sigrok.org Git - libsigrok.git/blobdiff - src/dmm/es519xx.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / dmm / es519xx.c
index 2385a2208fa181208f7cec0495c15fa28ea427b5..202b64859a9986105e2674e0367e9f273ac02592 100644 (file)
@@ -15,8 +15,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
@@ -25,6 +24,7 @@
  * Communication parameters: Unidirectional, 2400/7o1 or 19230/7o1
  */
 
+#include <config.h>
 #include <string.h>
 #include <ctype.h>
 #include <math.h>
 
 #define LOG_PREFIX "es519xx"
 
-/* Factors for the respective measurement mode (0 means "invalid"). */
-static const float factors_2400_11b[9][8] = {
-       {1e-4,  1e-3,  1e-2,  1e-1, 1,    0,    0,    0   }, /* V */
-       {1e-7,  1e-6,  0,     0,    0,    0,    0,    0   }, /* uA */
-       {1e-5,  1e-4,  0,     0,    0,    0,    0,    0   }, /* mA */
-       {1e-2,  0,     0,     0,    0,    0,    0,    0   }, /* A */
-       {1e1,   1e2,   1e3,   1e4,  1e5,  1e6,  0,    0   }, /* RPM */
-       {1e-1,  1,     1e1,   1e2,  1e3,  1e4,  0,    0   }, /* Resistance */
-       {1,     1e1,   1e2,   1e3,  1e4,  1e5,  0,    0   }, /* Frequency */
-       {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
-       {1e-3,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
+/* Exponents for the respective measurement mode. */
+static const int exponents_2400_11b[9][8] = {
+       {  -4,  -3,  -2, -1,  0,  0,  0,  0 }, /* V */
+       {  -7,  -6,   0,  0,  0,  0,  0,  0 }, /* uA */
+       {  -5,  -4,   0,  0,  0,  0,  0,  0 }, /* mA */
+       {  -2,   0,   0,  0,  0,  0,  0,  0 }, /* A */
+       {   1,   2,   3,  4,  5,  6,  0,  0 }, /* RPM */
+       {  -1,   0,   1,  2,  3,  4,  0,  0 }, /* Resistance */
+       {   0,   1,   2,  3,  4,  5,  0,  0 }, /* Frequency */
+       { -12, -11, -10, -9, -8, -7, -6, -5 }, /* Capacitance */
+       {  -3,   0,   0,  0,  0,  0,  0,  0 }, /* Diode */
 };
-static const float factors_19200_11b_5digits[9][8] = {
-       {1e-4,  1e-3,  1e-2,  1e-1, 1e-5, 0,    0,    0},    /* V */
-       {1e-8,  1e-7,  0,     0,    0,    0,    0,    0},    /* uA */
-       {1e-6,  1e-5,  0,     0,    0,    0,    0,    0},    /* mA */
-       {0,     1e-3,  0,     0,    0,    0,    0,    0},    /* A */
-       {1e-4,  1e-3,  1e-2,  1e-1, 1,    0,    0,    0},    /* Manual A */
-       {1e-2,  1e-1,  1,     1e1,  1e2,  1e3,  1e4,  0},    /* Resistance */
-       {1e-1,  0,     1,     1e1,  1e2,  1e3,  1e4,  0},    /* Frequency */
-       {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
-       {1e-4,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
+static const int exponents_19200_11b_5digits[9][8] = {
+       {  -4,  -3,  -2, -1, -5,  0,  0,  0 }, /* V */
+       {  -8,  -7,   0,  0,  0,  0,  0,  0 }, /* uA */
+       {  -6,  -5,   0,  0,  0,  0,  0,  0 }, /* mA */
+       {   0,  -3,   0,  0,  0,  0,  0,  0 }, /* A */
+       {  -4,  -3,  -2, -1,  0,  0,  0,  0 }, /* Manual A */
+       {  -2,  -1,   0,  1,  2,  3,  4,  0 }, /* Resistance */
+       {  -1,   0,   0,  1,  2,  3,  4,  0 }, /* Frequency */
+       { -12, -11, -10, -9, -8, -7, -6, -5 }, /* Capacitance */
+       {  -4,   0,   0,  0,  0,  0,  0,  0 }, /* Diode */
 };
-static const float factors_19200_11b_clampmeter[9][8] = {
-       {1e-3,  1e-2,  1e-1,  1,    1e-4, 0,    0,    0},    /* V */
-       {1e-7,  1e-6,  0,     0,    0,    0,    0,    0},    /* uA */
-       {1e-5,  1e-4,  0,     0,    0,    0,    0,    0},    /* mA */
-       {1e-2,  0,     0,     0,    0,    0,    0,    0},    /* A */
-       {1e-3,  1e-2,  1e-1,  1,    0,    0,    0,    0},    /* Manual A */
-       {1e-1,  1,     1e1,   1e2,  1e3,  1e4,  0,    0},    /* Resistance */
-       {1e-1,  0,     1,     1e1,  1e2,  1e3,  1e4,  0},    /* Frequency */
-       {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
-       {1e-3,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
+static const int exponents_19200_11b_clampmeter[9][8] = {
+       {  -3,  -2,  -1,  0, -4,  0,  0,  0 }, /* V */
+       {  -7,  -6,   0,  0,  0,  0,  0,  0 }, /* uA */
+       {  -5,  -4,   0,  0,  0,  0,  0,  0 }, /* mA */
+       {  -2,   0,   0,  0,  0,  0,  0,  0 }, /* A */
+       {  -3,  -2,  -1,  0,  0,  0,  0,  0 }, /* Manual A */
+       {  -1,   0,   1,  2,  3,  4,  0,  0 }, /* Resistance */
+       {  -1,   0,   0,  1,  2,  3,  4,  0 }, /* Frequency */
+       { -12, -11, -10, -9, -8, -7, -6, -5 }, /* Capacitance */
+       {  -3,   0,   0,  0,  0,  0,  0,  0 }, /* Diode */
 };
-static const float factors_19200_11b[9][8] = {
-       {1e-3,  1e-2,  1e-1,  1,    1e-4, 0,    0,    0},    /* V */
-       {1e-7,  1e-6,  0,     0,    0,    0,    0,    0},    /* uA */
-       {1e-5,  1e-4,  0,     0,    0,    0,    0,    0},    /* mA */
-       {1e-3,  1e-2,  0,     0,    0,    0,    0,    0},    /* A */
-       {0,     0,     0,     0,    0,    0,    0,    0},    /* Manual A */
-       {1e-1,  1,     1e1,   1e2,  1e3,  1e4,  0,    0},    /* Resistance */
-       {1,     1e1,   1e2,   1e3,  1e4,  0,    0,    0},    /* Frequency */
-       {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 0},    /* Capacitance */
-       {1e-3,  0,     0,     0,    0,    0,    0,    0},    /* Diode */
+static const int exponents_19200_11b[9][8] = {
+       {  -3,  -2,  -1,  0, -4,  0,  0,  0 }, /* V */
+       {  -7,  -6,   0,  0,  0,  0,  0,  0 }, /* uA */
+       {  -5,  -4,   0,  0,  0,  0,  0,  0 }, /* mA */
+       {  -3,  -2,   0,  0,  0,  0,  0,  0 }, /* A */
+       {   0,   0,   0,  0,  0,  0,  0,  0 }, /* Manual A */
+       {  -1,   0,   1,  2,  3,  4,  0,  0 }, /* Resistance */
+       {   0,   1,   2,  3,  4,  0,  0,  0 }, /* Frequency */
+       { -12, -11, -10, -9, -8, -7, -6,  0 }, /* Capacitance */
+       {  -3,   0,   0,  0,  0,  0,  0,  0 }, /* Diode */
 };
-static const float factors_19200_14b[9][8] = {
-       {1e-4,  1e-3,  1e-2,  1e-1, 1e-5, 0,    0,    0},    /* V */
-       {1e-8,  1e-7,  0,     0,    0,    0,    0,    0},    /* uA */
-       {1e-6,  1e-5,  0,     0,    0,    0,    0,    0},    /* mA */
-       {1e-3,  0,     0,     0,    0,    0,    0,    0},    /* A */
-       {1e-4,  1e-3,  1e-2,  1e-1, 1,    0,    0,    0},    /* Manual A */
-       {1e-2,  1e-1,  1,     1e1,  1e2,  1e3,  1e4,  0},    /* Resistance */
-       {1e-2,  1e-1,  0,     1,    1e1,  1e2,  1e3,  1e4},  /* Frequency */
-       {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5}, /* Capacitance */
-       {1e-4,  0,     0,     0,    0,    0,    0,    0   }, /* Diode */
+static const int exponents_19200_14b[9][8] = {
+       {  -4,  -3,  -2, -1, -5,  0,  0,  0 }, /* V */
+       {  -8,  -7,   0,  0,  0,  0,  0,  0 }, /* uA */
+       {  -6,  -5,   0,  0,  0,  0,  0,  0 }, /* mA */
+       {  -3,   0,   0,  0,  0,  0,  0,  0 }, /* A */
+       {  -4,  -3,  -2, -1,  0,  0,  0,  0 }, /* Manual A */
+       {  -2,  -1,   0,  1,  2,  3,  4,  0 }, /* Resistance */
+       {  -2,  -1,   0,  0,  1,  2,  3,  4 }, /* Frequency */
+       { -12, -11, -10, -9, -8, -7, -6, -5 }, /* Capacitance */
+       {  -4,   0,   0,  0,  0,  0,  0,  0 }, /* Diode */
 };
 
 static int parse_value(const uint8_t *buf, struct es519xx_info *info,
@@ -134,11 +134,10 @@ static int parse_value(const uint8_t *buf, struct es519xx_info *info,
        return SR_OK;
 }
 
-static int parse_range(uint8_t b, float *floatval,
-                       const struct es519xx_info *info)
+static int parse_range(uint8_t b, float *floatval, struct es519xx_info *info)
 {
        int idx, mode;
-       float factor = 0;
+       int exponent = 0;
 
        idx = b - '0';
 
@@ -159,7 +158,7 @@ static int parse_range(uint8_t b, float *floatval,
        else if (info->is_current && !info->is_auto)
                mode = 4; /* Manual A */
        else if (info->is_rpm)
-               /* Not a typo, it's really index 4 in factors_2400_11b[][]. */
+               /* Not a typo, it's really index 4 in exponents_2400_11b[][]. */
                mode = 4; /* RPM */
        else if (info->is_resistance || info->is_continuity)
                mode = 5; /* Resistance */
@@ -178,31 +177,28 @@ static int parse_range(uint8_t b, float *floatval,
 
        if (info->is_vbar) {
                if (info->is_micro)
-                       factor = (const float[]){1e-1, 1}[idx];
+                       exponent = (const int[]){-1,  0}[idx];
                else if (info->is_milli)
-                       factor = (const float[]){1e-2, 1e-1}[idx];
+                       exponent = (const int[]){-2, -1}[idx];
        }
        else if (info->is_duty_cycle)
-               factor = 1e-1;
+               exponent = -1;
        else if (info->baudrate == 2400)
-               factor = factors_2400_11b[mode][idx];
+               exponent = exponents_2400_11b[mode][idx];
        else if (info->fivedigits)
-               factor = factors_19200_11b_5digits[mode][idx];
+               exponent = exponents_19200_11b_5digits[mode][idx];
        else if (info->clampmeter)
-               factor = factors_19200_11b_clampmeter[mode][idx];
+               exponent = exponents_19200_11b_clampmeter[mode][idx];
        else if (info->packet_size == 11)
-               factor = factors_19200_11b[mode][idx];
+               exponent = exponents_19200_11b[mode][idx];
        else if (info->packet_size == 14)
-               factor = factors_19200_14b[mode][idx];
+               exponent = exponents_19200_14b[mode][idx];
 
-       if (factor == 0) {
-               sr_dbg("Invalid factor for range byte: 0x%02x.", b);
-               return SR_ERR;
-       }
+       /* Apply respective exponent (mode-dependent) on the value. */
+       *floatval *= powf(10, exponent);
+       sr_dbg("Applying exponent %d, new value is %f.", exponent, *floatval);
 
-       /* Apply respective factor (mode-dependent) on the value. */
-       *floatval *= factor;
-       sr_dbg("Applying factor %f, new value is %f.", factor, *floatval);
+       info->digits = -exponent;
 
        return SR_OK;
 }
@@ -448,78 +444,78 @@ static void handle_flags(struct sr_datafeed_analog *analog,
 {
        /*
         * Note: is_micro etc. are not used directly to multiply/divide
-        * floatval, this is handled via parse_range() and factors[][].
+        * floatval, this is handled via parse_range() and exponents[][].
         */
 
        /* Measurement modes */
        if (info->is_voltage) {
-               analog->mq = SR_MQ_VOLTAGE;
-               analog->unit = SR_UNIT_VOLT;
+               analog->meaning->mq = SR_MQ_VOLTAGE;
+               analog->meaning->unit = SR_UNIT_VOLT;
        }
        if (info->is_current) {
-               analog->mq = SR_MQ_CURRENT;
-               analog->unit = SR_UNIT_AMPERE;
+               analog->meaning->mq = SR_MQ_CURRENT;
+               analog->meaning->unit = SR_UNIT_AMPERE;
        }
        if (info->is_resistance) {
-               analog->mq = SR_MQ_RESISTANCE;
-               analog->unit = SR_UNIT_OHM;
+               analog->meaning->mq = SR_MQ_RESISTANCE;
+               analog->meaning->unit = SR_UNIT_OHM;
        }
        if (info->is_frequency) {
-               analog->mq = SR_MQ_FREQUENCY;
-               analog->unit = SR_UNIT_HERTZ;
+               analog->meaning->mq = SR_MQ_FREQUENCY;
+               analog->meaning->unit = SR_UNIT_HERTZ;
        }
        if (info->is_capacitance) {
-               analog->mq = SR_MQ_CAPACITANCE;
-               analog->unit = SR_UNIT_FARAD;
+               analog->meaning->mq = SR_MQ_CAPACITANCE;
+               analog->meaning->unit = SR_UNIT_FARAD;
        }
        if (info->is_temperature && info->is_celsius) {
-               analog->mq = SR_MQ_TEMPERATURE;
-               analog->unit = SR_UNIT_CELSIUS;
+               analog->meaning->mq = SR_MQ_TEMPERATURE;
+               analog->meaning->unit = SR_UNIT_CELSIUS;
        }
        if (info->is_temperature && info->is_fahrenheit) {
-               analog->mq = SR_MQ_TEMPERATURE;
-               analog->unit = SR_UNIT_FAHRENHEIT;
+               analog->meaning->mq = SR_MQ_TEMPERATURE;
+               analog->meaning->unit = SR_UNIT_FAHRENHEIT;
        }
        if (info->is_continuity) {
-               analog->mq = SR_MQ_CONTINUITY;
-               analog->unit = SR_UNIT_BOOLEAN;
+               analog->meaning->mq = SR_MQ_CONTINUITY;
+               analog->meaning->unit = SR_UNIT_BOOLEAN;
                *floatval = (*floatval < 0.0 || *floatval > 25.0) ? 0.0 : 1.0;
        }
        if (info->is_diode) {
-               analog->mq = SR_MQ_VOLTAGE;
-               analog->unit = SR_UNIT_VOLT;
+               analog->meaning->mq = SR_MQ_VOLTAGE;
+               analog->meaning->unit = SR_UNIT_VOLT;
        }
        if (info->is_rpm) {
-               analog->mq = SR_MQ_FREQUENCY;
-               analog->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;
+               analog->meaning->mq = SR_MQ_FREQUENCY;
+               analog->meaning->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;
        }
        if (info->is_duty_cycle) {
-               analog->mq = SR_MQ_DUTY_CYCLE;
-               analog->unit = SR_UNIT_PERCENTAGE;
+               analog->meaning->mq = SR_MQ_DUTY_CYCLE;
+               analog->meaning->unit = SR_UNIT_PERCENTAGE;
        }
 
        /* Measurement related flags */
        if (info->is_ac)
-               analog->mqflags |= SR_MQFLAG_AC;
+               analog->meaning->mqflags |= SR_MQFLAG_AC;
        if (info->is_dc)
-               analog->mqflags |= SR_MQFLAG_DC;
+               analog->meaning->mqflags |= SR_MQFLAG_DC;
        if (info->is_auto)
-               analog->mqflags |= SR_MQFLAG_AUTORANGE;
+               analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE;
        if (info->is_diode)
-               analog->mqflags |= SR_MQFLAG_DIODE;
+               analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
        if (info->is_hold)
                /*
                * Note: HOLD only affects the number displayed on the LCD,
                * but not the value sent via the protocol! It also does not
                * affect the bargraph on the LCD.
                */
-               analog->mqflags |= SR_MQFLAG_HOLD;
+               analog->meaning->mqflags |= SR_MQFLAG_HOLD;
        if (info->is_max)
-               analog->mqflags |= SR_MQFLAG_MAX;
+               analog->meaning->mqflags |= SR_MQFLAG_MAX;
        if (info->is_min)
-               analog->mqflags |= SR_MQFLAG_MIN;
+               analog->meaning->mqflags |= SR_MQFLAG_MIN;
        if (info->is_rel)
-               analog->mqflags |= SR_MQFLAG_RELATIVE;
+               analog->meaning->mqflags |= SR_MQFLAG_RELATIVE;
 
        /* Other flags */
        if (info->is_judge)
@@ -619,6 +615,9 @@ static int sr_es519xx_parse(const uint8_t *buf, float *floatval,
        if ((ret = parse_range(buf[0], floatval, info)) != SR_OK)
                return ret;
 
+       analog->encoding->digits  = info->digits;
+       analog->spec->spec_digits = info->digits;
+
        handle_flags(analog, floatval, info);
        return SR_OK;
 }