Skip to content

Commit a0a9367

Browse files
committed
csharp: added missing bindings.
Added the missing C# bindings. Signed-off-by: Iacob <[email protected]>
1 parent 831d858 commit a0a9367

File tree

6 files changed

+531
-8
lines changed

6 files changed

+531
-8
lines changed

bindings/csharp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ if (MCS_EXECUTABLE)
4343
${CMAKE_CURRENT_SOURCE_DIR}/Device.cs
4444
${CMAKE_CURRENT_SOURCE_DIR}/IOBuffer.cs
4545
${CMAKE_CURRENT_SOURCE_DIR}/Trigger.cs
46+
${CMAKE_CURRENT_SOURCE_DIR}/IioLib.cs
4647
${LIBIIO_CS_INFO}
4748
)
4849

bindings/csharp/Channel.cs

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,99 @@ public override void write(string str)
6969
}
7070
}
7171

72+
/// <summary><see cref="iio.Channel.ChannelModifier"/> class:
73+
/// Contains the available channel modifiers.</summary>
74+
public enum ChannelModifier
75+
{
76+
IIO_NO_MOD,
77+
IIO_MOD_X,
78+
IIO_MOD_Y,
79+
IIO_MOD_Z,
80+
IIO_MOD_X_AND_Y,
81+
IIO_MOD_X_AND_Z,
82+
IIO_MOD_Y_AND_Z,
83+
IIO_MOD_X_AND_Y_AND_Z,
84+
IIO_MOD_X_OR_Y,
85+
IIO_MOD_X_OR_Z,
86+
IIO_MOD_Y_OR_Z,
87+
IIO_MOD_X_OR_Y_OR_Z,
88+
IIO_MOD_LIGHT_BOTH,
89+
IIO_MOD_LIGHT_IR,
90+
IIO_MOD_ROOT_SUM_SQUARED_X_Y,
91+
IIO_MOD_SUM_SQUARED_X_Y_Z,
92+
IIO_MOD_LIGHT_CLEAR,
93+
IIO_MOD_LIGHT_RED,
94+
IIO_MOD_LIGHT_GREEN,
95+
IIO_MOD_LIGHT_BLUE,
96+
IIO_MOD_QUATERNION,
97+
IIO_MOD_TEMP_AMBIENT,
98+
IIO_MOD_TEMP_OBJECT,
99+
IIO_MOD_NORTH_MAGN,
100+
IIO_MOD_NORTH_TRUE,
101+
IIO_MOD_NORTH_MAGN_TILT_COMP,
102+
IIO_MOD_NORTH_TRUE_TILT_COMP,
103+
IIO_MOD_RUNNING,
104+
IIO_MOD_JOGGING,
105+
IIO_MOD_WALKING,
106+
IIO_MOD_STILL,
107+
IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z,
108+
IIO_MOD_I,
109+
IIO_MOD_Q,
110+
IIO_MOD_CO2,
111+
IIO_MOD_VOC,
112+
IIO_MOD_LIGHT_UV,
113+
IIO_MOD_LIGHT_DUV,
114+
IIO_MOD_PM1,
115+
IIO_MOD_PM2P5,
116+
IIO_MOD_PM4,
117+
IIO_MOD_PM10,
118+
IIO_MOD_ETHANOL,
119+
IIO_MOD_H2
120+
}
121+
122+
/// <summary><see cref="iio.Channel.ChannelType"/> class:
123+
/// Contains the available channel types.</summary>
124+
public enum ChannelType
125+
{
126+
IIO_VOLTAGE,
127+
IIO_CURRENT,
128+
IIO_POWER,
129+
IIO_ACCEL,
130+
IIO_ANGL_VEL,
131+
IIO_MAGN,
132+
IIO_LIGHT,
133+
IIO_INTENSITY,
134+
IIO_PROXIMITY,
135+
IIO_TEMP,
136+
IIO_INCLI,
137+
IIO_ROT,
138+
IIO_ANGL,
139+
IIO_TIMESTAMP,
140+
IIO_CAPACITANCE,
141+
IIO_ALTVOLTAGE,
142+
IIO_CCT,
143+
IIO_PRESSURE,
144+
IIO_HUMIDITYRELATIVE,
145+
IIO_ACTIVITY,
146+
IIO_STEPS,
147+
IIO_ENERGY,
148+
IIO_DISTANCE,
149+
IIO_VELOCITY,
150+
IIO_CONCENTRATION,
151+
IIO_RESISTANCE,
152+
IIO_PH,
153+
IIO_UVINDEX,
154+
IIO_ELECTRICALCONDUCTIVITY,
155+
IIO_COUNT,
156+
IIO_INDEX,
157+
IIO_GRAVITY,
158+
IIO_POSITIONRELATIVE,
159+
IIO_PHASE,
160+
IIO_MASSCONCENTRATION,
161+
IIO_CHAN_TYPE_UNKNOWN = Int32.MaxValue
162+
}
72163

