Skip to content

Commit

Permalink
add an NSOperationQueue to read the outputPipe, low the CPU usage
Browse files Browse the repository at this point in the history
remove the KVO for availablePorts, because it never handle and may crash the app
  • Loading branch information
bestwnh committed Oct 5, 2016
1 parent d10e59b commit 8952fce
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions Sources/HexLoaderController.m
@@ -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 8952fce

Please sign in to comment.