private void ConnectJoy(BTDeviceInfo joy)
        {
            string   addr  = joy.bluetoothDeviceInfo.DeviceAddress.ToString();
            JoyStick joyst = new JoyStick(addr);

            joy.joyStick = joyst;
            if (joy.joyStick.StartConnect(0))
            {
                joy.State = deviceState.CONNECT;
                OnJoyStateChange?.Invoke(this.timeBoxJoyList);
                OnMessage?.Invoke(addr + "连接成功!");
                joy.joyStick.OnReceive = buffer => {
                    //string text = HexHelper.byteToHexStr(buffer, 18);
                    //this.ShowMsg(text);
                    this.OnJoyMessageReceive?.Invoke(buffer);
                };
                //joy.joyStick.SetJoyMap(new KeyBoardJoyMap());

                joy.SetJoyMap(this, joy.mapConfig);
                joy.joyStick.startFeed();

                this.rememberMac.Remove(addr);
                this.rememberMac.Add(addr + ":" + joy.mapConfig.Name);
                FileHelper fh = new FileHelper();
                fh.SaveFile("remember.txt", string.Join("\r\n", this.rememberMac));
            }
        }
Beispiel #2
0
        public MapEdit(JoyManager _manager, List <DefaultMapConfig> _configs, BTDeviceInfo _device)
        {
            InitializeComponent();
            this.manager      = _manager;
            this.configs      = _configs;
            this.device       = _device;
            allXinputBtEnum   = GetEnumArray <Xbox360Buttons>();
            allXinputAxesEnum = GetEnumArray <Xbox360Axes>();
            allScripts        = new List <ButtonEnumModel>();
            allKeysEnum       = GetEnumArray <Keys>();
            allMixBt          = new List <ButtonEnumModel>();
            allMixBt.AddRange(this.allXinputBtEnum.Select(i => new ButtonEnumModel("360:" + i.ToString(), (int)i, 65535)));
            allMixBt.AddRange(this.allKeysEnum.Select(i => new ButtonEnumModel(i.ToString(), (int)i)));
            allMixBt.Add(new ButtonEnumModel("未设定", (int)TimeBoxButton.NONE));

            if (!Directory.Exists("joyScripts"))
            {
                Directory.CreateDirectory("joyScripts");
            }
            foreach (var file in Directory.GetFiles("joyScripts"))
            {
                var name     = System.IO.Path.GetFileNameWithoutExtension(file);
                var nameHash = name.GetHashCode();
                allScripts.Add(new ButtonEnumModel(name, nameHash));
            }
        }
Beispiel #3
0
 public MapEdit(JoyManager _manager, List <DefaultMapConfig> _configs, BTDeviceInfo _device)
 {
     InitializeComponent();
     this.manager      = _manager;
     this.configs      = _configs;
     this.device       = _device;
     allXinputBtEnum   = GetEnumArray <Xbox360Buttons>();
     allXinputAxesEnum = GetEnumArray <Xbox360Axes>();
     allKeysEnum       = GetEnumArray <Keys>();
     allMixBt          = new List <ButtonEnumModel>();
     allMixBt.AddRange(this.allXinputBtEnum.Select(i => new ButtonEnumModel("Xinput:" + i.ToString(), (int)i, 65535)));
     allMixBt.AddRange(this.allKeysEnum.Select(i => new ButtonEnumModel("键盘:" + i.ToString(), (int)i)));
     allMixBt.Add(new ButtonEnumModel("未设定", (int)TimeBoxButton.NONE));
 }
        public JoyManager(
            Action <List <BTDeviceInfo> > OnJoyStateChange = null,
            Action <Byte[]> OnJoyMessageReceive            = null,
            Action <string> OnMessage = null,
            Action OnStartScanDevice  = null,
            Action OnEndScanDevice    = null)
        {
            timeBoxJoyList           = new List <BTDeviceInfo>();
            this.OnJoyStateChange    = OnJoyStateChange;
            this.OnJoyMessageReceive = OnJoyMessageReceive;
            this.OnMessage           = OnMessage;
            this.OnStartScanDevice   = OnStartScanDevice;
            this.OnEndScanDevice     = OnEndScanDevice;

            //读取所有配置文件
            mapConfigs = new List <DefaultMapConfig>();
            if (!Directory.Exists("maps"))
            {
                Directory.CreateDirectory("maps");
            }
            mapConfigs.Add(new KeyBoardConfig());
            mapConfigs.Add(new XInputConfig());
            mapConfigs.Add(new MixModeConfig());
            mapConfigs.Add(new ScriptModeConfig());
            foreach (var file in Directory.GetFiles("maps"))
            {
                var _config = DefaultMapConfig.GetConfig(file);
                //FileInfo finfo = new FileInfo(file);
                _config.Name = System.IO.Path.GetFileNameWithoutExtension(file);
                if (!mapConfigs.Any(p => p.Name == _config.Name))
                {
                    mapConfigs.Add(_config);
                }
            }

            //读取已记忆的MAC设备
            rememberMac = new List <string>();
            if (File.Exists("remember.txt"))
            {
                FileHelper fh = new FileHelper();
                foreach (var addr in fh.readFileLine("remember.txt"))
                {
                    try
                    {
                        if (string.IsNullOrWhiteSpace(addr))
                        {
                            continue;
                        }
                        if (!addr.Contains(":"))
                        {
                            continue;
                        }
                        string macAddr    = addr.Split(':')[0];
                        string configName = addr.Split(':')[1];
                        if (rememberMac.Contains(macAddr))
                        {
                            continue;
                        }
                        rememberMac.Add(macAddr);
                        BluetoothAddress    address = BluetoothAddress.Parse(macAddr);
                        BluetoothDeviceInfo info    = new BluetoothDeviceInfo(address);
                        var btDevice = new BTDeviceInfo(info);
                        btDevice.State     = deviceState.LOST;
                        btDevice.mapConfig = mapConfigs.Where(i => i.Name == configName).FirstOrDefault();
                        this.timeBoxJoyList.Add(btDevice);
                    }
                    catch
                    {
                    }
                }
                this.OnJoyStateChange?.Invoke(this.timeBoxJoyList);
            }

            //初始化蓝牙设备
            blueClient    = new BluetoothClient();
            blueComponent = new BluetoothComponent(blueClient);
            string name = "timebox";

            blueComponent.DiscoverDevicesProgress += (sender, e) => {
                foreach (var device in e.Devices)
                {
                    if (device.DeviceName == name && !this.timeBoxJoyList.Any(p => p.bluetoothDeviceInfo.DeviceAddress.ToString() == device.DeviceAddress.ToString()))
                    {
                        this.timeBoxJoyList.Add(new BTDeviceInfo(device));
                    }
                }
                this.OnJoyStateChange?.Invoke(this.timeBoxJoyList);
            };
            blueComponent.DiscoverDevicesComplete += (sender, e) => {
                if ((DateTime.Now - startScan) < ScanBlueTimeOut)
                {
                    blueComponent.DiscoverDevicesAsync(30, true, true, true, true, null);
                }
                else
                {
                    this.State = 0;
                    this.OnEndScanDevice?.Invoke();
                }
            };
            //启动自动扫描手柄状态线程
            Thread tr = new Thread(new ThreadStart(() => {
                ScanJoyConnection();
            }));

            tr.Start();
        }