Factor out common sigrok_samplerate_string().

This commit is contained in:
Uwe Hermann 2010-04-07 19:43:41 +02:00
parent bc010c054b
commit 25e7d9b115
7 changed files with 85 additions and 38 deletions

View File

@ -41,6 +41,7 @@ libsigrok_la_SOURCES = \
output/output_text.c \ output/output_text.c \
output/output_vcd.c \ output/output_vcd.c \
output/output_gnuplot.c \ output/output_gnuplot.c \
output/common.c \
output/output.c output/output.c
libsigrok_la_LIBADD = $(LIBOBJS) libsigrok_la_LIBADD = $(LIBOBJS)

60
output/common.c Normal file
View File

@ -0,0 +1,60 @@
/*
* This file is part of the sigrok project.
*
* Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* 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
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sigrok.h>
/**
* Convert a numeric samplerate value to its "natural" string representation.
*
* E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 KHz".
*
* @param samplerate The samplerate in Hz.
* @return A malloc()ed string representation of the samplerate value,
* or NULL upon errors. The caller is responsible to free() the memory.
*/
char *sigrok_samplerate_string(uint64_t samplerate)
{
char *o;
int r;
o = malloc(30 + 1); /* Enough for a uint64_t as string + " GHz". */
if (o == NULL)
return NULL;
if (samplerate >= GHZ(1))
r = snprintf(o, 30, "%"PRIu64" GHz", samplerate / 1000000000);
else if (samplerate >= MHZ(1))
r = snprintf(o, 30, "%"PRIu64" MHz", samplerate / 1000000);
else if (samplerate >= KHZ(1))
r = snprintf(o, 30, "%"PRIu64" KHz", samplerate / 1000);
else
r = snprintf(o, 30, "%"PRIu64" Hz", samplerate);
if (r < 0) {
/* Something went wrong... */
free(o);
return NULL;
}
return o;
}

View File

@ -49,8 +49,8 @@ static int init(struct output *o)
GSList *l; GSList *l;
uint64_t samplerate; uint64_t samplerate;
int i, b, num_probes; int i, b, num_probes;
char *c; char *c, *samplerate_s;
char sbuf[10], wbuf[1000]; char wbuf[1000];
ctx = malloc(sizeof(struct context)); ctx = malloc(sizeof(struct context));
if (ctx == NULL) if (ctx == NULL)
@ -75,16 +75,9 @@ static int init(struct output *o)
/* TODO: Handle num_probes == 0, too many probes, etc. */ /* TODO: Handle num_probes == 0, too many probes, etc. */
samplerate = *((uint64_t *) o->device->plugin->get_device_info( samplerate = *((uint64_t *) o->device->plugin->get_device_info(
o->device->plugin_index, DI_CUR_SAMPLERATE)); o->device->plugin_index, DI_CUR_SAMPLERATE));
/* Samplerate string */ if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
if (samplerate >= GHZ(1)) return -1; // FIXME
snprintf(sbuf, 10, "%"PRIu64" GHz", samplerate / 1000000000);
else if (samplerate >= MHZ(1))
snprintf(sbuf, 10, "%"PRIu64" MHz", samplerate / 1000000);
else if (samplerate >= KHZ(1))
snprintf(sbuf, 10, "%"PRIu64" KHz", samplerate / 1000);
else
snprintf(sbuf, 10, "%"PRIu64" Hz", samplerate);
/* Columns / channels */ /* Columns / channels */
wbuf[0] = '\0'; wbuf[0] = '\0';
@ -97,7 +90,9 @@ static int init(struct output *o)
/* TODO: Timescale */ /* TODO: Timescale */
b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
PACKAGE_STRING, "TODO", ctx->num_enabled_probes, PACKAGE_STRING, "TODO", ctx->num_enabled_probes,
num_probes, (char *)&sbuf, 1, "ns", (char *)&wbuf); num_probes, samplerate_s, 1, "ns", (char *)&wbuf);
free(samplerate_s);
/* TODO: Handle snprintf errors. */ /* TODO: Handle snprintf errors. */

View File

