Exemple #1
0
END_TEST

START_TEST(test_analog_si_prefix_null)
{
	float value = 1.23;
	int digits = 1;
	const char *si_prefix;

	si_prefix = sr_analog_si_prefix(NULL, &digits);
	fail_unless(!strcmp(si_prefix, ""));
	si_prefix = sr_analog_si_prefix(&value, NULL);
	fail_unless(!strcmp(si_prefix, ""));
	si_prefix = sr_analog_si_prefix(NULL, NULL);
	fail_unless(!strcmp(si_prefix, ""));
}
Exemple #2
0
END_TEST

START_TEST(test_analog_si_prefix)
{
	struct {
		float input_value;
		int input_digits;
		float output_value;
		int output_digits;
		const char *output_si_prefix;
	} v[] = {
		{   12.0     ,  0,  12.0  ,    0, ""  },
		{   12.0     ,  1,  12.0  ,    1, ""  },
		{   12.0     , -1,   0.012,    2, "k" },
		{ 1024.0     ,  0,   1.024,    3, "k" },
		{ 1024.0     , -1,   1.024,    2, "k" },
		{ 1024.0     , -3,   1.024,    0, "k" },
		{   12.0e5   ,  0,   1.2,      6, "M" },
		{    0.123456,  0,   0.123456, 0, ""  },
		{    0.123456,  1,   0.123456, 1, ""  },
		{    0.123456,  2,   0.123456, 2, ""  },
		{    0.123456,  3, 123.456,    0, "m" },
		{    0.123456,  4, 123.456,    1, "m" },
		{    0.123456,  5, 123.456,    2, "m" },
		{    0.123456,  6, 123.456,    3, "m" },
		{    0.123456,  7, 123.456,    4, "m" },
		{    0.0123  ,  4,  12.3,      1, "m" },
		{    0.00123 ,  5,   1.23,     2, "m" },
		{    0.000123,  4,   0.123,    1, "m" },
		{    0.000123,  5,   0.123,    2, "m" },
		{    0.000123,  6, 123.0,      0, "µ" },
		{    0.000123,  7, 123.0,      1, "µ" },
	};

	for (unsigned int i = 0; i < ARRAY_SIZE(v); i++) {
		float value = v[i].input_value;
		int digits = v[i].input_digits;
		const char *si_prefix = sr_analog_si_prefix(&value, &digits);

		fail_unless(fabs(value - v[i].output_value) <= 0.00001,
			"sr_analog_si_prefix() unexpected output value %f (i=%d).",
			value , i);
		fail_unless(digits == v[i].output_digits,
			"sr_analog_si_prefix() unexpected output digits %d (i=%d).",
			digits, i);
		fail_unless(!strcmp(si_prefix, v[i].output_si_prefix),
			"sr_analog_si_prefix() unexpected output prefix \"%s\" (i=%d).",
			si_prefix, i);
	}
}
Exemple #3
0
static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
		GString **out)
{
	struct context *ctx;
	const struct sr_datafeed_analog *analog;
	struct sr_channel *ch;
	GSList *l;
	float *fdata;
	unsigned int i;
	int num_channels, c, ret, digits;
	char *number, *suffix;

	*out = NULL;
	if (!o || !o->sdi)
		return SR_ERR_ARG;
	ctx = o->priv;

	switch (packet->type) {
	case SR_DF_FRAME_BEGIN:
		*out = g_string_new("FRAME-BEGIN\n");
		break;
	case SR_DF_FRAME_END:
		*out = g_string_new("FRAME-END\n");
		break;
	case SR_DF_ANALOG:
		analog = packet->payload;
		num_channels = g_slist_length(analog->meaning->channels);
		if (!(fdata = g_try_realloc(ctx->fdata,
						analog->num_samples * num_channels * sizeof(float))))
			return SR_ERR_MALLOC;
		ctx->fdata = fdata;
		if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK)
			return ret;
		*out = g_string_sized_new(512);
		if (analog->encoding->is_digits_decimal) {
			if (ctx->digits == DIGITS_ALL)
				digits = analog->encoding->digits;
			else
				digits = analog->spec->spec_digits;
		} else {
			/* TODO we don't know how to print by number of bits yet. */
			digits = 6;
		}
		gboolean si_friendly = sr_analog_si_prefix_friendly(analog->meaning->unit);
		sr_analog_unit_to_string(analog, &suffix);
		for (i = 0; i < analog->num_samples; i++) {
			for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) {
				float value = fdata[i * num_channels + c];
				const char *prefix = "";
				if (si_friendly)
					prefix = sr_analog_si_prefix(&value, &digits);
				ch = l->data;
				g_string_append_printf(*out, "%s: ", ch->name);
				number = g_strdup_printf("%.*f", MAX(digits, 0), value);
				g_string_append(*out, number);
				g_free(number);
				g_string_append(*out, " ");
				g_string_append(*out, prefix);
				g_string_append(*out, suffix);
				g_string_append(*out, "\n");
			}
		}
		g_free(suffix);
		break;
	}

	return SR_OK;
}