Exemplo n.º 1
0
        private void ResetRotation()
        {
            var rotData = new USBRotationData();

            rotData.w = 1;
            rotData.x = 0;
            rotData.y = 0;
            rotData.z = 0;

            var packet = USBPacket.Create((byte)(ROTATION_DATA | HMD_SOURCE), (ushort)(DateTime.Now.Ticks / 1000), rotData);
            var d      = StructToBytes(packet);

            SetPacketCrc(ref d);
            _sharedMem.WriteIncomingPacket(d);
        }
Exemplo n.º 2
0
        private void USBProcessor()
        {
            List <byte[]> outgoingPackets = null;
            HIDDev        _usb            = null;
            var           data            = new byte[33];
            DateTime      lastOutgoing    = DateTime.MinValue;

            while (_running)
            {
                if (_usb == null)
                {
                    var hd = HIDBrowse.Browse().FirstOrDefault(h => h.Vid == 0x1974 && h.Pid == 0x001);
                    if (hd != null)
                    {
                        _usb = new HIDDev();
                        _usb.Open(hd);
                        State = CommState.Connected;
                        _sharedMem.SetState(State);
                    }
                }
                try
                {
                    if (_usb != null)
                    {
                        outgoingPackets = _sharedMem.ReadOutgoingPackets();
                        if (outgoingPackets != null)
                        {
                            lock (OutgoingPackets)
                                foreach (var item in outgoingPackets)
                                {
                                    OutgoingPackets.Enqueue(item);
                                }
                        }
                        lock (OutgoingPackets)
                            if (OutgoingPackets.Count > 0 && (DateTime.Now - lastOutgoing).TotalMilliseconds >= 100)
                            {
                                lastOutgoing = DateTime.Now;
                                var d = OutgoingPackets.Dequeue(); //33 byte packets in queue
                                _usb.Write(d);                     //send before monitor process
                                if (IsDebug && IsVisible)
                                {
                                    var packet = StructFromBytes <USBPacket>(d, 1);
                                    packet.ParseFields();
                                    lock (OutPacketMonitor)
                                        OutPacketMonitor.Add(packet);
                                }
                            }

                        _usb.Read(data);
                        if (CheckPacketCrc(data, 1))
                        {
                            _sharedMem.WriteIncomingPacket(data); //send before monitor process
                            State = _sharedMem.IsDriverActive ? CommState.Active : CommState.ActiveNoDriver;
                            if (IsDebug && IsVisible)
                            {
                                //var x = Marshal.SizeOf(typeof(USBPacket));
                                var packet = StructFromBytes <USBPacket>(data, 1);
                                packet.ParseFields();
                                lock (InPacketMonitor)
                                    InPacketMonitor.Add(packet);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    if (_usb != null)
                    {
                        _usb.Close();
                        _usb.Dispose();
                    }
                    _usb  = null;
                    State = CommState.Disconnected;
                    _sharedMem.SetState(State);

                    Thread.Sleep(100);
                }
            }
            if (_usb != null)
            {
                _usb.Close();
                _usb.Dispose();
            }
            _usb  = null;
            State = CommState.Disconnected;
            _sharedMem.SetState(CommState.Disconnected);
        }