@ -18,11 +18,8 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include "sigrok.h" #include "sigrok.h"
static int init(struct output *o) static int init(struct output *o)
{ {
return 0; return 0;

View File

@ -21,7 +21,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <glib.h> #include <glib.h>
#include "sigrok.h" #include <sigrok.h>
#define DEFAULT_BPL_BIN 64 #define DEFAULT_BPL_BIN 64
#define DEFAULT_BPL_HEX 256 #define DEFAULT_BPL_HEX 256
@ -73,6 +73,7 @@ static int init(struct output *o, int default_spl)
GSList *l; GSList *l;
uint64_t samplerate; uint64_t samplerate;
int num_probes; int num_probes;
char *samplerate_s;
ctx = malloc(sizeof(struct context)); ctx = malloc(sizeof(struct context));
o->internal = ctx; o->internal = ctx;
@ -95,15 +96,11 @@ static int init(struct output *o, int default_spl)
num_probes = g_slist_length(o->device->probes); num_probes = g_slist_length(o->device->probes);
samplerate = *((uint64_t *) o->device->plugin->get_device_info(o->device->plugin_index, DI_CUR_SAMPLERATE)); samplerate = *((uint64_t *) o->device->plugin->get_device_info(o->device->plugin_index, DI_CUR_SAMPLERATE));
snprintf(ctx->header, 512, "Acquisition with %d/%d probes at ", ctx->num_enabled_probes, num_probes); snprintf(ctx->header, 512, "Acquisition with %d/%d probes at ", ctx->num_enabled_probes, num_probes);
if(samplerate >= GHZ(1))
snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" GHz", samplerate / 1000000000); if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
else if(samplerate >= MHZ(1)) return -1; // FIXME
snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" MHz", samplerate / 1000000); snprintf(ctx->header + strlen(ctx->header), 512, "%s\n", samplerate_s);
else if(samplerate >= KHZ(1)) free(samplerate_s);
snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" KHz", samplerate / 1000);
else
snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" Hz", samplerate);
snprintf(ctx->header + strlen(ctx->header), 512, "\n");
ctx->linebuf_len = ctx->samples_per_line * 2; ctx->linebuf_len = ctx->samples_per_line * 2;
ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len); ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len);

View File

@ -53,8 +53,8 @@ static int init(struct output *o)
GSList *l; GSList *l;
uint64_t samplerate; uint64_t samplerate;
int i, b, num_probes; int i, b, num_probes;
char *c; char *c, *samplerate_s;
char sbuf[10], wbuf[1000]; char wbuf[1000];
ctx = malloc(sizeof(struct context)); ctx = malloc(sizeof(struct context));
if (ctx == NULL) if (ctx == NULL)
@ -80,15 +80,8 @@ static int init(struct output *o)
samplerate = *((uint64_t *) o->device->plugin->get_device_info( samplerate = *((uint64_t *) o->device->plugin->get_device_info(
o->device->plugin_index, DI_CUR_SAMPLERATE)); o->device->plugin_index, DI_CUR_SAMPLERATE));
/* Samplerate string */ if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
if (samplerate >= GHZ(1)) return -1; // FIXME
snprintf(sbuf, 10, "%"PRIu64" GHz", samplerate / 1000000000);
else if (samplerate >= MHZ(1))
snprintf(sbuf, 10, "%"PRIu64" MHz", samplerate / 1000000);
else if (samplerate >= KHZ(1))
snprintf(sbuf, 10, "%"PRIu64" KHz", samplerate / 1000);
else
snprintf(sbuf, 10, "%"PRIu64" Hz", samplerate);
/* Wires / channels */ /* Wires / channels */
wbuf[0] = '\0'; wbuf[0] = '\0';
@ -101,9 +94,11 @@ static int init(struct output *o)
/* TODO: date: File or signals? Make y/n configurable. */ /* TODO: date: File or signals? Make y/n configurable. */
b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date", b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
PACKAGE_STRING, ctx->num_enabled_probes, num_probes, PACKAGE_STRING, ctx->num_enabled_probes, num_probes,
(char *)&sbuf, 1, "ns", PACKAGE, (char *)&wbuf); samplerate_s, 1, "ns", PACKAGE, (char *)&wbuf);
/* TODO: Handle snprintf errors. */ /* TODO: Handle snprintf errors. */
free(samplerate_s);
ctx->prevbits = calloc(sizeof(int), num_probes); ctx->prevbits = calloc(sizeof(int), num_probes);
if (ctx->prevbits == NULL) if (ctx->prevbits == NULL)
return SIGROK_ERR_MALLOC; return SIGROK_ERR_MALLOC;

View File

@ -127,6 +127,8 @@ int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
char *data_in, uint64_t length_in, char **data_out, char *data_in, uint64_t length_in, char **data_out,
uint64_t *length_out); uint64_t *length_out);
char *sigrok_samplerate_string(uint64_t samplerate);
/*--- analyzer.c ------------------------------------------------------------*/ /*--- analyzer.c ------------------------------------------------------------*/
struct analyzer { struct analyzer {