com.shinolab.autd3-sdk 0.4.0 → 0.6.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/Client.cs CHANGED
@@ -1,4 +1,5 @@
1
1
  using System;
2
+ using System.Threading;
2
3
  using System.Collections.Generic;
3
4
  using System.Runtime.CompilerServices;
4
5
  using System.Runtime.InteropServices;
@@ -136,19 +137,20 @@ namespace AUTD3
136
137
 
137
138
  public void Dispose()
138
139
  {
139
- if (_handle != IntPtr.Zero)
140
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
141
+ if (handle != IntPtr.Zero)
140
142
  {
141
- NativeClient.autd3_checker_free(_handle);
142
- _handle = IntPtr.Zero;
143
+ NativeClient.autd3_checker_free(handle);
143
144
  }
144
145
  GC.SuppressFinalize(this);
145
146
  }
146
147
 
147
148
  ~Checker()
148
149
  {
149
- if (_handle != IntPtr.Zero)
150
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
151
+ if (handle != IntPtr.Zero)
150
152
  {
151
- NativeClient.autd3_checker_free(_handle);
153
+ NativeClient.autd3_checker_free(handle);
152
154
  }
153
155
  }
154
156
  }
@@ -166,7 +168,7 @@ namespace AUTD3
166
168
 
167
169
  public void Check()