73-
private IntPtr chn;
164+
internal IntPtr chn;
74165
private uint sample_size;
75166

76167
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
@@ -118,6 +209,30 @@ public override void write(string str)
118209
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
119210
private static extern IntPtr iio_channel_get_data_format(IntPtr chn);
120211

212+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
213+
private static extern int iio_channel_get_index(IntPtr chn);
214+
215+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
216+
private static extern IntPtr iio_channel_get_device(IntPtr chn);
217+
218+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
219+
private static extern IntPtr iio_device_get_context(IntPtr dev);
220+
221+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
222+
private static extern int iio_channel_get_modifier(IntPtr chn);
223+
224+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
225+
private static extern int iio_channel_get_type(IntPtr chn);
226+
227+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
228+
private static extern IntPtr iio_channel_find_attr(IntPtr chn, [In] string name);
229+
230+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
231+
private static extern void iio_channel_convert(IntPtr chn, IntPtr dst, IntPtr src);
232+
233+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
234+
private static extern void iio_channel_convert_inverse(IntPtr chn, IntPtr dst, IntPtr src);
235+
121236
/// <summary>The name of this channel.</summary>
122237
public readonly string name;
123238

@@ -139,11 +254,19 @@ public override void write(string str)
139254
/// <summary>A <c>list</c> of all the attributes that this channel has.</summary>
140255
public readonly List<Attr> attrs;
141256

257+
/// <summary>The modifier of this channel.</summary>
258+
public ChannelModifier modifier { get; private set; }
259+
260+
/// <summary>The type of this channel.</summary>
261+
public ChannelType type { get; private set; }
262+
142263
internal Channel(IntPtr chn)
143264
{
144265
this.chn = chn;
145266
attrs = new List<Attr>();
146267
sample_size = (uint)Marshal.ReadInt32(iio_channel_get_data_format(this.chn)) / 8;
268+
modifier = (ChannelModifier) iio_channel_get_modifier(chn);
269+
type = (ChannelType) iio_channel_get_type(chn);
147270
uint nb_attrs = iio_channel_get_attrs_count(chn);
148271

149272
for (uint i = 0; i < nb_attrs; i++)
@@ -258,5 +381,46 @@ public uint write(IOBuffer buffer, byte[] array, bool raw = false)
258381

259382
return count;
260383
}
384+
385+
/// <summary>Get the index of this channel.</summary>
386+
public long get_index()
387+
{
388+
return iio_channel_get_index(chn);
389+
}
390+
391+
/// <summary>Finds the attribute of the current channel with the given name.</summary>
392+
/// <returns><see cref="iio.Channel.ChannelAttr"/></returns>
393+
/// <exception cref="System.Exception">There is no attribute with the given name.</exception>
394+
public Attr find_attribute(string attribute)
395+
{
396+
IntPtr attr = iio_channel_find_attr(chn, attribute);
397+
398+
if (attr == IntPtr.Zero)
399+
{
400+
throw new Exception("There is no attribute with the given name!");
401+
}
402+
403+
return new ChannelAttr(chn, Marshal.PtrToStringAnsi(attr));
404+
}
405+
406+
/// <summary>Finds the device of the current channel.</summary>
407+
/// <returns><see cref="iio.Device"/></returns>
408+
public Device get_device()
409+
{
410+
IntPtr dev_ptr = iio_channel_get_device(chn);
411+
return new Device(new Context(dev_ptr), dev_ptr);
412+
}
413+
414+
/// <summary>Converts the data from the hardware format to the format of the arhitecture on which libiio is running.</summary>
415+
public void convert(IntPtr dst, IntPtr src)
416+
{
417+
iio_channel_convert(chn, dst, src);
418+
}
419+
420+
/// <summary>Converts the data from the arhitecture on which libiio is running to the hardware format.</summary>
421+
public void convert_inverse(IntPtr dst, IntPtr src)
422+
{
423+
iio_channel_convert_inverse(chn, dst, src);
424+
}
261425
}
262426
}

