libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
text.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 <stdio.h>
00023 #include <string.h>
00024 #include <glib.h>
00025 #include "config.h"
00026 #include "sigrok.h"
00027 #include "sigrok-internal.h"
00028 #include "text.h"
00029 
00030 SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf)
00031 {
00032         static int max_probename_len = 0;
00033         int len, i;
00034 
00035         if (ctx->linebuf[0] == 0)
00036                 return;
00037 
00038         if (max_probename_len == 0) {
00039                 /* First time through... */
00040                 for (i = 0; ctx->probelist[i]; i++) {
00041                         len = strlen(ctx->probelist[i]);
00042                         if (len > max_probename_len)
00043                                 max_probename_len = len;
00044                 }
00045         }
00046 
00047         for (i = 0; ctx->probelist[i]; i++) {
00048                 sprintf((char *)outbuf + strlen((const char *)outbuf),
00049                         "%*s:%s\n", max_probename_len,
00050                         ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
00051         }
00052 
00053         /* Mark trigger with a ^ character. */
00054         if (ctx->mark_trigger != -1)
00055         {
00056                 int space_offset = ctx->mark_trigger / 8;
00057 
00058                 if (ctx->mode == MODE_ASCII)
00059                         space_offset = 0;
00060 
00061                 sprintf((char *)outbuf + strlen((const char *)outbuf),
00062                         "T:%*s^\n", ctx->mark_trigger + space_offset, "");
00063         }
00064 
00065         memset(ctx->linebuf, 0, i * ctx->linebuf_len);
00066 }
00067 
00068 SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
00069 {
00070         struct context *ctx;
00071         struct sr_probe *probe;
00072         GSList *l;
00073         uint64_t samplerate;
00074         int num_probes;
00075         char *samplerate_s;
00076 
00077         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
00078                 sr_err("text out: %s: ctx malloc failed", __func__);
00079                 return SR_ERR_MALLOC;
00080         }
00081 
00082         o->internal = ctx;
00083         ctx->num_enabled_probes = 0;
00084 
00085         for (l = o->dev->probes; l; l = l->next) {
00086                 probe = l->data;
00087                 if (!probe->enabled)
00088                         continue;
00089                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
00090         }
00091 
00092         ctx->probelist[ctx->num_enabled_probes] = 0;
00093         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
00094         ctx->line_offset = 0;
00095         ctx->spl_cnt = 0;
00096         ctx->mark_trigger = -1;
00097         ctx->mode = mode;
00098 
00099         if (o->param && o->param[0]) {
00100                 ctx->samples_per_line = strtoul(o->param, NULL, 10);
00101                 if (ctx->samples_per_line < 1)
00102                         return SR_ERR;
00103         } else
00104                 ctx->samples_per_line = default_spl;
00105 
00106         if (!(ctx->header = g_try_malloc0(512))) {
00107                 g_free(ctx);
00108                 sr_err("text out: %s: ctx->header malloc failed", __func__);
00109                 return SR_ERR_MALLOC;
00110         }
00111 
00112         snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
00113         num_probes = g_slist_length(o->dev->probes);
00114         if (o->dev->driver || sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
00115                 samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
00116                                 o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
00117                 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
00118                         g_free(ctx->header);
00119                         g_free(ctx);
00120                         return SR_ERR;
00121                 }
00122                 snprintf(ctx->header + strlen(ctx->header),
00123                          511 - strlen(ctx->header),
00124                          "Acquisition with %d/%d probes at %s\n",
00125                          ctx->num_enabled_probes, num_probes, samplerate_s);
00126                 g_free(samplerate_s);
00127         }
00128 
00129         ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
00130         if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
00131                 g_free(ctx->header);
00132                 g_free(ctx);
00133                 sr_err("text out: %s: ctx->linebuf malloc failed", __func__);
00134                 return SR_ERR_MALLOC;
00135         }
00136         if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
00137                 g_free(ctx->header);
00138                 g_free(ctx);
00139                 sr_err("text out: %s: ctx->linevalues malloc failed", __func__);
00140                 return SR_ERR_MALLOC;
00141         }
00142 
00143         return SR_OK;
00144 }
00145 
00146 SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
00147                   uint64_t *length_out)
00148 {
00149         struct context *ctx;
00150         int outsize;
00151         uint8_t *outbuf;
00152 
00153         ctx = o->internal;
00154         switch (event_type) {
00155         case SR_DF_TRIGGER:
00156                 ctx->mark_trigger = ctx->spl_cnt;
00157                 *data_out = NULL;
00158                 *length_out = 0;
00159                 break;
00160         case SR_DF_END:
00161                 outsize = ctx->num_enabled_probes
00162                                 * (ctx->samples_per_line + 20) + 512;
00163                 if (!(outbuf = g_try_malloc0(outsize))) {
00164                         sr_err("text out: %s: outbuf malloc failed", __func__);
00165                         return SR_ERR_MALLOC;
00166                 }
00167                 flush_linebufs(ctx, outbuf);
00168                 *data_out = outbuf;
00169                 *length_out = strlen((const char *)outbuf);
00170                 g_free(o->internal);
00171                 o->internal = NULL;
00172                 break;
00173         default:
00174                 *data_out = NULL;
00175                 *length_out = 0;
00176                 break;
00177         }
00178 
00179         return SR_OK;
00180 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines