]> sigrok.org Git - libsigrok.git/blame - src/binary_helpers.c
rdtech-um: increase digits count for models with higher resolution
[libsigrok.git] / src / binary_helpers.c
CommitLineData
18698b40
AS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2020 Andreas Sandberg <andreas@sandberg.pp.se>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <config.h>
21#include <glib.h>
22#include <libsigrok/libsigrok.h>
23#include "libsigrok-internal.h"
24
01671e56
GS
25SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec,
26 const void *data, size_t length)
18698b40
AS
27{
28 float value;
29
30 if (!out || !spec || !data)
31 return SR_ERR_ARG;
32
33#define VALUE_TYPE(T, R, L) \
34 case T: \
35 if (spec->offset + (L) > length) \
36 return SR_ERR_DATA; \
37 value = R(data + spec->offset); \
38 break
39
40 switch (spec->type) {
d65f51bf 41 VALUE_TYPE(BVT_UINT8, R8, sizeof(uint8_t));
18698b40 42
d65f51bf
GS
43 VALUE_TYPE(BVT_BE_UINT16, RB16, sizeof(uint16_t));
44 VALUE_TYPE(BVT_BE_UINT32, RB32, sizeof(uint32_t));
45 VALUE_TYPE(BVT_BE_UINT64, RB64, sizeof(uint64_t));
46 VALUE_TYPE(BVT_BE_FLOAT, RBFL, sizeof(float));
18698b40 47
d65f51bf
GS
48 VALUE_TYPE(BVT_LE_UINT16, RL16, sizeof(uint16_t));
49 VALUE_TYPE(BVT_LE_UINT32, RL32, sizeof(uint32_t));
50 VALUE_TYPE(BVT_LE_UINT64, RL64, sizeof(uint64_t));
51 VALUE_TYPE(BVT_LE_FLOAT, RLFL, sizeof(float));
18698b40
AS
52
53 default:
54 return SR_ERR_ARG;
55 }
56
57#undef VALUE_TYPE
58
59 *out = value * spec->scale;
60 return SR_OK;
61}
62
01671e56
GS
63SR_PRIV int bv_send_analog_channel(const struct sr_dev_inst *sdi,
64 struct sr_channel *ch, const struct binary_analog_channel *bac,
65 const void *data, size_t length)
18698b40
AS
66{
67 int err;
68 struct sr_analog_encoding encoding;
69 struct sr_analog_meaning meaning;
70 struct sr_analog_spec spec;
71 struct sr_datafeed_analog analog;
72 struct sr_datafeed_packet packet = {
73 .type = SR_DF_ANALOG,
74 .payload = &analog,
75 };
76 float value;
77
78 err = bv_get_value(&value, &bac->spec, data, length);
79 if (err != SR_OK)
80 goto err_out;
81
82 err = sr_analog_init(&analog, &encoding, &meaning, &spec, bac->digits);
83 if (err != SR_OK)
84 goto err_out;
85
86 meaning.mq = bac->mq;
87 meaning.unit = bac->unit;
88 meaning.mqflags = 0;
89 meaning.channels = g_slist_append(NULL, ch);
18698b40
AS
90
91 spec.spec_digits = bac->digits;
92
93 analog.data = &value;
94 analog.num_samples = 1;
95
96 err = sr_session_send(sdi, &packet);
97 if (err != SR_OK)
98 goto err_free;
99
100 return SR_OK;
101
102err_free:
103 g_slist_free(meaning.channels);
104
105err_out:
106 return err;
107}