-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChannel.cs
109 lines (85 loc) · 3.23 KB
/
Channel.cs
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
// Copyright (C) 2024 - Nordic Space Link
using System;
using System.Collections.Generic;
namespace NordicSpaceLink.IIO
{
public class Channel
{
private readonly (string, bool)[] labels;
private readonly IntPtr chn;
public Context Context { get; }
public Device Device { get; }
public string Name { get; }
public string ID { get; }
public bool IsOutput { get; }
public bool IsScanElement { get; }
/// <summary>
/// True if the channel is enabled
/// </summary>
public bool Enabled
{
get => NativeMethods.iio_channel_is_enabled(chn); set
{
if (value)
NativeMethods.iio_channel_enable(chn);
else
NativeMethods.iio_channel_disable(chn);
}
}
public ChannelType ChannelType => NativeMethods.iio_channel_get_type(chn);
public Modifier Modifier => NativeMethods.iio_channel_get_modifier(chn);
public Indexer<string, Attribute> Attributes { get; }
internal (string, bool)[] GetLabels()
{
return labels;
}
internal unsafe Channel(Device device, IntPtr chn, string name, string id)
{
this.chn = chn;
Context = device.Context;
Device = device;
Name = name;
ID = id;
IsOutput = NativeMethods.iio_channel_is_output(chn);
IsScanElement = NativeMethods.iio_channel_is_scan_element(chn);
var result = new List<(string, bool)>();
if (!string.IsNullOrWhiteSpace(Name))
result.Add((Name, IsOutput));
if (!string.IsNullOrWhiteSpace(ID))
result.Add((ID, IsOutput));
labels = result.ToArray();
// Device attributes
var attrs = new List<(Attribute, string[])>();
var count = NativeMethods.iio_channel_get_attrs_count(chn);
for (int i = 0; i < (int)count; i++)
{
var namePtr = NativeMethods.iio_channel_get_attr(chn, (nuint)i);
if (namePtr != null)
{
var attrName = Common.ConstCString(namePtr, 1024);
attrs.Add((new ChannelAttribute(chn, attrName), new string[] { attrName }));
}
}
Attributes = new Indexer<string, Attribute>(attrs);
}
private class ChannelAttribute : Attribute
{
private readonly IntPtr chan;
public ChannelAttribute(IntPtr chan, string name) : base(name)
{
this.chan = chan;
}
protected unsafe override string GetValue()
{
const int buffersize = 4096;
byte* buffer = stackalloc byte[buffersize];
var size = Common.CheckError(NativeMethods.iio_channel_attr_read(chan, Name, buffer, buffersize));
return new Span<byte>(buffer, (int)size).CString();
}
protected override void SetValue(string value)
{
Common.CheckError(NativeMethods.iio_channel_attr_write(chan, Name, value));
}
}
}
}