Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoflash improvements #257

Merged
merged 3 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions osx/qmk_toolbox/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
fauxpark marked this conversation as resolved.
Show resolved Hide resolved
}

[_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];
Expand All @@ -77,31 +88,73 @@ - (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];
}
}
}

- (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];
}
}
}

- (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 {
Expand Down Expand Up @@ -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];
Expand Down
13 changes: 8 additions & 5 deletions osx/qmk_toolbox/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17156" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17156"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17701"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down Expand Up @@ -196,7 +196,7 @@
<constraint firstAttribute="height" constant="21" id="0c7-uS-SRH"/>
<constraint firstAttribute="width" constant="75" id="mbB-cQ-ug3"/>
</constraints>
<buttonCell key="cell" type="push" title="Exit DFU" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="euK-aj-UAI">
<buttonCell key="cell" type="push" title="Exit DFU" bezelStyle="rounded" alignment="center" enabled="NO" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="euK-aj-UAI">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="label" size="12"/>
</buttonCell>
Expand All @@ -210,7 +210,7 @@
<constraint firstAttribute="height" constant="21" id="igB-eN-Klh"/>
<constraint firstAttribute="width" constant="68" id="sS6-6I-Jy2"/>
</constraints>
<buttonCell key="cell" type="push" title="Flash" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="WBr-hK-w8Y">
<buttonCell key="cell" type="push" title="Flash" bezelStyle="rounded" alignment="center" enabled="NO" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="WBr-hK-w8Y">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="label" size="12"/>
</buttonCell>
Expand All @@ -228,6 +228,9 @@
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="label" size="12"/>
</buttonCell>
<connections>
<action selector="autoFlashButtonClick:" target="Voe-Tx-rLC" id="5Rn-zn-8HN"/>
</connections>
</button>
<box borderType="line" title="Local file" translatesAutoresizingMaskIntoConstraints="NO" id="t67-0j-kLe">
<rect key="frame" x="2" y="418" width="667" height="53"/>
Expand Down Expand Up @@ -383,7 +386,7 @@
<constraint firstAttribute="width" constant="113" id="OrW-P7-Fri"/>
<constraint firstAttribute="height" constant="21" id="ZJj-0M-KZ7"/>
</constraints>
<buttonCell key="cell" type="push" title="Clear EEPROM" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="aot-j7-w9r">
<buttonCell key="cell" type="push" title="Clear EEPROM" bezelStyle="rounded" alignment="center" enabled="NO" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="aot-j7-w9r">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="label" size="12"/>
</buttonCell>
Expand Down
6 changes: 5 additions & 1 deletion osx/qmk_toolbox/Flashing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <FlashingDelegate> delegate;
Expand Down
31 changes: 24 additions & 7 deletions osx/qmk_toolbox/Flashing.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSNumber *> *resettable = @[
@(AtmelDFU),
Expand All @@ -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<NSNumber *> *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 {
Expand Down
4 changes: 2 additions & 2 deletions osx/qmk_toolbox/USB.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

Expand Down
48 changes: 24 additions & 24 deletions windows/QMK Toolbox/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@
<userSettings>

<QMK_Toolbox.Properties.Settings>
<setting name="hexFileSetting" serializeAs="String">
<value />
</setting>
<setting name="targetSetting" serializeAs="String">
<value>atmega32u4</value>
</setting>
<setting name="autoSetting" serializeAs="String">
<value>False</value>
</setting>
<setting name="outputZoom" serializeAs="String">
<value>1</value>
</setting>
<setting name="keyboard" serializeAs="String">
<value />
</setting>
<setting name="keymap" serializeAs="String">
<value />
</setting>
<setting name="firstStart" serializeAs="String">
<value>True</value>
</setting>
<setting name="driversInstalled" serializeAs="String">
<value>False</value>
</setting>
<setting name="hexFileSetting" serializeAs="String">
<value />
</setting>
<setting name="targetSetting" serializeAs="String">
<value>atmega32u4</value>
</setting>
<setting name="autoSetting" serializeAs="String">
<value>False</value>
</setting>
<setting name="outputZoom" serializeAs="String">
<value>1</value>
</setting>
<setting name="keyboard" serializeAs="String">
<value />
</setting>
<setting name="keymap" serializeAs="String">
<value />
</setting>
<setting name="firstStart" serializeAs="String">
<value>True</value>
</setting>
<setting name="driversInstalled" serializeAs="String">
<value>False</value>
</setting>
</QMK_Toolbox.Properties.Settings>
</userSettings>
<runtime>
Expand Down
32 changes: 25 additions & 7 deletions windows/QMK Toolbox/Flashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Chipset> {
Expand All @@ -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>
{
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)
Expand Down
Loading