/// <summary> /// Called when the 'Connect' or 'Disconnect' button is clicked. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnConnect_Click(object sender, RoutedEventArgs e) { // If we are in reconnect mode.. if (bReconnect) { disconnectProviders(); btnConnect.Content = "Connect"; btnCalibrate.IsEnabled = false; btnCalibrate.Content = "Calibrate"; bConnected = false; bReconnect = false; barBattery.Value = 0; } // If we have been asked to connect. if (!bConnected) { // Connect. if (createProviders()) { // Update the button to say we are connected. btnConnect.Content = "Disconnect"; btnCalibrate.IsEnabled = true; bConnected = true; // Load calibration data. PersistentCalibrationData oData = loadPersistentCalibration("./Calibration.dat"); if (oData != null) { this.pWiiProvider.setCalibrationData(oData.Source, oData.Destination, oData.ScreenSize); btnCalibrate.Content = "Re-Calibrate"; App.TB.ShowBalloonTip("WiiTUIO", "Calibration loaded", BalloonIcon.Info); } } else { disconnectProviders(); btnConnect.Content = "Connect"; btnCalibrate.IsEnabled = false; btnCalibrate.Content = "Calibrate"; bConnected = false; barBattery.Value = 0; } } // Otherwise be sure I am disconnected. else { disconnectProviders(); btnConnect.Content = "Connect"; btnCalibrate.IsEnabled = false; btnCalibrate.Content = "Calibrate"; bConnected = false; barBattery.Value = 0; } }
/// <summary> /// Creates and saves a file which contains the calibration data. /// </summary> /// <param name="sFile">The location of the file to persist to</param> /// <param name="oData">The calibration data to persist</param> public static bool savePersistentCalibration(string sFile, PersistentCalibrationData oData) { try { FileStream stream = File.Open(sFile, FileMode.Create); new BinaryFormatter().Serialize(stream, oData); stream.Close(); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Loads the specified file, which should contain calibration data. /// </summary> /// <param name="sFile">The location of the file to load</param> public static PersistentCalibrationData loadPersistentCalibration(string sFile) { try { if (File.Exists(sFile)) { // De-serialise data from file Stream stream = File.Open(sFile, FileMode.Open); PersistentCalibrationData data = (PersistentCalibrationData) new BinaryFormatter().Deserialize(stream); stream.Close(); return(data); } return(null); } catch (Exception) { return(null); } }