/// <summary> /// Main method where all the action happens. /// Called by the Upload button. /// </summary> private void ButtonUpload_Click(object sender, EventArgs e) { // Clear the output box Debug.WriteLine("Clearing the output textboxes..."); this.textActivity.Clear(); this.textVerbose.Clear(); this.progressBar1.Value = 0; // Check if the file exists if (!File.Exists(this.textFileName.Text)) { this.AppendLog(string.Format("File {0} does not exist", this.textFileName.Text)); MessageBox.Show("Firmware file does not exist.", "Write Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } // Check that the file size is OK // Max size is 120,832B (118KB) with bootloader, 129,024B (126KB) without int maxFileSize = 129024; if (this.writeBootloader_Yes.Checked) { maxFileSize = 120832; } long length = new System.IO.FileInfo(this.textFileName.Text).Length; if (length > maxFileSize) { this.AppendLog(string.Format("Firmware file is too large.\r\nFile is {1:n0} KB, maximum size is {2:n0} KB.", this.textFileName.Text, length / 1024, maxFileSize / 1024)); MessageBox.Show("Firmware file is too large.", "Write Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } // Get the selected COM port string comPort = this.comPortSelector.SelectedValue.ToString(); // Check if the port can be opened if (!ComPort.CheckPort(comPort)) { this.AppendLog(string.Format("Couldn't open port {0}", comPort)); MessageBox.Show(string.Format("Couldn't open port {0}", comPort), "Write Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } // Disable the buttons until this flash attempt is complete Debug.WriteLine("Disabling the controls..."); this.EnableControls(false); // Determine if we should use Maple or serial interface MapleDevice mapleResult = MapleDevice.FindMaple(); if (mapleResult.DeviceFound == true) { this.AppendLog(string.Format("Maple device found in {0} mode\r\n", mapleResult.Mode)); } // Do the selected flash using the appropriate method if (mapleResult.DeviceFound == true) { // MapleFlashWrite(textFileName.Text, comPort); MapleDevice.WriteFlash(this, this.textFileName.Text, comPort); } else { SerialDevice.WriteFlash(this, this.textFileName.Text, comPort); } }
/// <summary> /// Main method where all the action happens. /// Called by the Upload button. /// </summary> private async void ButtonUpload_Click(object sender, EventArgs e) { // Disable the buttons until this flash attempt is complete Debug.WriteLine("Disabling the controls..."); this.EnableControls(false); // Clear the output box Debug.WriteLine("Clearing the output textboxes..."); this.textActivity.Clear(); this.textVerbose.Clear(); this.progressBar1.Value = 0; this.outputLineBuffer = string.Empty; // Check if the file exists if (!File.Exists(this.textFileName.Text)) { this.AppendLog(string.Format("File {0} does not exist", this.textFileName.Text)); MessageBox.Show("Firmware file does not exist.", "Write Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } // Check the file size if (!FileUtils.CheckFirmwareFileSize(this.textFileName.Text)) { this.EnableControls(true); return; } // Determine if we should use Maple device MapleDevice mapleResult = MapleDevice.FindMaple(); // Determine if we should use a USBasp device UsbAspDevice usbaspResult = UsbAspDevice.FindUsbAsp(); // Determine if the selected file contains USB / bootloader support bool firmwareSupportsUsb = FileUtils.CheckForUsbSupport(this.textFileName.Text); // Get the signature from the firmware file FileUtils.FirmwareFile fileSignature = FileUtils.GetFirmwareSignature(this.textFileName.Text); // Error if flashing non-USB firmware via native USB port if (mapleResult.DeviceFound && !firmwareSupportsUsb) { string msgBoxMessage = "The selected firmware file was compiled without USB support.\r\n\r\nFlashing this firmware would prevent the Multiprotocol module from functioning correctly.\r\n\r\nPlease select a different firmware file."; MessageBox.Show(msgBoxMessage, "Incompatible Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } // Get the selected COM port string comPort = this.comPortSelector.SelectedValue.ToString(); // Do the selected flash using the appropriate method if (mapleResult.DeviceFound == true) { Debug.WriteLine($"Maple device found in {mapleResult.Mode} mode\r\n"); await MapleDevice.WriteFlash(this, this.textFileName.Text, comPort); } else if (usbaspResult.DeviceFound == true && comPort == "USBasp") { if (fileSignature == null) { string msgBoxMessage = "Unable to check the specified firmware file for compatibility with this upload method."; MessageBox.Show(msgBoxMessage, "Incompatible Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } if (fileSignature.ModuleType != "AVR") { string msgBoxMessage = "The selected firmware file is not compatible with this upload method."; MessageBox.Show(msgBoxMessage, "Incompatible Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } await UsbAspDevice.WriteFlash(this, this.textFileName.Text, fileSignature.BootloaderSupport); } else { await SerialDevice.WriteFlash(this, this.textFileName.Text, comPort, firmwareSupportsUsb); } }