public void HandleMidiMessage(MidiMessage theMessage) { //theDevice.FlashLed(1, 10); mrSound.ProcessMessage(theMessage); }
//LISTEN FOR NEXT RECEIVE private async Task <int> Listen() { const int NUMBER_OF_BYTES_TO_RECEIVE = 1; //<<<<<SET THE NUMBER OF BYTES YOU WANT TO WAIT FOR byte[] ReceiveData; UInt32 bytesRead; MidiMessage ChannelMessage = new MidiMessage(); //MidiMessage GlobalMessage = new MidiMessage(); try { if (UartPort != null) { while (true) { //###### WINDOWS IoT MEMORY LEAK BUG 2017-03 - USING CancellationToken WITH LoadAsync() CAUSES A BAD MEMORY LEAK. WORKAROUND IS //TO BUILD RELEASE WITHOUT USING THE .NET NATIVE TOOLCHAIN OR TO NOT USE A CancellationToken IN THE CALL ##### //bytesRead = await DataReaderObject.LoadAsync(NUMBER_OF_BYTES_TO_RECEIVE).AsTask(ReadCancellationTokenSource.Token); //Wait until buffer is full Windows.Foundation.IAsyncOperation <uint> taskLoad = DataReaderObject.LoadAsync(NUMBER_OF_BYTES_TO_RECEIVE); taskLoad.AsTask().Wait(); bytesRead = taskLoad.GetResults(); //bytesRead = await DataReaderObject.LoadAsync(NUMBER_OF_BYTES_TO_RECEIVE).AsTask(); //Wait until buffer is full if ((ReadCancellationTokenSource.Token.IsCancellationRequested) || (UartPort == null)) { break; } if (bytesRead > 0) { ReceiveData = new byte[NUMBER_OF_BYTES_TO_RECEIVE]; DataReaderObject.ReadBytes(ReceiveData); foreach (byte Data in ReceiveData) { //------------------------------- //------------------------------- //----- RECEIVED NEXT BYTE ------ //------------------------------- //------------------------------- if (Data >= 0x80 && Data <= 0xEF) //new Channel Message { if (ChannelMessage.IsSet1) //We already have data in this object { //there is data in the current message RouteMessage(ChannelMessage); ChannelMessage = new MidiMessage(Data); } else //No data in the message { ChannelMessage.Status = Data; if (MidiMessage.ByteCount(ChannelMessage.MessageClass) == 0) { RouteMessage(ChannelMessage); ChannelMessage = new MidiMessage(); } } } else //not a status byte { if (ChannelMessage.Status != 0 && !ChannelMessage.IsSet1) { ChannelMessage.Data1 = Data; if (MidiMessage.ByteCount(ChannelMessage.MessageClass) == 1) { RouteMessage(ChannelMessage); ChannelMessage = new MidiMessage(); } } else //Data1 is full { if (ChannelMessage.Status != 0 && !ChannelMessage.IsSet2) { ChannelMessage.Data2 = Data; RouteMessage(ChannelMessage); ChannelMessage = new MidiMessage(); } } } } /*if (Data > 0xF0 ) //SysEx message (Global) * { * //Throw away data until we get to F7 * }*/ } } //while } //check the port } catch (Exception e) { //We will get here often if the USB serial cable is removed so reset ready for a new connection (otherwise a never ending error occurs) if (ReadCancellationTokenSource != null) { ReadCancellationTokenSource.Cancel(); } System.Diagnostics.Debug.WriteLine("UART ReadAsync Exception: {0}", e.Message); } return(await Task.FromResult(0)); }