// Code Linsence: MIT by akio0911 // http://d.hatena.ne.jp/akio0911/ // akio0911@gmail.com // // MenuView.m // CocoaAnimeCamera // // Created by ssatou on 08/04/19. // Copyright 2008 __MyCompanyName__. All rights reserved. // #import "MenuView.h" #import #import "Downloader.h" //////////////////////////// #include #include #include #include #include #include #include #include #define BAUDRATE B9600 /* 通信速度の設定 */ #define FALSE 0 #define TRUE 1 #define SLEEP_NSEC (99 * 1000 * 1000) // 99msec volatile int STOP=FALSE; //////////////////////////// typedef struct{ NSString *caption; double latitude; // 緯度 double longitude; // 経度 } Landmark; //#include // for pi() #define PI 3.1415926535 #define GEOCLIP_COUNT 11 typedef struct{ NSString *title; const char *filename; CALayer *layer; double latitude; double longitude; } Geoclip; Geoclip geoclips[GEOCLIP_COUNT]; @implementation MenuView - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code here. } return self; } - (void)drawRect:(NSRect)rect { // Drawing code here. } - (void)awakeFromNib { names=[[NSArray arrayWithObjects:@"Item1",@"Item2", @"Item3",@"Item4",@"Item5", nil]retain]; [self setupLayers]; // Detach the new thread. [NSThread detachNewThreadSelector:@selector(MyInstanceThreadMethod:) toTarget:self withObject:self]; [NSThread detachNewThreadSelector:@selector(MyInstanceThreadMethod2:) toTarget:self withObject:self]; [NSThread detachNewThreadSelector:@selector(MyInstanceThreadMethod3:) toTarget:self withObject:self]; [NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector( timerAdjustWindowSize:) userInfo:nil repeats:YES ]; [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector( timerAdjustWindowSize2:) userInfo:nil repeats:YES ]; } -(void)setupLayers; { CGFloat fontSize=32.0; [[self window]makeFirstResponder:self]; //Create the capture session mCaptureSession = [[QTCaptureSession alloc] init]; //Connect inputs and outputs to the session BOOL success = NO; NSError *error; NSArray *input_devices = [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo]; NSEnumerator *enumerator = [input_devices objectEnumerator]; id anObject; QTCaptureDevice *devBuilt = nil; QTCaptureDevice *devUSB = nil; while(anObject = [enumerator nextObject]){ NSString *localizedDisplayName = [anObject localizedDisplayName]; if([localizedDisplayName compare:@"Built-in iSight"] == NSOrderedSame){ devBuilt = anObject; }else if([localizedDisplayName compare:@"USB2.0 1.3MP UVC Camera"] == NSOrderedSame){ devUSB = anObject; } } QTCaptureDevice *device = nil; if(devUSB != nil){ device = devUSB; }else if(devBuilt != nil){ device = devBuilt; } [device open:&error]; // Add the video device to the session as device input mCaptureDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice:device]; success = [mCaptureSession addInput:mCaptureDeviceInput error:&error]; if (!success) { // Handle error } rootLayer = [[[QTCaptureLayer alloc] initWithSession:mCaptureSession] retain]; // Start the capture session running [mCaptureSession startRunning]; [self setLayer:rootLayer]; [self setWantsLayer:YES]; ////////////////////////////////////// CGRect rect = [rootLayer frame]; countLayer=[CATextLayer layer]; countLayer.string=@"9999"; countLayer.font=@"Lucida-Grande"; countLayer.fontSize=fontSize; countLayer.foregroundColor=CGColorCreateGenericRGB(1.0,1.0,1.0,1.0); countLayer.frame=CGRectMake(0.0, 0.0, rect.size.width, rect.size.height/4); countLayer.backgroundColor=CGColorCreateGenericRGB(0.0,0.0,0.0,0.5); [rootLayer addSublayer:countLayer]; ////////////////////////////////////// CGRect rectCurrentLocation = [rootLayer frame]; currentLocationLayer=[CATextLayer layer]; currentLocationLayer.string=@"現在地"; currentLocationLayer.font=@"Lucida-Grande"; currentLocationLayer.fontSize=fontSize; currentLocationLayer.foregroundColor=CGColorCreateGenericRGB(1.0,1.0,1.0,1.0); currentLocationLayer.frame=CGRectMake(0.0, 0.0, rect.size.width, rect.size.height/4); currentLocationLayer.backgroundColor=CGColorCreateGenericRGB(0.0,0.0,0.0,0.5); [rootLayer addSublayer:currentLocationLayer]; // selectionLayerの最初の位置を設定し、次に最初のselectedIndexを0に設定 int idx = 0; geoclips[idx].title = @"東京都現代美術館"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/1992.jpg"; geoclips[idx].latitude = 35.679667; geoclips[idx].longitude = 139.807350; idx++; geoclips[idx].title = @"[080202]両国なう!"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/1902.jpg"; geoclips[idx].latitude = 35.699852; geoclips[idx].longitude = 139.799511; idx++; geoclips[idx].title = @"Spaceforyourfuture"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/1716.jpg"; geoclips[idx].latitude = 35.680728; geoclips[idx].longitude = 139.800703; idx++; geoclips[idx].title = @"門前仲町『ディデアン』"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/1212.jpg"; geoclips[idx].latitude = 35.672700; geoclips[idx].longitude = 139.796881; idx++; geoclips[idx].title = @"深川八幡"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/1210.jpg"; geoclips[idx].latitude = 35.671603; geoclips[idx].longitude = 139.799633; idx++; geoclips[idx].title = @"でかいし"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/869.jpg"; geoclips[idx].latitude = 35.695278; geoclips[idx].longitude = 139.814158; idx++; geoclips[idx].title = @"いちごジュース"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/868.jpg"; geoclips[idx].latitude = 35.696261; geoclips[idx].longitude = 139.814256; idx++; geoclips[idx].title = @"くじら丼"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/733.jpg"; geoclips[idx].latitude = 35.685403; geoclips[idx].longitude = 139.780119; idx++; geoclips[idx].title = @"相撲でもちゃんこでもない"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/720.jpg"; geoclips[idx].latitude = 35.696000; geoclips[idx].longitude = 139.791508; idx++; geoclips[idx].title = @"錦糸町不二家"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/621.jpg"; geoclips[idx].latitude = 35.695561; geoclips[idx].longitude = 139.814869; idx++; geoclips[idx].title = @"仕事場所その1"; geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/552.jpg"; geoclips[idx].latitude = 35.698372; geoclips[idx].longitude = 139.781203; int i; for(i = 0; i < GEOCLIP_COUNT; i++){ CALayer *imageLayer = [CALayer layer]; geoclips[i].layer = imageLayer; CGDataProviderRef imageDataProvider = CGDataProviderCreateWithFilename(geoclips[i].filename); CGImageRef image = CGImageCreateWithJPEGDataProvider(imageDataProvider, NULL, NO, kCGRenderingIntentDefault); imageLayer.contents = (id)image; imageLayer.frame = CGRectMake(10, rootLayer.bounds.size.height - 150 - 10, 200, 150); imageLayer.opacity = 0.5f; [rootLayer addSublayer:imageLayer]; CGImageRelease(image); CGDataProviderRelease(imageDataProvider); } } -(void)dealloc { [mCaptureSession stopRunning]; [[mCaptureDeviceInput device] close]; [mCaptureSession release]; [mCaptureDeviceInput release]; [rootLayer autorelease]; [countLayer autorelease]; [currentLocationLayer autorelease]; [names autorelease]; [super dealloc]; } - (void)MyInstanceThreadMethod:(id)param { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; int i; for(i=0; i<60*60*24; i++){ [NSThread sleepForTimeInterval:1.0]; selectedIndex = i%5; } [pool release]; } - (void)MyInstanceThreadMethod2:(id)param { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //////////////////////////////////////////////////////////////////////////////////// /* MacでUSB経由にてTDS01Vの値を取得する */ // nanosleepをつっこむ事で受信を待つという、暫定的な対応をしている。 // 同期・非カノニカルにする事で、ちゃんとした作りにできるはず。 int fd, res; /* fd:ファイルディスクリプタ res:受け取った文字数 */ struct termios oldtio, newtio; /* 通信ポートを制御するためのインターフェイス */ char buf[255]; /* 受信文字を格納 */ char crlf[2]; char modem_device[4096]; char send_command[4096]; int i; struct timespec treq, trem; char dir_dig_str[5]; short dir_dig; treq.tv_sec = (time_t)0; treq.tv_nsec = SLEEP_NSEC; strcpy(modem_device, "/dev/tty.usbserial-0000101D"); if((fd = open(modem_device, O_RDWR | O_NOCTTY ))==-1){ /* O_RDWR:読み書き両用 O_NOCTTY:tty制御をしない */ perror(modem_device); mDirDig = 0.0; while(1){ nanosleep(&treq, &trem); mDirDig += 1.0; if(mDirDig>=360.0){ mDirDig -= 360.0; } } } tcgetattr(fd, &oldtio); /* 現在のシリアルポートの設定を待避させる*/ bzero(&newtio, sizeof(newtio)); /* 新しいポートの設定の構造体をクリアする */ newtio.c_cflag = (BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD); /* BAUDRATE: ボーレートの設定 CRTSCTS : 出力のハードウェアフロー制御 CS8 : 8n1 (8 ビット,ノンパリティ,ストップビット 1) CLOCAL : ローカル接続,モデム制御なし CREAD : 受信文字(receiving characters)を有効にする. */ newtio.c_iflag = (IGNPAR | ICRNL); /* IGNPAR : パリティエラーのデータは無視する ICRNL : CR を NL に対応させる(これを行わないと,他のコンピュータで CR を入力しても,入力が終りにならない) それ以外の設定では,デバイスは raw モードである(他の入力処理は行わない) */ newtio.c_oflag = 0; /* Raw モードでの出力 */ newtio.c_lflag = ICANON; /* ICANON : カノニカル入力を有効にする 全てのエコーを無効にし,プログラムに対してシグナルは送らせない */ tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); /* モデムラインをクリアし,ポートの設定を有効にする */ /* 端末の設定終了. 行の先頭に 'z' を入力することでプログラムを終了させる */ puts("START"); // センサ情報項目設定 全センサ全データ // (ベクトルデータ+計測データ) sprintf(send_command, "%s", "0DF7"); sprintf(buf, "%s\r\n", send_command); write(fd,buf,strlen(buf)); // ACK センサ情報項目設定応答 nanosleep(&treq, &trem); res = read(fd,buf,2); read(fd,crlf,2); buf[res]=0; // 計測条件設定 sprintf(send_command, "%s", "050027950000"); sprintf(buf, "%s\r\n", send_command); write(fd,buf,strlen(buf)); // [ACK 計測条件設定応答]を読み取ってみる nanosleep(&treq, &trem); res = read(fd,buf,2); buf[res]=0; read(fd,crlf,2); // 地磁気センサ初期化要求 sprintf(send_command, "%s", "27"); sprintf(buf, "%s\r\n", send_command); write(fd,buf,strlen(buf)); // ACK 地磁気センサ初期化要求応答 nanosleep(&treq, &trem); res = read(fd,buf,2); buf[res]=0; read(fd,crlf,2); for(i = 0; i < 10*60; i ++){ // 計測開始 sprintf(send_command, "%s", "21"); sprintf(buf, "%s\r\n", send_command); write(fd,buf,strlen(buf)); // ACK 計測開始応答 nanosleep(&treq, &trem); res = read(fd,buf,2); buf[res]=0; read(fd,crlf,2); // センサ情報要求 sprintf(send_command, "%s", "29"); sprintf(buf, "%s\r\n", send_command); write(fd,buf,strlen(buf)); // センサ情報 全センサ全データ(ベクトルデータ+計測データ) nanosleep(&treq, &trem); res = read(fd,buf,52); buf[res]=0; read(fd,crlf,2); memcpy(dir_dig_str, &buf[12], 4); dir_dig_str[4] = '\0'; dir_dig = (short)strtol(dir_dig_str,NULL,16); mDirDig = dir_dig/10.0; } tcsetattr(fd, TCSANOW, &oldtio); /* 退避させた設定に戻す */ close(fd); /* シリアルポートを閉じる */ //////////////////////////////////////////////////////////////////////////////////// [pool release]; } - (void)MyInstanceThreadMethod3:(id)param { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; while(1){ [[WifiGetter alloc] initWifiWithDelegate:self]; struct timespec treq, trem; treq.tv_sec = (time_t)10; treq.tv_nsec = 0; nanosleep(&treq, &trem); } [pool release]; } - (void)timerAdjustWindowSize:( NSTimer *)aTimer { NSString *direction; if(0.0 <= mDirDig && mDirDig <= 45.0) direction = @"北"; else if(45.0 <= mDirDig && mDirDig <= 135.0) direction = @"東"; else if(135.0 <= mDirDig && mDirDig <= 225.0) direction = @"南"; else if(225.0 <= mDirDig && mDirDig <= 315.0) direction = @"西"; else direction = @"北"; Landmark presentPlace; presentPlace.caption = @"東京都江東区白河1-5-15"; presentPlace.latitude = 35.682708 ; // 緯度 presentPlace.longitude = 139.800612; // 経度 const int LANDMARK_COUNT = 9; Landmark landmarks[LANDMARK_COUNT]; landmarks[0].caption = @"森下駅"; landmarks[0].latitude = 35.688324; // 緯度 landmarks[0].longitude = 139.797034; // 経度 landmarks[1].caption = @"菊川駅"; landmarks[1].latitude = 35.688742; // 緯度 landmarks[1].longitude = 139.806025; // 経度 landmarks[2].caption = @"住吉駅"; landmarks[2].latitude = 35.689439; // 緯度 landmarks[2].longitude = 139.81566; // 経度 landmarks[3].caption = @"東陽町駅"; landmarks[3].latitude = 35.669988; // 緯度 landmarks[3].longitude = 139.817591; // 経度 landmarks[4].caption = @"木場駅"; landmarks[4].latitude = 35.669709; // 緯度 landmarks[4].longitude = 139.807034; // 経度 landmarks[5].caption = @"門前仲町駅"; landmarks[5].latitude = 35.672219; // 緯度 landmarks[5].longitude = 139.796219; // 経度 landmarks[6].caption = @"水天宮前駅"; landmarks[6].latitude = 35.683043; // 緯度 landmarks[6].longitude = 139.785383; // 経度 landmarks[7].caption = @"東日本橋駅"; landmarks[7].latitude = 35.692489; // 緯度 landmarks[7].longitude = 139.784825; // 経度 landmarks[8].caption = @"錦糸町駅"; landmarks[8].latitude = 35.696811; // 緯度 landmarks[8].longitude = 139.813943; // 経度 NSString *to_land = @"NONE"; int i; for(i=0; i