X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fdmm%2Fut372.c;h=ce21273b688a9f63a9f11a942bb5a545dd3e68da;hb=2622b4297fd4cc4bed5c06bb6ae0aaa8b40e0ece;hp=c5944cc47558badcad7b8a5b6603d2d9a5c99272;hpb=8c9092b00b9c2451a8daf62bdfe75082566b2f89;p=libsigrok.git diff --git a/src/dmm/ut372.c b/src/dmm/ut372.c index c5944cc4..ce21273b 100644 --- a/src/dmm/ut372.c +++ b/src/dmm/ut372.c @@ -14,23 +14,23 @@ * 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 . */ /* * UNI-T UT372 protocol parser. */ +#include #include #include #include -#include "libsigrok.h" +#include #include "libsigrok-internal.h" #define LOG_PREFIX "ut372" -uint8_t lookup[] = { +static const uint8_t lookup[] = { 0x7B, 0x60, 0x5E, @@ -40,7 +40,7 @@ uint8_t lookup[] = { 0x3F, 0x70, 0x7F, - 0x7D + 0x7D, }; #define DECIMAL_POINT_MASK 0x80 @@ -48,9 +48,10 @@ uint8_t lookup[] = { #define FLAGS1_HOLD_MASK (1 << 2) #define FLAGS2_RPM_MASK (1 << 0) +#define FLAGS2_COUNT_MASK (1 << 1) #define FLAGS2_MAX_MASK (1 << 4) #define FLAGS2_MIN_MASK (1 << 5) -#define FLAGS2_AVE_MASK (1 << 6) +#define FLAGS2_AVG_MASK (1 << 6) /* Decode a pair of characters into a byte. */ static uint8_t decode_pair(const uint8_t *buf) @@ -71,40 +72,54 @@ static uint8_t decode_pair(const uint8_t *buf) SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf) { - return (buf[25] == '\r' && buf[26] == '\n'); + uint8_t flags2; + + if (!(buf[25] == '\r' && buf[26] == '\n')) + return FALSE; + + flags2 = decode_pair(buf + 23); + + if (!(flags2 & (FLAGS2_RPM_MASK | FLAGS2_COUNT_MASK))) + /* Device is in the setup menu - no valid data shown. */ + return FALSE; + + return TRUE; } SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval, struct sr_datafeed_analog *analog, void *info) { - unsigned int i, j, value, divisor; + unsigned int i, j, value; uint8_t segments, flags1, flags2; + int exponent; (void) info; flags1 = decode_pair(buf + 21); flags2 = decode_pair(buf + 23); - /* If not in RPM mode, ignore packet. */ - if (!(flags2 & FLAGS2_RPM_MASK)) { - sr_dbg("Not in RPM mode. Count mode is unsupported."); - return SR_ERR_NA; + if (flags2 & FLAGS2_RPM_MASK) { + analog->meaning->mq = SR_MQ_FREQUENCY; + analog->meaning->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE; + } else if (flags2 & FLAGS2_COUNT_MASK) { + analog->meaning->mq = SR_MQ_COUNT; + analog->meaning->unit = SR_UNIT_UNITLESS; } if (flags1 & FLAGS1_HOLD_MASK) - analog->mqflags |= SR_MQFLAG_HOLD; + analog->meaning->mqflags |= SR_MQFLAG_HOLD; if (flags2 & FLAGS2_MIN_MASK) - analog->mqflags |= SR_MQFLAG_MIN; + analog->meaning->mqflags |= SR_MQFLAG_MIN; if (flags2 & FLAGS2_MAX_MASK) - analog->mqflags |= SR_MQFLAG_MAX; - if (flags2 & FLAGS2_AVE_MASK) - analog->mqflags |= SR_MQFLAG_AVG; + analog->meaning->mqflags |= SR_MQFLAG_MAX; + if (flags2 & FLAGS2_AVG_MASK) + analog->meaning->mqflags |= SR_MQFLAG_AVG; value = 0; - divisor = 1; + exponent = 0; for (i = 0; i < 5; i++) { - segments = decode_pair(buf + 1 + 2*i); + segments = decode_pair(buf + 1 + (2 * i)); for (j = 0; j < ARRAY_SIZE(lookup); j++) { if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) { value += j * pow(10, i); @@ -112,13 +127,13 @@ SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval, } } if (segments & DECIMAL_POINT_MASK) - divisor = pow(10, i); + exponent = -i; } - *floatval = (float) value / divisor; + *floatval = (float) value * powf(10, exponent); - analog->mq = SR_MQ_FREQUENCY; - analog->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE; + analog->encoding->digits = -exponent; + analog->spec->spec_digits = -exponent; return SR_OK; }