Example #1
0
        public void UpdateReceivedJSONObject(BluetoothDeviceConnection conection, JSONDataSource dataSource)
        {
            // TODO: enlever ça ou l'implémenter.
            //_DataHistory.Add(dataSource);

            if (dataSource is IMU)
            {
                IMU = dataSource as IMU;
            }
            else if (dataSource is PID)
            {
                PID = dataSource as PID;
            }
            else if (dataSource is sensors)
            {
                Sensors = dataSource as sensors;
            }
            else if (dataSource is radio)
            {
                Radio = dataSource as radio;
            }
            else if (dataSource is rawEcho)
            {
                RawEcho = dataSource as rawEcho;
            }
        }
Example #2
0
        public async void StartTivaCopterCommunication(BluetoothDeviceConnection connection, DeviceInformation deviceInfo)
        {
            // TODO: let user choose the HID device
            await _hidConnection.EnumerateDevicesAsync();

            if (_hidConnection.AvailableDevices.Count > 0)
            {
                await _hidConnection.OpenDeviceAsync(_hidConnection.AvailableDevices[0]);
            }

            if (_hidConnection.IsDeviceConnected)
            {
                try
                {
                    StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(HID_KEYMAP_FILENAME);

                    // Open existing HID key map xml file
                    var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(HIDDataMap <RemoteControl>));
                    using (var stream = await file.OpenStreamForReadAsync())
                    {
                        var result = serializer.ReadObject(stream);
                        await stream.FlushAsync();

                        if (result.GetType() == typeof(HIDDataMap <RemoteControl>))
                        {
                            ControlMap = result as HIDDataMap <RemoteControl>;
                        }
                    }
                }
                catch (FileNotFoundException e)
                {
                    // TODO : remove static key map and copy a asset keymap xml file to local state at first app lunch ?
                    ControlMap.DataMap[0].UsagePage = 0x0001;
                    ControlMap.DataMap[0].UsageId   = 0x0031;
                    ControlMap.DataMap[1].UsagePage = 0x0001;
                    ControlMap.DataMap[1].UsageId   = 0x0031;
                    ControlMap.DataMap[2].UsagePage = 0x0001;
                    ControlMap.DataMap[2].UsageId   = 0x0030;
                    ControlMap.DataMap[3].UsagePage = 0x0001;
                    ControlMap.DataMap[3].UsageId   = 0x0030;
                    ControlMap.DataMap[4].UsagePage = 0x0009;
                    ControlMap.DataMap[4].UsageId   = 0x000C;
                    ControlMap.DataMap[5].UsagePage = 0x0009;
                    ControlMap.DataMap[5].UsageId   = 0x0001;
                    await SaveHid();
                }
            }

            // TODO: faire mieux que ça !!
            await Task.Delay(TimeSpan.FromSeconds(3));

            await _bluetoothConnection.Start();

            await Task.Delay(TimeSpan.FromSeconds(0.1));
        }
Example #3
0
        public TivaCopterViewModel()
        {
            var UITaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();

            _bluetoothConnection = new BluetoothDeviceConnection(UITaskScheduler);
            _hidConnection       = new HIDDeviceConnection(UITaskScheduler, 0x0001, 0x0004);

            _controlMap = new HIDDataMap <RemoteControl>();

            // TODO: enlever ça ou l'implémenter.
            //_DataHistory = new List<JSONDataSource>();

            ConnectCommand = new RelayCommand(async param =>
            {
                if (_bluetoothConnection != null && SelectedBluetoothDevice != null)
                {
                    try
                    {
                        await _bluetoothConnection.OpenDeviceAsync(SelectedBluetoothDevice);
                    }
                    catch (System.Exception)
                    {
                        IsConnectionFailedPopupOpen = true;
                    }
                }
            });

            OkConnectionFailedPopupCommand = new RelayCommand(param =>
            {
                IsConnectionFailedPopupOpen = false;
            });

            ChangeControlSettingCommand = new RelayCommand(param =>
            {
                if (_controlMap != null && param is PropertyToHidAttributeBinding && _hidConnection.IsDeviceConnected)
                {
                    IsControlsSettingPopupOpen  = true;
                    _waitingHIDToControlBinding = param as PropertyToHidAttributeBinding;

                    HidInputReport lastInputReport;

                    _controlsSettingsWaitForHidKey_EventHandler = new EventHandler <object>(async(sender, arg) =>
                    {
                        if (IsControlsSettingPopupOpen)
                        {
                            if (((System.Reflection.PropertyInfo)_waitingHIDToControlBinding?.Property).PropertyType == typeof(bool))
                            {
                                // TODO : savoir si ActivatedBooleanControls peut réellement devenir null
                                if (_hidConnection.Report.ActivatedBooleanControls?.Count > 0)
                                {
                                    var ActivatedBooleanControls = _hidConnection.Report.ActivatedBooleanControls;
                                    if (ActivatedBooleanControls.Count > 0)
                                    {
                                        var ActivatedBooleanControl           = ActivatedBooleanControls[0];
                                        _waitingHIDToControlBinding.UsageId   = ActivatedBooleanControl.UsageId;
                                        _waitingHIDToControlBinding.UsagePage = ActivatedBooleanControl.UsagePage;
                                    }

                                    CloseControlsSettingPopup();
                                    await SaveHid();
                                }
                            }
                            else
                            {
                                // TODO: check changed numeric values by comparing lastInputReport with _hidConnection.Report
                                lastInputReport = _hidConnection.Report;
                            }
                        }
                    });
                    _timer.Tick += _controlsSettingsWaitForHidKey_EventHandler;
                    _hidConnection.OnDeviceClose += CloseControlsSettingPopup;
                }
            });

            CancelControlsSettingPopupCommand = new RelayCommand(param => { CloseControlsSettingPopup(); });

            _hidConnection.OnDeviceConnected += StartHidInputListening;
            _hidConnection.OnDeviceClose     += new TypedEventHandler <DeviceConnection, DeviceInformation>((connection, deviceInfo) =>
            {
                _timer?.Stop();
            });

            _bluetoothConnection.OnSocketConnected    += StartTivaCopterCommunication;
            _bluetoothConnection.ConsoleBufferChanged += UpdateConsoleBuffer;
            _bluetoothConnection.OnJSONObjectReceived += UpdateReceivedJSONObject;
        }