Skip to content

Commit

Permalink
Merge pull request paulkaplan#4 from bestwnh/master
Browse files Browse the repository at this point in the history
lower the cpu usage after hex file upload and fix a KVO bug
  • Loading branch information
paulkaplan authored Oct 8, 2016
2 parents d10e59b + 8952fce commit d9dc9b3
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions Sources/HexLoaderController.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#import "HexLoaderController.h"
#import "ORSSerialPortManager.h"

@interface HexLoaderController ()

@property (nonatomic, strong) NSOperationQueue *bgQueue;

@end

@implementation HexLoaderController

@synthesize sendTextField = _sendTextField;
Expand All @@ -17,6 +23,10 @@ - (id)init
self = [super init];
if (self)
{
self.bgQueue = [[NSOperationQueue alloc] init];
if ([self.bgQueue respondsToSelector:@selector(setQualityOfService:)]) {
[self.bgQueue setQualityOfService:NSQualityOfServiceBackground];
}
self.serialPortManager = [ORSSerialPortManager sharedSerialPortManager];
self.availableBaudRates = [NSArray arrayWithObjects:
[NSNumber numberWithInteger:300],
Expand Down Expand Up @@ -195,20 +205,30 @@ - (IBAction)sendFileButtonAction:(id)sender{

[[outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify];

[[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification object:[outputPipe fileHandleForReading] queue:nil usingBlock:^(NSNotification *notification){

[[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification object:[outputPipe fileHandleForReading] queue:self.bgQueue usingBlock:^(NSNotification *notification){


NSData *output = [[outputPipe fileHandleForReading] availableData];
NSString *outStr = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding];
[self.receivedDataTextView.textStorage.mutableString appendString:[NSString stringWithFormat:@"%@", outStr]];
if ([outStr isEqualToString:@""]) {
[[outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify];
return ;
}

dispatch_async(dispatch_get_main_queue(), ^{
[self.receivedDataTextView.textStorage.mutableString appendString:[NSString stringWithFormat:@"%@", outStr]];

// Scroll to end of outputText field
NSRange range;
range = NSMakeRange([self.receivedDataTextView.string length], 0);
[self.receivedDataTextView setFont:[NSFont fontWithName:@"Monaco" size:10]];

[self.receivedDataTextView scrollRangeToVisible:range];
[self.receivedDataTextView setNeedsDisplay:YES];
[[outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify];
});


// Scroll to end of outputText field
NSRange range;
range = NSMakeRange([self.receivedDataTextView.string length], 0);
[self.receivedDataTextView setFont:[NSFont fontWithName:@"Monaco" size:10]];

[self.receivedDataTextView scrollRangeToVisible:range];
[self.receivedDataTextView setNeedsDisplay:YES];
[[outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify];
}];

NSLog(@"");
Expand All @@ -231,17 +251,6 @@ - (IBAction)sendFileButtonAction:(id)sender{
}
}

- (void)setSerialPortManager:(ORSSerialPortManager *)manager
{
if (manager != _serialPortManager)
{
[_serialPortManager removeObserver:self forKeyPath:@"availablePorts"];
_serialPortManager = manager;
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
[_serialPortManager addObserver:self forKeyPath:@"availablePorts" options:options context:NULL];
}
}

- (void)serialPort:(ORSSerialPort *)serialPort didReceiveData:(NSData *)data {
NSLog(@"%@", data);
}
Expand Down

0 comments on commit d9dc9b3

Please sign in to comment.