]> sigrok.org Git - libsigrok.git/blobdiff - output/output_vcd.c
Win: Fix compile, serial port code (unfinished).
[libsigrok.git] / output / output_vcd.c
index 0122b61541a1c8410cbdf62f39832b1721eaf3e4..76d662e20e665b953b5cc4c82800af94ef4b19ad 100644 (file)
@@ -53,8 +53,8 @@ static int init(struct output *o)
        GSList *l;
        uint64_t samplerate;
        int i, b, num_probes;
-       char *c;
-       char sbuf[10], wbuf[1000];
+       char *c, *samplerate_s;
+       char wbuf[1000];
 
        ctx = malloc(sizeof(struct context));
        if (ctx == NULL)
@@ -80,30 +80,25 @@ static int init(struct output *o)
        samplerate = *((uint64_t *) o->device->plugin->get_device_info(
                        o->device->plugin_index, DI_CUR_SAMPLERATE));
 
-       /* Samplerate string */
-       if (samplerate >= GHZ(1))
-               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);
+       if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
+               return -1; /* FIXME */
 
        /* Wires / channels */
        wbuf[0] = '\0';
        for (i = 0; i < ctx->num_enabled_probes; i++) {
                c = (char *)&wbuf + strlen((char *)&wbuf);
                sprintf(c, "$var wire 1 %c channel%s $end\n",
-                        (char)('!' + i), ctx->probelist[i]);
+                       (char)('!' + i), ctx->probelist[i]);
        }
 
-       /* 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",
                     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. */
 
+       free(samplerate_s);
+
        ctx->prevbits = calloc(sizeof(int), num_probes);
        if (ctx->prevbits == NULL)
                return SIGROK_ERR_MALLOC;
@@ -119,7 +114,7 @@ static int event(struct output *o, int event_type, char **data_out,
        int outlen;
 
        ctx = o->internal;
-       switch(event_type) {
+       switch (event_type) {
        case DF_TRIGGER:
                break;
        case DF_END:
@@ -142,13 +137,14 @@ static int data(struct output *o, char *data_in, uint64_t length_in,
                char **data_out, uint64_t *length_out)
 {
        struct context *ctx;
-       int offset, outsize, p, curbit, prevbit;
+       unsigned int offset, outsize;
+       int p, curbit, prevbit;
        uint64_t sample, prevsample;
        char *outbuf, *c;
 
        ctx = o->internal;
        outsize = strlen(ctx->header);
-       outbuf = calloc(1, outsize + 1 + 10000); // FIXME: Use realloc().
+       outbuf = calloc(1, outsize + 1 + 10000); /* FIXME: Use realloc(). */
        if (outbuf == NULL)
                return SIGROK_ERR_MALLOC;
        if (ctx->header) {
@@ -163,15 +159,17 @@ static int data(struct output *o, char *data_in, uint64_t length_in,
        /* TODO: Are disabled probes handled correctly? */
 
        for (offset = 0; offset <= length_in - ctx->unitsize;
-                                               offset += ctx->unitsize) {
+            offset += ctx->unitsize) {
                memcpy(&sample, data_in + offset, ctx->unitsize);
                for (p = 0; p < ctx->num_enabled_probes; p++) {
                        curbit = (sample & ((uint64_t) (1 << p))) != 0;
                        if (offset == 0) {
                                prevbit = ~curbit;
                        } else {
-                               memcpy(&prevsample, data_in + offset - 1, ctx->unitsize);
-                               prevbit = (prevsample & ((uint64_t) (1 << p))) != 0;
+                               memcpy(&prevsample, data_in + offset - 1,
+                                      ctx->unitsize);
+                               prevbit =
+                                   (prevsample & ((uint64_t) (1 << p))) != 0;
                        }
 
                        if (prevbit != curbit) {
@@ -180,11 +178,12 @@ static int data(struct output *o, char *data_in, uint64_t length_in,
                                sprintf(c, "#%i\n", offset * 1 /* TODO */);
 
                                c = outbuf + strlen(outbuf);
-                               sprintf(c, "%i%c\n", curbit, (char)('!' + p /* FIXME? */));
+                               sprintf(c, "%i%c\n", curbit,
+                                       (char)('!' + p /* FIXME? */));
                        }
                }
 
-               /* TODO: Do a realloc() here if strlen(outbuf) is almost "full"... */
+               /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */
        }
 
        *data_out = outbuf;