bindings/csharp/Context.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal Version(uint major, uint minor, string git_tag)
4343
/// Contains the representation of an IIO context.</summary>
4444
public class Context : IDisposable
4545
{
46-
private IntPtr ctx;
46+
internal IntPtr ctx;
4747

4848
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
4949
private static extern IntPtr iio_create_network_context(
@@ -58,6 +58,9 @@ private static extern IntPtr iio_create_context_from_uri(
5858
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
5959
private static extern IntPtr iio_create_default_context();
6060

61+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
62+
private static extern IntPtr iio_create_local_context();
63+
6164
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
6265
private static extern void iio_context_destroy(IntPtr ctx);
6366

@@ -92,6 +95,18 @@ private static extern IntPtr iio_create_context_from_uri(
9295
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
9396
private static extern IntPtr iio_context_clone(IntPtr ctx);
9497

98+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
99+
private static extern uint iio_context_get_attrs_count(IntPtr ctx);
100+
101+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
102+
private static extern int iio_context_get_attr(IntPtr ctx, uint index, IntPtr name_ptr, IntPtr value_ptr);
103+
104+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
105+
private static extern IntPtr iio_context_find_device(IntPtr ctx, [In] string name);
106+
107+
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
108+
private static extern IntPtr iio_context_info_get_description(IntPtr info);
109+
95110
/// <summary>A XML representation of the current context.</summary>
96111
public readonly string xml;
97112

@@ -106,6 +121,9 @@ private static extern IntPtr iio_create_context_from_uri(
106121
/// <summary>A <c>List</c> of all the IIO devices present on the current context.</summary>
107122
public readonly List<Device> devices;
108123

124+
/// <summary>A <c>Dictionary</c> of all the attributes of the current channel. (key, value) = (name, value)</summary>
125+
public Dictionary<string, string> attrs { get; private set; }
126+
109127
/// <summary>Initializes a new instance of the <see cref="iio.Context"/> class,
110128
/// using the provided URI. For compatibility with existing code, providing
111129
/// an IP address or a hostname here will automatically create a network
@@ -135,7 +153,7 @@ private static IntPtr getContextFromString(string str)
135153
return ptr;
136154
}
137155

138-
private Context(IntPtr ctx)
156+
internal Context(IntPtr ctx)
139157
{
140158
this.ctx = ctx;
141159

@@ -179,6 +197,24 @@ private Context(IntPtr ctx)
179197
throw new Exception("Unable to read backend version");
180198
}
181199
backend_version = new Version(major, minor, builder.ToString());
200+
201+
attrs = new Dictionary<string, string>();
202+
203+
for (uint i = 0; i < iio_context_get_attrs_count(ctx); i++)
204+
{
205+
string attr_name = "";
206+
GCHandle name_handle = GCHandle.Alloc(attr_name, GCHandleType.Pinned);
207+
IntPtr name_ptr = GCHandle.ToIntPtr(name_handle);
208+
string attr_value = "";
209+
GCHandle value_handle = GCHandle.Alloc(attr_value, GCHandleType.Pinned);
210+
IntPtr value_ptr = GCHandle.ToIntPtr(value_handle);
211+
212+
iio_context_get_attr(ctx, i, name_ptr, value_ptr);
213+
attr_name = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(name_ptr));
214+
attr_value = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(value_ptr));
215+
216+
attrs[attr_name] = attr_value;
217+
}
182218
}
183219

184220
~Context()
@@ -246,5 +282,20 @@ private void Dispose(bool clean)
246282
ctx = IntPtr.Zero;
247283
}
248284
}
285+
286+
/// <summary>Finds the device with the given name from the current context.</summary>
287+
/// <param name="device">The name of the device.</param>
288+
/// <exception cref="System.Exception">There is no device with the given name.</exception>
289+
public Device find_device(string device)
290+
{
291+
IntPtr dev = iio_context_find_device(ctx, device);
292+
293+
if (dev == IntPtr.Zero)
294+
{
295+
throw new Exception("There is no device with the given name!");
296+
}
297+
298+
return new Device(this, dev);
299+
}
249300
}
250301
}

0 commit comments

Comments
 (0)