|
// ViewController.m |
|
// |
|
// ViewController.m |
|
// ServoIRViewer |
|
// |
|
// Created by 倉橋浩一 on 2016/02/19. |
|
// Copyright © 2016年 倉橋屋. All rights reserved. |
|
// |
|
|
|
#import "ViewController.h" |
|
//#import <CoreMotion/CoreMotion.h> |
|
#import "ControlView.h" |
|
|
|
|
|
|
|
|
|
@interface ViewController () |
|
|
|
@property (strong, nonatomic) NSInputStream *inputStream; |
|
@property (strong, nonatomic) NSOutputStream *outputStream; |
|
@property (strong, nonatomic) NSMutableArray *messages; |
|
|
|
@property CGRect viewRect; |
|
@property NSString *strShouldSend; |
|
|
|
@end |
|
|
|
@implementation ViewController |
|
|
|
const int BUFSIZE = 128; |
|
|
|
|
|
- (void)viewDidLoad { |
|
[super viewDidLoad]; |
|
|
|
// Do any additional setup after loading the view, typically from a nib. |
|
|
|
self.zValue.text = @""; |
|
self.xRaw.text = @""; |
|
self.yRaw.text = @""; |
|
self.xRaw.text = @""; |
|
|
|
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(actionPan:)]; |
|
[self.dragView addGestureRecognizer:pan]; |
|
|
|
[self initNetworkCommunication]; |
|
|
|
} |
|
|
|
- (void)viewDidAppear:(BOOL)animated { |
|
|
|
_viewRect = self.dragView.frame; |
|
|
|
self.dragView.xy = CGPointMake(_viewRect.size.width / 2, 0); |
|
self.dragView.init = YES; |
|
|
|
[super viewDidAppear:animated]; |
|
[self.dragView setNeedsDisplay]; |
|
} |
|
|
|
- (void)didReceiveMemoryWarning { |
|
[super didReceiveMemoryWarning]; |
|
// Dispose of any resources that can be recreated. |
|
} |
|
|
|
|
|
#pragma mark - gesture |
|
|
|
- (void) actionPan:(UIPanGestureRecognizer *)pan { |
|
if ([pan numberOfTouches] > 0) { |
|
CGPoint t = [pan locationOfTouch:0 inView:self.dragView]; |
|
|
|
double yValue = (t.x / _viewRect.size.height) * 120 + 40; |
|
double xValue = (t.y / _viewRect.size.width) * 120 + 10; |
|
|
|
if (yValue >= 40 && yValue <= 160 && xValue >= 10 && xValue <= 130) { |
|
NSString *strX = [NSString stringWithFormat:@"%3.lf", xValue]; |
|
self.xValue.text = strX; |
|
NSString *strY = [NSString stringWithFormat:@"%3.lf", yValue]; |
|
self.yValue.text = strY; |
|
|
|
[self.dragView setXy:t]; |
|
[self.dragView setNeedsDisplay]; |
|
|
|
if (self.outputStream != nil) { |
|
self.strShouldSend = [NSString stringWithFormat:@"%3.0lf,%3.0lf\n", xValue, yValue]; |
|
const uint8_t * cstr = (const uint8_t*)[self.strShouldSend UTF8String]; |
|
[self.outputStream write:cstr maxLength:strlen((char *)cstr)]; |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
#pragma mark - socket |
|
|
|
// |
|
// thanks! |
|
// http://internetcom.jp/developer/20100406/26.html |
|
// |
|
- (void)initNetworkCommunication { |
|
|
|
uint portNo = 51515; |
|
CFReadStreamRef readStream; |
|
CFWriteStreamRef writeStream; |
|
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.0.68", portNo, &readStream, &writeStream); |
|
self.inputStream = (__bridge NSInputStream *)readStream; |
|
self.outputStream = (__bridge NSOutputStream *)writeStream; |
|
|
|
[self.inputStream setDelegate:self]; |
|
[self.outputStream setDelegate:self]; |
|
|
|
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; |
|
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; |
|
|
|
[self openStreams]; |
|
} |
|
|
|
|
|
// |
|
// thanks: |
|
// CFNetwork programming guide |
|
// https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Streams/Articles/NetworkStreams.html#//apple_ref/doc/uid/20002277-BCIDFCDI |
|
// |
|
|
|
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode { |
|
switch(eventCode) { |
|
|
|
case NSStreamEventOpenCompleted: |
|
NSLog(@"stream : connected"); |
|
// self.buttonConnect.enabled = NO; |
|
break; |
|
|
|
case NSStreamEventEndEncountered: |
|
case NSStreamEventErrorOccurred: |
|
NSLog(@"NSStreamEventErrorOccurred"); |
|
[self.outputStream close]; |
|
[self.inputStream close]; |
|
self.inputStream = nil; |
|
self.outputStream = nil; |
|
self.buttonConnect.enabled = YES; |
|
break; |
|
|
|
case NSStreamEventHasBytesAvailable: |
|
NSLog(@"NSStreamEventHasBytesAvailable"); |
|
if (stream == self.inputStream) { |
|
uint8_t cstr[BUFSIZE]; |
|
[self.inputStream read:cstr maxLength:BUFSIZE]; |
|
NSLog(@"received=%s", cstr); |
|
int remote, local; |
|
sscanf((char *)cstr, "%d,%d\n", &local, &remote); |
|
self.remoteTemp.text = [NSString stringWithFormat:@"%4.2lf", (float)remote/100.0]; |
|
self.localTemp.text = [NSString stringWithFormat:@"%4.2lf", (float)local /100.0]; |
|
} |
|
break; |
|
case NSStreamEventHasSpaceAvailable: |
|
// NSLog(@"NSStreamEventHasSpaceAvailable"); |
|
// if (stream == self.outputStream) { |
|
// if (self.strShouldSend != nil) { |
|
// NSLog(@"output should send"); |
|
// const uint8_t * cstr = (const uint8_t*)[self.strShouldSend UTF8String]; |
|
// [self.outputStream write:cstr maxLength:strlen((char *)cstr)]; |
|
// self.strShouldSend = nil; |
|
// } |
|
// } |
|
break; |
|
|
|
default: |
|
break; |
|
} |
|
} |
|
|
|
- (IBAction)actionConnect:(id)sender { |
|
[self.inputStream close]; |
|
[self.outputStream close]; |
|
|
|
[self initNetworkCommunication]; |
|
} |
|
|
|
|
|
- (void)openStreams { |
|
[self.inputStream open]; |
|
[self.outputStream open]; |
|
} |
|
|
|
|
|
@end |
|
|
|
|
|
// ViewController.h |
|
// |
|
// ViewController.h |
|
// ServoIRViewer |
|
// |
|
// Created by 倉橋浩一 on 2016/02/19. |
|
// Copyright © 2016年 倉橋屋. All rights reserved. |
|
// |
|
|
|
#import <UIKit/UIKit.h> |
|
|
|
@class ControlView; |
|
|
|
@interface ViewController : UIViewController <NSStreamDelegate> |
|
|
|
@property (weak, nonatomic) IBOutlet UILabel *xValue; |
|
@property (weak, nonatomic) IBOutlet UILabel *yValue; |
|
@property (weak, nonatomic) IBOutlet UILabel *zValue; |
|
@property (weak, nonatomic) IBOutlet UILabel *xRaw; |
|
@property (weak, nonatomic) IBOutlet UILabel *yRaw; |
|
@property (weak, nonatomic) IBOutlet UILabel *zRaw; |
|
|
|
@property (weak, nonatomic) IBOutlet UILabel *remoteTemp; |
|
@property (weak, nonatomic) IBOutlet UILabel *localTemp; |
|
|
|
@property (weak, nonatomic) IBOutlet ControlView *dragView; |
|
@property (weak, nonatomic) IBOutlet UIButton *buttonConnect; |
|
|
|
- (IBAction)actionConnect:(id)sender; |
|
|
|
@end |
|
|
|
// ControlView.h |
|
// |
|
// ControlView.m |
|
// ServoIRViewer |
|
// |
|
// Created by 倉橋浩一 on 2016/02/19. |
|
// Copyright © 2016年 倉橋屋. All rights reserved. |
|
// |
|
|
|
#import "ControlView.m" |
|
|
|
const float r = 30; |
|
|
|
@implementation ControlView |
|
|
|
|
|
- (instancetype)initWithCoder:(NSCoder *)coder |
|
{ |
|
self = [super initWithCoder:coder]; |
|
if (self) { |
|
_init = NO; |
|
} |
|
return self; |
|
} |
|
|
|
- (void)drawRect:(CGRect)rect { |
|
CGContextRef context = UIGraphicsGetCurrentContext(); |
|
|
|
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); |
|
CGContextStrokeRect(context, rect); |
|
|
|
if (_init) { |
|
// NSLog(@"%lf, %lf", self.xy.x, self.xy.y); |
|
|
|
CGContextSetFillColorWithColor(context, [UIColor darkGrayColor].CGColor); |
|
CGContextFillEllipseInRect(context, CGRectMake(self.xy.x - r, self.xy.y - r, r*2, r*2)); |
|
} |
|
} |
|
|
|
|
|
@end |
|
|
|
|
|
// ControlView.h |
|
// |
|
// ControlView.h |
|
// ServoIRViewer |
|
// |
|
// Created by 倉橋浩一 on 2016/02/19. |
|
// Copyright © 2016年 倉橋屋. All rights reserved. |
|
// |
|
|
|
#import <UIKit/UIKit.h> |
|
|
|
@interface ControlView : UIView |
|
|
|
@property CGPoint xy; |
|
@property BOOL init; |
|
|
|
@end |