]> sigrok.org Git - libsigrok.git/commitdiff
output/wavedrom: separate data processing logic from init/cleanup
authorGerhard Sittig <redacted>
Sat, 21 Dec 2019 11:27:18 +0000 (12:27 +0100)
committerGerhard Sittig <redacted>
Sat, 21 Dec 2019 12:58:43 +0000 (13:58 +0100)
Rearrange the order of routines in the wavedrom output module. Keep the
flow of .receive() -> .process_logic() -> .wavedrom_render() in one common
group of routines, which is not disrupted by the .init() and .cleanup()
routines which are kind of boilerplate in the source file. This increases
readability and maintainability.

src/output/wavedrom.c

index 67f33b60da8b6f1c10f9baf8abbdfd3315779a6e..12e506d293d0fc5f3e4fe8192b69928b0ce1ebf2 100644 (file)
@@ -69,6 +69,56 @@ static GString *wavedrom_render(const struct context *ctx)
        return output;
 }
 
+static void process_logic(const struct context *ctx,
+       const struct sr_datafeed_logic *logic)
+{
+       size_t sample_count, ch, i;
+       uint8_t *sample;
+
+       sample_count = logic->length / logic->unitsize;
+
+       /*
+        * Extract the logic bits for each channel and store them
+        * as wavedrom letters (1/0) in each channel's text string.
+        */
+       for (ch = 0; ch < ctx->channel_count; ch++) {
+               if (ctx->channels[ch]) {
+                       for (i = 0; i < sample_count; i++) {
+                               sample = logic->data + i * logic->unitsize;
+
+                               if (ctx->channel_outputs[ch]) {
+                                       g_string_append_c(ctx->channel_outputs[ch],
+                                               sample[ch / 8] & (1 << (ch % 8)) ? '1' : '0');
+                               }
+                       }
+               }
+       }
+}
+
+static int receive(const struct sr_output *o,
+       const struct sr_datafeed_packet *packet, GString **out)
+{
+       struct context *ctx;
+
+       *out = NULL;
+
+       if (!o || !o->sdi || !o->priv)
+               return SR_ERR_ARG;
+
+       ctx = o->priv;
+
+       switch (packet->type) {
+       case SR_DF_LOGIC:
+               process_logic(ctx, packet->payload);
+               break;
+       case SR_DF_END:
+               *out = wavedrom_render(ctx);
+               break;
+       }
+
+       return SR_OK;
+}
+
 static int init(struct sr_output *o, GHashTable *options)
 {
        struct context *ctx;
@@ -125,56 +175,6 @@ static int cleanup(struct sr_output *o)
        return SR_OK;
 }
 
-static void process_logic(const struct context *ctx,
-       const struct sr_datafeed_logic *logic)
-{
-       size_t sample_count, ch, i;
-       uint8_t *sample;
-
-       sample_count = logic->length / logic->unitsize;
-
-       /*
-        * Extract the logic bits for each channel and store them
-        * as wavedrom letters (1/0) in each channel's text string.
-        */
-       for (ch = 0; ch < ctx->channel_count; ch++) {
-               if (ctx->channels[ch]) {
-                       for (i = 0; i < sample_count; i++) {
-                               sample = logic->data + i * logic->unitsize;
-
-                               if (ctx->channel_outputs[ch]) {
-                                       g_string_append_c(ctx->channel_outputs[ch],
-                                               sample[ch / 8] & (1 << (ch % 8)) ? '1' : '0');
-                               }
-                       }
-               }
-       }
-}
-
-static int receive(const struct sr_output *o,
-       const struct sr_datafeed_packet *packet, GString **out)
-{
-       struct context *ctx;
-
-       *out = NULL;
-
-       if (!o || !o->sdi || !o->priv)
-               return SR_ERR_ARG;
-
-       ctx = o->priv;
-
-       switch (packet->type) {
-       case SR_DF_LOGIC:
-               process_logic(ctx, packet->payload);
-               break;
-       case SR_DF_END:
-               *out = wavedrom_render(ctx);
-               break;
-       }
-
-       return SR_OK;
-}
-
 SR_PRIV struct sr_output_module output_wavedrom = {
        .id = "wavedrom",
        .name = "WaveDrom",