|
1 | 1 | using Bonsai.Harp;
|
| 2 | +using System.Threading; |
2 | 3 | using System.Threading.Tasks;
|
3 | 4 |
|
4 | 5 | namespace Harp.AudioSwitch
|
@@ -47,249 +48,309 @@ internal AsyncDevice(string portName)
|
47 | 48 | /// <summary>
|
48 | 49 | /// Asynchronously reads the contents of the ControlMode register.
|
49 | 50 | /// </summary>
|
| 51 | + /// <param name="cancellationToken"> |
| 52 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 53 | + /// </param> |
50 | 54 | /// <returns>
|
51 | 55 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
52 | 56 | /// property contains the register payload.
|
53 | 57 | /// </returns>
|
54 |
| - public async Task<ControlSource> ReadControlModeAsync() |
| 58 | + public async Task<ControlSource> ReadControlModeAsync(CancellationToken cancellationToken = default) |
55 | 59 | {
|
56 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(ControlMode.Address)); |
| 60 | + var reply = await CommandAsync(HarpCommand.ReadByte(ControlMode.Address), cancellationToken); |
57 | 61 | return ControlMode.GetPayload(reply);
|
58 | 62 | }
|
59 | 63 |
|
60 | 64 | /// <summary>
|
61 | 65 | /// Asynchronously reads the timestamped contents of the ControlMode register.
|
62 | 66 | /// </summary>
|
| 67 | + /// <param name="cancellationToken"> |
| 68 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 69 | + /// </param> |
63 | 70 | /// <returns>
|
64 | 71 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
65 | 72 | /// property contains the timestamped register payload.
|
66 | 73 | /// </returns>
|
67 |
| - public async Task<Timestamped<ControlSource>> ReadTimestampedControlModeAsync() |
| 74 | + public async Task<Timestamped<ControlSource>> ReadTimestampedControlModeAsync(CancellationToken cancellationToken = default) |
68 | 75 | {
|
69 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(ControlMode.Address)); |
| 76 | + var reply = await CommandAsync(HarpCommand.ReadByte(ControlMode.Address), cancellationToken); |
70 | 77 | return ControlMode.GetTimestampedPayload(reply);
|
71 | 78 | }
|
72 | 79 |
|
73 | 80 | /// <summary>
|
74 | 81 | /// Asynchronously writes a value to the ControlMode register.
|
75 | 82 | /// </summary>
|
76 | 83 | /// <param name="value">The value to be stored in the register.</param>
|
| 84 | + /// <param name="cancellationToken"> |
| 85 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 86 | + /// </param> |
77 | 87 | /// <returns>The task object representing the asynchronous write operation.</returns>
|
78 |
| - public async Task WriteControlModeAsync(ControlSource value) |
| 88 | + public async Task WriteControlModeAsync(ControlSource value, CancellationToken cancellationToken = default) |
79 | 89 | {
|
80 | 90 | var request = ControlMode.FromPayload(MessageType.Write, value);
|
81 |
| - await CommandAsync(request); |
| 91 | + await CommandAsync(request, cancellationToken); |
82 | 92 | }
|
83 | 93 |
|
84 | 94 | /// <summary>
|
85 | 95 | /// Asynchronously reads the contents of the EnableChannels register.
|
86 | 96 | /// </summary>
|
| 97 | + /// <param name="cancellationToken"> |
| 98 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 99 | + /// </param> |
87 | 100 | /// <returns>
|
88 | 101 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
89 | 102 | /// property contains the register payload.
|
90 | 103 | /// </returns>
|
91 |
| - public async Task<AudioChannels> ReadEnableChannelsAsync() |
| 104 | + public async Task<AudioChannels> ReadEnableChannelsAsync(CancellationToken cancellationToken = default) |
92 | 105 | {
|
93 |
| - var reply = await CommandAsync(HarpCommand.ReadUInt16(EnableChannels.Address)); |
| 106 | + var reply = await CommandAsync(HarpCommand.ReadUInt16(EnableChannels.Address), cancellationToken); |
94 | 107 | return EnableChannels.GetPayload(reply);
|
95 | 108 | }
|
96 | 109 |
|
97 | 110 | /// <summary>
|
98 | 111 | /// Asynchronously reads the timestamped contents of the EnableChannels register.
|
99 | 112 | /// </summary>
|
| 113 | + /// <param name="cancellationToken"> |
| 114 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 115 | + /// </param> |
100 | 116 | /// <returns>
|
101 | 117 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
102 | 118 | /// property contains the timestamped register payload.
|
103 | 119 | /// </returns>
|
104 |
| - public async Task<Timestamped<AudioChannels>> ReadTimestampedEnableChannelsAsync() |
| 120 | + public async Task<Timestamped<AudioChannels>> ReadTimestampedEnableChannelsAsync(CancellationToken cancellationToken = default) |
105 | 121 | {
|
106 |
| - var reply = await CommandAsync(HarpCommand.ReadUInt16(EnableChannels.Address)); |
| 122 | + var reply = await CommandAsync(HarpCommand.ReadUInt16(EnableChannels.Address), cancellationToken); |
107 | 123 | return EnableChannels.GetTimestampedPayload(reply);
|
108 | 124 | }
|
109 | 125 |
|
110 | 126 | /// <summary>
|
111 | 127 | /// Asynchronously writes a value to the EnableChannels register.
|
112 | 128 | /// </summary>
|
113 | 129 | /// <param name="value">The value to be stored in the register.</param>
|
| 130 | + /// <param name="cancellationToken"> |
| 131 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 132 | + /// </param> |
114 | 133 | /// <returns>The task object representing the asynchronous write operation.</returns>
|
115 |
| - public async Task WriteEnableChannelsAsync(AudioChannels value) |
| 134 | + public async Task WriteEnableChannelsAsync(AudioChannels value, CancellationToken cancellationToken = default) |
116 | 135 | {
|
117 | 136 | var request = EnableChannels.FromPayload(MessageType.Write, value);
|
118 |
| - await CommandAsync(request); |
| 137 | + await CommandAsync(request, cancellationToken); |
119 | 138 | }
|
120 | 139 |
|
121 | 140 | /// <summary>
|
122 | 141 | /// Asynchronously reads the contents of the DigitalInputState register.
|
123 | 142 | /// </summary>
|
| 143 | + /// <param name="cancellationToken"> |
| 144 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 145 | + /// </param> |
124 | 146 | /// <returns>
|
125 | 147 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
126 | 148 | /// property contains the register payload.
|
127 | 149 | /// </returns>
|
128 |
| - public async Task<DigitalInputs> ReadDigitalInputStateAsync() |
| 150 | + public async Task<DigitalInputs> ReadDigitalInputStateAsync(CancellationToken cancellationToken = default) |
129 | 151 | {
|
130 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(DigitalInputState.Address)); |
| 152 | + var reply = await CommandAsync(HarpCommand.ReadByte(DigitalInputState.Address), cancellationToken); |
131 | 153 | return DigitalInputState.GetPayload(reply);
|
132 | 154 | }
|
133 | 155 |
|
134 | 156 | /// <summary>
|
135 | 157 | /// Asynchronously reads the timestamped contents of the DigitalInputState register.
|
136 | 158 | /// </summary>
|
| 159 | + /// <param name="cancellationToken"> |
| 160 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 161 | + /// </param> |
137 | 162 | /// <returns>
|
138 | 163 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
139 | 164 | /// property contains the timestamped register payload.
|
140 | 165 | /// </returns>
|
141 |
| - public async Task<Timestamped<DigitalInputs>> ReadTimestampedDigitalInputStateAsync() |
| 166 | + public async Task<Timestamped<DigitalInputs>> ReadTimestampedDigitalInputStateAsync(CancellationToken cancellationToken = default) |
142 | 167 | {
|
143 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(DigitalInputState.Address)); |
| 168 | + var reply = await CommandAsync(HarpCommand.ReadByte(DigitalInputState.Address), cancellationToken); |
144 | 169 | return DigitalInputState.GetTimestampedPayload(reply);
|
145 | 170 | }
|
146 | 171 |
|
147 | 172 | /// <summary>
|
148 | 173 | /// Asynchronously reads the contents of the DO0State register.
|
149 | 174 | /// </summary>
|
| 175 | + /// <param name="cancellationToken"> |
| 176 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 177 | + /// </param> |
150 | 178 | /// <returns>
|
151 | 179 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
152 | 180 | /// property contains the register payload.
|
153 | 181 | /// </returns>
|
154 |
| - public async Task<EnableFlag> ReadDO0StateAsync() |
| 182 | + public async Task<EnableFlag> ReadDO0StateAsync(CancellationToken cancellationToken = default) |
155 | 183 | {
|
156 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(DO0State.Address)); |
| 184 | + var reply = await CommandAsync(HarpCommand.ReadByte(DO0State.Address), cancellationToken); |
157 | 185 | return DO0State.GetPayload(reply);
|
158 | 186 | }
|
159 | 187 |
|
160 | 188 | /// <summary>
|
161 | 189 | /// Asynchronously reads the timestamped contents of the DO0State register.
|
162 | 190 | /// </summary>
|
| 191 | + /// <param name="cancellationToken"> |
| 192 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 193 | + /// </param> |
163 | 194 | /// <returns>
|
164 | 195 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
165 | 196 | /// property contains the timestamped register payload.
|
166 | 197 | /// </returns>
|
167 |
| - public async Task<Timestamped<EnableFlag>> ReadTimestampedDO0StateAsync() |
| 198 | + public async Task<Timestamped<EnableFlag>> ReadTimestampedDO0StateAsync(CancellationToken cancellationToken = default) |
168 | 199 | {
|
169 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(DO0State.Address)); |
| 200 | + var reply = await CommandAsync(HarpCommand.ReadByte(DO0State.Address), cancellationToken); |
170 | 201 | return DO0State.GetTimestampedPayload(reply);
|
171 | 202 | }
|
172 | 203 |
|
173 | 204 | /// <summary>
|
174 | 205 | /// Asynchronously writes a value to the DO0State register.
|
175 | 206 | /// </summary>
|
176 | 207 | /// <param name="value">The value to be stored in the register.</param>
|
| 208 | + /// <param name="cancellationToken"> |
| 209 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 210 | + /// </param> |
177 | 211 | /// <returns>The task object representing the asynchronous write operation.</returns>
|
178 |
| - public async Task WriteDO0StateAsync(EnableFlag value) |
| 212 | + public async Task WriteDO0StateAsync(EnableFlag value, CancellationToken cancellationToken = default) |
179 | 213 | {
|
180 | 214 | var request = DO0State.FromPayload(MessageType.Write, value);
|
181 |
| - await CommandAsync(request); |
| 215 | + await CommandAsync(request, cancellationToken); |
182 | 216 | }
|
183 | 217 |
|
184 | 218 | /// <summary>
|
185 | 219 | /// Asynchronously reads the contents of the DI4Trigger register.
|
186 | 220 | /// </summary>
|
| 221 | + /// <param name="cancellationToken"> |
| 222 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 223 | + /// </param> |
187 | 224 | /// <returns>
|
188 | 225 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
189 | 226 | /// property contains the register payload.
|
190 | 227 | /// </returns>
|
191 |
| - public async Task<DI4TriggerConfig> ReadDI4TriggerAsync() |
| 228 | + public async Task<DI4TriggerConfig> ReadDI4TriggerAsync(CancellationToken cancellationToken = default) |
192 | 229 | {
|
193 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(DI4Trigger.Address)); |
| 230 | + var reply = await CommandAsync(HarpCommand.ReadByte(DI4Trigger.Address), cancellationToken); |
194 | 231 | return DI4Trigger.GetPayload(reply);
|
195 | 232 | }
|
196 | 233 |
|
197 | 234 | /// <summary>
|
198 | 235 | /// Asynchronously reads the timestamped contents of the DI4Trigger register.
|
199 | 236 | /// </summary>
|
| 237 | + /// <param name="cancellationToken"> |
| 238 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 239 | + /// </param> |
200 | 240 | /// <returns>
|
201 | 241 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
202 | 242 | /// property contains the timestamped register payload.
|
203 | 243 | /// </returns>
|
204 |
| - public async Task<Timestamped<DI4TriggerConfig>> ReadTimestampedDI4TriggerAsync() |
| 244 | + public async Task<Timestamped<DI4TriggerConfig>> ReadTimestampedDI4TriggerAsync(CancellationToken cancellationToken = default) |
205 | 245 | {
|
206 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(DI4Trigger.Address)); |
| 246 | + var reply = await CommandAsync(HarpCommand.ReadByte(DI4Trigger.Address), cancellationToken); |
207 | 247 | return DI4Trigger.GetTimestampedPayload(reply);
|
208 | 248 | }
|
209 | 249 |
|
210 | 250 | /// <summary>
|
211 | 251 | /// Asynchronously writes a value to the DI4Trigger register.
|
212 | 252 | /// </summary>
|
213 | 253 | /// <param name="value">The value to be stored in the register.</param>
|
| 254 | + /// <param name="cancellationToken"> |
| 255 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 256 | + /// </param> |
214 | 257 | /// <returns>The task object representing the asynchronous write operation.</returns>
|
215 |
| - public async Task WriteDI4TriggerAsync(DI4TriggerConfig value) |
| 258 | + public async Task WriteDI4TriggerAsync(DI4TriggerConfig value, CancellationToken cancellationToken = default) |
216 | 259 | {
|
217 | 260 | var request = DI4Trigger.FromPayload(MessageType.Write, value);
|
218 |
| - await CommandAsync(request); |
| 261 | + await CommandAsync(request, cancellationToken); |
219 | 262 | }
|
220 | 263 |
|
221 | 264 | /// <summary>
|
222 | 265 | /// Asynchronously reads the contents of the DO0Sync register.
|
223 | 266 | /// </summary>
|
| 267 | + /// <param name="cancellationToken"> |
| 268 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 269 | + /// </param> |
224 | 270 | /// <returns>
|
225 | 271 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
226 | 272 | /// property contains the register payload.
|
227 | 273 | /// </returns>
|
228 |
| - public async Task<DO0SyncConfig> ReadDO0SyncAsync() |
| 274 | + public async Task<DO0SyncConfig> ReadDO0SyncAsync(CancellationToken cancellationToken = default) |
229 | 275 | {
|
230 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(DO0Sync.Address)); |
| 276 | + var reply = await CommandAsync(HarpCommand.ReadByte(DO0Sync.Address), cancellationToken); |
231 | 277 | return DO0Sync.GetPayload(reply);
|
232 | 278 | }
|
233 | 279 |
|
234 | 280 | /// <summary>
|
235 | 281 | /// Asynchronously reads the timestamped contents of the DO0Sync register.
|
236 | 282 | /// </summary>
|
| 283 | + /// <param name="cancellationToken"> |
| 284 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 285 | + /// </param> |
237 | 286 | /// <returns>
|
238 | 287 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
239 | 288 | /// property contains the timestamped register payload.
|
240 | 289 | /// </returns>
|
241 |
| - public async Task<Timestamped<DO0SyncConfig>> ReadTimestampedDO0SyncAsync() |
| 290 | + public async Task<Timestamped<DO0SyncConfig>> ReadTimestampedDO0SyncAsync(CancellationToken cancellationToken = default) |
242 | 291 | {
|
243 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(DO0Sync.Address)); |
| 292 | + var reply = await CommandAsync(HarpCommand.ReadByte(DO0Sync.Address), cancellationToken); |
244 | 293 | return DO0Sync.GetTimestampedPayload(reply);
|
245 | 294 | }
|
246 | 295 |
|
247 | 296 | /// <summary>
|
248 | 297 | /// Asynchronously writes a value to the DO0Sync register.
|
249 | 298 | /// </summary>
|
250 | 299 | /// <param name="value">The value to be stored in the register.</param>
|
| 300 | + /// <param name="cancellationToken"> |
| 301 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 302 | + /// </param> |
251 | 303 | /// <returns>The task object representing the asynchronous write operation.</returns>
|
252 |
| - public async Task WriteDO0SyncAsync(DO0SyncConfig value) |
| 304 | + public async Task WriteDO0SyncAsync(DO0SyncConfig value, CancellationToken cancellationToken = default) |
253 | 305 | {
|
254 | 306 | var request = DO0Sync.FromPayload(MessageType.Write, value);
|
255 |
| - await CommandAsync(request); |
| 307 | + await CommandAsync(request, cancellationToken); |
256 | 308 | }
|
257 | 309 |
|
258 | 310 | /// <summary>
|
259 | 311 | /// Asynchronously reads the contents of the EnableEvents register.
|
260 | 312 | /// </summary>
|
| 313 | + /// <param name="cancellationToken"> |
| 314 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 315 | + /// </param> |
261 | 316 | /// <returns>
|
262 | 317 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
263 | 318 | /// property contains the register payload.
|
264 | 319 | /// </returns>
|
265 |
| - public async Task<AudioSwitchEvents> ReadEnableEventsAsync() |
| 320 | + public async Task<AudioSwitchEvents> ReadEnableEventsAsync(CancellationToken cancellationToken = default) |
266 | 321 | {
|
267 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(EnableEvents.Address)); |
| 322 | + var reply = await CommandAsync(HarpCommand.ReadByte(EnableEvents.Address), cancellationToken); |
268 | 323 | return EnableEvents.GetPayload(reply);
|
269 | 324 | }
|
270 | 325 |
|
271 | 326 | /// <summary>
|
272 | 327 | /// Asynchronously reads the timestamped contents of the EnableEvents register.
|
273 | 328 | /// </summary>
|
| 329 | + /// <param name="cancellationToken"> |
| 330 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 331 | + /// </param> |
274 | 332 | /// <returns>
|
275 | 333 | /// A task that represents the asynchronous read operation. The <see cref="Task{TResult}.Result"/>
|
276 | 334 | /// property contains the timestamped register payload.
|
277 | 335 | /// </returns>
|
278 |
| - public async Task<Timestamped<AudioSwitchEvents>> ReadTimestampedEnableEventsAsync() |
| 336 | + public async Task<Timestamped<AudioSwitchEvents>> ReadTimestampedEnableEventsAsync(CancellationToken cancellationToken = default) |
279 | 337 | {
|
280 |
| - var reply = await CommandAsync(HarpCommand.ReadByte(EnableEvents.Address)); |
| 338 | + var reply = await CommandAsync(HarpCommand.ReadByte(EnableEvents.Address), cancellationToken); |
281 | 339 | return EnableEvents.GetTimestampedPayload(reply);
|
282 | 340 | }
|
283 | 341 |
|
284 | 342 | /// <summary>
|
285 | 343 | /// Asynchronously writes a value to the EnableEvents register.
|
286 | 344 | /// </summary>
|
287 | 345 | /// <param name="value">The value to be stored in the register.</param>
|
| 346 | + /// <param name="cancellationToken"> |
| 347 | + /// A <see cref="CancellationToken"/> which can be used to cancel the operation. |
| 348 | + /// </param> |
288 | 349 | /// <returns>The task object representing the asynchronous write operation.</returns>
|
289 |
| - public async Task WriteEnableEventsAsync(AudioSwitchEvents value) |
| 350 | + public async Task WriteEnableEventsAsync(AudioSwitchEvents value, CancellationToken cancellationToken = default) |
290 | 351 | {
|
291 | 352 | var request = EnableEvents.FromPayload(MessageType.Write, value);
|
292 |
| - await CommandAsync(request); |
| 353 | + await CommandAsync(request, cancellationToken); |
293 | 354 | }
|
294 | 355 | }
|
295 | 356 | }
|
0 commit comments