1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2015 Analog Devices, Inc.
* Author: Paul Cercueil <paul.cercueil@analog.com>
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace iio
{
/// <summary><see cref="iio.Channel"/> class:
/// Contains the representation of an input or output channel.</summary>
public class Channel
{
private class ChannelAttr : Attr
{
private IntPtr chn;
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_channel_attr_read(IntPtr chn, [In()] string name, [Out()] StringBuilder val, uint len);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_channel_attr_write(IntPtr chn, [In()] string name, string val);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_channel_attr_get_filename(IntPtr chn, [In()] string attr);
public ChannelAttr(IntPtr chn, string name) : base(name, Marshal.PtrToStringAnsi(iio_channel_attr_get_filename(chn, name)))
{
this.chn = chn;
}
public override string read()
{
StringBuilder builder = new StringBuilder(1024);
int err = iio_channel_attr_read(chn, name, builder, (uint) builder.Capacity);
if (err < 0)
{
throw new Exception("Unable to read channel attribute " + err);
}
return builder.ToString();
}
public override void write(string str)
{
int err = iio_channel_attr_write(chn, name, str);
if (err < 0)
{
throw new Exception("Unable to write channel attribute " + err);
}
}
}
/// <summary><see cref="iio.Channel.ChannelModifier"/> class:
/// Contains the available channel modifiers.</summary>
public enum ChannelModifier
{
IIO_NO_MOD,
IIO_MOD_X,
IIO_MOD_Y,
IIO_MOD_Z,
IIO_MOD_X_AND_Y,
IIO_MOD_X_AND_Z,
IIO_MOD_Y_AND_Z,
IIO_MOD_X_AND_Y_AND_Z,
IIO_MOD_X_OR_Y,
IIO_MOD_X_OR_Z,
IIO_MOD_Y_OR_Z,
IIO_MOD_X_OR_Y_OR_Z,
IIO_MOD_LIGHT_BOTH,
IIO_MOD_LIGHT_IR,
IIO_MOD_ROOT_SUM_SQUARED_X_Y,
IIO_MOD_SUM_SQUARED_X_Y_Z,
IIO_MOD_LIGHT_CLEAR,
IIO_MOD_LIGHT_RED,
IIO_MOD_LIGHT_GREEN,
IIO_MOD_LIGHT_BLUE,
IIO_MOD_QUATERNION,
IIO_MOD_TEMP_AMBIENT,
IIO_MOD_TEMP_OBJECT,
IIO_MOD_NORTH_MAGN,
IIO_MOD_NORTH_TRUE,
IIO_MOD_NORTH_MAGN_TILT_COMP,
IIO_MOD_NORTH_TRUE_TILT_COMP,
IIO_MOD_RUNNING,
IIO_MOD_JOGGING,
IIO_MOD_WALKING,
IIO_MOD_STILL,
IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z,
IIO_MOD_I,
IIO_MOD_Q,
IIO_MOD_CO2,
IIO_MOD_VOC,
IIO_MOD_LIGHT_UV,
IIO_MOD_LIGHT_DUV,
IIO_MOD_PM1,
IIO_MOD_PM2P5,
IIO_MOD_PM4,
IIO_MOD_PM10,
IIO_MOD_ETHANOL,
IIO_MOD_H2,
IIO_MOD_O2
}
/// <summary><see cref="iio.Channel.ChannelType"/> class:
/// Contains the available channel types.</summary>
public enum ChannelType
{
IIO_VOLTAGE,
IIO_CURRENT,
IIO_POWER,
IIO_ACCEL,
IIO_ANGL_VEL,
IIO_MAGN,
IIO_LIGHT,
IIO_INTENSITY,
IIO_PROXIMITY,
IIO_TEMP,
IIO_INCLI,
IIO_ROT,
IIO_ANGL,
IIO_TIMESTAMP,
IIO_CAPACITANCE,
IIO_ALTVOLTAGE,
IIO_CCT,
IIO_PRESSURE,
IIO_HUMIDITYRELATIVE,
IIO_ACTIVITY,
IIO_STEPS,
IIO_ENERGY,
IIO_DISTANCE,
IIO_VELOCITY,
IIO_CONCENTRATION,
IIO_RESISTANCE,
IIO_PH,
IIO_UVINDEX,
IIO_ELECTRICALCONDUCTIVITY,
IIO_COUNT,
IIO_INDEX,
IIO_GRAVITY,
IIO_POSITIONRELATIVE,
IIO_PHASE,
IIO_MASSCONCENTRATION,
IIO_CHAN_TYPE_UNKNOWN = Int32.MaxValue
}
public struct DataFormat
{
/// <summary>Total length of the sample, in bits</summary>
public uint length;
/// <summary>Length of valuable data in the sample, in bits</summary>
public uint bits;
/// <summary>Right-shift to apply when converting sample</summary>
public uint shift;
/// <summary>True if the sample is signed</summary>
[MarshalAs(UnmanagedType.I1)] public bool is_signed;
/// <summary>True if the sample if fully defined, sign-extended, etc.</summary>
[MarshalAs(UnmanagedType.I1)] public bool is_fully_defined;
/// <summary>True if the sample is in big-endian format</summary>
[MarshalAs(UnmanagedType.I1)] public bool is_be;
/// <summary>True if the sample should be scaled when converted</summary>
[MarshalAs(UnmanagedType.I1)] public bool with_scale;
/// <summary>Scale to apply if with_scale is True</summary>
public double scale;
/// <summary>Number of times length repeats</summary>
public uint repeat;
}
internal IntPtr chn;
private uint sample_size;
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_channel_get_id(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_channel_get_name(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern uint iio_channel_get_attrs_count(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_channel_get_attr(IntPtr chn, uint index);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
private static extern bool iio_channel_is_output(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
private static extern bool iio_channel_is_scan_element(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void iio_channel_enable(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void iio_channel_disable(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
private static extern bool iio_channel_is_enabled(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern uint iio_channel_read_raw(IntPtr chn, IntPtr buf, IntPtr dst, uint len);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern uint iio_channel_write_raw(IntPtr chn, IntPtr buf, IntPtr src, uint len);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern uint iio_channel_read(IntPtr chn, IntPtr buf, IntPtr dst, uint len);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern uint iio_channel_write(IntPtr chn, IntPtr buf, IntPtr src, uint len);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_channel_get_data_format(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_channel_get_index(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_channel_get_device(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_device_get_context(IntPtr dev);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_channel_get_modifier(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_channel_get_type(IntPtr chn);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_channel_find_attr(IntPtr chn, [In] string name);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void iio_channel_convert(IntPtr chn, IntPtr dst, IntPtr src);
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void iio_channel_convert_inverse(IntPtr chn, IntPtr dst, IntPtr src);
/// <summary>The name of this channel.</summary>
public readonly string name;
/// <summary>An identifier of this channel.</summary>
/// <remarks>It is possible that two channels have the same ID,
/// if one is an input channel and the other is an output channel.</remarks>
public readonly string id;
/// <summary>Contains <c>true</c> if the channel is an output channel,
/// <c>false</c> otherwise.</summary>
public readonly bool output;
/// <summary>Contains <c>true</c> if the channel is a scan element,
/// <c>false</c> otherwise.</summary>
/// <remarks>If a channel is a scan element, then it is possible to enable it
/// and use it for I/O operations.</remarks>
public readonly bool scan_element;
/// <summary>A <c>list</c> of all the attributes that this channel has.</summary>
public readonly List<Attr> attrs;
/// <summary>The modifier of this channel.</summary>
public ChannelModifier modifier { get; private set; }
/// <summary>The type of this channel.</summary>
public ChannelType type { get; private set; }
/// <summary>Represents the format of a data sample.</summary>
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<Attr>();
modifier = (ChannelModifier) iio_channel_get_modifier(chn);
type = (ChannelType) iio_channel_get_type(chn);
format = (DataFormat)Marshal.PtrToStructure(fmt_struct, typeof(DataFormat));
sample_size = format.length / 8;
for (uint i = 0; i < nb_attrs; i++)
{
attrs.Add(new ChannelAttr(this.chn, Marshal.PtrToStringAnsi(iio_channel_get_attr(chn, i))));
}
IntPtr name_ptr = iio_channel_get_name(this.chn);
if (name_ptr == IntPtr.Zero)
{
name = "";
}
else
{
name = Marshal.PtrToStringAnsi(name_ptr);
}
id = Marshal.PtrToStringAnsi(iio_channel_get_id(this.chn));
output = iio_channel_is_output(this.chn);
scan_element = iio_channel_is_scan_element(this.chn);
}
/// <summary>Enable the current channel, so that it can be used for I/O operations.</summary>
public void enable()
{
iio_channel_enable(this.chn);
}
/// <summary>Disable the current channel.</summary>
public void disable()
{
iio_channel_disable(this.chn);
}
/// <summary>Returns whether or not the channel has been enabled.</summary>
public bool is_enabled()
{
return iio_channel_is_enabled(this.chn);
}
/// <summary>Extract the samples corresponding to this channel from the
/// given <see cref="iio.IOBuffer"/> object.</summary>
/// <param name="buffer">A valid instance of the <see cref="iio.IOBuffer"/> class.</param>
/// <param name="raw">If set to <c>true</c>, the samples are not converted from their
/// hardware format to their host format.</param>
/// <returns>A <c>byte</c> array containing the extracted samples.</returns>
/// <exception cref="System.Exception">The samples could not be read.</exception>
public byte[] read(IOBuffer buffer, bool raw = false)
{
if (!is_enabled())
{
throw new Exception("Channel must be enabled before the IOBuffer is instantiated");
}
if (this.output)
{
throw new Exception("Unable to read from output channel");
}
byte[] array = new byte[(int) (buffer.samples_count * sample_size)];
MemoryStream stream = new MemoryStream(array, true);
GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
IntPtr addr = handle.AddrOfPinnedObject();
uint count;
if (raw)
{
count = iio_channel_read_raw(this.chn, buffer.buf, addr, buffer.samples_count * sample_size);
}
else
{
count = iio_channel_read(this.chn, buffer.buf, addr, buffer.samples_count * sample_size);
}
handle.Free();
stream.SetLength((long) count);
return stream.ToArray();
}
/// <summary>
/// Write the specified array of samples corresponding to this channel into the
/// given <see cref="iio.IOBuffer"/> object.</summary>
/// <param name="buffer">A valid instance of the <see cref="iio.IOBuffer"/> class.</param>
/// <param name="array">A <c>byte</c> array containing the samples to write.</param>
/// <param name="raw">If set to <c>true</c>, the samples are not converted from their
/// host format to their native format.</param>
/// <returns>The number of bytes written.</returns>
/// <exception cref="System.Exception">The samples could not be written.</exception>
public uint write(IOBuffer buffer, byte[] array, bool raw = false)
{
if (!is_enabled())
{
throw new Exception("Channel must be enabled before the IOBuffer is instantiated");
}
if (!this.output)
{
throw new Exception("Unable to write to an input channel");
}
GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
IntPtr addr = handle.AddrOfPinnedObject();
uint count;
if (raw)
{
count = iio_channel_write_raw(this.chn, buffer.buf, addr, (uint) array.Length);
}
else
{
count = iio_channel_write(this.chn, buffer.buf, addr, (uint) array.Length);
}
handle.Free();
return count;
}
/// <summary>Get the index of this channel.</summary>
public long get_index()
{
return iio_channel_get_index(chn);
}
/// <summary>Finds the attribute of the current channel with the given name.</summary>
/// <returns><see cref="iio.Channel.ChannelAttr"/></returns>
/// <exception cref="System.Exception">There is no attribute with the given name.</exception>
public Attr find_attribute(string attribute)
{
IntPtr attr = iio_channel_find_attr(chn, attribute);
if (attr == IntPtr.Zero)
{
throw new Exception("There is no attribute with the given name!");
}
return new ChannelAttr(chn, Marshal.PtrToStringAnsi(attr));
}
/// <summary>Finds the device of the current channel.</summary>
/// <returns><see cref="iio.Device"/></returns>
public Device get_device()
{
IntPtr dev_ptr = iio_channel_get_device(chn);
return new Device(new Context(dev_ptr), dev_ptr);
}
/// <summary>Converts the data from the hardware format to the format of the arhitecture on which libiio is running.</summary>
public void convert(IntPtr dst, IntPtr src)
{
iio_channel_convert(chn, dst, src);
}
/// <summary>Converts the data from the arhitecture on which libiio is running to the hardware format.</summary>
public void convert_inverse(IntPtr dst, IntPtr src)
{
iio_channel_convert_inverse(chn, dst, src);
}
}
}
|