public async void DisplayLedMatrixAsync(NuimoLedMatrix matrix, double displayInterval = 2.0, int options = 0) { if (ConnectionState != NuimoConnectionState.Connected) { return; } var withFadeTransition = (options & (int)NuimoLedMatrixWriteOption.WithFadeTransition) != 0; var writeWithoutResponse = (options & (int)NuimoLedMatrixWriteOption.WithoutWriteResponse) != 0; var byteArray = new byte[13]; matrix.GattBytes().CopyTo(byteArray, 0); byteArray[10] |= Convert.ToByte(withFadeTransition ? 1 << 4 : 0); byteArray[11] = Convert.ToByte(Math.Max(0, Math.Min(255, MatrixBrightness * 255))); byteArray[12] = Convert.ToByte(Math.Max(0, Math.Min(255, displayInterval * 10))); if (writeWithoutResponse) { #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed // ReSharper disable once InconsistentlySynchronizedField _gattCharacteristicsForGuid[CharacteristicsGuids.LedMatrixCharacteristicGuid].WriteValueAsync(byteArray.AsBuffer(), GattWriteOption.WriteWithoutResponse); #pragma warning restore CS4014 } else { // ReSharper disable once InconsistentlySynchronizedField var gattWriteResponse = await _gattCharacteristicsForGuid[CharacteristicsGuids.LedMatrixCharacteristicGuid].WriteValueAsync(byteArray.AsBuffer(), GattWriteOption.WriteWithResponse); if (gattWriteResponse == GattCommunicationStatus.Success) { LedMatrixDisplayed?.Invoke(this); } } }
public static byte[] GattBytes(this NuimoLedMatrix matrix) { return(matrix.Leds .Chunk(8) .Select(chunk => chunk .Select((led, i) => Convert.ToByte(led ? 1 << i : 0)) .Aggregate((a, b) => Convert.ToByte(a + b))) .ToArray()); }