com.shinolab.autd3-sdk 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AUTD3.asmdef ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "AUTD3",
3
+ "rootNamespace": "AUTD3",
4
+ "references": [
5
+ "AUTD3.Core",
6
+ "AUTD3.Pattern",
7
+ "AUTD3.Modulation"
8
+ ],
9
+ "includePlatforms": [],
10
+ "excludePlatforms": [],
11
+ "allowUnsafeCode": false,
12
+ "overrideReferences": false,
13
+ "precompiledReferences": [],
14
+ "autoReferenced": true,
15
+ "defineConstraints": [],
16
+ "versionDefines": [],
17
+ "noEngineReferences": false
18
+ }
@@ -0,0 +1,7 @@
1
+ fileFormatVersion: 2
2
+ guid: c71830b8f89d3cc0c740e2ff9b6290cb
3
+ AssemblyDefinitionImporter:
4
+ externalObjects: {}
5
+ userData:
6
+ assetBundleName:
7
+ assetBundleVariant:
package/AsyncOps.cs ADDED
@@ -0,0 +1,45 @@
1
+ using System;
2
+ using System.Runtime.InteropServices;
3
+ using System.Threading.Tasks;
4
+
5
+ namespace AUTD3
6
+ {
7
+ internal static class AsyncOps
8
+ {
9
+
10
+
11
+ internal static readonly CompletionCallback Callback = OnComplete;
12
+
13
+ internal static Task<IntPtr> InvokeAsync(Action<CompletionCallback, IntPtr> call)
14
+ {
15
+ var tcs = new TaskCompletionSource<IntPtr>(TaskCreationOptions.RunContinuationsAsynchronously);
16
+ var handle = GCHandle.Alloc(tcs);
17
+ try
18
+ {
19
+ call(Callback, GCHandle.ToIntPtr(handle));
20
+ }
21
+ catch
22
+ {
23
+ handle.Free();
24
+ throw;
25
+ }
26
+ return tcs.Task;
27
+ }
28
+
29
+ [MonoPInvokeCallback(typeof(CompletionCallback))]
30
+ private static void OnComplete(int code, IntPtr value, IntPtr msg, IntPtr userData)
31
+ {
32
+ var handle = GCHandle.FromIntPtr(userData);
33
+ var tcs = (TaskCompletionSource<IntPtr>)handle.Target!;
34
+ handle.Free();
35
+ if (code == 0)
36
+ {
37
+ tcs.SetResult(value);
38
+ }
39
+ else
40
+ {
41
+ tcs.SetException(new Autd3Exception(NativeUtil.PtrToString(msg)));
42
+ }
43
+ }
44
+ }
45
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: 964632ab6f3b01132dda79ce72989036
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
package/Client.cs ADDED
@@ -0,0 +1,357 @@
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Runtime.CompilerServices;
4
+ using System.Runtime.InteropServices;
5
+ using System.Threading.Tasks;
6
+
7
+ namespace AUTD3
8
+ {
9
+ public readonly struct FpgaState
10
+ {
11
+ public byte Raw { get; }
12
+
13
+ public FpgaState(byte raw)
14
+ {
15
+ Raw = raw;
16
+ }
17
+
18
+ public bool IsThermalAsserted => (Raw & (1 << 0)) != 0;
19
+ public bool ReadsEnabled => (Raw & (1 << 7)) != 0;
20
+ }
21
+
22
+ public sealed class LinkStatus
23
+ {
24
+ public IReadOnlyList<DeviceState> Devices { get; }
25
+ public ulong Recoveries { get; }
26
+
27
+ internal LinkStatus(IReadOnlyList<DeviceState> devices, ulong recoveries)
28
+ {
29
+ Devices = devices;
30
+ Recoveries = recoveries;
31
+ }
32
+
33
+ public bool AllOp
34
+ {
35
+ get
36
+ {
37
+ foreach (var state in Devices)
38
+ {
39
+ if (state != DeviceState.Op)
40
+ {
41
+ return false;
42
+ }
43
+ }
44
+ return true;
45
+ }
46
+ }
47
+
48
+ public bool AnyLost
49
+ {
50
+ get
51
+ {
52
+ foreach (var state in Devices)
53
+ {
54
+ if (state == DeviceState.Lost)
55
+ {
56
+ return true;
57
+ }
58
+ }
59
+ return false;
60
+ }
61
+ }
62
+
63
+ public override bool Equals(object? obj)
64
+ {
65
+ if (obj is not LinkStatus other)
66
+ {
67
+ return false;
68
+ }
69
+ if (Recoveries != other.Recoveries || Devices.Count != other.Devices.Count)
70
+ {
71
+ return false;
72
+ }
73
+ for (var i = 0; i < Devices.Count; i++)
74
+ {
75
+ if (Devices[i] != other.Devices[i])
76
+ {
77
+ return false;
78
+ }
79
+ }
80
+ return true;
81
+ }
82
+
83
+ public override int GetHashCode()
84
+ {
85
+ var hash = new HashCode();
86
+ hash.Add(Recoveries);
87
+ foreach (var state in Devices)
88
+ {
89
+ hash.Add(state);
90
+ }
91
+ return hash.ToHashCode();
92
+ }
93
+
94
+ public static bool operator ==(LinkStatus? left, LinkStatus? right) =>
95
+ left is null ? right is null : left.Equals(right);
96
+
97
+ public static bool operator !=(LinkStatus? left, LinkStatus? right) => !(left == right);
98
+ }
99
+
100
+ public sealed class Checker : IDisposable
101
+ {
102
+ private IntPtr _handle;
103
+
104
+ internal Checker(IntPtr handle)
105
+ {
106
+ _handle = handle;
107
+ }
108
+
109
+ public async Task<LinkStatus> CheckAsync()
110
+ {
111
+ var handle = _handle;
112
+ var status = await AsyncOps.InvokeAsync((cb, ud) =>
113
+ NativeClient.autd3_checker_check(handle, cb, ud)).ConfigureAwait(false);
114
+ try
115
+ {
116
+ var count = (int)NativeClient.autd3_link_status_num_devices(status);
117
+ var devices = new List<DeviceState>(count);
118
+ for (var i = 0; i < count; i++)
119
+ {
120
+ if (!NativeClient.autd3_link_status_device_state(status, (UIntPtr)i, out var kind, out var bits))
121
+ {
122
+ throw new Autd3Exception("failed to read device state");
123
+ }
124
+ devices.Add(DeviceState.FromNative(kind, bits));
125
+ }
126
+ return new LinkStatus(devices, NativeClient.autd3_link_status_recoveries(status));
127
+ }
128
+ finally
129
+ {
130
+ NativeClient.autd3_link_status_free(status);
131
+ }
132
+ }
133
+
134
+ public void Dispose()
135
+ {
136
+ if (_handle != IntPtr.Zero)
137
+ {
138
+ NativeClient.autd3_checker_free(_handle);
139
+ _handle = IntPtr.Zero;
140
+ }
141
+ GC.SuppressFinalize(this);
142
+ }
143
+
144
+ ~Checker()
145
+ {
146
+ if (_handle != IntPtr.Zero)
147
+ {
148
+ NativeClient.autd3_checker_free(_handle);
149
+ }
150
+ }
151
+ }
152
+
153
+ public sealed class Response
154
+ {
155
+ private readonly byte[] _data;
156
+
157
+ internal Response(byte[] data)
158
+ {
159
+ _data = data;
160
+ }
161
+
162
+ public IReadOnlyList<byte> Data => _data;
163
+
164
+ public void Check()
165
+ {
166
+ var err = new byte[256];
167
+ if (!NativeClient.autd3_response_check(_data, (UIntPtr)_data.Length, err, (UIntPtr)err.Length))
168
+ {
169
+ throw new Autd3Exception(NativeUtil.Utf8(err));
170
+ }
171
+ }
172
+ }
173
+
174
+ public sealed class ResponseToken : IDisposable
175
+ {
176
+ private IntPtr _handle;
177
+
178
+ internal ResponseToken(IntPtr handle)
179
+ {
180
+ _handle = handle;
181
+ }
182
+
183
+ public async Task<Response> AwaitAsync()
184
+ {
185
+ var handle = _handle;
186
+ _handle = IntPtr.Zero;
187
+ if (handle == IntPtr.Zero)
188
+ {
189
+ throw new Autd3Exception("ResponseToken has already been awaited");
190
+ }
191
+ GC.SuppressFinalize(this);
192
+ var data = await Client.ReadByteArrayAsync((cb, ud) =>
193
+ NativeClient.autd3_response_token_await(handle, cb, ud)).ConfigureAwait(false);
194
+ return new Response(data);
195
+ }
196
+
197
+ public TaskAwaiter<Response> GetAwaiter() => AwaitAsync().GetAwaiter();
198
+
199
+ public void Dispose()
200
+ {
201
+ if (_handle != IntPtr.Zero)
202
+ {
203
+ NativeClient.autd3_response_token_free(_handle);
204
+ _handle = IntPtr.Zero;
205
+ }
206
+ GC.SuppressFinalize(this);
207
+ }
208
+
209
+ ~ResponseToken()
210
+ {
211
+ if (_handle != IntPtr.Zero)
212
+ {
213
+ NativeClient.autd3_response_token_free(_handle);
214
+ }
215
+ }
216
+ }
217
+
218
+ public sealed class Client : IDisposable
219
+ {
220
+ public const int MaxInflight = 127;
221
+
222
+ internal IntPtr Handle { get; private set; }
223
+
224
+ private Client(IntPtr handle)
225
+ {
226
+ Handle = handle;
227
+ }
228
+
229
+ public static async Task<Client> OpenAsync(Geometry geometry, ILink link, ClientConfig config)
230
+ {
231
+ var opener = link.TakeOpener();
232
+
233
+
234
+ var configHandle = config.CreateHandle();
235
+ Task<IntPtr> task;
236
+ try
237
+ {
238
+ task = AsyncOps.InvokeAsync((cb, ud) =>
239
+ NativeClient.autd3_client_open(geometry.Handle, opener, configHandle, cb, ud));
240
+ }
241
+ finally
242
+ {
243
+ NativeClient.autd3_client_config_free(configHandle);
244
+ }
245
+ var value = await task.ConfigureAwait(false);
246
+ return new Client(value);
247
+ }
248
+
249
+ public static async Task<(Client Client, Checker Checker)> OpenWithCheckerAsync(Geometry geometry, ILink link, ClientConfig config)
250
+ {
251
+ var client = await OpenAsync(geometry, link, config).ConfigureAwait(false);
252
+ var checker = NativeClient.autd3_client_checker(client.Handle);
253
+ if (checker == IntPtr.Zero)
254
+ {
255
+ client.Dispose();
256
+ throw new Autd3Exception("failed to create checker");
257
+ }
258
+ return (client, new Checker(checker));
259
+ }
260
+
261
+ public int NumDevices => (int)NativeClient.autd3_client_num_devices(Handle);
262
+
263
+
264
+ public DatagramBuilder DatagramBuilder() => new DatagramBuilder(NumDevices);
265
+
266
+
267
+
268
+ public Task SendCheckedAsync(Frame frame) =>
269
+ AsyncOps.InvokeAsync((cb, ud) =>
270
+ NativeClient.autd3_client_send_checked(Handle, frame.Frames.Handle, frame.Index, cb, ud));
271
+
272
+ public async Task<ResponseToken> SendAsync(Frame frame)
273
+ {
274
+ var token = await AsyncOps.InvokeAsync((cb, ud) =>
275
+ NativeClient.autd3_client_send(Handle, frame.Frames.Handle, frame.Index, cb, ud)).ConfigureAwait(false);
276
+ return new ResponseToken(token);
277
+ }
278
+
279
+ public async Task<IReadOnlyList<string>> ReadFirmwareVersionAsync()
280
+ {
281
+ var array = await AsyncOps.InvokeAsync((cb, ud) =>
282
+ NativeClient.autd3_client_read_firmware_version(Handle, cb, ud)).ConfigureAwait(false);
283
+ try
284
+ {
285
+ var count = (int)NativeClient.autd3_string_array_len(array);
286
+ var versions = new List<string>(count);
287
+ for (var i = 0; i < count; i++)
288
+ {
289
+ versions.Add(NativeUtil.PtrToString(NativeClient.autd3_string_array_get(array, (UIntPtr)i)));
290
+ }
291
+ return versions;
292
+ }
293
+ finally
294
+ {
295
+ NativeClient.autd3_string_array_free(array);
296
+ }
297
+ }
298
+
299
+ public async Task<IReadOnlyList<FpgaState>> ReadFpgaStateAsync()
300
+ {
301
+ var bytes = await ReadByteArrayAsync((cb, ud) =>
302
+ NativeClient.autd3_client_read_fpga_state(Handle, cb, ud)).ConfigureAwait(false);
303
+ var states = new FpgaState[bytes.Length];
304
+ for (var i = 0; i < bytes.Length; i++)
305
+ {
306
+ states[i] = new FpgaState(bytes[i]);
307
+ }
308
+ return states;
309
+ }
310
+
311
+ public Task<byte[]> ReadErrorDetailAsync() =>
312
+ ReadByteArrayAsync((cb, ud) => NativeClient.autd3_client_read_error_detail(Handle, cb, ud));
313
+
314
+ internal static async Task<byte[]> ReadByteArrayAsync(Action<CompletionCallback, IntPtr> invoke)
315
+ {
316
+ var array = await AsyncOps.InvokeAsync(invoke).ConfigureAwait(false);
317
+ try
318
+ {
319
+ var len = (int)NativeClient.autd3_byte_array_len(array);
320
+ var bytes = new byte[len];
321
+ if (len > 0)
322
+ {
323
+ Marshal.Copy(NativeClient.autd3_byte_array_data(array), bytes, 0, len);
324
+ }
325
+ return bytes;
326
+ }
327
+ finally
328
+ {
329
+ NativeClient.autd3_byte_array_free(array);
330
+ }
331
+ }
332
+
333
+ public Task StopAsync() =>
334
+ AsyncOps.InvokeAsync((cb, ud) => NativeClient.autd3_client_stop(Handle, cb, ud));
335
+
336
+ public Task CloseAsync() =>
337
+ AsyncOps.InvokeAsync((cb, ud) => NativeClient.autd3_client_close(Handle, cb, ud));
338
+
339
+ public void Dispose()
340
+ {
341
+ if (Handle != IntPtr.Zero)
342
+ {
343
+ NativeClient.autd3_client_free(Handle);
344
+ Handle = IntPtr.Zero;
345
+ }
346
+ GC.SuppressFinalize(this);
347
+ }
348
+
349
+ ~Client()
350
+ {
351
+ if (Handle != IntPtr.Zero)
352
+ {
353
+ NativeClient.autd3_client_free(Handle);
354
+ }
355
+ }
356
+ }
357
+ }
package/Client.cs.meta ADDED
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: a92cc14a8c3165fe58e434e42dfdd33b
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant: