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.
This commit is contained in:
Aurelien Jacobs 2014-11-14 00:50:46 +01:00 committed by Bert Vermeulen
parent 62f155f0c7
commit 41caa31909
4 changed files with 44 additions and 44 deletions

View File

@ -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)
{

View File

@ -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);

View File

@ -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;

View File

@ -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);