libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
bits.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_bits(struct sr_output *o)
00029 {
00030         return init(o, DEFAULT_BPL_BITS, MODE_BITS);
00031 }
00032 
00033 SR_PRIV int data_bits(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, c;
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("bits 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                 /* Ensure first transition. */
00066                 memcpy(&ctx->prevsample, data_in, ctx->unitsize);
00067                 ctx->prevsample = ~ctx->prevsample;
00068         }
00069 
00070         if (length_in >= ctx->unitsize) {
00071                 for (offset = 0; offset <= length_in - ctx->unitsize;
00072                      offset += ctx->unitsize) {
00073                         memcpy(&sample, data_in + offset, ctx->unitsize);
00074                         for (p = 0; p < ctx->num_enabled_probes; p++) {
00075                                 c = (sample & ((uint64_t) 1 << p)) ? '1' : '0';
00076                                 ctx->linebuf[p * ctx->linebuf_len +
00077                                              ctx->line_offset] = c;
00078                         }
00079                         ctx->line_offset++;
00080                         ctx->spl_cnt++;
00081 
00082                         /* Add a space every 8th bit. */
00083                         if ((ctx->spl_cnt & 7) == 0) {
00084                                 for (p = 0; p < ctx->num_enabled_probes; p++)
00085                                         ctx->linebuf[p * ctx->linebuf_len +
00086                                                      ctx->line_offset] = ' ';
00087                                 ctx->line_offset++;
00088                         }
00089 
00090                         /* End of line. */
00091                         if (ctx->spl_cnt >= ctx->samples_per_line) {
00092                                 flush_linebufs(ctx, outbuf);
00093                                 ctx->line_offset = ctx->spl_cnt = 0;
00094                                 ctx->mark_trigger = -1;
00095                         }
00096                 }
00097         } else {
00098                 sr_info("bits out: short buffer (length_in=%" PRIu64 ")",
00099                         length_in);
00100         }
00101 
00102         *data_out = outbuf;
00103         *length_out = strlen((const char *)outbuf);
00104 
00105         return SR_OK;
00106 }
00107 
00108 SR_PRIV struct sr_output_format output_text_bits = {
00109         .id = "bits",
00110         .description = "Bits",
00111         .df_type = SR_DF_LOGIC,
00112         .init = init_bits,
00113         .data = data_bits,
00114         .event = event,
00115 };
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines