From: Gerhard Sittig Date: Sat, 21 Dec 2019 11:27:18 +0000 (+0100) Subject: output/wavedrom: separate data processing logic from init/cleanup X-Git-Url: http://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=7a0d1bdc2044cfb1836caaf9e28c1929d06bf66d output/wavedrom: separate data processing logic from init/cleanup 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. --- diff --git a/src/output/wavedrom.c b/src/output/wavedrom.c index 67f33b60..12e506d2 100644 --- a/src/output/wavedrom.c +++ b/src/output/wavedrom.c @@ -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",