X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=output%2Fgnuplot.c;h=e2655ff1b73b4ae10f83744936210c62435d1c0f;hb=1c5b099a1312ae64307fb7d9a0030643936a3636;hp=7a7c2d4619060c4aba719cc51336417ec15b3bf9;hpb=a0a23863daecce5b7aaff352ad2bc5f47eb9bd0b;p=libsigrok.git diff --git a/output/gnuplot.c b/output/gnuplot.c index 7a7c2d46..e2655ff1 100644 --- a/output/gnuplot.c +++ b/output/gnuplot.c @@ -1,5 +1,5 @@ /* - * This file is part of the sigrok project. + * This file is part of the libsigrok project. * * Copyright (C) 2010 Uwe Hermann * @@ -39,6 +39,7 @@ struct context { unsigned int unitsize; char *probelist[SR_MAX_NUM_PROBES + 1]; char *header; + uint8_t *old_sample; }; #define MAX_HEADER_LEN \ @@ -63,7 +64,8 @@ static int init(struct sr_output *o) struct context *ctx; struct sr_probe *probe; GSList *l; - uint64_t *samplerate; + GVariant *gvar; + uint64_t samplerate; unsigned int i; int b, num_probes; char *c, *frequency_s; @@ -99,7 +101,7 @@ static int init(struct sr_output *o) o->internal = ctx; ctx->num_enabled_probes = 0; for (l = o->sdi->probes; l; l = l->next) { - probe = l->data; /* TODO: Error checks. */ + probe = l->data; if (!probe->enabled) continue; ctx->probelist[ctx->num_enabled_probes++] = probe->name; @@ -109,18 +111,21 @@ static int init(struct sr_output *o) num_probes = g_slist_length(o->sdi->probes); comment[0] = '\0'; - if (sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) { - o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE, - (const void **)&samplerate, o->sdi); - if (!(frequency_s = sr_samplerate_string(*samplerate))) { + samplerate = 0; + if (sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) { + o->sdi->driver->config_get(SR_CONF_SAMPLERATE, &gvar, o->sdi); + samplerate = g_variant_get_uint64(gvar); + if (!(frequency_s = sr_samplerate_string(samplerate))) { sr_err("%s: sr_samplerate_string failed", __func__); g_free(ctx->header); g_free(ctx); + g_variant_unref(gvar); return SR_ERR; } snprintf(comment, 127, gnuplot_header_comment, ctx->num_enabled_probes, num_probes, frequency_s); g_free(frequency_s); + g_variant_unref(gvar); } /* Columns / channels */ @@ -130,7 +135,7 @@ static int init(struct sr_output *o) sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]); } - if (!(frequency_s = sr_period_string(*samplerate))) { + if (!(frequency_s = sr_period_string(samplerate))) { sr_err("%s: sr_period_string failed", __func__); g_free(ctx->header); g_free(ctx); @@ -150,6 +155,13 @@ static int init(struct sr_output *o) return SR_ERR; } + if (!(ctx->old_sample = g_try_malloc0(ctx->unitsize))) { + sr_err("%s: ctx->old_sample malloc failed", __func__); + g_free(ctx->header); + g_free(ctx); + return SR_ERR_MALLOC; + } + return 0; } @@ -195,8 +207,8 @@ static int data(struct sr_output *o, const uint8_t *data_in, { struct context *ctx; unsigned int max_linelen, outsize, p, curbit, i; - uint64_t sample; - static uint64_t samplecount = 0, old_sample = 0; + const uint8_t *sample; + static uint64_t samplecount = 0; uint8_t *outbuf, *c; if (!o) { @@ -245,17 +257,18 @@ static int data(struct sr_output *o, const uint8_t *data_in, for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) { - memcpy(&sample, data_in + i, ctx->unitsize); + sample = data_in + i; /* * Don't output the same samples multiple times. However, make * sure to output at least the first and last sample. */ - if (samplecount++ != 0 && sample == old_sample) { + if (samplecount++ != 0 && + !memcmp(sample, ctx->old_sample, ctx->unitsize)) { if (i != (length_in - ctx->unitsize)) continue; } - old_sample = sample; + memcpy(ctx->old_sample, sample, ctx->unitsize); /* The first column is a counter (needed for gnuplot). */ c = outbuf + strlen((const char *)outbuf); @@ -263,7 +276,7 @@ static int data(struct sr_output *o, const uint8_t *data_in, /* The next columns are the values of all channels. */ for (p = 0; p < ctx->num_enabled_probes; p++) { - curbit = (sample & ((uint64_t) (1 << p))) >> p; + curbit = (sample[p / 8] & ((uint8_t) (1 << (p % 8)))) >> (p % 8); c = outbuf + strlen((const char *)outbuf); sprintf((char *)c, "%d ", curbit); }