libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
ascii.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the sigrok project.
00003  *
00004  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
00005  * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
00006  *
00007  * This program is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <glib.h>
00024 #include "sigrok.h"
00025 #include "sigrok-internal.h"
00026 #include "text.h"
00027 
00028 SR_PRIV int init_ascii(struct sr_output *o)
00029 {
00030         return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
00031 }
00032 
00033 SR_PRIV int data_ascii(struct sr_output *o, const uint8_t *data_in,
00034                        uint64_t length_in, uint8_t **data_out,
00035                        uint64_t *length_out)
00036 {
00037         struct context *ctx;
00038         unsigned int outsize, offset, p;
00039         int max_linelen;
00040         uint64_t sample;
00041         uint8_t *outbuf;
00042 
00043         ctx = o->internal;
00044         max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
00045                         + ctx->samples_per_line / 8;
00046         /*
00047          * Calculate space needed for probes. Set aside 512 bytes for
00048          * extra output, e.g. trigger.
00049          */
00050         outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
00051             * (ctx->num_enabled_probes * max_linelen);
00052 
00053         if (!(outbuf = g_try_malloc0(outsize + 1))) {
00054                 sr_err("ascii out: %s: outbuf malloc failed", __func__);
00055                 return SR_ERR_MALLOC;
00056         }
00057 
00058         outbuf[0] = '\0';
00059         if (ctx->header) {
00060                 /* The header is still here, this must be the first packet. */
00061                 strncpy((char *)outbuf, ctx->header, outsize);
00062                 g_free(ctx->header);
00063                 ctx->header = NULL;
00064         }
00065 
00066         if (length_in >= ctx->unitsize) {
00067                 for (offset = 0; offset <= length_in - ctx->unitsize;
00068                      offset += ctx->unitsize) {
00069                         memcpy(&sample, data_in + offset, ctx->unitsize);
00070 
00071                         char tmpval[ctx->num_enabled_probes];
00072 
00073                         for (p = 0; p < ctx->num_enabled_probes; p++) {
00074                                 uint64_t curbit = (sample & ((uint64_t) 1 << p));
00075                                 uint64_t prevbit = (ctx->prevsample &
00076                                                 ((uint64_t) 1 << p));
00077 
00078                                 if (curbit < prevbit && ctx->line_offset > 0) {
00079                                         ctx->linebuf[p * ctx->linebuf_len +
00080                                                 ctx->line_offset-1] = '\\';
00081                                 }
00082 
00083                                 if (curbit > prevbit) {
00084                                         tmpval[p] = '/';
00085                                 } else {
00086                                         if (curbit)
00087                                                 tmpval[p] = '"';
00088                                         else
00089                                                 tmpval[p] = '.';
00090                                 }
00091                         }
00092 
00093                         /* End of line. */
00094                         if (ctx->spl_cnt >= ctx->samples_per_line) {
00095                                 flush_linebufs(ctx, outbuf);
00096                                 ctx->line_offset = ctx->spl_cnt = 0;
00097                                 ctx->mark_trigger = -1;
00098                         }
00099 
00100                         for (p = 0; p < ctx->num_enabled_probes; p++) {
00101                                 ctx->linebuf[p * ctx->linebuf_len +
00102                                              ctx->line_offset] = tmpval[p];
00103                         }
00104 
00105                         ctx->line_offset++;
00106                         ctx->spl_cnt++;
00107 
00108                         ctx->prevsample = sample;
00109                 }
00110         } else {
00111                 sr_info("ascii out: short buffer (length_in=%" PRIu64 ")",
00112                         length_in);
00113         }
00114 
00115         *data_out = outbuf;
00116         *length_out = strlen((const char *)outbuf);
00117 
00118         return SR_OK;
00119 }
00120 
00121 SR_PRIV struct sr_output_format output_text_ascii = {
00122         .id = "ascii",
00123         .description = "ASCII",
00124         .df_type = SR_DF_LOGIC,
00125         .init = init_ascii,
00126         .data = data_ascii,
00127         .event = event,
00128 };
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines