X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fscale%2Fkern.c;h=dd18bfc188b0be6395c97d620931693c09ea4a60;hb=22fdb67fa0714c11cc0a58ee1423f55d18a4f080;hp=b4872ea49284b9ea7ba54d49c0217304cff83efb;hpb=5faebab2903dc91949edc31f0a4b118d86090a30;p=libsigrok.git diff --git a/src/scale/kern.c b/src/scale/kern.c index b4872ea4..dd18bfc1 100644 --- a/src/scale/kern.c +++ b/src/scale/kern.c @@ -14,8 +14,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 . */ /* @@ -44,9 +43,9 @@ static int get_buflen(const uint8_t *buf) } static int parse_value(const uint8_t *buf, float *result, - const struct kern_info *info) + int *digits, const struct kern_info *info) { - char *strval; + char *strval, *ptrdot; float floatval; int s2, len; @@ -62,6 +61,9 @@ static int parse_value(const uint8_t *buf, float *result, strval = g_strndup((const char *)buf, len); floatval = g_ascii_strtod(strval, NULL); + ptrdot = strchr(strval, '.'); + if (ptrdot) + *digits = len - (ptrdot - strval + 1); g_free(strval); *result = floatval; @@ -110,43 +112,43 @@ static void parse_flags(const uint8_t *buf, struct kern_info *info) /* Byte LF: Always '\n' (newline, 0x0a, 10) */ } -static void handle_flags(struct sr_datafeed_analog_old *analog, float *floatval, +static void handle_flags(struct sr_datafeed_analog *analog, float *floatval, const struct kern_info *info) { (void)floatval; /* Measured quantity: mass. */ - analog->mq = SR_MQ_MASS; + analog->meaning->mq = SR_MQ_MASS; /* Unit */ if (info->is_gram) - analog->unit = SR_UNIT_GRAM; + analog->meaning->unit = SR_UNIT_GRAM; if (info->is_carat) - analog->unit = SR_UNIT_CARAT; + analog->meaning->unit = SR_UNIT_CARAT; if (info->is_ounce) - analog->unit = SR_UNIT_OUNCE; + analog->meaning->unit = SR_UNIT_OUNCE; if (info->is_pound) - analog->unit = SR_UNIT_POUND; + analog->meaning->unit = SR_UNIT_POUND; if (info->is_troy_ounce) - analog->unit = SR_UNIT_TROY_OUNCE; + analog->meaning->unit = SR_UNIT_TROY_OUNCE; if (info->is_pennyweight) - analog->unit = SR_UNIT_PENNYWEIGHT; + analog->meaning->unit = SR_UNIT_PENNYWEIGHT; if (info->is_grain) - analog->unit = SR_UNIT_GRAIN; + analog->meaning->unit = SR_UNIT_GRAIN; if (info->is_tael) - analog->unit = SR_UNIT_TAEL; + analog->meaning->unit = SR_UNIT_TAEL; if (info->is_momme) - analog->unit = SR_UNIT_MOMME; + analog->meaning->unit = SR_UNIT_MOMME; if (info->is_tola) - analog->unit = SR_UNIT_TOLA; + analog->meaning->unit = SR_UNIT_TOLA; if (info->is_percentage) - analog->unit = SR_UNIT_PERCENTAGE; + analog->meaning->unit = SR_UNIT_PERCENTAGE; if (info->is_piece) - analog->unit = SR_UNIT_PIECE; + analog->meaning->unit = SR_UNIT_PIECE; /* Measurement related flags */ if (info->is_unstable) - analog->mqflags |= SR_MQFLAG_UNSTABLE; + analog->meaning->mqflags |= SR_MQFLAG_UNSTABLE; } SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf) @@ -187,7 +189,7 @@ SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf) * @param buf Buffer containing the protocol packet. Must not be NULL. * @param floatval Pointer to a float variable. That variable will contain the * result value upon parsing success. Must not be NULL. - * @param analog Pointer to a struct sr_datafeed_analog_old. The struct will be + * @param analog Pointer to a struct sr_datafeed_analog. The struct will be * filled with data according to the protocol packet. * Must not be NULL. * @param info Pointer to a struct kern_info. The struct will be filled @@ -197,20 +199,23 @@ SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf) * 'analog' variable contents are undefined and should not be used. */ SR_PRIV int sr_kern_parse(const uint8_t *buf, float *floatval, - struct sr_datafeed_analog_old *analog, void *info) + struct sr_datafeed_analog *analog, void *info) { - int ret; + int ret, digits = 0; struct kern_info *info_local; info_local = (struct kern_info *)info; info_local->buflen = get_buflen(buf); - if ((ret = parse_value(buf, floatval, info_local)) != SR_OK) { + if ((ret = parse_value(buf, floatval, &digits, info_local)) != SR_OK) { sr_dbg("Error parsing value: %d.", ret); return ret; } + analog->encoding->digits = digits; + analog->spec->spec_digits = digits; + parse_flags(buf, info_local); handle_flags(analog, floatval, info_local);