-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.cs
150 lines (127 loc) · 4.77 KB
/
Buffer.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
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
// Copyright (C) 2024 - Nordic Space Link
using System;
namespace NordicSpaceLink.IIO
{
public class Buffer : NativeObject
{
private readonly Device device;
private readonly IntPtr buf;
private bool blocking = true;
/// <summary>
/// Get the step size between two samples of one channel
/// </summary>
public int Step => (int)NativeMethods.iio_buffer_step(buf);
/// <summary>
/// Make Refill() and Push() blocking or not. Defaults to true.
/// </summary>
public bool BlockingMode { get => blocking; set { Common.CheckError(NativeMethods.iio_buffer_set_blocking_mode(buf, value)); blocking = value; } }
/// <summary>
/// Get a pollable file descriptor
///
/// Can be used to know when iio_buffer_refill() or iio_buffer_push() can be called
/// </summary>
public nint PollFD => Common.CheckError(NativeMethods.iio_buffer_get_poll_fd(buf));
/// <summary>
/// Get a span pointing to the current buffer.
/// </summary>
/// <returns>A full span with the current buffer</returns>
public unsafe Span<byte> GetBuffer()
{
var start = (byte*)NativeMethods.iio_buffer_start(buf);
var end = (byte*)NativeMethods.iio_buffer_end(buf);
var diff = end - start;
return new Span<byte>(start, (int)diff);
}
internal Buffer(Device device, IntPtr buf)
{
this.device = device;
this.buf = buf;
if (buf == IntPtr.Zero)
throw new Exception("Could not allocate buffer");
}
/// <summary>
/// Cancel all buffer operations
/// </summary>
public void Cancel()
{
NativeMethods.iio_buffer_cancel(buf);
}
/// <summary>
/// Try to fetch more samples from the hardware.
/// </summary>
/// <param name="bytesRead">Will return the number of bytes read</param>
/// <returns>True if the refill call succeeded.</returns>
public bool TryRefill(out int bytesRead)
{
bytesRead = 0;
var result = NativeMethods.iio_buffer_refill(buf);
if (result < 0)
return false;
bytesRead = (int)result;
return true;
}
/// <summary>
/// Fetch more samples from the hardware
/// </summary>
/// <returns>A span containing the bytes read</returns>
public ReadOnlySpan<byte> Refill()
{
var read = (int)Common.CheckError(NativeMethods.iio_buffer_refill(buf));
return GetBuffer()[..read];
}
/// <summary>
/// Try to send the samples to the hardware
/// </summary>
/// <param name="pushedBytes">The number of bytes written</param>
/// <returns>True if the push succeeded</returns>
public bool TryPush(out int pushedBytes)
{
pushedBytes = 0;
var result = (int)Common.CheckError(NativeMethods.iio_buffer_push(buf));
if (result < 0)
return false;
pushedBytes = result;
return true;
}
/// <summary>
/// Send the samples to the hardware
/// </summary>
/// <returns>The number of bytes written</returns>
public int Push()
{
return (int)Common.CheckError(NativeMethods.iio_buffer_push(buf));
}
/// <summary>
/// Try to send a given number of samples to the hardware
/// </summary>
/// <param name="sample_count">The number of samples to submit</param>
/// <param name="pushedBytes">The number of bytes written</param>
/// <returns>True if the push succeeded</returns>
public bool TryPushPartial(int sample_count, out int pushedBytes)
{
pushedBytes = 0;
var result = (int)Common.CheckError(NativeMethods.iio_buffer_push_partial(buf, (nuint)sample_count));
if (result < 0)
return false;
pushedBytes = result;
return true;
}
/// <summary>
/// Send a given number of samples to the hardware
/// </summary>
/// <param name="sample_count">The number of samples to submit</param>
/// <returns>The number of bytes written</returns>
public int PushPartial(int sample_count)
{
return (int)Common.CheckError(NativeMethods.iio_buffer_push_partial(buf, (nuint)sample_count));
}
protected override void DoDispose()
{
}
protected override void Free()
{
NativeMethods.iio_buffer_destroy(buf);
device.FreeBuffer(this);
}
}
}