From 27f8490f1978f13709eedd37c40e74d77e31ae51 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Tue, 5 Oct 2021 11:03:23 +0100 Subject: [PATCH] C#: Add support for data format Provide a "format" field in the Channel object, which contains a read-only DataFormat structure similar to iio_data_format. Signed-off-by: Paul Cercueil --- bindings/csharp/Channel.cs | 40 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/bindings/csharp/Channel.cs b/bindings/csharp/Channel.cs index 017a07228..0ca9c909b 100644 --- a/bindings/csharp/Channel.cs +++ b/bindings/csharp/Channel.cs @@ -152,6 +152,36 @@ public enum ChannelType IIO_CHAN_TYPE_UNKNOWN = Int32.MaxValue } + public struct DataFormat + { + /// Total length of the sample, in bits + public uint length; + + /// Length of valuable data in the sample, in bits + public uint bits; + + /// Right-shift to apply when converting sample + public uint shift; + + /// True if the sample is signed + [MarshalAs(UnmanagedType.I1)] public bool is_signed; + + /// True if the sample if fully defined, sign-extended, etc. + [MarshalAs(UnmanagedType.I1)] public bool is_fully_defined; + + /// True if the sample is in big-endian format + [MarshalAs(UnmanagedType.I1)] public bool is_be; + + /// True if the sample should be scaled when converted + [MarshalAs(UnmanagedType.I1)] public bool with_scale; + + /// Scale to apply if with_scale is True + public double scale; + + /// Number of times length repeats + public uint repeat; + } + internal IntPtr chn; private uint sample_size; @@ -251,14 +281,20 @@ public enum ChannelType /// The type of this channel. public ChannelType type { get; private set; } + /// Represents the format of a data sample. + public DataFormat format { get; private set; } + internal Channel(IntPtr chn) { + IntPtr fmt_struct = iio_channel_get_data_format(chn); + uint nb_attrs = iio_channel_get_attrs_count(chn); + this.chn = chn; attrs = new List(); - sample_size = (uint)Marshal.ReadInt32(iio_channel_get_data_format(this.chn)) / 8; modifier = (ChannelModifier) iio_channel_get_modifier(chn); type = (ChannelType) iio_channel_get_type(chn); - uint nb_attrs = iio_channel_get_attrs_count(chn); + format = (DataFormat)Marshal.PtrToStructure(fmt_struct, typeof(DataFormat)); + sample_size = format.length / 8; for (uint i = 0; i < nb_attrs; i++) {