168
170
  {
169
- var err = new byte[256];
171
+ var err = new byte[NativeAbi.ErrorBufferLength];
170
172
  if (!NativeClient.autd3_response_check(_data, (UIntPtr)_data.Length, err, (UIntPtr)err.Length))
171
173
  {
172
174
  throw new Autd3Exception(NativeUtil.Utf8(err));
@@ -185,8 +187,7 @@ namespace AUTD3
185
187
 
186
188
  public async Task<Response> AwaitAsync()
187
189
  {
188
- var handle = _handle;
189
- _handle = IntPtr.Zero;
190
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
190
191
  if (handle == IntPtr.Zero)
191
192
  {
192
193
  throw new Autd3Exception("ResponseToken has already been awaited");
@@ -201,19 +202,20 @@ namespace AUTD3
201
202
 
202
203
  public void Dispose()
203
204
  {
204
- if (_handle != IntPtr.Zero)
205
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
206
+ if (handle != IntPtr.Zero)
205
207
  {
206
- NativeClient.autd3_response_token_free(_handle);
207
- _handle = IntPtr.Zero;
208
+ NativeClient.autd3_response_token_free(handle);
208
209
  }
209
210
  GC.SuppressFinalize(this);
210
211
  }
211
212
 
212
213
  ~ResponseToken()
213
214
  {
214
- if (_handle != IntPtr.Zero)
215
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
216
+ if (handle != IntPtr.Zero)
215
217
  {
216
- NativeClient.autd3_response_token_free(_handle);
218
+ NativeClient.autd3_response_token_free(handle);
217
219
  }
218
220
  }
219
221
  }
@@ -222,13 +224,15 @@ namespace AUTD3
222
224
  {
223
225
  public const int MaxInflight = 127;
224
226
 
225
- internal IntPtr Handle { get; private set; }
227
+ private IntPtr _handle;
228
+
229
+ internal IntPtr Handle => _handle;
226
230
 
227
231
  private readonly Geometry _geometry;
228
232
 
229
233
  private Client(IntPtr handle, Geometry geometry)
230
234
  {
231
- Handle = handle;
235
+ _handle = handle;
232
236
  _geometry = geometry;
233
237
  }
234
238
 
@@ -236,8 +240,17 @@ namespace AUTD3
236
240
  {
237
241
  var opener = link.TakeOpener();
238
242
 
243
+ IntPtr configHandle;
244
+ try
245
+ {
246
+ configHandle = config.CreateHandle();
247
+ }
248
+ catch
249
+ {
250
+ NativeClient.autd3_client_opener_free(opener);
251
+ throw;
252
+ }
239
253
 
240
- var configHandle = config.CreateHandle();
241
254
  Task<IntPtr> task;
242
255
  try
243
256
  {
@@ -267,7 +280,7 @@ namespace AUTD3
267
280
  public int NumDevices => (int)NativeClient.autd3_client_num_devices(Handle);
268
281
 
269
282
 
270
- public DatagramBuilder DatagramBuilder() => new DatagramBuilder(_geometry);
283
+ public DatagramBuilder DatagramBuilder() => new DatagramBuilder(_geometry, Handle);
271
284
 
272
285
 
273
286
 
@@ -347,19 +360,20 @@ namespace AUTD3
347
360
 
348
361
  public void Dispose()
349
362
  {
350
- if (Handle != IntPtr.Zero)
363
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
364
+ if (handle != IntPtr.Zero)
351
365
  {
352
- NativeClient.autd3_client_free(Handle);
353
- Handle = IntPtr.Zero;
366
+ NativeClient.autd3_client_free(handle);
354
367
  }
355
368
  GC.SuppressFinalize(this);
356
369
  }
357
370
 
358
371
  ~Client()
359
372
  {
360
- if (Handle != IntPtr.Zero)
373
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
374
+ if (handle != IntPtr.Zero)
361
375
  {
362
- NativeClient.autd3_client_free(Handle);
376
+ NativeClient.autd3_client_free(handle);
363
377
  }
364
378
  }
365
379
  }
package/Commands.cs CHANGED
@@ -14,6 +14,11 @@ namespace AUTD3
14
14
  {
15
15
  private const string Lib = "autd3capi";
16
16
 
17
+ static NativeCommand() => NativeAbi.Verify(Lib, autd3_abi_version());
18
+
19
+ [DllImport(Lib)]
20
+ private static extern uint autd3_abi_version();
21
+
17
22
  [DllImport(Lib)]
18
23
  internal static extern IntPtr autd3_op_clear();
19
24
 
package/Datagram.cs CHANGED
@@ -1,4 +1,5 @@
1
1
  using System;
2
+ using System.Threading;
2
3
  using System.Collections;
3
4
  using System.Collections.Generic;
4
5
 
@@ -6,6 +7,13 @@ namespace AUTD3
6
7
  {
7
8
 
8
9
 
10
+ public enum RtSchedulePolicy : byte
11
+ {
12
+ Normal = 0,
13
+ Fifo = 1,
14
+ RoundRobin = 2,
15
+ }
16
+
9
17
  public readonly struct ClientConfig
10
18
  {
11
19
  public bool LowLatency { get; }
@@ -14,8 +22,11 @@ namespace AUTD3
14
22
  public uint MaxResyncRounds { get; }
15
23
  public uint ResetResendCycles { get; }
16
24
  public byte? RtPriority { get; }
25
+ public bool DisableRtPriority { get; }
26
+ public RtSchedulePolicy RtPolicy { get; }
17
27
  public ulong? RtAffinity { get; }
18
28
  public bool ValidateState { get; }
29
+ public bool RequireSupportedFirmware { get; }
19
30
 
20
31
  public ClientConfig() : this(lowLatency: false)
21
32
  {
@@ -28,52 +39,100 @@ namespace AUTD3
28
39
  uint maxResyncRounds = 8,
29
40
  uint resetResendCycles = 2,
30
41
  byte? rtPriority = null,
42
+ bool disableRtPriority = false,
43
+ RtSchedulePolicy rtPolicy = RtSchedulePolicy.Fifo,
31
44
  ulong? rtAffinity = null,
32
- bool validateState = true)
45
+ bool validateState = true,
46
+ bool requireSupportedFirmware = false)
33
47
  {
48
+ if (rtPriority.HasValue && disableRtPriority)
49
+ {
50
+ throw new ArgumentException("rtPriority and disableRtPriority are mutually exclusive");
51
+ }
34
52
  LowLatency = lowLatency;
35
53
  TimeoutCycles = timeoutCycles;
36
54
  MaxInflight = maxInflight;
37
55
  MaxResyncRounds = maxResyncRounds;
38
56
  ResetResendCycles = resetResendCycles;
39
57
  RtPriority = rtPriority;
58
+ DisableRtPriority = disableRtPriority;
59
+ RtPolicy = rtPolicy;
40
60
  RtAffinity = rtAffinity;
41
61
  ValidateState = validateState;
62
+ RequireSupportedFirmware = requireSupportedFirmware;
42
63
  }
43
64
 
44
65
  internal IntPtr CreateHandle()
45
66
  {
46
- var handle = NativeClient.autd3_client_config_new(
47
- LowLatency,
48
- TimeoutCycles,
49
- (UIntPtr)MaxInflight,
50
- MaxResyncRounds,
51
- ResetResendCycles,
52
- RtPriority.HasValue,
53
- RtPriority ?? 0,
54
- RtAffinity.HasValue,
55
- (UIntPtr)(RtAffinity ?? 0),
56
- ValidateState);
67
+ var handle = NativeClient.autd3_client_config_new();
57
68
  if (handle == IntPtr.Zero)
58
69
  {
59
70
  throw new Autd3Exception("failed to create client config");
60
71
  }
72
+ try
73
+ {
74
+ NativeConfig.Apply("lowLatency", NativeClient.autd3_client_config_set_low_latency(handle, LowLatency));
75
+ NativeConfig.Apply("timeoutCycles", NativeClient.autd3_client_config_set_timeout_cycles(handle, TimeoutCycles));
76
+ NativeConfig.Apply("maxInflight", NativeClient.autd3_client_config_set_max_inflight(handle, (UIntPtr)MaxInflight));
77
+ NativeConfig.Apply("maxResyncRounds", NativeClient.autd3_client_config_set_max_resync_rounds(handle, MaxResyncRounds));
78
+ NativeConfig.Apply("resetResendCycles", NativeClient.autd3_client_config_set_reset_resend_cycles(handle, ResetResendCycles));
79
+ NativeConfig.Apply("rtPriority", NativeClient.autd3_client_config_set_rt_priority(handle, NativeConfig.RtPriorityMode(RtPriority, DisableRtPriority), RtPriority ?? 0));
80
+ NativeConfig.Apply("rtPolicy", NativeClient.autd3_client_config_set_rt_policy(handle, (byte)RtPolicy));
81
+ NativeConfig.Apply("rtAffinity", NativeClient.autd3_client_config_set_rt_affinity(handle, RtAffinity.HasValue, (UIntPtr)(RtAffinity ?? 0)));
82
+ NativeConfig.Apply("validateState", NativeClient.autd3_client_config_set_validate_state(handle, ValidateState));
83
+ NativeConfig.Apply("requireSupportedFirmware", NativeClient.autd3_client_config_set_require_supported_firmware(handle, RequireSupportedFirmware));
84
+ }
85
+ catch
86
+ {
87
+ NativeClient.autd3_client_config_free(handle);
88
+ throw;
89
+ }
61
90
  return handle;
62
91
  }
63
92
  }
64
93
 
94
+ internal static class NativeConfig
95
+ {
96
+ internal const byte RtPriorityModeDefault = 0;
97
+ internal const byte RtPriorityModeDisabled = 1;
98
+ internal const byte RtPriorityModeExplicit = 2;
99
+
100
+ internal static byte RtPriorityMode(byte? rtPriority, bool disabled) =>
101
+ rtPriority.HasValue
102
+ ? RtPriorityModeExplicit
103
+ : disabled
104
+ ? RtPriorityModeDisabled
105
+ : RtPriorityModeDefault;
106
+
107
+ internal static void Apply(string field, int code)
108
+ {
109
+ if (code != 0)
110
+ {
111
+ throw new Autd3Exception($"`{field}` is out of the range the native library accepts");
112
+ }
113
+ }
114
+ }
115
+
65
116
  public sealed class DatagramBuilder : IDisposable
66
117
  {
67
118
  private readonly Geometry _geometry;
68
119
  private readonly int _numDevices;
120
+ private readonly IntPtr _client;
69
121
 
70
- internal IntPtr Handle { get; private set; }
122
+ private IntPtr _handle;
71
123
 
72
- public DatagramBuilder(Geometry geometry)
124
+ internal IntPtr Handle => _handle;
125
+
126
+ public DatagramBuilder(Geometry geometry) : this(geometry, IntPtr.Zero)
127
+ {
128
+ }
129
+
130
+ internal DatagramBuilder(Geometry geometry, IntPtr client)
73
131
  {
74
132
  _geometry = geometry;
75
133
  _numDevices = geometry.NumDevices;
76
- Handle = NativeClient.autd3_datagram_builder_new(geometry.Handle);
134
+ _client = client;
135
+ _handle = NativeClient.autd3_datagram_builder_new(geometry.Handle);
77
136
  if (Handle == IntPtr.Zero)
78
137
  {
79
138
  throw new Autd3Exception("failed to create datagram builder");
@@ -83,7 +142,10 @@ namespace AUTD3
83
142
  public DatagramBuilder Push(ICommand command)
84
143
  {
85
144
  var op = command.CreateOp();
86
- NativeClient.autd3_datagram_builder_push(Handle, op);
145
+ if (NativeClient.autd3_datagram_builder_push(Handle, op) != 0)
146
+ {
147
+ throw new Autd3Exception("failed to push the command onto the datagram builder");
148
+ }
87
149
  return this;
88
150
  }
89
151
 
@@ -109,14 +171,17 @@ namespace AUTD3
109
171
  }
110
172
  throw;
111
173
  }
112
- NativeClient.autd3_datagram_builder_push_each(Handle, ops, (UIntPtr)_numDevices);
174
+ if (NativeClient.autd3_datagram_builder_push_each(Handle, ops, (UIntPtr)_numDevices) != 0)
175
+ {
176
+ throw new Autd3Exception("failed to push the per-device commands onto the datagram builder");
177
+ }
113
178
  return this;
114
179
  }
115
180
 
116
181
  public Frames Build()
117
182
  {
118
- var err = new byte[256];
119
- var handle = NativeClient.autd3_datagram_builder_build(Handle, err, (UIntPtr)err.Length);
183
+ var err = new byte[NativeAbi.ErrorBufferLength];
184
+ var handle = NativeClient.autd3_datagram_builder_build(Handle, _client, err, (UIntPtr)err.Length);
120
185
  if (handle == IntPtr.Zero)
121
186
  {
122
187
  throw new Autd3Exception(NativeUtil.Utf8(err));
@@ -126,19 +191,20 @@ namespace AUTD3
126
191
 
127
192
  public void Dispose()
128
193
  {
129
- if (Handle != IntPtr.Zero)
194
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
195
+ if (handle != IntPtr.Zero)
130
196
  {
131
- NativeClient.autd3_datagram_builder_free(Handle);
132
- Handle = IntPtr.Zero;
197
+ NativeClient.autd3_datagram_builder_free(handle);
133
198
  }
134
199
  GC.SuppressFinalize(this);
135
200
  }
136
201
 
137
202
  ~DatagramBuilder()
138
203
  {
139
- if (Handle != IntPtr.Zero)
204
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
205
+ if (handle != IntPtr.Zero)
140
206
  {
141
- NativeClient.autd3_datagram_builder_free(Handle);
207
+ NativeClient.autd3_datagram_builder_free(handle);
142
208
  }
143
209
  }
144
210
  }
@@ -158,11 +224,13 @@ namespace AUTD3
158
224
 
159
225
  public sealed class Frames : IDisposable, IEnumerable<Frame>
160
226
  {
161
- internal IntPtr Handle { get; private set; }
227
+ private IntPtr _handle;
228
+
229
+ internal IntPtr Handle => _handle;
162
230
 
163
231
  internal Frames(IntPtr handle)
164
232
  {
165
- Handle = handle;
233
+ _handle = handle;
166
234
  }
167
235
 
168
236
  public int Length => (int)NativeClient.autd3_datagrams_num_frames(Handle);
@@ -192,19 +260,20 @@ namespace AUTD3
192
260
 
193
261
  public void Dispose()
194
262
  {
195
- if (Handle != IntPtr.Zero)
263
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
264
+ if (handle != IntPtr.Zero)
196
265
  {
197
- NativeClient.autd3_datagrams_free(Handle);
198
- Handle = IntPtr.Zero;
266
+ NativeClient.autd3_datagrams_free(handle);
199
267
  }
200
268
  GC.SuppressFinalize(this);
201
269
  }
202
270
 
203
271
  ~Frames()
204
272
  {
205
- if (Handle != IntPtr.Zero)
273
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
274
+ if (handle != IntPtr.Zero)
206
275
  {
207
- NativeClient.autd3_datagrams_free(Handle);
276
+ NativeClient.autd3_datagrams_free(handle);
208
277
  }
209
278
  }
210
279
  }
package/Legacy.cs ADDED
@@ -0,0 +1,393 @@
1
+ using System;
2
+ using System.Threading;
3
+ using System.Collections;
4
+ using System.Collections.Generic;
5
+ using System.Threading.Tasks;
6
+
7
+ namespace AUTD3.Legacy
8
+ {
9
+
10
+
11
+ public readonly struct LegacyClientConfig
12
+ {
13
+ public uint TimeoutCycles { get; }
14
+ public byte? RtPriority { get; }
15
+ public bool DisableRtPriority { get; }
16
+ public RtSchedulePolicy RtPolicy { get; }
17
+ public ulong? RtAffinity { get; }
18
+
19
+ public LegacyClientConfig() : this(timeoutCycles: 2000)
20
+ {
21
+ }
22
+
23
+ public LegacyClientConfig(
24
+ uint timeoutCycles = 2000,
25
+ byte? rtPriority = null,
26
+ bool disableRtPriority = false,
27
+ RtSchedulePolicy rtPolicy = RtSchedulePolicy.Fifo,
28
+ ulong? rtAffinity = null)
29
+ {
30
+ if (rtPriority.HasValue && disableRtPriority)
31
+ {
32
+ throw new ArgumentException("rtPriority and disableRtPriority are mutually exclusive");
33
+ }
34
+ TimeoutCycles = timeoutCycles;
35
+ RtPriority = rtPriority;
36
+ DisableRtPriority = disableRtPriority;
37
+ RtPolicy = rtPolicy;
38
+ RtAffinity = rtAffinity;
39
+ }
40
+
41
+ internal IntPtr CreateHandle()
42
+ {
43
+ var handle = NativeLegacyClient.autd3_legacy_client_config_new();
44
+ if (handle == IntPtr.Zero)
45
+ {
46
+ throw new Autd3Exception("failed to create legacy client config");
47
+ }
48
+ try
49
+ {
50
+ NativeConfig.Apply("timeoutCycles", NativeLegacyClient.autd3_legacy_client_config_set_timeout_cycles(handle, TimeoutCycles));
51
+ NativeConfig.Apply("rtPriority", NativeLegacyClient.autd3_legacy_client_config_set_rt_priority(handle, NativeConfig.RtPriorityMode(RtPriority, DisableRtPriority), RtPriority ?? 0));
52
+ NativeConfig.Apply("rtPolicy", NativeLegacyClient.autd3_legacy_client_config_set_rt_policy(handle, (byte)RtPolicy));
53
+ NativeConfig.Apply("rtAffinity", NativeLegacyClient.autd3_legacy_client_config_set_rt_affinity(handle, RtAffinity.HasValue, (UIntPtr)(RtAffinity ?? 0)));
54
+ }
55
+ catch
56
+ {
57
+ NativeLegacyClient.autd3_legacy_client_config_free(handle);
58
+ throw;
59
+ }
60
+ return handle;
61
+ }
62
+ }
63
+
64
+ public interface ILegacyCommand
65
+ {
66
+ internal IntPtr CreateOp();
67
+ }
68
+
69
+ public sealed class LegacyChangePatternBank : ILegacyCommand
70
+ {
71
+ private readonly byte _kind;
72
+ private readonly PatternBank _bank;
73
+ private readonly TransitionMode _transitionMode;
74
+
75
+ private LegacyChangePatternBank(byte kind, PatternBank bank, TransitionMode transitionMode)
76
+ {
77
+ _kind = kind;
78
+ _bank = bank;
79
+ _transitionMode = transitionMode;
80
+ }
81
+
82
+ public static LegacyChangePatternBank Pattern(PatternBank bank) =>
83
+ new LegacyChangePatternBank(0, bank, TransitionMode.Immediate);
84
+
85
+ public static LegacyChangePatternBank FociStm(PatternBank bank, TransitionMode? transitionMode = null) =>
86
+ new LegacyChangePatternBank(1, bank, transitionMode ?? TransitionMode.Immediate);
87
+
88
+ public static LegacyChangePatternBank PatternStm(PatternBank bank, TransitionMode? transitionMode = null) =>
89
+ new LegacyChangePatternBank(2, bank, transitionMode ?? TransitionMode.Immediate);
90
+
91
+ IntPtr ILegacyCommand.CreateOp() =>
92
+ NativeLegacyClient.autd3_legacy_op_change_segment(_kind, (byte)_bank, _transitionMode.Mode, _transitionMode.Value);
93
+ }
94
+
95
+ public sealed class LegacyDatagramBuilder : IDisposable
96
+ {
97
+ private readonly Geometry _geometry;
98
+ private readonly int _numDevices;
99
+ private readonly IntPtr _client;
100
+
101
+ private IntPtr _handle;
102
+
103
+ internal IntPtr Handle => _handle;
104
+
105
+ public LegacyDatagramBuilder(Geometry geometry) : this(geometry, IntPtr.Zero)
106
+ {
107
+ }
108
+
109
+ internal LegacyDatagramBuilder(Geometry geometry, IntPtr client)
110
+ {
111
+ _geometry = geometry;
112
+ _numDevices = geometry.NumDevices;
113
+ _client = client;
114
+ _handle = NativeLegacyClient.autd3_legacy_datagram_builder_new(geometry.Handle);
115
+ if (Handle == IntPtr.Zero)
116
+ {
117
+ throw new Autd3Exception("failed to create legacy datagram builder");
118
+ }
119
+ }
120
+
121
+ public LegacyDatagramBuilder Push(ICommand command)
122
+ {
123
+ var op = command.CreateOp();
124
+ if (NativeLegacyClient.autd3_legacy_datagram_builder_push(Handle, op) != 0)
125
+ {
126
+ throw new Autd3Exception("failed to push the command onto the legacy datagram builder");
127
+ }
128
+ return this;
129
+ }
130
+
131
+ public LegacyDatagramBuilder Push(ILegacyCommand command)
132
+ {
133
+ var op = command.CreateOp();
134
+ if (NativeLegacyClient.autd3_legacy_datagram_builder_push_legacy(Handle, op) != 0)
135
+ {
136
+ throw new Autd3Exception("failed to push the command onto the legacy datagram builder");
137
+ }
138
+ return this;
139
+ }
140
+
141
+ public LegacyDatagramBuilder PushEach(Func<Device, ICommand?> factory)
142
+ {
143
+ var ops = new IntPtr[_numDevices];
144
+ try
145
+ {
146
+ for (var i = 0; i < _numDevices; i++)
147
+ {
148
+ var command = factory(_geometry[i]);
149
+ ops[i] = command == null ? IntPtr.Zero : command.CreateOp();
150
+ }
151
+ }
152
+ catch
153
+ {
154
+ foreach (var op in ops)
155
+ {
156
+ if (op != IntPtr.Zero)
157
+ {
158
+ NativeClient.autd3_op_free(op);
159
+ }
160
+ }
161
+ throw;
162
+ }
163
+ if (NativeLegacyClient.autd3_legacy_datagram_builder_push_each(Handle, ops, (UIntPtr)_numDevices) != 0)
164
+ {
165
+ throw new Autd3Exception("failed to push the per-device commands onto the legacy datagram builder");
166
+ }
167
+ return this;
168
+ }
169
+
170
+ public LegacyFrames Build()
171
+ {
172
+ var err = new byte[NativeAbi.ErrorBufferLength];
173
+ var handle = NativeLegacyClient.autd3_legacy_datagram_builder_build(Handle, _client, err, (UIntPtr)err.Length);
174
+ if (handle == IntPtr.Zero)
175
+ {
176
+ throw new Autd3Exception(NativeUtil.Utf8(err));
177
+ }
178
+ return new LegacyFrames(handle);
179
+ }
180
+
181
+ public void Dispose()
182
+ {
183
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
184
+ if (handle != IntPtr.Zero)
185
+ {
186
+ NativeLegacyClient.autd3_legacy_datagram_builder_free(handle);
187
+ }
188
+ GC.SuppressFinalize(this);
189
+ }
190
+
191
+ ~LegacyDatagramBuilder()
192
+ {
193
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
194
+ if (handle != IntPtr.Zero)
195
+ {
196
+ NativeLegacyClient.autd3_legacy_datagram_builder_free(handle);
197
+ }
198
+ }
199
+ }
200
+
201
+ public readonly struct LegacyFrame
202
+ {
203
+ internal LegacyFrames Frames { get; }
204
+ internal long Index { get; }
205
+
206
+ internal LegacyFrame(LegacyFrames frames, long index)
207
+ {
208
+ Frames = frames;
209
+ Index = index;
210
+ }
211
+ }
212
+
213
+ public sealed class LegacyFrames : IDisposable, IEnumerable<LegacyFrame>
214
+ {
215
+ private IntPtr _handle;
216
+
217
+ internal IntPtr Handle => _handle;
218
+
219
+ internal LegacyFrames(IntPtr handle)
220
+ {
221
+ _handle = handle;
222
+ }
223
+
224
+ public int Length => (int)NativeLegacyClient.autd3_legacy_frames_num_frames(Handle);
225
+
226
+ public LegacyFrame this[int index]
227
+ {
228
+ get
229
+ {
230
+ if (index < 0 || index >= Length)
231
+ {
232
+ throw new ArgumentOutOfRangeException(nameof(index));
233
+ }
234
+ return new LegacyFrame(this, index);
235
+ }
236
+ }
237
+
238
+ public IEnumerator<LegacyFrame> GetEnumerator()
239
+ {
240
+ var count = Length;
241
+ for (long i = 0; i < count; i++)
242
+ {
243
+ yield return new LegacyFrame(this, i);
244
+ }
245
+ }
246
+
247
+ IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
248
+
249
+ public void Dispose()
250
+ {
251
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
252
+ if (handle != IntPtr.Zero)
253
+ {
254
+ NativeLegacyClient.autd3_legacy_frames_free(handle);
255
+ }
256
+ GC.SuppressFinalize(this);
257
+ }
258
+
259
+ ~LegacyFrames()
260
+ {
261
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
262
+ if (handle != IntPtr.Zero)
263
+ {
264
+ NativeLegacyClient.autd3_legacy_frames_free(handle);
265
+ }
266
+ }
267
+ }
268
+
269
+ public sealed class LegacyClient : IDisposable
270
+ {
271
+ private IntPtr _handle;
272
+
273
+ internal IntPtr Handle => _handle;
274
+
275
+ private readonly Geometry _geometry;
276
+
277
+ private LegacyClient(IntPtr handle, Geometry geometry)
278
+ {
279
+ _handle = handle;
280
+ _geometry = geometry;
281
+ }
282
+
283
+ public static async Task<LegacyClient> OpenAsync(Geometry geometry, ILegacyLink link, LegacyClientConfig config)
284
+ {
285
+ var opener = link.TakeLegacyOpener();
286
+
287
+ IntPtr configHandle;
288
+ try
289
+ {
290
+ configHandle = config.CreateHandle();
291
+ }
292
+ catch
293
+ {
294
+ NativeLegacyClient.autd3_legacy_client_opener_free(opener);
295
+ throw;
296
+ }
297
+
298
+ Task<IntPtr> task;
299
+ try
300
+ {
301
+ task = AsyncOps.InvokeAsync((cb, ud) =>
302
+ NativeLegacyClient.autd3_legacy_client_open(geometry.Handle, opener, configHandle, cb, ud));
303
+ }
304
+ finally
305
+ {
306
+ NativeLegacyClient.autd3_legacy_client_config_free(configHandle);
307
+ }
308
+ var value = await task.ConfigureAwait(false);
309
+ return new LegacyClient(value, geometry);
310
+ }
311
+
312
+ public static async Task<(LegacyClient Client, Checker Checker)> OpenWithCheckerAsync(Geometry geometry, ILegacyLink link, LegacyClientConfig config)
313
+ {
314
+ var client = await OpenAsync(geometry, link, config).ConfigureAwait(false);
315
+ var checker = NativeLegacyClient.autd3_legacy_client_checker(client.Handle);
316
+ if (checker == IntPtr.Zero)
317
+ {
318
+ client.Dispose();
319
+ throw new Autd3Exception("failed to create checker");
320
+ }
321
+ return (client, new Checker(checker));
322
+ }
323
+
324
+ public int NumDevices => (int)NativeLegacyClient.autd3_legacy_client_num_devices(Handle);
325
+
326
+ public LegacyDatagramBuilder DatagramBuilder() => new LegacyDatagramBuilder(_geometry, Handle);
327
+
328
+ public Task SendCheckedAsync(LegacyFrame frame) =>
329
+ AsyncOps.InvokeAsync((cb, ud) =>
330
+ NativeLegacyClient.autd3_legacy_client_send_checked(Handle, frame.Frames.Handle, frame.Index, cb, ud));
331
+
332
+ public Task<byte[]> SendAsync(LegacyFrame frame) =>
333
+ Client.ReadByteArrayAsync((cb, ud) =>
334
+ NativeLegacyClient.autd3_legacy_client_send(Handle, frame.Frames.Handle, frame.Index, cb, ud));
335
+
336
+ public async Task<IReadOnlyList<string>> ReadFirmwareVersionAsync()
337
+ {
338
+ var array = await AsyncOps.InvokeAsync((cb, ud) =>
339
+ NativeLegacyClient.autd3_legacy_client_read_firmware_version(Handle, cb, ud)).ConfigureAwait(false);
340
+ try
341
+ {
342
+ var count = (int)NativeClient.autd3_string_array_len(array);
343
+ var versions = new List<string>(count);
344
+ for (var i = 0; i < count; i++)
345
+ {
346
+ versions.Add(NativeUtil.PtrToString(NativeClient.autd3_string_array_get(array, (UIntPtr)i)));
347
+ }
348
+ return versions;
349
+ }
350
+ finally
351
+ {
352
+ NativeClient.autd3_string_array_free(array);
353
+ }
354
+ }
355
+
356
+ public async Task<IReadOnlyList<FpgaState>> ReadFpgaStateAsync()
357
+ {
358
+ var bytes = await Client.ReadByteArrayAsync((cb, ud) =>
359
+ NativeLegacyClient.autd3_legacy_client_read_fpga_state(Handle, cb, ud)).ConfigureAwait(false);
360
+ var states = new FpgaState[bytes.Length];
361
+ for (var i = 0; i < bytes.Length; i++)
362
+ {
363
+ states[i] = new FpgaState(bytes[i]);
364
+ }
365
+ return states;
366
+ }
367
+
368
+ public Task StopAsync() =>
369
+ AsyncOps.InvokeAsync((cb, ud) => NativeLegacyClient.autd3_legacy_client_stop(Handle, cb, ud));
370
+
371
+ public Task CloseAsync() =>
372
+ AsyncOps.InvokeAsync((cb, ud) => NativeLegacyClient.autd3_legacy_client_close(Handle, cb, ud));
373
+
374
+ public void Dispose()
375
+ {
376
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
377
+ if (handle != IntPtr.Zero)
378
+ {
379
+ NativeLegacyClient.autd3_legacy_client_free(handle);
380
+ }
381
+ GC.SuppressFinalize(this);
382
+ }
383
+
384
+ ~LegacyClient()
385
+ {
386
+ var handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
387
+ if (handle != IntPtr.Zero)
388
+ {
389
+ NativeLegacyClient.autd3_legacy_client_free(handle);
390
+ }
391
+ }
392
+ }
393
+ }
package/Legacy.cs.meta ADDED
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: ebced8db974bff3ec7adb23b47c254e3
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
package/Link.cs CHANGED
@@ -9,4 +9,10 @@ namespace AUTD3
9
9
 
10
10
  IntPtr TakeOpener();
11
11
  }
12
+
13
+ public interface ILegacyLink
14
+ {
15
+
16
+ IntPtr TakeLegacyOpener();
17
+ }
12
18
  }
package/Native.cs CHANGED
@@ -12,30 +12,58 @@ namespace AUTD3
12
12
  {
13
13
  private const string Lib = "autd3capi";
14
14
 
15
+ static NativeClient() => NativeAbi.Verify(Lib, autd3_abi_version());
16
+
17
+ [DllImport(Lib)]
18
+ private static extern uint autd3_abi_version();
19
+
20
+ [DllImport(Lib)]
21
+ internal static extern IntPtr autd3_client_config_new();
22
+
23
+ [DllImport(Lib)]
24
+ internal static extern int autd3_client_config_set_low_latency(IntPtr config, [MarshalAs(UnmanagedType.I1)] bool value);
25
+
26
+ [DllImport(Lib)]
27
+ internal static extern int autd3_client_config_set_validate_state(IntPtr config, [MarshalAs(UnmanagedType.I1)] bool value);
28
+
29
+ [DllImport(Lib)]
30
+ internal static extern int autd3_client_config_set_require_supported_firmware(IntPtr config, [MarshalAs(UnmanagedType.I1)] bool value);
31
+
32
+ [DllImport(Lib)]
33
+ internal static extern int autd3_client_config_set_timeout_cycles(IntPtr config, uint value);
34
+
35
+ [DllImport(Lib)]
36
+ internal static extern int autd3_client_config_set_max_inflight(IntPtr config, UIntPtr value);
37
+
38
+ [DllImport(Lib)]
39
+ internal static extern int autd3_client_config_set_max_resync_rounds(IntPtr config, uint value);
40
+
41
+ [DllImport(Lib)]
42
+ internal static extern int autd3_client_config_set_reset_resend_cycles(IntPtr config, uint value);
43
+
44
+ [DllImport(Lib)]
45
+ internal static extern int autd3_client_config_set_rt_priority(IntPtr config, byte mode, byte value);
46
+
15
47
  [DllImport(Lib)]
16
- internal static extern IntPtr autd3_client_config_new(
17
- [MarshalAs(UnmanagedType.I1)] bool lowLatency,
18
- uint timeoutCycles,
19
- UIntPtr maxInflight,
20
- uint maxResyncRounds,
21
- uint resetResendCycles,
22
- [MarshalAs(UnmanagedType.I1)] bool hasRtPriority,
23
- byte rtPriority,
24
- [MarshalAs(UnmanagedType.I1)] bool hasRtAffinity,
25
- UIntPtr rtAffinity,
26
- [MarshalAs(UnmanagedType.I1)] bool validateState);
48
+ internal static extern int autd3_client_config_set_rt_policy(IntPtr config, byte value);
49
+
50
+ [DllImport(Lib)]
51
+ internal static extern int autd3_client_config_set_rt_affinity(IntPtr config, [MarshalAs(UnmanagedType.I1)] bool hasAffinity, UIntPtr coreId);
27
52
 
28
53
  [DllImport(Lib)]
29
54
  internal static extern void autd3_client_config_free(IntPtr config);
30
55
 
56
+ [DllImport(Lib)]
57
+ internal static extern void autd3_client_opener_free(IntPtr opener);
58
+
31
59
  [DllImport(Lib)]
32
60
  internal static extern IntPtr autd3_datagram_builder_new(IntPtr geometry);
33
61
 
34
62
  [DllImport(Lib)]
35
- internal static extern void autd3_datagram_builder_push(IntPtr builder, IntPtr op);
63
+ internal static extern int autd3_datagram_builder_push(IntPtr builder, IntPtr op);
36
64
 
37
65
  [DllImport(Lib)]
38
- internal static extern void autd3_datagram_builder_push_each(IntPtr builder, IntPtr[] ops, UIntPtr numDevices);
66
+ internal static extern int autd3_datagram_builder_push_each(IntPtr builder, IntPtr[] ops, UIntPtr numDevices);
39
67
 
40
68
  [DllImport(Lib)]
41
69
  internal static extern void autd3_op_free(IntPtr op);
@@ -44,7 +72,7 @@ namespace AUTD3
44
72
  internal static extern void autd3_datagram_builder_free(IntPtr builder);
45
73
 
46
74
  [DllImport(Lib)]
47
- internal static extern IntPtr autd3_datagram_builder_build(IntPtr builder, byte[] outErr, UIntPtr outErrLen);
75
+ internal static extern IntPtr autd3_datagram_builder_build(IntPtr builder, IntPtr client, byte[] outErr, UIntPtr outErrLen);
48
76
 
49
77
  [DllImport(Lib)]
50
78
  internal static extern UIntPtr autd3_datagrams_num_frames(IntPtr datagrams);
@@ -135,4 +163,90 @@ namespace AUTD3
135
163
  [DllImport(Lib)]
136
164
  internal static extern void autd3_link_status_free(IntPtr status);
137
165
  }
166
+
167
+ internal static class NativeLegacyClient
168
+ {
169
+ private const string Lib = "autd3capi";
170
+
171
+ [DllImport(Lib)]
172
+ internal static extern IntPtr autd3_legacy_client_config_new();
173
+
174
+ [DllImport(Lib)]
175
+ internal static extern int autd3_legacy_client_config_set_timeout_cycles(IntPtr config, uint value);
176
+
177
+ [DllImport(Lib)]
178
+ internal static extern int autd3_legacy_client_config_set_rt_priority(IntPtr config, byte mode, byte value);
179
+
180
+ [DllImport(Lib)]
181
+ internal static extern int autd3_legacy_client_config_set_rt_policy(IntPtr config, byte value);
182
+
183
+ [DllImport(Lib)]
184
+ internal static extern int autd3_legacy_client_config_set_rt_affinity(IntPtr config, [MarshalAs(UnmanagedType.I1)] bool hasAffinity, UIntPtr coreId);
185
+
186
+ [DllImport(Lib)]
187
+ internal static extern void autd3_legacy_client_config_free(IntPtr config);
188
+
189
+ [DllImport(Lib)]
190
+ internal static extern void autd3_legacy_client_opener_free(IntPtr opener);
191
+
192
+ [DllImport(Lib)]
193
+ internal static extern IntPtr autd3_legacy_datagram_builder_new(IntPtr geometry);
194
+
195
+ [DllImport(Lib)]
196
+ internal static extern int autd3_legacy_datagram_builder_push(IntPtr builder, IntPtr op);
197
+
198
+ [DllImport(Lib)]
199
+ internal static extern IntPtr autd3_legacy_op_change_segment(byte kind, byte bank, byte transitionMode, ulong transitionValue);
200
+
201
+ [DllImport(Lib)]
202
+ internal static extern void autd3_legacy_op_free(IntPtr op);
203
+
204
+ [DllImport(Lib)]
205
+ internal static extern int autd3_legacy_datagram_builder_push_legacy(IntPtr builder, IntPtr op);
206
+
207
+ [DllImport(Lib)]
208
+ internal static extern int autd3_legacy_datagram_builder_push_each(IntPtr builder, IntPtr[] ops, UIntPtr numDevices);
209
+
210
+ [DllImport(Lib)]
211
+ internal static extern void autd3_legacy_datagram_builder_free(IntPtr builder);
212
+
213
+ [DllImport(Lib)]
214
+ internal static extern IntPtr autd3_legacy_datagram_builder_build(IntPtr builder, IntPtr client, byte[] outErr, UIntPtr outErrLen);
215
+
216
+ [DllImport(Lib)]
217
+ internal static extern UIntPtr autd3_legacy_frames_num_frames(IntPtr frames);
218
+
219
+ [DllImport(Lib)]
220
+ internal static extern void autd3_legacy_frames_free(IntPtr frames);
221
+
222
+ [DllImport(Lib)]
223
+ internal static extern void autd3_legacy_client_open(IntPtr geometry, IntPtr link, IntPtr config, CompletionCallback cb, IntPtr userData);
224
+
225
+ [DllImport(Lib)]
226
+ internal static extern UIntPtr autd3_legacy_client_num_devices(IntPtr client);
227
+
228
+ [DllImport(Lib)]
229
+ internal static extern void autd3_legacy_client_send(IntPtr client, IntPtr frames, long frame, CompletionCallback cb, IntPtr userData);
230
+
231
+ [DllImport(Lib)]
232
+ internal static extern void autd3_legacy_client_send_checked(IntPtr client, IntPtr frames, long frame, CompletionCallback cb, IntPtr userData);
233
+
234
+ [DllImport(Lib)]
235
+ internal static extern void autd3_legacy_client_read_firmware_version(IntPtr client, CompletionCallback cb, IntPtr userData);
236
+
237
+ [DllImport(Lib)]
238
+ internal static extern void autd3_legacy_client_read_fpga_state(IntPtr client, CompletionCallback cb, IntPtr userData);
239
+
240
+ [DllImport(Lib)]
241
+ internal static extern IntPtr autd3_legacy_client_checker(IntPtr client);
242
+
243
+ [DllImport(Lib)]
244
+ internal static extern void autd3_legacy_client_stop(IntPtr client, CompletionCallback cb, IntPtr userData);
245
+
246
+ [DllImport(Lib)]
247
+ internal static extern void autd3_legacy_client_close(IntPtr client, CompletionCallback cb, IntPtr userData);
248
+
249
+ [DllImport(Lib)]
250
+ internal static extern void autd3_legacy_client_free(IntPtr client);
251
+ }
138
252
  }
Binary file
Binary file
package/README.md CHANGED
@@ -2,41 +2,7 @@
2
2
 
3
3
  The AUTD3 client: opening a link, sending datagrams and driving STM.
4
4
 
5
- ## Install
6
-
7
- Add the scoped registry and the package to `Packages/manifest.json`; sibling
8
- AUTD3 packages are resolved from the registry automatically.
9
-
10
- ```json
11
- {
12
- "scopedRegistries": [
13
- { "name": "npmjs", "url": "https://registry.npmjs.org", "scopes": ["com.shinolab"] }
14
- ],
15
- "dependencies": {
16
- "com.shinolab.autd3-sdk": "0.1.0"
17
- }
18
- }
19
- ```
20
-
21
- Packages served from `registry.npmjs.org` do **not** appear in the Package Manager's
22
- *My Registries* tab, because npmjs does not implement the legacy `/-/all` route that
23
- Unity's package listing relies on. Resolution is unaffected: edit the manifest as above,
24
- or use **+ → Add package by name**.
25
-
26
- The bundled `Focus Sine` sample additionally needs `com.shinolab.autd3-sdk.link.nop`.
27
-
28
- ## Platforms
29
-
30
- Editor and Standalone builds on Windows (`win-x64`), Linux (`linux-x64`) and Apple
31
- Silicon macOS (`osx-arm64`). Intel macOS is not shipped.
32
-
33
- ## Notes
34
-
35
- Minimum Unity version is 6 (6000.x). The API speaks Unity's left-handed, metre frame;
36
- conversion to the AUTD3 canonical frame happens at the binding boundary.
37
-
38
- See the [Unity binding README](https://github.com/shinolab/autd3-sdk/blob/main/bindings/unity/README.md) for the
39
- coordinate system, the full package list and local development with `file:` references.
5
+ See the [Unity binding README](https://github.com/shinolab/autd3-sdk/blob/main/bindings/unity/README.md) for more details.
40
6
 
41
7
  ## License
42
8
 
package/Stm.cs CHANGED
@@ -25,6 +25,11 @@ namespace AUTD3
25
25
  {
26
26
  private const string Lib = "autd3capi";
27
27
 
28
+ static NativeStm() => NativeAbi.Verify(Lib, autd3_abi_version());
29
+
30
+ [DllImport(Lib)]
31
+ private static extern uint autd3_abi_version();
32
+
28
33
  [DllImport(Lib)]
29
34
  internal static extern IntPtr autd3_stm_config_freq(float hz);
30
35
 
@@ -886,11 +886,41 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
886
886
 
887
887
  Used by:
888
888
 
889
- - autd3-ffi 0.4.0 — https://github.com/shinolab/autd3-sdk
890
- - autd3-ffi-abi 0.4.0 — https://github.com/shinolab/autd3-sdk
891
- - autd3-rs 0.4.0 — https://github.com/shinolab/autd3-sdk
892
- - autd3-rs-core 0.4.0 — https://github.com/shinolab/autd3-sdk
893
- - autd3-cpu-wire 0.4.0 — https://github.com/shinolab/autd3-sdk
889
+ - autd3-rs 0.6.0 — https://github.com/shinolab/autd3-sdk
890
+ - autd3-rs-core 0.6.0 — https://github.com/shinolab/autd3-sdk
891
+ - autd3-cpu-wire 0.6.0 — https://github.com/shinolab/autd3-sdk
892
+
893
+ ```
894
+ MIT License
895
+
896
+ Copyright (c) 2026 Shun Suzuki
897
+
898
+ Permission is hereby granted, free of charge, to any person obtaining a copy
899
+ of this software and associated documentation files (the "Software"), to deal
900
+ in the Software without restriction, including without limitation the rights
901
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
902
+ copies of the Software, and to permit persons to whom the Software is
903
+ furnished to do so, subject to the following conditions:
904
+
905
+ The above copyright notice and this permission notice shall be included in all
906
+ copies or substantial portions of the Software.
907
+
908
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
909
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
910
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
911
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
912
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
913
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
914
+ SOFTWARE.
915
+
916
+ ```
917
+
918
+ ## MIT License
919
+
920
+ Used by:
921
+
922
+ - autd3-ffi 0.6.0 — https://github.com/shinolab/autd3-sdk
923
+ - autd3-ffi-abi 0.6.0 — https://github.com/shinolab/autd3-sdk
894
924
  - chrono 0.4.45 — https://github.com/chronotope/chrono
895
925
  - wide 1.5.0 — https://github.com/Lokathor/wide
896
926
  - winapi-i686-pc-windows-gnu 0.4.0 — https://github.com/retep998/winapi-rs
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "url": "git+https://github.com/shinolab/autd3-sdk.git",
6
6
  "directory": "bindings/unity/com.shinolab.autd3-sdk"
7
7
  },
8
- "version": "0.4.0",
8
+ "version": "0.6.0",
9
9
  "displayName": "AUTD3",
10
10
  "description": "AUTD3 client, datagrams and STM (Unity binding).",
11
11
  "unity": "6000.0",
@@ -15,9 +15,9 @@
15
15
  "url": "https://github.com/shinolab"
16
16
  },
17
17
  "dependencies": {
18
- "com.shinolab.autd3-sdk.core": "0.4.0",
19
- "com.shinolab.autd3-sdk.pattern": "0.4.0",
20
- "com.shinolab.autd3-sdk.modulation": "0.4.0"
18
+ "com.shinolab.autd3-sdk.core": "0.6.0",
19
+ "com.shinolab.autd3-sdk.pattern": "0.6.0",
20
+ "com.shinolab.autd3-sdk.modulation": "0.6.0"
21
21
  },
22
22
  "samples": [
23
23
  {