From: Uwe Hermann Date: Sat, 10 Nov 2012 12:30:02 +0000 (+0100) Subject: radioshack-dmm: Move some stuff out of .h file. X-Git-Tag: dsupstream~572 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4e172b8f8d725667d632484f340b1eec47597097;p=libsigrok.git radioshack-dmm: Move some stuff out of .h file. Most of the enums and #defines are not needed in multiple files, just put them in the respective .c file where they're used. --- diff --git a/hardware/radioshack-dmm/api.c b/hardware/radioshack-dmm/api.c index 3a8f8e65..a920d3a9 100644 --- a/hardware/radioshack-dmm/api.c +++ b/hardware/radioshack-dmm/api.c @@ -102,7 +102,7 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm) const struct rs_22_812_packet *rs_packet; if ((fd = serial_open(conn, O_RDONLY | O_NONBLOCK)) < 0) { - sr_err("Unable to open '%s': %s.", conn, fd); + sr_err("Unable to open '%s': %d.", conn, fd); return NULL; } diff --git a/hardware/radioshack-dmm/radioshack-dmm.h b/hardware/radioshack-dmm/radioshack-dmm.h index b63ea7d8..5d13e7f8 100644 --- a/hardware/radioshack-dmm/radioshack-dmm.h +++ b/hardware/radioshack-dmm/radioshack-dmm.h @@ -32,60 +32,6 @@ #define RS_DMM_BUFSIZE 256 -/* Byte 1 of the packet, and the modes it represents */ -#define RS_22_812_IND1_HZ 0x80 -#define RS_22_812_IND1_OHM 0x40 -#define RS_22_812_IND1_KILO 0x20 -#define RS_22_812_IND1_MEGA 0x10 -#define RS_22_812_IND1_FARAD 0x08 -#define RS_22_812_IND1_AMP 0x04 -#define RS_22_812_IND1_VOLT 0x02 -#define RS_22_812_IND1_MILI 0x01 -/* Byte 2 of the packet, and the modes it represents */ -#define RS_22_812_IND2_MICRO 0x80 -#define RS_22_812_IND2_NANO 0x40 -#define RS_22_812_IND2_DBM 0x20 -#define RS_22_812_IND2_SEC 0x10 -#define RS_22_812_IND2_DUTY 0x08 -#define RS_22_812_IND2_HFE 0x04 -#define RS_22_812_IND2_REL 0x02 -#define RS_22_812_IND2_MIN 0x01 -/* Byte 7 of the packet, and the modes it represents */ -#define RS_22_812_INFO_BEEP 0x80 -#define RS_22_812_INFO_DIODE 0x30 -#define RS_22_812_INFO_BAT 0x20 -#define RS_22_812_INFO_HOLD 0x10 -#define RS_22_812_INFO_NEG 0x08 -#define RS_22_812_INFO_AC 0x04 -#define RS_22_812_INFO_RS232 0x02 -#define RS_22_812_INFO_AUTO 0x01 -/* Instead of a decimal point, digit 4 carries the MAX flag */ -#define RS_22_812_DIG4_MAX 0x08 -/* Mask to remove the decimal point from a digit */ -#define RS_22_812_DP_MASK 0x08 - -/* What the LCD values represent */ -#define RS_22_812_LCD_0 0xd7 -#define RS_22_812_LCD_1 0x50 -#define RS_22_812_LCD_2 0xb5 -#define RS_22_812_LCD_3 0xf1 -#define RS_22_812_LCD_4 0x72 -#define RS_22_812_LCD_5 0xe3 -#define RS_22_812_LCD_6 0xe7 -#define RS_22_812_LCD_7 0x51 -#define RS_22_812_LCD_8 0xf7 -#define RS_22_812_LCD_9 0xf3 - -#define RS_22_812_LCD_C 0x87 -#define RS_22_812_LCD_E -#define RS_22_812_LCD_F -#define RS_22_812_LCD_h 0x66 -#define RS_22_812_LCD_H 0x76 -#define RS_22_812_LCD_I -#define RS_22_812_LCD_n -#define RS_22_812_LCD_P 0x37 -#define RS_22_812_LCD_r - #define RS_22_812_PACKET_SIZE 9 struct rs_22_812_packet { @@ -100,38 +46,6 @@ struct rs_22_812_packet { uint8_t checksum; }; -enum { - RS_22_812_MODE_DC_V = 0, - RS_22_812_MODE_AC_V = 1, - RS_22_812_MODE_DC_UA = 2, - RS_22_812_MODE_DC_MA = 3, - RS_22_812_MODE_DC_A = 4, - RS_22_812_MODE_AC_UA = 5, - RS_22_812_MODE_AC_MA = 6, - RS_22_812_MODE_AC_A = 7, - RS_22_812_MODE_OHM = 8, - RS_22_812_MODE_FARAD = 9, - RS_22_812_MODE_HZ = 10, - RS_22_812_MODE_VOLT_HZ = 11, - RS_22_812_MODE_AMP_HZ = 12, - RS_22_812_MODE_DUTY = 13, - RS_22_812_MODE_VOLT_DUTY = 14, - RS_22_812_MODE_AMP_DUTY = 15, - RS_22_812_MODE_WIDTH = 16, - RS_22_812_MODE_VOLT_WIDTH = 17, - RS_22_812_MODE_AMP_WIDTH = 18, - RS_22_812_MODE_DIODE = 19, - RS_22_812_MODE_CONT = 20, - RS_22_812_MODE_HFE = 21, - RS_22_812_MODE_LOGIC = 22, - RS_22_812_MODE_DBM = 23, - // RS_22_812_MODE_EF = 24, - RS_22_812_MODE_TEMP = 25, - RS_22_812_MODE_INVALID = 26, -}; - -SR_PRIV gboolean rs_22_812_packet_valid(const struct rs_22_812_packet *rs_packet); - /* Private, per-device-instance driver context. */ struct dev_context { uint64_t limit_samples; @@ -149,5 +63,6 @@ struct dev_context { }; SR_PRIV int radioshack_dmm_receive_data(int fd, int revents, void *cb_data); +SR_PRIV gboolean rs_22_812_packet_valid(const struct rs_22_812_packet *rs_packet); #endif diff --git a/hardware/radioshack-dmm/radioshack.c b/hardware/radioshack-dmm/radioshack.c index 541c936e..cb77339c 100644 --- a/hardware/radioshack-dmm/radioshack.c +++ b/hardware/radioshack-dmm/radioshack.c @@ -27,6 +27,90 @@ #include "libsigrok-internal.h" #include "radioshack-dmm.h" +/* Byte 1 of the packet, and the modes it represents */ +#define IND1_HZ 0x80 +#define IND1_OHM 0x40 +#define IND1_KILO 0x20 +#define IND1_MEGA 0x10 +#define IND1_FARAD 0x08 +#define IND1_AMP 0x04 +#define IND1_VOLT 0x02 +#define IND1_MILI 0x01 +/* Byte 2 of the packet, and the modes it represents */ +#define IND2_MICRO 0x80 +#define IND2_NANO 0x40 +#define IND2_DBM 0x20 +#define IND2_SEC 0x10 +#define IND2_DUTY 0x08 +#define IND2_HFE 0x04 +#define IND2_REL 0x02 +#define IND2_MIN 0x01 +/* Byte 7 of the packet, and the modes it represents */ +#define INFO_BEEP 0x80 +#define INFO_DIODE 0x30 +#define INFO_BAT 0x20 +#define INFO_HOLD 0x10 +#define INFO_NEG 0x08 +#define INFO_AC 0x04 +#define INFO_RS232 0x02 +#define INFO_AUTO 0x01 +/* Instead of a decimal point, digit 4 carries the MAX flag */ +#define DIG4_MAX 0x08 +/* Mask to remove the decimal point from a digit */ +#define DP_MASK 0x08 + +/* What the LCD values represent */ +#define LCD_0 0xd7 +#define LCD_1 0x50 +#define LCD_2 0xb5 +#define LCD_3 0xf1 +#define LCD_4 0x72 +#define LCD_5 0xe3 +#define LCD_6 0xe7 +#define LCD_7 0x51 +#define LCD_8 0xf7 +#define LCD_9 0xf3 + +#define LCD_C 0x87 +#define LCD_E +#define LCD_F +#define LCD_h 0x66 +#define LCD_H 0x76 +#define LCD_I +#define LCD_n +#define LCD_P 0x37 +#define LCD_r + +enum { + MODE_DC_V = 0, + MODE_AC_V = 1, + MODE_DC_UA = 2, + MODE_DC_MA = 3, + MODE_DC_A = 4, + MODE_AC_UA = 5, + MODE_AC_MA = 6, + MODE_AC_A = 7, + MODE_OHM = 8, + MODE_FARAD = 9, + MODE_HZ = 10, + MODE_VOLT_HZ = 11, + MODE_AMP_HZ = 12, + MODE_DUTY = 13, + MODE_VOLT_DUTY = 14, + MODE_AMP_DUTY = 15, + MODE_WIDTH = 16, + MODE_VOLT_WIDTH = 17, + MODE_AMP_WIDTH = 18, + MODE_DIODE = 19, + MODE_CONT = 20, + MODE_HFE = 21, + MODE_LOGIC = 22, + MODE_DBM = 23, + // MODE_EF = 24, + MODE_TEMP = 25, + MODE_INVALID = 26, +}; + enum { READ_ALL, READ_TEMP, @@ -55,11 +139,11 @@ static gboolean selection_good(const struct rs_22_812_packet *rs_packet) /* Does the packet have more than one multiplier ? */ count = 0; - count += (rs_packet->indicatrix1 & RS_22_812_IND1_KILO) ? 1 : 0; - count += (rs_packet->indicatrix1 & RS_22_812_IND1_MEGA) ? 1 : 0; - count += (rs_packet->indicatrix1 & RS_22_812_IND1_MILI) ? 1 : 0; - count += (rs_packet->indicatrix2 & RS_22_812_IND2_MICRO) ? 1 : 0; - count += (rs_packet->indicatrix2 & RS_22_812_IND2_NANO) ? 1 : 0; + count += (rs_packet->indicatrix1 & IND1_KILO) ? 1 : 0; + count += (rs_packet->indicatrix1 & IND1_MEGA) ? 1 : 0; + count += (rs_packet->indicatrix1 & IND1_MILI) ? 1 : 0; + count += (rs_packet->indicatrix2 & IND2_MICRO) ? 1 : 0; + count += (rs_packet->indicatrix2 & IND2_NANO) ? 1 : 0; if (count > 1) { sr_err("More than one multiplier detected in packet."); return FALSE; @@ -67,15 +151,15 @@ static gboolean selection_good(const struct rs_22_812_packet *rs_packet) /* Does the packet "measure" more than one type of value? */ count = 0; - count += (rs_packet->indicatrix1 & RS_22_812_IND1_HZ) ? 1 : 0; - count += (rs_packet->indicatrix1 & RS_22_812_IND1_OHM) ? 1 : 0; - count += (rs_packet->indicatrix1 & RS_22_812_IND1_FARAD) ? 1 : 0; - count += (rs_packet->indicatrix1 & RS_22_812_IND1_AMP) ? 1 : 0; - count += (rs_packet->indicatrix1 & RS_22_812_IND1_VOLT) ? 1 : 0; - count += (rs_packet->indicatrix2 & RS_22_812_IND2_DBM) ? 1 : 0; - count += (rs_packet->indicatrix2 & RS_22_812_IND2_SEC) ? 1 : 0; - count += (rs_packet->indicatrix2 & RS_22_812_IND2_DUTY) ? 1 : 0; - count += (rs_packet->indicatrix2 & RS_22_812_IND2_HFE) ? 1 : 0; + count += (rs_packet->indicatrix1 & IND1_HZ) ? 1 : 0; + count += (rs_packet->indicatrix1 & IND1_OHM) ? 1 : 0; + count += (rs_packet->indicatrix1 & IND1_FARAD) ? 1 : 0; + count += (rs_packet->indicatrix1 & IND1_AMP) ? 1 : 0; + count += (rs_packet->indicatrix1 & IND1_VOLT) ? 1 : 0; + count += (rs_packet->indicatrix2 & IND2_DBM) ? 1 : 0; + count += (rs_packet->indicatrix2 & IND2_SEC) ? 1 : 0; + count += (rs_packet->indicatrix2 & IND2_DUTY) ? 1 : 0; + count += (rs_packet->indicatrix2 & IND2_HFE) ? 1 : 0; if (count > 1) { sr_err("More than one measurement type detected in packet."); return FALSE; @@ -95,7 +179,7 @@ SR_PRIV gboolean rs_22_812_packet_valid(const struct rs_22_812_packet *rs_packet if (!checksum_valid(rs_packet)) return FALSE; - if (!(rs_packet->mode < RS_22_812_MODE_INVALID)) + if (!(rs_packet->mode < MODE_INVALID)) return FALSE; if (!selection_good(rs_packet)) @@ -107,29 +191,29 @@ SR_PRIV gboolean rs_22_812_packet_valid(const struct rs_22_812_packet *rs_packet static uint8_t decode_digit(uint8_t raw_digit) { /* Take out the decimal point, so we can use a simple switch(). */ - raw_digit &= ~RS_22_812_DP_MASK; + raw_digit &= ~DP_MASK; switch (raw_digit) { case 0x00: - case RS_22_812_LCD_0: + case LCD_0: return 0; - case RS_22_812_LCD_1: + case LCD_1: return 1; - case RS_22_812_LCD_2: + case LCD_2: return 2; - case RS_22_812_LCD_3: + case LCD_3: return 3; - case RS_22_812_LCD_4: + case LCD_4: return 4; - case RS_22_812_LCD_5: + case LCD_5: return 5; - case RS_22_812_LCD_6: + case LCD_6: return 6; - case RS_22_812_LCD_7: + case LCD_7: return 7; - case RS_22_812_LCD_8: + case LCD_8: return 8; - case RS_22_812_LCD_9: + case LCD_9: return 9; default: sr_err("Invalid digit byte: 0x%02x.", raw_digit); @@ -137,8 +221,7 @@ static uint8_t decode_digit(uint8_t raw_digit) } } -static double lcdraw_to_double(const struct rs_22_812_packet *rs_packet, - int type) +static double lcd_to_double(const struct rs_22_812_packet *rs_packet, int type) { double rawval, multiplier = 1; uint8_t digit, raw_digit; @@ -160,26 +243,26 @@ static double lcdraw_to_double(const struct rs_22_812_packet *rs_packet, * Digit 1 does not have a decimal point. Instead, the decimal * point is used to indicate MAX, so we must avoid testing it. */ - if ((i < 3) && (raw_digit & RS_22_812_DP_MASK)) + if ((i < 3) && (raw_digit & DP_MASK)) dp_reached = TRUE; if (dp_reached) multiplier /= 10; rawval = rawval * 10 + digit; } rawval *= multiplier; - if (rs_packet->info & RS_22_812_INFO_NEG) + if (rs_packet->info & INFO_NEG) rawval *= -1; /* See if we need to multiply our raw value by anything. */ - if (rs_packet->indicatrix1 & RS_22_812_IND2_NANO) { + if (rs_packet->indicatrix1 & IND2_NANO) { rawval *= 1E-9; - } else if (rs_packet->indicatrix2 & RS_22_812_IND2_MICRO) { + } else if (rs_packet->indicatrix2 & IND2_MICRO) { rawval *= 1E-6; - } else if (rs_packet->indicatrix1 & RS_22_812_IND1_MILI) { + } else if (rs_packet->indicatrix1 & IND1_MILI) { rawval *= 1E-3; - } else if (rs_packet->indicatrix1 & RS_22_812_IND1_KILO) { + } else if (rs_packet->indicatrix1 & IND1_KILO) { rawval *= 1E3; - } else if (rs_packet->indicatrix1 & RS_22_812_IND1_MEGA) { + } else if (rs_packet->indicatrix1 & IND1_MEGA) { rawval *= 1E6; } @@ -188,18 +271,18 @@ static double lcdraw_to_double(const struct rs_22_812_packet *rs_packet, static gboolean is_celsius(struct rs_22_812_packet *rs_packet) { - return ((rs_packet->digit4 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_C); + return ((rs_packet->digit4 & ~DP_MASK) == LCD_C); } static gboolean is_shortcirc(struct rs_22_812_packet *rs_packet) { - return ((rs_packet->digit2 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_h); + return ((rs_packet->digit2 & ~DP_MASK) == LCD_h); } static gboolean is_logic_high(struct rs_22_812_packet *rs_packet) { - sr_spew("Digit 2: 0x%02x.", rs_packet->digit2 & ~RS_22_812_DP_MASK); - return ((rs_packet->digit2 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_H); + sr_spew("Digit 2: 0x%02x.", rs_packet->digit2 & ~DP_MASK); + return ((rs_packet->digit2 & ~DP_MASK) == LCD_H); } static void handle_packet(struct rs_22_812_packet *rs_packet, @@ -209,7 +292,7 @@ static void handle_packet(struct rs_22_812_packet *rs_packet, struct sr_datafeed_packet packet; struct sr_datafeed_analog *analog; - rawval = lcdraw_to_double(rs_packet, READ_ALL); + rawval = lcd_to_double(rs_packet, READ_ALL); /* TODO: Check malloc return value. */ analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)); @@ -220,55 +303,55 @@ static void handle_packet(struct rs_22_812_packet *rs_packet, analog->mq = -1; switch (rs_packet->mode) { - case RS_22_812_MODE_DC_V: + case MODE_DC_V: analog->mq = SR_MQ_VOLTAGE; analog->unit = SR_UNIT_VOLT; analog->mqflags |= SR_MQFLAG_DC; break; - case RS_22_812_MODE_AC_V: + case MODE_AC_V: analog->mq = SR_MQ_VOLTAGE; analog->unit = SR_UNIT_VOLT; analog->mqflags |= SR_MQFLAG_AC; break; - case RS_22_812_MODE_DC_UA: - case RS_22_812_MODE_DC_MA: - case RS_22_812_MODE_DC_A: + case MODE_DC_UA: + case MODE_DC_MA: + case MODE_DC_A: analog->mq = SR_MQ_CURRENT; analog->unit = SR_UNIT_AMPERE; analog->mqflags |= SR_MQFLAG_DC; break; - case RS_22_812_MODE_AC_UA: - case RS_22_812_MODE_AC_MA: - case RS_22_812_MODE_AC_A: + case MODE_AC_UA: + case MODE_AC_MA: + case MODE_AC_A: analog->mq = SR_MQ_CURRENT; analog->unit = SR_UNIT_AMPERE; analog->mqflags |= SR_MQFLAG_AC; break; - case RS_22_812_MODE_OHM: + case MODE_OHM: analog->mq = SR_MQ_RESISTANCE; analog->unit = SR_UNIT_OHM; break; - case RS_22_812_MODE_FARAD: + case MODE_FARAD: analog->mq = SR_MQ_CAPACITANCE; analog->unit = SR_UNIT_FARAD; break; - case RS_22_812_MODE_CONT: + case MODE_CONT: analog->mq = SR_MQ_CONTINUITY; analog->unit = SR_UNIT_BOOLEAN; *analog->data = is_shortcirc(rs_packet); break; - case RS_22_812_MODE_DIODE: + case MODE_DIODE: analog->mq = SR_MQ_VOLTAGE; analog->unit = SR_UNIT_VOLT; analog->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC; break; - case RS_22_812_MODE_HZ: - case RS_22_812_MODE_VOLT_HZ: - case RS_22_812_MODE_AMP_HZ: + case MODE_HZ: + case MODE_VOLT_HZ: + case MODE_AMP_HZ: analog->mq = SR_MQ_FREQUENCY; analog->unit = SR_UNIT_HERTZ; break; - case RS_22_812_MODE_LOGIC: + case MODE_LOGIC: /* * No matter whether or not we have an actual voltage reading, * we are measuring voltage, so we set our MQ as VOLTAGE. @@ -283,29 +366,29 @@ static void handle_packet(struct rs_22_812_packet *rs_packet, *analog->data = is_logic_high(rs_packet); } break; - case RS_22_812_MODE_HFE: + case MODE_HFE: analog->mq = SR_MQ_GAIN; analog->unit = SR_UNIT_UNITLESS; break; - case RS_22_812_MODE_DUTY: - case RS_22_812_MODE_VOLT_DUTY: - case RS_22_812_MODE_AMP_DUTY: + case MODE_DUTY: + case MODE_VOLT_DUTY: + case MODE_AMP_DUTY: analog->mq = SR_MQ_DUTY_CYCLE; analog->unit = SR_UNIT_PERCENTAGE; break; - case RS_22_812_MODE_WIDTH: - case RS_22_812_MODE_VOLT_WIDTH: - case RS_22_812_MODE_AMP_WIDTH: + case MODE_WIDTH: + case MODE_VOLT_WIDTH: + case MODE_AMP_WIDTH: analog->mq = SR_MQ_PULSE_WIDTH; analog->unit = SR_UNIT_SECOND; - case RS_22_812_MODE_TEMP: + case MODE_TEMP: analog->mq = SR_MQ_TEMPERATURE; /* We need to reparse. */ - *analog->data = lcdraw_to_double(rs_packet, READ_TEMP); + *analog->data = lcd_to_double(rs_packet, READ_TEMP); analog->unit = is_celsius(rs_packet) ? SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT; break; - case RS_22_812_MODE_DBM: + case MODE_DBM: analog->mq = SR_MQ_POWER; analog->unit = SR_UNIT_DECIBEL_MW; analog->mqflags |= SR_MQFLAG_AC; @@ -315,13 +398,13 @@ static void handle_packet(struct rs_22_812_packet *rs_packet, break; } - if (rs_packet->info & RS_22_812_INFO_HOLD) + if (rs_packet->info & INFO_HOLD) analog->mqflags |= SR_MQFLAG_HOLD; - if (rs_packet->digit4 & RS_22_812_DIG4_MAX) + if (rs_packet->digit4 & DIG4_MAX) analog->mqflags |= SR_MQFLAG_MAX; - if (rs_packet->indicatrix2 & RS_22_812_IND2_MIN) + if (rs_packet->indicatrix2 & IND2_MIN) analog->mqflags |= SR_MQFLAG_MIN; - if (rs_packet->info & RS_22_812_INFO_AUTO) + if (rs_packet->info & INFO_AUTO) analog->mqflags |= SR_MQFLAG_AUTORANGE; if (analog->mq != -1) {