public MainVM() { // getting current version Assembly assembly = Assembly.GetExecutingAssembly(); FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location); _config = new DeviceConfig(); _configExchanger = new DeviceConfigExchangerVM(); _configExchanger.Received += ConfigReceived; _configExchanger.Sent += ConfigSent; PinsVM = new PinsVM(Config); PinsVM.ConfigChanged += PinConfigChanged; _joystick = new Joystick(Config); AxesVM = new AxesVM(_joystick, Config); ButtonsVM = new ButtonsVM(_joystick, Config); ButtonsVM.ConfigChanged += ButtonsVM_ConfigChanged; AxesToButtonsVM = new AxesToButtonsVM(_joystick, Config); AxesToButtonsVM.ConfigChanged += AxesToButtonsVM_ConfigChanged; ShiftRegistersVM = new ShiftRegistersVM(_joystick, Config); ShiftRegistersVM.ConfigChanged += ShiftRegistersVM_ConfigChanged; LedVM = new LedVM(_joystick, Config); LedVM.ConfigChanged += LedVM_ConfigChanged; FirmwareUpdaterVM = new FirmwareUpdaterVM(); GetDeviceConfig = new DelegateCommand(() => { _configExchanger.GetConfigRequest(); WriteLog("Requesting config..", false); }); SendDeviceConfig = new DelegateCommand(() => { _configExchanger.SendConfig(Config); WriteLog("Writting config..", false); }); ResetAllPins = new DelegateCommand(() => PinsVM.ResetPins()); SaveConfig = new DelegateCommand(() => SaveConfigToFile()); LoadConfig = new DelegateCommand(() => ReadConfigFromFile()); SetDefault = new DelegateCommand(() => LoadDefaultConfig()); LoadDefaultConfig(); Hid.Start(Config.Vid); Hid.DeviceAdded += DeviceAddedEventHandler; Hid.DeviceRemoved += DeviceRemovedEventHandler; Hid.DeviceListUpdated += Hid_DeviceListUpdated; // Try to connect to device if (HidDevices.Count > 0) { SelectedDeviceIndex = 0; } WriteLog("Program started", true); }
private void PinConfigChanged() { ButtonsVM.Update(Config); AxesVM.Update(Config); AxesToButtonsVM.Update(Config); ShiftRegistersVM.Update(Config); LedVM.Update(Config); }
private void ConfigReceived(DeviceConfig deviceConfig) { Config = deviceConfig; DeviceFirmwareVersionVM = "Device firmware v" + Config.FirmwareVersion.ToString("X3").Insert(1, ".").Insert(3, ".").Insert(5, "b"); PinsVM.Update(Config); ButtonsVM.Update(Config); AxesVM.Update(Config); AxesToButtonsVM.Update(Config); ShiftRegistersVM.Update(Config); WriteLog("Config received", false); RaisePropertyChanged(nameof(Config)); }
private void LoadDefaultConfig() { { // TODO: fix serialization var xmlStr = Properties.Resources.default_config; DeviceConfig tmp = Config; tmp = DeSerializeObject <DeviceConfig>(xmlStr, xmlStr.Length); while (tmp.PinConfig.Count > 30) { tmp.PinConfig.RemoveAt(0); } while (tmp.AxisConfig.Count > 8) { tmp.AxisConfig.RemoveAt(0); } for (int i = 0; i < 8; i++) { while (tmp.AxisConfig[i].CurveShape.Count > 11) { tmp.AxisConfig[i].CurveShape.RemoveAt(0); } } while (tmp.ShiftModificatorConfig.Count > 5) { tmp.ShiftModificatorConfig.RemoveAt(0); } while (tmp.ButtonConfig.Count > 128) { tmp.ButtonConfig.RemoveAt(0); } while (tmp.AxisToButtonsConfig.Count > 8) { tmp.AxisToButtonsConfig.RemoveAt(0); } for (int i = 0; i < 8; i++) { while (tmp.AxisToButtonsConfig[i].Points.Count > 13) { tmp.AxisToButtonsConfig[i].Points.RemoveAt(0); } } while (tmp.ShiftRegistersConfig.Count > 4) { tmp.ShiftRegistersConfig.RemoveAt(0); } tmp.DeviceName = tmp.DeviceName.TrimEnd('\0'); Config = tmp; } PinsVM.Config = Config; AxesVM.Config = Config; ButtonsVM.Config = Config; PinsVM.Update(Config); ButtonsVM.Update(Config); AxesVM.Update(Config); AxesToButtonsVM.Update(Config); ShiftRegistersVM.Update(Config); }
private void ReadConfigFromFile() { OpenFileDialog dlg = new OpenFileDialog(); dlg.DefaultExt = ".conf"; dlg.Filter = "Config files (*.conf)|*.conf|All files (*.*)|*.*"; Nullable <bool> result = dlg.ShowDialog(); if (result == true) { { // TODO: fix serialization DeviceConfig tmp = DeSerializeObject <DeviceConfig>(dlg.FileName); if (tmp != null && (tmp.FirmwareVersion & 0xFFF0) != (Config.FirmwareVersion & 0xFFF0)) { MessageBoxService mbs = new MessageBoxService(); mbs.ShowMessage("Config file is broken or was created in other version of configurator!\r\n" + "Configuration loading will be canceled", "Error"); return; } while (tmp.PinConfig.Count > 30) { tmp.PinConfig.RemoveAt(0); } while (tmp.AxisConfig.Count > 8) { tmp.AxisConfig.RemoveAt(0); } for (int i = 0; i < 8; i++) { while (tmp.AxisConfig[i].CurveShape.Count > 11) { tmp.AxisConfig[i].CurveShape.RemoveAt(0); } } while (tmp.ShiftModificatorConfig.Count > 5) { tmp.ShiftModificatorConfig.RemoveAt(0); } while (tmp.ButtonConfig.Count > 128) { tmp.ButtonConfig.RemoveAt(0); } while (tmp.AxisToButtonsConfig.Count > 8) { tmp.AxisToButtonsConfig.RemoveAt(0); } for (int i = 0; i < 8; i++) { while (tmp.AxisToButtonsConfig[i].Points.Count > 13) { tmp.AxisToButtonsConfig[i].Points.RemoveAt(0); } } while (tmp.ShiftRegistersConfig.Count > 4) { tmp.ShiftRegistersConfig.RemoveAt(0); } tmp.DeviceName = tmp.DeviceName.TrimEnd('\0'); Config = tmp; } PinsVM.Config = Config; AxesVM.Config = Config; ButtonsVM.Config = Config; PinsVM.Update(Config); ButtonsVM.Update(Config); AxesVM.Update(Config); AxesToButtonsVM.Update(Config); ShiftRegistersVM.Update(Config); } }