public static bool ValidateConfig(config cfg, out string failure_message) { if (!Enum.IsDefined(typeof(ButtonModes), cfg.buttons_mode)) { failure_message = "buttons_mode incorrect."; return false; } if (!Enum.IsDefined(typeof(AFModes), cfg.af_mode)) { failure_message = "af_mode incorrect."; return false; } if (!Enum.IsDefined(typeof(RotaryModes), cfg.rotary_mode)) { failure_message = "rotary_mode incorrect."; return false; } if ((cfg.af_high_hz > 120) || (cfg.af_high_hz < 0.5)) { failure_message = "af_high_hz out of range."; return false; } if ((cfg.af_low_hz > 120) || (cfg.af_low_hz < 0.5)) { failure_message = "af_low_hz out of range."; return false; } failure_message = "No failures."; return true; }
// generate EEPROM to write to device public static bool GenerateEEPROMConfig(config cfg, out eep_config eep) { eep = new eep_config(); eep.config_id = CONFIG_ID; eep.config_size = (UInt16)Marshal.SizeOf(typeof(eep_config)); eep.button_mode = (byte)cfg.buttons_mode; eep.af_mode = (byte)cfg.af_mode; eep.rotary_mode = (byte)cfg.rotary_mode; eep.af_high_05hz = (byte)(cfg.af_high_hz * 2); eep.af_low_05hz = (byte)(cfg.af_low_hz * 2); //eep.af_high_hz_x10 = (UInt16)(cfg.af_high_hz * 10); //if (!calc_timer(cfg.af_high_hz, out eep.af_high_div, out eep.af_high_per)) // return false; //eep.af_low_hz_x10 = (UInt16)(cfg.af_low_hz * 10); //if (!calc_timer(cfg.af_low_hz, out eep.af_low_div, out eep.af_low_per)) // return false; eep.af_mask = 0; for (int i = 0; i < 16; i++) { if (cfg.af_mask[i]) eep.af_mask |= (UInt16)(1 << i); } if (cfg.start_af_toggle_mode) eep.start_af_toggle_mode = 0xFF; else eep.start_af_toggle_mode = 0x00; if (cfg.af_save_state) eep.af_save_state = 0xFF; else eep.af_save_state = 0; // calculate CRC32 int size = Marshal.SizeOf(eep); byte[] buffer = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(eep, ptr, true); Marshal.Copy(ptr, buffer, 0, size); Marshal.FreeHGlobal(ptr); eep.crc32 = crc32(buffer, size - 4); // don't include the 4 byte CRC return true; }
// decode EEPROM read from device public static config DecodeEEPROMConfig(eep_config eep, out string failure_message) { // check CRC32 int size = Marshal.SizeOf(eep); byte[] buffer = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(eep, ptr, true); Marshal.Copy(ptr, buffer, 0, size); Marshal.FreeHGlobal(ptr); if (eep.crc32 != crc32(buffer, size - 4)) { failure_message = "Bad CRC."; return null; } // decode config var cfg = new config(); cfg.buttons_mode = eep.button_mode; cfg.af_mode = eep.af_mode; cfg.rotary_mode = eep.rotary_mode; cfg.af_high_hz = eep.af_high_05hz / 2; cfg.af_low_hz = eep.af_low_05hz / 2; for (int i = 0; i < 16; i++) { if ((eep.af_mask & (1 << i)) != 0) cfg.af_mask[i] = true; else cfg.af_mask[i] = false; } if (eep.start_af_toggle_mode != 0) cfg.start_af_toggle_mode = true; else cfg.start_af_toggle_mode = false; if (eep.af_save_state != 0) cfg.af_save_state = true; else cfg.af_save_state = false; if (!config.ValidateConfig(cfg, out failure_message)) return null; return cfg; }
public MainWindow() { InitializeComponent(); this.DataContext = this; Closing += OnWindowClosing; dlgSettings = new MetroDialogSettings(); dlgSettings.AnimateHide = false; dlgSettings.AnimateShow = false; af_checkboxes = new CheckBox[] { chkBtn1AF, chkBtn2AF, chkBtn3AF, chkBtn4AF, chkBtn5AF, chkBtn6AF, chkBtn7AF, chkBtn8AF, chkBtn9AF, chkBtn10AF, chkBtn11AF, chkBtn12AF, chkBtn13AF, chkBtn14AF, chkBtn15AF, chkBtn16AF }; this.Show(); current_cfg = new config(); current_cfg.LoadDefaults(); string filename = Properties.Settings.Default.LastFileName; SetFilenameButton(filename); if (filename != "") { string failure_message; var new_config = config.ReadFromFile(filename, out failure_message); if (new_config == null) { Console.WriteLine(failure_message); //ShowMessage("Failed to open test.cfg", failure_message, MessageDialogStyle.Affirmative); } else { current_cfg = new_config; SyncUIToConfig(current_cfg); saved = true; SetFilenameButton(filename); } } }
// sync config to UI private void SyncConfigToUI(ref config cfg) { cfg.buttons_mode = cmbButtonMode.SelectedIndex; cfg.af_mode = cmbAFMode.SelectedIndex; cfg.rotary_mode = cmbRotaryMode.SelectedIndex; cfg.af_high_hz = (float)numAFHigh.Value; cfg.af_low_hz = (float)numAFLow.Value; cfg.af_mask[0] = (bool)chkBtn1AF.IsChecked; cfg.af_mask[1] = (bool)chkBtn2AF.IsChecked; cfg.af_mask[2] = (bool)chkBtn3AF.IsChecked; cfg.af_mask[3] = (bool)chkBtn4AF.IsChecked; cfg.af_mask[4] = (bool)chkBtn5AF.IsChecked; cfg.af_mask[5] = (bool)chkBtn6AF.IsChecked; cfg.af_mask[6] = (bool)chkBtn7AF.IsChecked; cfg.af_mask[7] = (bool)chkBtn8AF.IsChecked; cfg.af_mask[8] = (bool)chkBtn9AF.IsChecked; cfg.af_mask[9] = (bool)chkBtn10AF.IsChecked; cfg.af_mask[10] = (bool)chkBtn11AF.IsChecked; cfg.af_mask[11] = (bool)chkBtn12AF.IsChecked; cfg.af_mask[12] = (bool)chkBtn13AF.IsChecked; cfg.af_mask[13] = (bool)chkBtn14AF.IsChecked; cfg.af_mask[14] = (bool)chkBtn15AF.IsChecked; cfg.af_mask[15] = (bool)chkBtn16AF.IsChecked; cfg.af_save_state = (bool)chkAFSave.IsChecked; cfg.start_af_toggle_mode = (bool)chkStartToggleMode.IsChecked; }
// sync UI to config private void SyncUIToConfig(config cfg) { cmbButtonMode.SelectedIndex = cfg.buttons_mode; cmbAFMode.SelectedIndex = cfg.af_mode; cmbRotaryMode.SelectedIndex = cfg.rotary_mode; numAFHigh.Value = cfg.af_high_hz; numAFLow.Value = cfg.af_low_hz; chkBtn1AF.IsChecked = cfg.af_mask[0]; chkBtn2AF.IsChecked = cfg.af_mask[1]; chkBtn3AF.IsChecked = cfg.af_mask[2]; chkBtn4AF.IsChecked = cfg.af_mask[3]; chkBtn5AF.IsChecked = cfg.af_mask[4]; chkBtn6AF.IsChecked = cfg.af_mask[5]; chkBtn7AF.IsChecked = cfg.af_mask[6]; chkBtn8AF.IsChecked = cfg.af_mask[7]; chkBtn9AF.IsChecked = cfg.af_mask[8]; chkBtn10AF.IsChecked = cfg.af_mask[9]; chkBtn11AF.IsChecked = cfg.af_mask[10]; chkBtn12AF.IsChecked = cfg.af_mask[11]; chkBtn13AF.IsChecked = cfg.af_mask[12]; chkBtn14AF.IsChecked = cfg.af_mask[13]; chkBtn15AF.IsChecked = cfg.af_mask[14]; chkBtn16AF.IsChecked = cfg.af_mask[15]; chkAFSave.IsChecked = cfg.af_save_state; chkStartToggleMode.IsChecked = cfg.start_af_toggle_mode; }
/* load and save */ private void btnLoad_Click(object sender, RoutedEventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "SUPERPLAY configuration files|*.spconfig|All files|*.*"; dialog.CheckFileExists = true; dialog.DereferenceLinks = true; string filename = Properties.Settings.Default.LastFileName; if (filename != "") dialog.FileName = filename; else dialog.InitialDirectory = "%CSIDL_COMMON_DOCUMENTS%"; if (dialog.ShowDialog() == true) { filename = dialog.FileName; Console.WriteLine(filename); // try to load file string failure_message; var new_config = config.ReadFromFile(filename, out failure_message); if (new_config == null) { Console.WriteLine(failure_message); AsyncMessage("Failed to open file", filename = "\n\n" + failure_message, MessageDialogStyle.Affirmative); } else { current_cfg = new_config; SyncUIToConfig(current_cfg); saved = true; SetFilenameButton(System.IO.Path.GetFileName(filename)); Properties.Settings.Default.LastFileName = filename; Properties.Settings.Default.Save(); } } else Console.WriteLine("User cancelled."); }