From: Aurelien Jacobs Date: Thu, 13 Nov 2014 23:50:46 +0000 (+0100) Subject: Add a sr_analog_init() API to initialize sr_datafeed_analog2 struct. X-Git-Tag: libsigrok-0.4.0~764 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=41caa31909882a34b88c8b8a844d555816b78453;p=libsigrok.git Add a sr_analog_init() API to initialize sr_datafeed_analog2 struct. It fills the fields with reasonable default values that should suit most of the drivers. Drivers are obviously free to override the fields they want after initializing. --- diff --git a/src/analog.c b/src/analog.c index 0f7a3071..29fb8426 100644 --- a/src/analog.c +++ b/src/analog.c @@ -23,6 +23,40 @@ #define LOG_PREFIX "analog" +SR_PRIV int sr_analog_init(struct sr_datafeed_analog2 *analog, + struct sr_analog_encoding *encoding, + struct sr_analog_meaning *meaning, + struct sr_analog_spec *spec, + int digits) +{ + memset(analog, 0, sizeof(*analog)); + memset(encoding, 0, sizeof(*encoding)); + memset(meaning, 0, sizeof(*meaning)); + memset(spec, 0, sizeof(*spec)); + + analog->encoding = encoding; + analog->meaning = meaning; + analog->spec = spec; + + encoding->unitsize = sizeof(float); + encoding->is_float = TRUE; +#ifdef WORDS_BIGENDIAN + encoding->is_bigendian = TRUE; +#else + encoding->is_bigendian = FALSE; +#endif + encoding->digits = digits; + encoding->is_digits_decimal = TRUE; + encoding->scale.p = 1; + encoding->scale.q = 1; + encoding->offset.p = 0; + encoding->offset.q = 1; + + spec->spec_digits = digits; + + return SR_OK; +} + SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog, float *buf) { diff --git a/src/hardware/mic-985xx/protocol.c b/src/hardware/mic-985xx/protocol.c index eb9b48be..49f67980 100644 --- a/src/hardware/mic-985xx/protocol.c +++ b/src/hardware/mic-985xx/protocol.c @@ -111,32 +111,12 @@ static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx) return SR_ERR; } - memset(&analog, 0, sizeof(struct sr_datafeed_analog)); - memset(&encoding, 0, sizeof(struct sr_analog_encoding)); - memset(&meaning, 0, sizeof(struct sr_analog_meaning)); - memset(&spec, 0, sizeof(struct sr_analog_spec)); + sr_analog_init(&analog, &encoding, &meaning, &spec, 3); /* Common values for both channels. */ packet.type = SR_DF_ANALOG2; packet.payload = &analog; - analog.encoding = &encoding; - analog.meaning = &meaning; - analog.spec = &spec; analog.num_samples = 1; - encoding.unitsize = sizeof(float); - encoding.is_float = TRUE; -#ifdef WORDS_BIGENDIAN - encoding.is_bigendian = TRUE; -#else - encoding.is_bigendian = FALSE; -#endif - encoding.digits = 3; /* Values are always 3-digit numbers. */ - encoding.is_digits_decimal = TRUE; - encoding.scale.p = 1; - encoding.scale.q = 1; - encoding.offset.p = 0; - encoding.offset.q = 1; - spec.spec_digits = encoding.digits; /* Temperature. */ l = g_slist_copy(sdi->channels); diff --git a/src/hardware/victor-dmm/protocol.c b/src/hardware/victor-dmm/protocol.c index 1eaffda3..6f72d947 100644 --- a/src/hardware/victor-dmm/protocol.c +++ b/src/hardware/victor-dmm/protocol.c @@ -167,10 +167,7 @@ static void decode_buf(struct sr_dev_inst *sdi, unsigned char *data) if (minus) fvalue = -fvalue; - memset(&analog, 0, sizeof(struct sr_datafeed_analog2)); - memset(&encoding, 0, sizeof(struct sr_analog_encoding)); - memset(&meaning, 0, sizeof(struct sr_analog_meaning)); - memset(&spec, 0, sizeof(struct sr_analog_spec)); + sr_analog_init(&analog, &encoding, &meaning, &spec, 4); /* Measurement mode */ meaning.channels = sdi->channels; @@ -259,27 +256,8 @@ static void decode_buf(struct sr_dev_inst *sdi, unsigned char *data) if (is_relative) meaning.mqflags |= SR_MQFLAG_RELATIVE; - encoding.unitsize = sizeof(float); - encoding.is_float = TRUE; -#ifdef WORDS_BIGENDIAN - encoding.is_bigendian = TRUE; -#else - encoding.is_bigendian = FALSE; -#endif - encoding.digits = 4; /* Values are always 4-digit numbers. */ - encoding.is_digits_decimal = TRUE; - encoding.scale.p = 1; - encoding.scale.q = 1; - encoding.offset.p = 0; - encoding.offset.q = 1; - - spec.spec_digits = encoding.digits; - analog.data = &fvalue; analog.num_samples = 1; - analog.encoding = &encoding; - analog.meaning = &meaning; - analog.spec = &spec; packet.type = SR_DF_ANALOG2; packet.payload = &analog; diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index f98a35a8..bdbcb090 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -603,6 +603,14 @@ SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, struct sr_datafeed_packet **copy); SR_PRIV void sr_packet_free(struct sr_datafeed_packet *packet); +/*--- analog.c --------------------------------------------------------------*/ + +SR_PRIV int sr_analog_init(struct sr_datafeed_analog2 *analog, + struct sr_analog_encoding *encoding, + struct sr_analog_meaning *meaning, + struct sr_analog_spec *spec, + int digits); + /*--- std.c -----------------------------------------------------------------*/ typedef int (*dev_close_callback)(struct sr_dev_inst *sdi);