2 * This file is part of the libsigrok project.
4 * Copyright (C) 2020 Gerhard Sittig <gerhard.sittig@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <libsigrok/libsigrok.h>
21 #include "libsigrok-internal.h"
24 struct feed_queue_logic {
25 const struct sr_dev_inst *sdi;
30 struct sr_datafeed_packet packet;
31 struct sr_datafeed_logic logic;
34 SR_API struct feed_queue_logic *feed_queue_logic_alloc(
35 const struct sr_dev_inst *sdi,
36 size_t sample_count, size_t unit_size)
38 struct feed_queue_logic *q;
40 q = g_malloc0(sizeof(*q));
42 q->unit_size = unit_size;
43 q->alloc_count = sample_count;
44 q->data_bytes = g_try_malloc(q->alloc_count * q->unit_size);
50 memset(&q->packet, 0, sizeof(q->packet));
51 memset(&q->logic, 0, sizeof(q->logic));
52 q->packet.type = SR_DF_LOGIC;
53 q->packet.payload = &q->logic;
54 q->logic.unitsize = q->unit_size;
55 q->logic.data = q->data_bytes;
60 SR_API int feed_queue_logic_submit(struct feed_queue_logic *q,
61 const uint8_t *data, size_t count)
66 wrptr = &q->data_bytes[q->fill_count * q->unit_size];
68 memcpy(wrptr, data, q->unit_size);
69 wrptr += q->unit_size;
71 if (q->fill_count == q->alloc_count) {
72 ret = feed_queue_logic_flush(q);
75 wrptr = &q->data_bytes[0];
82 SR_API int feed_queue_logic_flush(struct feed_queue_logic *q)
89 q->logic.length = q->fill_count * q->unit_size;
90 ret = sr_session_send(q->sdi, &q->packet);
98 SR_API int feed_queue_logic_send_trigger(struct feed_queue_logic *q)
102 ret = feed_queue_logic_flush(q);
106 ret = std_session_send_df_trigger(q->sdi);
113 SR_API void feed_queue_logic_free(struct feed_queue_logic *q)
119 g_free(q->data_bytes);
123 struct feed_queue_analog {
124 const struct sr_dev_inst *sdi;
129 struct sr_datafeed_packet packet;
130 struct sr_datafeed_analog analog;
131 struct sr_analog_encoding encoding;
132 struct sr_analog_meaning meaning;
133 struct sr_analog_spec spec;
137 SR_API struct feed_queue_analog *feed_queue_analog_alloc(
138 const struct sr_dev_inst *sdi,
139 size_t sample_count, int digits, struct sr_channel *ch)
141 struct feed_queue_analog *q;
143 q = g_malloc0(sizeof(*q));
145 q->alloc_count = sample_count;
146 q->data_values = g_try_malloc(q->alloc_count * sizeof(float));
147 if (!q->data_values) {
152 q->channels = g_slist_append(NULL, ch);
154 memset(&q->packet, 0, sizeof(q->packet));
155 sr_analog_init(&q->analog, &q->encoding, &q->meaning, &q->spec, digits);
156 q->packet.type = SR_DF_ANALOG;
157 q->packet.payload = &q->analog;
158 q->encoding.is_signed = TRUE;
159 q->meaning.channels = q->channels;
160 q->analog.data = q->data_values;
165 SR_API int feed_queue_analog_mq_unit(struct feed_queue_analog *q,
166 enum sr_mq mq, enum sr_mqflag mq_flag, enum sr_unit unit)
173 ret = feed_queue_analog_flush(q);
178 q->meaning.mqflags = mq_flag;
179 q->meaning.unit = unit;
184 SR_API int feed_queue_analog_scale_offset(struct feed_queue_analog *q,
185 const struct sr_rational *scale, const struct sr_rational *offset)
192 ret = feed_queue_analog_flush(q);
197 q->encoding.scale = *scale;
199 q->encoding.offset = *offset;
204 SR_API int feed_queue_analog_submit(struct feed_queue_analog *q,
205 float data, size_t count)
210 q->data_values[q->fill_count++] = data;
211 if (q->fill_count == q->alloc_count) {
212 ret = feed_queue_analog_flush(q);
221 SR_API int feed_queue_analog_flush(struct feed_queue_analog *q)
228 q->analog.num_samples = q->fill_count;
229 ret = sr_session_send(q->sdi, &q->packet);
237 SR_API void feed_queue_analog_free(struct feed_queue_analog *q)
243 g_free(q->data_values);
244 g_slist_free(q->channels);