コード例 #1
0
        /// <summary>
        ///     Flush data to the port
        /// </summary>
        /// <param name="force">Whether to flush all data to the port, even if it doesn't appear to have changed.</param>
        /// <returns>An awaitable task that completes when finished</returns>
        public async Task FlushAsync(bool force = false)
        {
            if (!CurrentValue.SequenceEqual(lastValues) || force)
            {
                if (Parent != null)
                {
                    await Parent.FlushAsync().ConfigureAwait(false);
                }
                else
                {
                    using (await lockObject.LockAsync().ConfigureAwait(false))
                    {
                        // build the byte array to flush out of the port
                        var bytes = new List <byte>();

                        var reversed = Children.Reverse().ToList(); // bytes are shuffled out last-device-first
                        reversed.Add(this);                         // add ourselves

                        foreach (var shift in reversed)
                        {
                            var shiftBytes = shift.CurrentValue;
                            bytes.AddRange(shiftBytes.Reverse());
                        }

                        await spiDevice.SendReceiveAsync(bytes.ToArray(), SpiBurstMode.BurstTx).ConfigureAwait(false);
                    }
                }
                CurrentValue.CopyTo(lastValues, 0);
            }
        }