]> sigrok.org Git - libsigrok.git/blobdiff - src/input/csv.c
input/csv: update comments/helptext for analog input data
[libsigrok.git] / src / input / csv.c
index 47f5c2ed51511ddd7030a56b6a47e41017bc57c7..38ddf7822471467f956daa2de894440b68873cc2 100644 (file)
  *     "all remaining columns", only applicable to the last field), a format
  *     specifying character ('x' hexadecimal, 'o' octal, 'b' binary, 'l'
  *     single-bit logic), and an optional bit count (translating to: logic
- *     channels communicated in that column). This "column_formats" option
- *     is most versatile, other forms of specifying the column layout only
- *     exist for backwards compatibility.
+ *     channels communicated in that column). The 'a' format marks analog
+ *     data, an optionally following number is the digits count (resolution).
+ *     This "column_formats" option is most versatile, other forms of
+ *     specifying the column layout only exist for backwards compatibility.
  *
  * single_column: Specifies the column number which contains the logic data
  *     for single-column mode. All logic data is taken from several bits
  * - ... -I csv:start_line=20:header=yes:...
  *   Skip the first 19 text lines. Use line 20 to derive channel names.
  *   Data starts at line 21.
+ * - ... -I csv:column_formats=*a6 ...
+ *   Each column contains an analog value with six significant digits
+ *   after the decimal period.
  */
 
 /*
  * TODO
  *
- * - Extend support for analog input data? (optional)
- *   - Optionally get precision ('digits') from the column's format spec?
- *     From the position which is "bit count" for logic channels?
+ * - Extend support for analog input data.
  *   - Determine why analog samples of 'double' data type get scrambled
  *     in sigrok-cli screen output. Is analog.encoding->unitsize not
  *     handled properly? A sigrok-cli or libsigrok (src/output) issue?
+ *   - Reconsider the channel creation after format processing. Current
+ *     logic may "bleed" channel names into the analog group when logic
+ *     channels' columns follow analog columns (seen with "-,2a,x8").
+ *     Trying to sort it out, a naive change used to map logic channels'
+ *     data to incorrect bitmap positions. The whole channel numbering
+ *     needs reconsideration. Probably it's easiest to first create _all_
+ *     logic channels so that they have adjacent numbers starting at 0
+ *     (addressing logic bits), then all analog channels (again adjacent)
+ *     to simplify the calculation of their index in the sample set as
+ *     well as their sdi channel index from the "analog column index".
  * - Optionally get sample rate from timestamp column. Just best-effort
  *   approach, not necessarily reliable. Users can always specify rates.
  * - Add a test suite for input modules in general, and CSV in specific?
@@ -161,6 +173,8 @@ struct column_details {
        enum single_col_format text_format;
        size_t channel_offset;
        size_t channel_count;
+       size_t channel_index;
+       int analog_digits;
 };
 
 struct context {
@@ -170,10 +184,9 @@ struct context {
        uint64_t samplerate;
        gboolean samplerate_sent;
 
-       /* Number of logic channels. List of names for analog datafeed. */
+       /* Number of channels. */
        size_t logic_channels;
        size_t analog_channels;
-       GSList **analog_datafeed_channels;
 
        /* Column delimiter (actually separator), comment leader, EOL sequence. */
        GString *delimiter;
