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.
This commit is contained in:
Gerhard Sittig 2019-12-21 12:27:18 +01:00
parent dd5735c998
commit 7a0d1bdc20
1 changed files with 50 additions and 50 deletions

View 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",