public static void Main() { //Check if the FlashHelper and FlashUpdater are available if (!File.Exists("FlashHelper.exe") || (!File.Exists("FlashReboot.exe"))) { //Show message MessageBox.Show("The FlashHelper.exe or the FlashReboot.exe is missing!\n" + "Please copy those two files to the same folder than the updater.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //End program execution return; } //Check if we can get the version file if (CheckVersionFile()) { //Delete old local file if new one downloaded if (File.Exists("Versions.dat") && File.Exists("Versions_new.dat")) { File.Delete("Versions.dat"); } //Rename the new file to use ist if (File.Exists("Versions_new.dat")) { File.Move("Versions_new.dat", "Versions.dat"); } //Start the updater UpdaterLogic.Start(); } //If no version file is available, show message and end program else { MessageBox.Show("Unable to download version file and no old copy found, exit!\n" + "Please connect to the internet and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/* Load a firmware from file */ private void LoadButton_Click(object sender, EventArgs e) { UpdaterLogic.LoadFirmware(); }
/* Exit the program */ private void ExitButton_Click(object sender, EventArgs e) { UpdaterLogic.Exit(); }
/* Start the flash procedure */ private void FlashButton_Click(object sender, EventArgs e) { UpdaterLogic.StartFlash(); }
/* Connect to the DIY-Thermocam V1 / V2 */ private void ConnectButton_Click(object sender, EventArgs e) { UpdaterLogic.Connect(); }
/* Flash procedure, that runs in the thread */ private void FlashProcedure() { //Extract versions from file UpdaterLogic.ExtractVersions(); //Loop forever while (true) { //As long as the user did not start the flash, wait if (!UpdaterLogic.FlashStarted) { continue; } //If there is no active connection, show warning to turn the device on if (!UpdaterLogic.Connected) { UpdaterLogic.GUI.ShowMessageBox("Turn the device on and connect it to the PC\n" + "It should be put into program mode automatically after you press OK.\n" + "If this does not work, open the backside of the enclosure and\n" + "short the P & G pins on the Teensy or the program jumper (V2 only)\n" + "for about one second with a screwdriver, the flash should start then.", "Important infos", MessageBoxIcon.Exclamation); } //User wants to start the flash, show warning with active connection else { UpdaterLogic.GUI.ShowMessageBox("The flash procedure does start now.\n" + "Do not close the program or disconnect the device!", "Important infos", MessageBoxIcon.Exclamation); } //Close serial connection if still active if (UpdaterLogic.Serial.CheckConnection()) { UpdaterLogic.Serial.CloseConnection(); } //Trigger a software reboot and put the MCU into programmer mode Process flashReboot = new Process { StartInfo = { FileName = "FlashReboot.exe" } }; flashReboot.Start(); //Wait until the reboot is done flashReboot.WaitForExit(); //Change the status of the UI during the flash MethodInvoker mi; //Repeat until the flash has been completed while (true) { //Create the argument for the flash helper string arg; //DIY-Thermocam V2 if (UpdaterLogic.HardwareVersion == 2) { arg = "-mmcu=mk66fx1m0 -v -w " + UpdaterLogic.Filepath; } //DIY-Thermocam V1 else { arg = "-mmcu=mk20dx256 -v -w " + UpdaterLogic.Filepath; } //Teensy flash helper process with corresponding arguments Process flashHelper = new Process { StartInfo = { FileName = "FlashHelper.exe", Arguments = arg, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, UseShellExecute = false, CreateNoWindow = true } }; //Redirect messages flashHelper.ErrorDataReceived += FlashErrorHandler; flashHelper.OutputDataReceived += FlashDataHandler; flashHelper.EnableRaisingEvents = true; //Start process flashHelper.Start(); flashHelper.BeginOutputReadLine(); flashHelper.BeginErrorReadLine(); //As long as the process has not exited while (!flashHelper.HasExited) { //Update flash info during programming mi = delegate { //Wait for device if (UpdaterLogic.FlashCounter == 0) { //Disable flash button UpdaterLogic.GUI.FlashButton.Enabled = false; UpdaterLogic.GUI.FlashButton.BackColor = Color.DimGray; UpdaterLogic.GUI.FlashButton.Update(); //Show waiting for device UpdaterLogic.GUI.FlashInfo.Text = "Waiting for device..."; UpdaterLogic.GUI.FlashInfo.Update(); } //Program devices else { //Calculate percentage byte percentage = (byte)((UpdaterLogic.FlashCounter / (flashSize / 1024.0)) * 100.0); //Show programming status UpdaterLogic.GUI.FlashInfo.Text = "Programming... (" + percentage + "%)"; UpdaterLogic.GUI.FlashInfo.Update(); } }; UpdaterLogic.GUI.Invoke(mi); } //Error during transmission, try again if (finished == false) { //Show error message if not start problems if (UpdaterLogic.FlashCounter > 4) { UpdaterLogic.GUI.ShowMessageBox("There was a critical error during the flash procedure.\n" + "Open the backside of the enclosure and short the two pins\n" + "P & G or the program jumper (V2 only) for about a second.\n" + "This puts the device in update mode, so the flash could start again.", "Flash Error", MessageBoxIcon.Exclamation); } //Reset flash coutner and try again UpdaterLogic.FlashCounter = 0; } //Transmission worked, continue else { break; } } //Restore UI mi = UpdaterLogic.GUI.RestoreUI; UpdaterLogic.GUI.Invoke(mi); //Reset file path UpdaterLogic.Filepath = ""; //Update status finished = false; UpdaterLogic.FlashStarted = false; } }