From 25ba3dfb2da6eee7653f280c2b6889b59eeb726d Mon Sep 17 00:00:00 2001 From: fauxpark Date: Tue, 13 Apr 2021 16:41:11 +1000 Subject: [PATCH 1/3] [Windows] Autoflash improvements --- windows/QMK Toolbox/App.config | 48 +++---- windows/QMK Toolbox/Flashing.cs | 32 ++++- windows/QMK Toolbox/MainWindow.Designer.cs | 49 ++++--- windows/QMK Toolbox/MainWindow.cs | 120 +++++++++++------- .../Properties/Settings.Designer.cs | 2 +- 5 files changed, 148 insertions(+), 103 deletions(-) diff --git a/windows/QMK Toolbox/App.config b/windows/QMK Toolbox/App.config index c5c3f8cd9b..6ca6e0eddd 100644 --- a/windows/QMK Toolbox/App.config +++ b/windows/QMK Toolbox/App.config @@ -12,30 +12,30 @@ - - - - - atmega32u4 - - - False - - - 1 - - - - - - - - - True - - - False - + + + + + atmega32u4 + + + False + + + 1 + + + + + + + + + True + + + False + diff --git a/windows/QMK Toolbox/Flashing.cs b/windows/QMK Toolbox/Flashing.cs index 69db0a0e17..833f1150ae 100644 --- a/windows/QMK Toolbox/Flashing.cs +++ b/windows/QMK Toolbox/Flashing.cs @@ -175,6 +175,18 @@ public void Reset(string mcu) ResetAtmelSamBa(); } + public void ClearEeprom(string mcu) + { + if (Usb.CanFlash(Chipset.AtmelDfu)) + ClearEepromAtmelDfu(mcu); + if (Usb.CanFlash(Chipset.Caterina)) + ClearEepromCaterina(mcu); + if (Usb.CanFlash(Chipset.UsbAsp)) + ClearEepromUsbAsp(mcu); + } + + public bool CanFlash() => Usb.AreDevicesAvailable(); + public bool CanReset() { var resettable = new List { @@ -191,14 +203,20 @@ public bool CanReset() return false; } - public void ClearEeprom(string mcu) + public bool CanClearEeprom() { - if (Usb.CanFlash(Chipset.AtmelDfu)) - ClearEepromAtmelDfu(mcu); - if (Usb.CanFlash(Chipset.Caterina)) - ClearEepromCaterina(mcu); - if (Usb.CanFlash(Chipset.UsbAsp)) - ClearEepromUsbAsp(mcu); + var clearable = new List + { + Chipset.AtmelDfu, + Chipset.Caterina, + Chipset.UsbAsp + }; + foreach (Chipset chipset in clearable) + { + if (Usb.CanFlash(chipset)) + return true; + } + return false; } private void FlashAtmelDfu(string mcu, string file) diff --git a/windows/QMK Toolbox/MainWindow.Designer.cs b/windows/QMK Toolbox/MainWindow.Designer.cs index fde15225d5..cce41d659d 100644 --- a/windows/QMK Toolbox/MainWindow.Designer.cs +++ b/windows/QMK Toolbox/MainWindow.Designer.cs @@ -55,7 +55,6 @@ private void InitializeComponent() { this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripSeparator(); this.clearToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.hidList = new System.Windows.Forms.ComboBox(); - this.flashWhenReadyCheckbox = new System.Windows.Forms.CheckBox(); this.statusStrip.SuspendLayout(); this.qmkGroupBox.SuspendLayout(); this.fileGroupBox.SuspendLayout(); @@ -66,6 +65,7 @@ private void InitializeComponent() { // flashButton // this.flashButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.flashButton.Enabled = false; this.flashButton.Location = new System.Drawing.Point(657, 59); this.flashButton.Name = "flashButton"; this.flashButton.Size = new System.Drawing.Size(57, 23); @@ -81,7 +81,7 @@ private void InitializeComponent() { this.autoflashCheckbox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.autoflashCheckbox.AutoSize = true; this.autoflashCheckbox.BackColor = System.Drawing.Color.Transparent; - this.autoflashCheckbox.Location = new System.Drawing.Point(710, 86); + this.autoflashCheckbox.Location = new System.Drawing.Point(657, 86); this.autoflashCheckbox.Name = "autoflashCheckbox"; this.autoflashCheckbox.Size = new System.Drawing.Size(76, 17); this.autoflashCheckbox.TabIndex = 5; @@ -100,7 +100,7 @@ private void InitializeComponent() { // openFileButton // this.openFileButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.openFileButton.Location = new System.Drawing.Point(476, 19); + this.openFileButton.Location = new System.Drawing.Point(581, 19); this.openFileButton.Name = "openFileButton"; this.openFileButton.Size = new System.Drawing.Size(64, 23); this.openFileButton.TabIndex = 3; @@ -114,6 +114,7 @@ private void InitializeComponent() { // resetButton // this.resetButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.resetButton.Enabled = false; this.resetButton.Location = new System.Drawing.Point(720, 59); this.resetButton.Name = "resetButton"; this.resetButton.Size = new System.Drawing.Size(67, 23); @@ -143,7 +144,7 @@ private void InitializeComponent() { // this.mcuLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.mcuLabel.AutoSize = true; - this.mcuLabel.Location = new System.Drawing.Point(546, 0); + this.mcuLabel.Location = new System.Drawing.Point(648, 0); this.mcuLabel.Name = "mcuLabel"; this.mcuLabel.Size = new System.Drawing.Size(84, 13); this.mcuLabel.TabIndex = 22; @@ -151,21 +152,24 @@ private void InitializeComponent() { // // qmkGroupBox // + this.qmkGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); this.qmkGroupBox.Controls.Add(this.keymapLabel); this.qmkGroupBox.Controls.Add(this.keymapBox); this.qmkGroupBox.Controls.Add(this.keyboardBox); this.qmkGroupBox.Controls.Add(this.loadKeymap); this.qmkGroupBox.Location = new System.Drawing.Point(6, 59); this.qmkGroupBox.Name = "qmkGroupBox"; - this.qmkGroupBox.Size = new System.Drawing.Size(581, 48); + this.qmkGroupBox.Size = new System.Drawing.Size(645, 48); this.qmkGroupBox.TabIndex = 23; this.qmkGroupBox.TabStop = false; this.qmkGroupBox.Text = "Keyboard from qmk.fm"; // // keymapLabel // + this.keymapLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.keymapLabel.AutoSize = true; - this.keymapLabel.Location = new System.Drawing.Point(355, 0); + this.keymapLabel.Location = new System.Drawing.Point(457, 0); this.keymapLabel.Name = "keymapLabel"; this.keymapLabel.Size = new System.Drawing.Size(45, 13); this.keymapLabel.TabIndex = 24; @@ -173,14 +177,15 @@ private void InitializeComponent() { // // keymapBox // + this.keymapBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.keymapBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::QMK_Toolbox.Properties.Settings.Default, "keymap", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); this.keymapBox.Enabled = false; this.keymapBox.FormattingEnabled = true; this.keymapBox.Items.AddRange(new object[] { "later version!"}); - this.keymapBox.Location = new System.Drawing.Point(353, 20); + this.keymapBox.Location = new System.Drawing.Point(457, 20); this.keymapBox.Name = "keymapBox"; - this.keymapBox.Size = new System.Drawing.Size(152, 21); + this.keymapBox.Size = new System.Drawing.Size(112, 21); this.keymapBox.TabIndex = 4; this.keymapBox.Tag = "The target (MCU) of the flashing"; this.keymapBox.Text = global::QMK_Toolbox.Properties.Settings.Default.keymap; @@ -189,6 +194,8 @@ private void InitializeComponent() { // // keyboardBox // + this.keyboardBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); this.keyboardBox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend; this.keyboardBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems; this.keyboardBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::QMK_Toolbox.Properties.Settings.Default, "keyboard", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); @@ -198,7 +205,7 @@ private void InitializeComponent() { "this feature coming in"}); this.keyboardBox.Location = new System.Drawing.Point(6, 20); this.keyboardBox.Name = "keyboardBox"; - this.keyboardBox.Size = new System.Drawing.Size(341, 21); + this.keyboardBox.Size = new System.Drawing.Size(445, 21); this.keyboardBox.TabIndex = 4; this.keyboardBox.Tag = "The target (MCU) of the flashing"; this.keyboardBox.Text = global::QMK_Toolbox.Properties.Settings.Default.keyboard; @@ -208,8 +215,9 @@ private void InitializeComponent() { // // loadKeymap // + this.loadKeymap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.loadKeymap.Enabled = false; - this.loadKeymap.Location = new System.Drawing.Point(511, 19); + this.loadKeymap.Location = new System.Drawing.Point(575, 19); this.loadKeymap.Name = "loadKeymap"; this.loadKeymap.Size = new System.Drawing.Size(64, 23); this.loadKeymap.TabIndex = 3; @@ -243,7 +251,7 @@ private void InitializeComponent() { this.filepathBox.FormattingEnabled = true; this.filepathBox.Location = new System.Drawing.Point(6, 20); this.filepathBox.Name = "filepathBox"; - this.filepathBox.Size = new System.Drawing.Size(464, 21); + this.filepathBox.Size = new System.Drawing.Size(569, 21); this.filepathBox.TabIndex = 2; this.filepathBox.Tag = "The path for your firmware file"; this.filepathBox.Text = global::QMK_Toolbox.Properties.Settings.Default.hexFileSetting; @@ -257,9 +265,9 @@ private void InitializeComponent() { this.mcuBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::QMK_Toolbox.Properties.Settings.Default, "targetSetting", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); this.mcuBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.mcuBox.FormattingEnabled = true; - this.mcuBox.Location = new System.Drawing.Point(546, 20); + this.mcuBox.Location = new System.Drawing.Point(651, 20); this.mcuBox.Name = "mcuBox"; - this.mcuBox.Size = new System.Drawing.Size(214, 21); + this.mcuBox.Size = new System.Drawing.Size(129, 21); this.mcuBox.TabIndex = 4; this.mcuBox.Tag = "The target (MCU) of the flashing"; this.mcuBox.Text = global::QMK_Toolbox.Properties.Settings.Default.targetSetting; @@ -269,6 +277,7 @@ private void InitializeComponent() { // clearEepromButton // this.clearEepromButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.clearEepromButton.Enabled = false; this.clearEepromButton.Location = new System.Drawing.Point(12, 613); this.clearEepromButton.Name = "clearEepromButton"; this.clearEepromButton.Size = new System.Drawing.Size(110, 23); @@ -400,18 +409,6 @@ private void InitializeComponent() { this.hidList.Size = new System.Drawing.Size(658, 21); this.hidList.TabIndex = 29; // - // flashWhenReadyCheckbox - // - this.flashWhenReadyCheckbox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.flashWhenReadyCheckbox.AutoSize = true; - this.flashWhenReadyCheckbox.Location = new System.Drawing.Point(593, 86); - this.flashWhenReadyCheckbox.Name = "flashWhenReadyCheckbox"; - this.flashWhenReadyCheckbox.Size = new System.Drawing.Size(109, 17); - this.flashWhenReadyCheckbox.TabIndex = 30; - this.flashWhenReadyCheckbox.Text = "Flash when ready"; - this.flashWhenReadyCheckbox.UseVisualStyleBackColor = true; - this.flashWhenReadyCheckbox.CheckedChanged += new System.EventHandler(this.flashWhenReadyCheckbox_CheckedChanged); - // // MainWindow // this.AllowDrop = true; @@ -419,7 +416,6 @@ private void InitializeComponent() { this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(799, 661); this.ContextMenuStrip = this.contextMenuStrip1; - this.Controls.Add(this.flashWhenReadyCheckbox); this.Controls.Add(this.hidList); this.Controls.Add(this.clearEepromButton); this.Controls.Add(this.fileGroupBox); @@ -473,7 +469,6 @@ private void InitializeComponent() { private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem; private System.Windows.Forms.ComboBox hidList; - private System.Windows.Forms.CheckBox flashWhenReadyCheckbox; private System.Windows.Forms.ContextMenuStrip contextMenuStrip2; private System.Windows.Forms.ToolStripMenuItem clearToolStripMenuItem; private BetterComboBox filepathBox; diff --git a/windows/QMK Toolbox/MainWindow.cs b/windows/QMK Toolbox/MainWindow.cs index a42df0815e..76e4ed41d8 100644 --- a/windows/QMK Toolbox/MainWindow.cs +++ b/windows/QMK Toolbox/MainWindow.cs @@ -148,7 +148,6 @@ public MainWindow(string path) : this() _flasher = new Flashing(_printer); _usb = new Usb(_flasher, _printer); _flasher.Usb = _usb; - resetButton.Enabled = false; StartListeningForDeviceEvents(); } @@ -209,6 +208,9 @@ private void MainWindow_Load(object sender, EventArgs e) collection = searcher.Get(); _usb.DetectBootloaderFromCollection(collection); + flashButton.Enabled = _flasher.CanFlash(); + resetButton.Enabled = _flasher.CanReset(); + clearEepromButton.Enabled = _flasher.CanClearEeprom(); UpdateHidDevices(false); @@ -270,8 +272,6 @@ private void flashButton_Click(object sender, EventArgs e) { if (!InvokeRequired) { - flashButton.Enabled = false; - resetButton.Enabled = false; var mcu = mcuBox.Text; var filePath = filepathBox.Text; @@ -293,22 +293,34 @@ private void flashButton_Click(object sender, EventArgs e) } if (error == 0) { + if (!autoflashCheckbox.Checked) + { + this.Invoke((MethodInvoker)delegate + { + flashButton.Enabled = false; + resetButton.Enabled = false; + clearEepromButton.Enabled = false; + }); + } + _printer.Print("Attempting to flash, please don't remove device", MessageType.Bootloader); _flasher.Flash(mcu, filePath); + + if (!autoflashCheckbox.Checked) + { + this.Invoke((MethodInvoker)delegate + { + flashButton.Enabled = _flasher.CanFlash(); + resetButton.Enabled = _flasher.CanReset(); + clearEepromButton.Enabled = _flasher.CanClearEeprom(); + }); + } } } else { _printer.Print("There are no devices available", MessageType.Error); } - - // Re-enable flash/reset button after flashing - this.Invoke((MethodInvoker)delegate - { - flashButton.Enabled = true; - resetButton.Enabled = _flasher.CanReset(); - }); - }).Start(); } else @@ -321,9 +333,6 @@ private void resetButton_Click(object sender, EventArgs e) { if (!InvokeRequired) { - flashButton.Enabled = false; - resetButton.Enabled = false; - if (_usb.AreDevicesAvailable()) { var error = 0; @@ -334,16 +343,33 @@ private void resetButton_Click(object sender, EventArgs e) } if (error == 0) { + if (!autoflashCheckbox.Checked) + { + this.Invoke((MethodInvoker)delegate + { + flashButton.Enabled = false; + resetButton.Enabled = false; + clearEepromButton.Enabled = false; + }); + } + _flasher.Reset(mcuBox.Text); + + if (!autoflashCheckbox.Checked) + { + this.Invoke((MethodInvoker)delegate + { + flashButton.Enabled = _flasher.CanFlash(); + resetButton.Enabled = _flasher.CanReset(); + clearEepromButton.Enabled = _flasher.CanClearEeprom(); + }); + } } } else { _printer.Print("There are no devices available", MessageType.Error); } - - flashButton.Enabled = true; - resetButton.Enabled = _flasher.CanReset(); } else { @@ -355,8 +381,6 @@ private void clearEepromButton_Click(object sender, EventArgs e) { if (!InvokeRequired) { - clearEepromButton.Enabled = false; - if (_usb.AreDevicesAvailable()) { var error = 0; @@ -367,15 +391,33 @@ private void clearEepromButton_Click(object sender, EventArgs e) } if (error == 0) { + if (!autoflashCheckbox.Checked) + { + this.Invoke((MethodInvoker)delegate + { + flashButton.Enabled = false; + resetButton.Enabled = false; + clearEepromButton.Enabled = false; + }); + } + _flasher.ClearEeprom(mcuBox.Text); + + if (!autoflashCheckbox.Checked) + { + this.Invoke((MethodInvoker)delegate + { + flashButton.Enabled = _flasher.CanFlash(); + resetButton.Enabled = _flasher.CanReset(); + clearEepromButton.Enabled = _flasher.CanClearEeprom(); + }); + } } } else { _printer.Print("There are no devices available", MessageType.Error); } - - clearEepromButton.Enabled = true; } else { @@ -444,16 +486,6 @@ private static string GetManufacturerString(IHidDevice d) return System.Text.Encoding.Default.GetString(bs.Where(b => b > 0).ToArray()); } - private void DeviceAttachedHandler() - { - // not sure if this will be useful - } - - private void DeviceRemovedHandler() - { - // not sure if this will be useful - } - private void OnReport(HidReport report) { var data = report.Data; @@ -599,16 +631,20 @@ private void DeviceEvent(object sender, EventArrivedEventArgs e) else if (_usb.DetectBootloader(instance) && autoflashCheckbox.Checked) { flashButton_Click(sender, e); - - if (flashWhenReadyCheckbox.Checked) - { - Invoke(new Action(() => flashWhenReadyCheckbox.Checked = false)); - } } UpdateHidDevices(deviceDisconnected); (sender as ManagementEventWatcher)?.Start(); - resetButton.Enabled = _flasher.CanReset(); + + if (!autoflashCheckbox.Checked) + { + this.Invoke((MethodInvoker)delegate + { + flashButton.Enabled = _flasher.CanFlash(); + resetButton.Enabled = _flasher.CanReset(); + clearEepromButton.Enabled = _flasher.CanClearEeprom(); + }); + } } private void StartListeningForDeviceEvents() @@ -631,12 +667,14 @@ private void autoflashCheckbox_CheckedChanged(object sender, EventArgs e) _printer.Print("Auto-flash enabled", MessageType.Info); flashButton.Enabled = false; resetButton.Enabled = false; + clearEepromButton.Enabled = false; } else { _printer.Print("Auto-flash disabled", MessageType.Info); - flashButton.Enabled = true; + flashButton.Enabled = _flasher.CanFlash(); resetButton.Enabled = _flasher.CanReset(); + clearEepromButton.Enabled = _flasher.CanClearEeprom(); } } @@ -702,12 +740,6 @@ private void MainWindow_DragEnter(object sender, DragEventArgs e) if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy; } - private void flashWhenReadyCheckbox_CheckedChanged(object sender, EventArgs e) - { - flashButton.Enabled = !flashWhenReadyCheckbox.Checked; - autoflashCheckbox.Enabled = !flashWhenReadyCheckbox.Checked; - } - private void MainWindow_Shown(object sender, EventArgs e) { if (Settings.Default.firstStart) diff --git a/windows/QMK Toolbox/Properties/Settings.Designer.cs b/windows/QMK Toolbox/Properties/Settings.Designer.cs index b39b155229..65ca972a6f 100644 --- a/windows/QMK Toolbox/Properties/Settings.Designer.cs +++ b/windows/QMK Toolbox/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace QMK_Toolbox.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.7.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.8.1.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); From d16a30229fc0d9a6a27cabdb13ab8814bb65e287 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Tue, 13 Apr 2021 17:37:38 +1000 Subject: [PATCH 2/3] [macOS] Autoflash improvements --- osx/qmk_toolbox/AppDelegate.m | 60 +++++++++++++++++++++++-- osx/qmk_toolbox/Base.lproj/MainMenu.xib | 13 +++--- osx/qmk_toolbox/Flashing.h | 6 ++- osx/qmk_toolbox/Flashing.m | 31 ++++++++++--- osx/qmk_toolbox/USB.m | 4 +- 5 files changed, 95 insertions(+), 19 deletions(-) diff --git a/osx/qmk_toolbox/AppDelegate.m b/osx/qmk_toolbox/AppDelegate.m index 7fa4de4fd4..2b8a3983d9 100644 --- a/osx/qmk_toolbox/AppDelegate.m +++ b/osx/qmk_toolbox/AppDelegate.m @@ -59,14 +59,25 @@ - (IBAction) flashButtonClick:(id) sender { error++; } if (error == 0) { - [_printer print:@"Attempting to flash, please don't remove device" withType:MessageType_Bootloader]; + if (self.autoFlashButton.state != NSOnState) { + self.flashButton.enabled = NO; + self.resetButton.enabled = NO; + self.clearEEPROMButton.enabled = NO; + } + [_printer print:@"Attempting to flash, please don't remove device" withType:MessageType_Bootloader]; // this is dumb, but the delay is required to let the previous print command show up double delayInSeconds = .01; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self->_flasher performSelector:@selector(flash:withFile:) withObject:[self->_mcuBox objectValue] withObject:[self->_filepathBox objectValue]]; }); + + if (self.autoFlashButton.state != NSOnState) { + self.flashButton.enabled = [_flasher canFlash]; + self.resetButton.enabled = [_flasher canReset]; + self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; + } } } else { [_printer print:@"There are no devices available" withType:MessageType_Error]; @@ -77,7 +88,19 @@ - (IBAction) resetButtonClick:(id) sender { if ([[_mcuBox objectValue] isEqualToString:@""]) { [_printer print:@"Please select a microcontroller" withType:MessageType_Error]; } else { + if (self.autoFlashButton.state != NSOnState) { + self.flashButton.enabled = NO; + self.resetButton.enabled = NO; + self.clearEEPROMButton.enabled = NO; + } + [_flasher reset:(NSString *)[_mcuBox objectValue]]; + + if (self.autoFlashButton.state != NSOnState) { + self.flashButton.enabled = [_flasher canFlash]; + self.resetButton.enabled = [_flasher canReset]; + self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; + } } } @@ -85,7 +108,19 @@ - (IBAction) clearEEPROMButtonClick:(id) sender { if ([[_mcuBox objectValue] isEqualToString:@""]) { [_printer print:@"Please select a microcontroller" withType:MessageType_Error]; } else { + if (self.autoFlashButton.state != NSOnState) { + self.flashButton.enabled = NO; + self.resetButton.enabled = NO; + self.clearEEPROMButton.enabled = NO; + } + [_flasher clearEEPROM:(NSString *)[_mcuBox objectValue]]; + + if (self.autoFlashButton.state != NSOnState) { + self.flashButton.enabled = [_flasher canFlash]; + self.resetButton.enabled = [_flasher canReset]; + self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; + } } } @@ -93,15 +128,33 @@ - (void)setSerialPort:(NSString *)port { _flasher.serialPort = port; } +- (IBAction) autoFlashButtonClick:(id)sender { + if ([_autoFlashButton state] == NSOnState) { + [_printer print:@"Auto-flash enabled" withType:MessageType_Info]; + self.flashButton.enabled = NO; + self.resetButton.enabled = NO; + self.clearEEPROMButton.enabled = NO; + } else { + [_printer print:@"Auto-flash disabled" withType:MessageType_Info]; + self.flashButton.enabled = [_flasher canFlash]; + self.resetButton.enabled = [_flasher canReset]; + self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; + } +} + - (void)deviceConnected:(Chipset)chipset { if ([_autoFlashButton state] == NSOnState) { [self flashButtonClick:NULL]; } - self.resetButton.enabled = [self.flasher canReset]; + self.flashButton.enabled = [_flasher canFlash]; + self.resetButton.enabled = [_flasher canReset]; + self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; } - (void)deviceDisconnected:(Chipset)chipset { - self.resetButton.enabled = [self.flasher canReset]; + self.flashButton.enabled = [_flasher canFlash]; + self.resetButton.enabled = [_flasher canReset]; + self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; } - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename { @@ -177,7 +230,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { _printer = [[Printing alloc] initWithTextView:_textView]; _flasher = [[Flashing alloc] initWithPrinter:_printer]; _flasher.delegate = self; - _resetButton.enabled = NO; [[_textView menu] addItem: [NSMenuItem separatorItem]]; [[_textView menu] addItem: _clearMenuItem]; diff --git a/osx/qmk_toolbox/Base.lproj/MainMenu.xib b/osx/qmk_toolbox/Base.lproj/MainMenu.xib index 57b0b20aae..f1470717ac 100644 --- a/osx/qmk_toolbox/Base.lproj/MainMenu.xib +++ b/osx/qmk_toolbox/Base.lproj/MainMenu.xib @@ -1,8 +1,8 @@ - + - + @@ -196,7 +196,7 @@ - + @@ -210,7 +210,7 @@ - + @@ -228,6 +228,9 @@ + + + @@ -383,7 +386,7 @@ - + diff --git a/osx/qmk_toolbox/Flashing.h b/osx/qmk_toolbox/Flashing.h index 489ab956a6..cc1732d574 100644 --- a/osx/qmk_toolbox/Flashing.h +++ b/osx/qmk_toolbox/Flashing.h @@ -39,8 +39,12 @@ typedef enum { - (void)flash:(NSString *)mcu withFile:(NSString *)file; - (void)reset:(NSString *)mcu; -- (BOOL)canReset; - (void)clearEEPROM:(NSString *)mcu; + +- (BOOL)canFlash; +- (BOOL)canReset; +- (BOOL)canClearEEPROM; + @property NSString * serialPort; @property (nonatomic, assign) id delegate; diff --git a/osx/qmk_toolbox/Flashing.m b/osx/qmk_toolbox/Flashing.m index 7b22f3b4fb..1529e2c2fc 100644 --- a/osx/qmk_toolbox/Flashing.m +++ b/osx/qmk_toolbox/Flashing.m @@ -88,6 +88,19 @@ - (void)reset:(NSString *)mcu { [self resetBootloadHID]; } +- (void)clearEEPROM:(NSString *)mcu { + if ([USB canFlash:AtmelDFU]) + [self clearEEPROMAtmelDFU:mcu]; + if ([USB canFlash:Caterina]) + [self clearEEPROMCaterina:mcu]; + if ([USB canFlash:USBAsp]) + [self clearEEPROMUSBAsp:mcu]; +} + +- (BOOL)canFlash { + return [USB areDevicesAvailable]; +} + - (BOOL)canReset { NSArray *resettable = @[ @(AtmelDFU), @@ -102,13 +115,17 @@ - (BOOL)canReset { return NO; } -- (void)clearEEPROM:(NSString *)mcu { - if ([USB canFlash:AtmelDFU]) - [self clearEEPROMAtmelDFU:mcu]; - if ([USB canFlash:Caterina]) - [self clearEEPROMCaterina:mcu]; - if ([USB canFlash:USBAsp]) - [self clearEEPROMUSBAsp:mcu]; +- (BOOL)canClearEEPROM { + NSArray *clearable = @[ + @(AtmelDFU), + @(Caterina), + @(USBAsp) + ]; + for (NSNumber *chipset in clearable) { + if ([USB canFlash:(Chipset)chipset.intValue]) + return YES; + } + return NO; } - (void)flashAtmelDFU:(NSString *)mcu withFile:(NSString *)file { diff --git a/osx/qmk_toolbox/USB.m b/osx/qmk_toolbox/USB.m index 408808de8e..611a3fdbc3 100644 --- a/osx/qmk_toolbox/USB.m +++ b/osx/qmk_toolbox/USB.m @@ -257,11 +257,11 @@ + (void)deviceEvent:(io_service_t)device connected:(BOOL)connected { ] withType:MessageType_Bootloader]; if (connected) { - [delegate deviceConnected:deviceType]; devicesAvailable[deviceType]++; + [delegate deviceConnected:deviceType]; } else { - [delegate deviceDisconnected:deviceType]; devicesAvailable[deviceType]--; + [delegate deviceDisconnected:deviceType]; } } From affecae895fa6b517a3fd6b771bb4965e30d73b0 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Thu, 29 Apr 2021 14:08:44 +1000 Subject: [PATCH 3/3] Refactor button states into methods --- osx/qmk_toolbox/AppDelegate.m | 46 ++++++++----------- windows/QMK Toolbox/MainWindow.cs | 73 ++++++++++--------------------- 2 files changed, 41 insertions(+), 78 deletions(-) diff --git a/osx/qmk_toolbox/AppDelegate.m b/osx/qmk_toolbox/AppDelegate.m index 2b8a3983d9..af391969d0 100644 --- a/osx/qmk_toolbox/AppDelegate.m +++ b/osx/qmk_toolbox/AppDelegate.m @@ -60,9 +60,7 @@ - (IBAction) flashButtonClick:(id) sender { } if (error == 0) { if (self.autoFlashButton.state != NSOnState) { - self.flashButton.enabled = NO; - self.resetButton.enabled = NO; - self.clearEEPROMButton.enabled = NO; + [self disableUI]; } [_printer print:@"Attempting to flash, please don't remove device" withType:MessageType_Bootloader]; @@ -74,9 +72,7 @@ - (IBAction) flashButtonClick:(id) sender { }); if (self.autoFlashButton.state != NSOnState) { - self.flashButton.enabled = [_flasher canFlash]; - self.resetButton.enabled = [_flasher canReset]; - self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; + [self enableUI]; } } } else { @@ -89,17 +85,13 @@ - (IBAction) resetButtonClick:(id) sender { [_printer print:@"Please select a microcontroller" withType:MessageType_Error]; } else { if (self.autoFlashButton.state != NSOnState) { - self.flashButton.enabled = NO; - self.resetButton.enabled = NO; - self.clearEEPROMButton.enabled = NO; + [self disableUI]; } [_flasher reset:(NSString *)[_mcuBox objectValue]]; if (self.autoFlashButton.state != NSOnState) { - self.flashButton.enabled = [_flasher canFlash]; - self.resetButton.enabled = [_flasher canReset]; - self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; + [self enableUI]; } } } @@ -109,17 +101,13 @@ - (IBAction) clearEEPROMButtonClick:(id) sender { [_printer print:@"Please select a microcontroller" withType:MessageType_Error]; } else { if (self.autoFlashButton.state != NSOnState) { - self.flashButton.enabled = NO; - self.resetButton.enabled = NO; - self.clearEEPROMButton.enabled = NO; + [self disableUI]; } [_flasher clearEEPROM:(NSString *)[_mcuBox objectValue]]; if (self.autoFlashButton.state != NSOnState) { - self.flashButton.enabled = [_flasher canFlash]; - self.resetButton.enabled = [_flasher canReset]; - self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; + [self enableUI]; } } } @@ -131,14 +119,10 @@ - (void)setSerialPort:(NSString *)port { - (IBAction) autoFlashButtonClick:(id)sender { if ([_autoFlashButton state] == NSOnState) { [_printer print:@"Auto-flash enabled" withType:MessageType_Info]; - self.flashButton.enabled = NO; - self.resetButton.enabled = NO; - self.clearEEPROMButton.enabled = NO; + [self disableUI]; } else { [_printer print:@"Auto-flash disabled" withType:MessageType_Info]; - self.flashButton.enabled = [_flasher canFlash]; - self.resetButton.enabled = [_flasher canReset]; - self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; + [self enableUI]; } } @@ -146,12 +130,20 @@ - (void)deviceConnected:(Chipset)chipset { if ([_autoFlashButton state] == NSOnState) { [self flashButtonClick:NULL]; } - self.flashButton.enabled = [_flasher canFlash]; - self.resetButton.enabled = [_flasher canReset]; - self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; + [self enableUI]; } - (void)deviceDisconnected:(Chipset)chipset { + [self enableUI]; +} + +- (void)disableUI { + self.flashButton.enabled = NO; + self.resetButton.enabled = NO; + self.clearEEPROMButton.enabled = NO; +} + +- (void)enableUI { self.flashButton.enabled = [_flasher canFlash]; self.resetButton.enabled = [_flasher canReset]; self.clearEEPROMButton.enabled = [_flasher canClearEEPROM]; diff --git a/windows/QMK Toolbox/MainWindow.cs b/windows/QMK Toolbox/MainWindow.cs index 76e4ed41d8..76790dc937 100644 --- a/windows/QMK Toolbox/MainWindow.cs +++ b/windows/QMK Toolbox/MainWindow.cs @@ -208,9 +208,7 @@ private void MainWindow_Load(object sender, EventArgs e) collection = searcher.Get(); _usb.DetectBootloaderFromCollection(collection); - flashButton.Enabled = _flasher.CanFlash(); - resetButton.Enabled = _flasher.CanReset(); - clearEepromButton.Enabled = _flasher.CanClearEeprom(); + EnableUI(); UpdateHidDevices(false); @@ -295,12 +293,7 @@ private void flashButton_Click(object sender, EventArgs e) { if (!autoflashCheckbox.Checked) { - this.Invoke((MethodInvoker)delegate - { - flashButton.Enabled = false; - resetButton.Enabled = false; - clearEepromButton.Enabled = false; - }); + this.Invoke(new Action(DisableUI)); } _printer.Print("Attempting to flash, please don't remove device", MessageType.Bootloader); @@ -308,12 +301,7 @@ private void flashButton_Click(object sender, EventArgs e) if (!autoflashCheckbox.Checked) { - this.Invoke((MethodInvoker)delegate - { - flashButton.Enabled = _flasher.CanFlash(); - resetButton.Enabled = _flasher.CanReset(); - clearEepromButton.Enabled = _flasher.CanClearEeprom(); - }); + this.Invoke(new Action(EnableUI)); } } } @@ -345,24 +333,14 @@ private void resetButton_Click(object sender, EventArgs e) { if (!autoflashCheckbox.Checked) { - this.Invoke((MethodInvoker)delegate - { - flashButton.Enabled = false; - resetButton.Enabled = false; - clearEepromButton.Enabled = false; - }); + this.Invoke(new Action(DisableUI)); } _flasher.Reset(mcuBox.Text); if (!autoflashCheckbox.Checked) { - this.Invoke((MethodInvoker)delegate - { - flashButton.Enabled = _flasher.CanFlash(); - resetButton.Enabled = _flasher.CanReset(); - clearEepromButton.Enabled = _flasher.CanClearEeprom(); - }); + this.Invoke(new Action(EnableUI)); } } } @@ -393,24 +371,14 @@ private void clearEepromButton_Click(object sender, EventArgs e) { if (!autoflashCheckbox.Checked) { - this.Invoke((MethodInvoker)delegate - { - flashButton.Enabled = false; - resetButton.Enabled = false; - clearEepromButton.Enabled = false; - }); + this.Invoke(new Action(DisableUI)); } _flasher.ClearEeprom(mcuBox.Text); if (!autoflashCheckbox.Checked) { - this.Invoke((MethodInvoker)delegate - { - flashButton.Enabled = _flasher.CanFlash(); - resetButton.Enabled = _flasher.CanReset(); - clearEepromButton.Enabled = _flasher.CanClearEeprom(); - }); + this.Invoke(new Action(EnableUI)); } } } @@ -638,12 +606,7 @@ private void DeviceEvent(object sender, EventArrivedEventArgs e) if (!autoflashCheckbox.Checked) { - this.Invoke((MethodInvoker)delegate - { - flashButton.Enabled = _flasher.CanFlash(); - resetButton.Enabled = _flasher.CanReset(); - clearEepromButton.Enabled = _flasher.CanClearEeprom(); - }); + this.Invoke(new Action(EnableUI)); } } @@ -665,19 +628,27 @@ private void autoflashCheckbox_CheckedChanged(object sender, EventArgs e) if (autoflashCheckbox.Checked) { _printer.Print("Auto-flash enabled", MessageType.Info); - flashButton.Enabled = false; - resetButton.Enabled = false; - clearEepromButton.Enabled = false; + DisableUI(); } else { _printer.Print("Auto-flash disabled", MessageType.Info); - flashButton.Enabled = _flasher.CanFlash(); - resetButton.Enabled = _flasher.CanReset(); - clearEepromButton.Enabled = _flasher.CanClearEeprom(); + EnableUI(); } } + private void DisableUI() { + flashButton.Enabled = false; + resetButton.Enabled = false; + clearEepromButton.Enabled = false; + } + + private void EnableUI() { + flashButton.Enabled = _flasher.CanFlash(); + resetButton.Enabled = _flasher.CanReset(); + clearEepromButton.Enabled = _flasher.CanClearEeprom(); + } + // Set the button's status tip. private void btn_MouseEnter(object sender, EventArgs e) {