@@ -207,6 +220,8 @@ struct context {
        csv_analog_t *analog_datafeed_buffer;   /**!< Queue for analog datafeed. */
        size_t analog_datafeed_buf_size;
        size_t analog_datafeed_buf_fill;
+       GSList **analog_datafeed_channels;
+       int *analog_datafeed_digits;
 
        /* Current line number. */
        size_t line_number;
@@ -347,9 +362,6 @@ static void set_analog_value(struct context *inc, size_t ch_idx, csv_analog_t va
 
 static int flush_analog_samples(const struct sr_input *in)
 {
-       /* TODO Use proper 'digits' value for this input module. */
-       static const int digits = 3;
-
        struct context *inc;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
@@ -358,6 +370,7 @@ static int flush_analog_samples(const struct sr_input *in)
        struct sr_analog_spec spec;
        csv_analog_t *samples;
        size_t ch_idx;
+       int digits;
        int rc;
 
        inc = in->priv;
@@ -370,6 +383,7 @@ static int flush_analog_samples(const struct sr_input *in)
 
        samples = inc->analog_datafeed_buffer;
        for (ch_idx = 0; ch_idx < inc->analog_channels; ch_idx++) {
+               digits = inc->analog_datafeed_digits[ch_idx];
                sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
                memset(&packet, 0, sizeof(packet));
                packet.type = SR_DF_ANALOG;
@@ -479,13 +493,11 @@ static int split_column_format(const char *spec,
        if (!endp)
                return SR_ERR_ARG;
        if (endp == spec)
-               count = 1;
+               count = (format_code == FORMAT_ANALOG) ? 3 : 1;
        if (!format_code)
                count = 0;
        if (format_char == 'l')
                count = 1;
-       if (format_code == FORMAT_ANALOG)
-               count = 1;
        if (bit_count)
                *bit_count = count;
        spec = endp;
@@ -584,6 +596,7 @@ static int make_column_details_from_format(const struct sr_input *in,
                        if (detail->text_format == FORMAT_ANALOG) {
                                detail->channel_offset = analog_idx;
                                detail->channel_count = 1;
+                               detail->analog_digits = b;
                                analog_idx += detail->channel_count;
                        } else if (detail->text_format) {
                                detail->channel_offset = channel_idx;
@@ -596,7 +609,7 @@ static int make_column_details_from_format(const struct sr_input *in,
                        if (!detail->text_format)
                                continue;
                        /*
-                        * Create channels with appropriate names. Optionally
+                        * Pick most appropriate channel names. Optionally
                         * use text from a header line (when requested by the
                         * user). In the absence of header text, channels are
                         * assigned rather generic names.
@@ -612,6 +625,10 @@ static int make_column_details_from_format(const struct sr_input *in,
                                caption = NULL;
                        if (!caption || !*caption)
                                caption = NULL;
+                       /*
+                        * TODO Need we first create _all_ logic channels,
+                        * before creating analog channels?
+                        */
                        for (create_idx = 0; create_idx < detail->channel_count; create_idx++) {
                                if (caption && detail->channel_count == 1) {
                                        g_string_assign(channel_name, caption);
@@ -625,6 +642,7 @@ static int make_column_details_from_format(const struct sr_input *in,
                                if (detail->text_format == FORMAT_ANALOG) {
                                        channel_sdi_nr = logic_count + detail->channel_offset + create_idx;
                                        channel_type = SR_CHANNEL_ANALOG;
+                                       detail->channel_index = g_slist_length(in->sdi->channels);
                                } else {
                                        channel_sdi_nr = detail->channel_offset + create_idx;
                                        channel_type = SR_CHANNEL_LOGIC;
@@ -1028,7 +1046,7 @@ static int initial_parse(const struct sr_input *in, GString *buf)
 {
        struct context *inc;
        size_t num_columns;
-       size_t line_number, line_idx, ch_idx;
+       size_t line_number, line_idx;
        int ret;
        char **lines, *line, **columns;
 
@@ -1125,6 +1143,10 @@ static int initial_parse(const struct sr_input *in, GString *buf)
 
        if (inc->analog_channels) {
                size_t sample_size, sample_count;
+               size_t detail_idx;
+               struct column_details *detail;
+               int *digits_item;
+               void *channel;
                sample_size = sizeof(inc->analog_datafeed_buffer[0]);
                inc->analog_datafeed_buf_size = CHUNK_SIZE;
                inc->analog_datafeed_buf_size /= sample_size;
@@ -1137,11 +1159,16 @@ static int initial_parse(const struct sr_input *in, GString *buf)
                        goto out;
                }
                inc->analog_datafeed_buf_fill = 0;
-               inc->analog_datafeed_channels = g_malloc0_n(inc->analog_channels, sizeof(inc->analog_datafeed_channels[0]));
-               for (ch_idx = 0; ch_idx < inc->analog_channels; ch_idx++) {
-                       void *channel;
-                       channel = g_slist_nth_data(in->sdi->channels, inc->logic_channels + ch_idx);
-                       inc->analog_datafeed_channels[ch_idx] = g_slist_append(NULL, channel);
+               inc->analog_datafeed_channels = g_malloc0(inc->analog_channels * sizeof(inc->analog_datafeed_channels[0]));
+               inc->analog_datafeed_digits = g_malloc0(inc->analog_channels * sizeof(inc->analog_datafeed_digits[0]));
+               digits_item = inc->analog_datafeed_digits;
+               for (detail_idx = 0; detail_idx < inc->column_want_count; detail_idx++) {
+                       detail = &inc->column_details[detail_idx];
+                       if (detail->text_format != FORMAT_ANALOG)
+                               continue;
+                       channel = g_slist_nth_data(in->sdi->channels, detail->channel_index);
+                       inc->analog_datafeed_channels[detail->channel_offset] = g_slist_append(NULL, channel);
+                       *digits_item++ = detail->analog_digits;
                }
        }
 
@@ -1428,17 +1455,17 @@ enum option_index {
 static struct sr_option options[] = {
        [OPT_COL_FMTS] = {
                "column_formats", "Column format specs",
-               "Specifies text columns data types: comma separated list of [<cols>]<fmt>[<bits>], with -/x/o/b/l format specifiers.",
+               "Specifies text columns data types: A comma separated list of [<cols>]<fmt>[<bits>] items, with - to ignore columns, x/o/b/l for logic data, a (and resolution) for analog data.",
                NULL, NULL,
        },
        [OPT_SINGLE_COL] = {
                "single_column", "Single column",
-               "Enable single-column mode, exclusively use text from the specified column (number starting at 1).",
+               "Enable single-column mode, exclusively use text from the specified column (number starting at 1). Obsoleted by 'column_formats'.",
                NULL, NULL,
        },
        [OPT_FIRST_COL] = {
                "first_column", "First column",
-               "Number of the first column with logic data in simple multi-column mode (number starting at 1, default 1).",
+               "Number of the first column with logic data in simple multi-column mode (number starting at 1, default 1). Obsoleted by 'column_formats'.",
                NULL, NULL,
        },
        [OPT_NUM_LOGIC] = {
@@ -1448,7 +1475,7 @@ static struct sr_option options[] = {
        },
        [OPT_FORMAT] = {
                "single_format", "Data format for simple single-column mode.",
-               "The number format of single-column mode input data: bin, hex, oct.",
+               "The number format of single-column mode input data: bin, hex, oct. Obsoleted by 'column_formats'.",
                NULL, NULL,
        },
        [OPT_START] = {
@@ -1458,12 +1485,12 @@ static struct sr_option options[] = {
        },
        [OPT_HEADER] = {
                "header", "Get channel names from first line.",
-               "Use the first processed line's column captions (when available) as channel names.",
+               "Use the first processed line's column captions (when available) as channel names. Off by default",
                NULL, NULL,
        },
        [OPT_RATE] = {
                "samplerate", "Samplerate (Hz)",
-               "The input data's sample rate in Hz.",
+               "The input data's sample rate in Hz. No default value.",
                NULL, NULL,
        },
        [OPT_DELIM] = {
@@ -1473,7 +1500,7 @@ static struct sr_option options[] = {
        },
        [OPT_COMMENT] = {
                "comment_leader", "Comment leader character",
-               "The text which starts comments at the end of text lines.",
+               "The text which starts comments at the end of text lines, semicolon by default.",
                NULL, NULL,
        },
        [OPT_MAX] = ALL_ZERO,