private void Work()
        {
            while (true)
            {
                while (mSuspend)
                {
                    Thread.Sleep(100);
                }

                EAccordeon.MidiHelper.ResetDevice();

                if (mVirtialStateSequence == null)
                {
                    if (mSelectedPiese == null)
                    {
                        continue;
                    }
                    mVirtialStateSequence = mSelectedPiese.Sequience;
                }

                foreach (var s in mVirtialStateSequence)
                {
                    if (mSuspend)
                    {
                        break;
                    }

                    EAccordeon.HandleKeys(s.PressureCode, s.RightKeysState, s.LeftVoicesState);
                    Thread.Sleep(s.Duration_ms);
                }
            }
        }
 public MidiControllerVirtual(EAccordeon eAccordeon)
     : base(eAccordeon)
 {
     mSuspend       = true;
     mVirtualTask   = Task.Factory.StartNew(Work);
     mSelectedPiese = VirtualPieses.DefaultPiese;
 }
Exemplo n.º 3
0
        public MidiControllerSerial(EAccordeon eAccordeon)
            : base(eAccordeon)
        {
            mPort = new SerialPort();

            mPort.ReadTimeout = 1000;

            try { mPort.BaudRate = Properties.Settings.Default.SerialportController_BaudRate; }
            catch { }

            try { mPort.PortName = Properties.Settings.Default.SerialportController_PortName; }
            catch { }

            mPortListenerTask = Task.Factory.StartNew(ListenSerial);
        }
Exemplo n.º 4
0
 public MidiControllerBase(EAccordeon eAccordeon)
 {
     this.eAccordeon = eAccordeon;
 }
Exemplo n.º 5
0
        private void ListenSerial()
        {
            const int packetSize = 1 + 2 + 8 + 8 + 1 + 1;

            byte[] packetData = new byte[packetSize];

            UInt16             pressure        = 0;
            AccordeonRightKeys rightKeysState  = AccordeonRightKeys.None;
            UInt64             leftVoicesState = 0;


            while (true)
            {
                // Если порт закрыт, то ждем когда откроется
                while (!mPort.IsOpen)
                {
                    Thread.Sleep(100);
                    DebugText = "Offline";
                }

                // Блокировка нужна, чтобы не возникало казусов при открытии и закрытии порта
                lock (mPort)
                {
                    try
                    {
                        var readed = 0;
                        packetData[0] = 0;
                        while (packetData[0] != 0x5B)
                        {
                            readed = mPort.Read(packetData, 0, 1);
                        }

                        while (readed < packetSize)
                        {
                            readed += mPort.Read(packetData, readed, packetSize - readed);
                        }
                    }
                    catch (TimeoutException)
                    {
                        mPort.DiscardInBuffer();
                        mPort.DiscardOutBuffer();
                        DebugText = "timeout";
                        continue;
                    }
                    catch
                    {
                        DebugText = "IO Error";
                        continue;
                    }

                    var startByte = packetData[0];
                    var endByte   = packetData[packetData.Length - 1];

                    // Проверка контрольных сумм
                    if (startByte != 0x5B || endByte != 0x5E)
                    {
                        EAccordeon.MidiHelper.ResetDevice();
                        mPort.DiscardInBuffer();
                        mPort.DiscardOutBuffer();

                        DebugText = "Checksum error";
                        continue;
                    }

                    int offset = 1;
                    pressure = BitConverter.ToUInt16(packetData, offset);
                    offset  += sizeof(UInt16);

                    rightKeysState = (AccordeonRightKeys)BitConverter.ToUInt64(packetData, offset);
                    offset        += sizeof(ulong);

                    leftVoicesState = BitConverter.ToUInt64(packetData, offset);
                    offset         += sizeof(ulong);

                    var pressedKey     = System.Text.Encoding.ASCII.GetChars(packetData, offset, 1)[0];
                    var pressedKeyCode = (int)pressedKey;

                    byte bPressure = EAccordeon.TransformPressure(pressure);
                    EAccordeon.HandleKeys(bPressure, rightKeysState, leftVoicesState);

                    DebugText = $"pressure={pressure}; rightKeysState={rightKeysState}; leftVoicesState={leftVoicesState}; pressedKey={pressedKey} ({pressedKeyCode})";
                }
            }
        }