root/platform/DennouMegane/MenuView.m @ 11923

Revision 11923, 16.4 kB (checked in by akio0911, 5 years ago)
Line 
1// Code Linsence: MIT by akio0911
2// http://d.hatena.ne.jp/akio0911/
3// akio0911@gmail.com
4//
5//  MenuView.m
6//  CocoaAnimeCamera
7//
8//  Created by ssatou on 08/04/19.
9//  Copyright 2008 __MyCompanyName__. All rights reserved.
10//
11
12#import "MenuView.h"
13#import <Quartz/Quartz.h>   
14#import "Downloader.h"
15
16////////////////////////////
17#include<stdio.h>
18#include<stdlib.h>
19#include<strings.h>
20#include<termios.h>
21#include<unistd.h>
22#include<fcntl.h>
23
24#include <unistd.h>
25
26#include <time.h>
27
28#define BAUDRATE B9600           /* 通信速度の設定 */
29
30#define FALSE 0
31#define TRUE 1
32
33#define SLEEP_NSEC (99 * 1000 * 1000) // 99msec
34
35volatile int STOP=FALSE;
36////////////////////////////
37
38typedef struct{
39        NSString *caption;
40        double latitude; // 緯度
41        double longitude; // 経度
42} Landmark;
43
44//#include <fp.h> // for pi()
45#define PI 3.1415926535
46
47#define GEOCLIP_COUNT 11
48typedef struct{
49        NSString *title;
50        const char *filename;
51        CALayer *layer;
52        double latitude;
53        double longitude;
54} Geoclip;
55
56Geoclip geoclips[GEOCLIP_COUNT];
57
58@implementation MenuView
59
60- (id)initWithFrame:(NSRect)frame {
61    self = [super initWithFrame:frame];
62    if (self) {
63        // Initialization code here.
64    }
65    return self;
66}
67
68- (void)drawRect:(NSRect)rect {
69    // Drawing code here.
70}
71
72- (void)awakeFromNib
73{
74        names=[[NSArray arrayWithObjects:@"Item1",@"Item2",
75                @"Item3",@"Item4",@"Item5",
76                nil]retain];
77        [self   setupLayers];
78       
79        // Detach the new thread.
80        [NSThread detachNewThreadSelector:@selector(MyInstanceThreadMethod:)
81                toTarget:self withObject:self];
82        [NSThread detachNewThreadSelector:@selector(MyInstanceThreadMethod2:)
83                toTarget:self withObject:self];
84        [NSThread detachNewThreadSelector:@selector(MyInstanceThreadMethod3:)
85                toTarget:self withObject:self];
86
87         [NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0
88                                      target:self
89                                    selector:@selector( timerAdjustWindowSize:)
90                                    userInfo:nil
91                                     repeats:YES
92     ];
93         [NSTimer scheduledTimerWithTimeInterval:10.0
94                                      target:self
95                                    selector:@selector( timerAdjustWindowSize2:)
96                                    userInfo:nil
97                                     repeats:YES
98     ];
99}
100
101-(void)setupLayers;
102{
103        CGFloat fontSize=32.0;
104
105        [[self window]makeFirstResponder:self];
106
107        //Create the capture session
108        mCaptureSession = [[QTCaptureSession alloc] init];
109        //Connect inputs and outputs to the session
110        BOOL success = NO;
111        NSError *error;
112
113        NSArray *input_devices = [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo];
114        NSEnumerator *enumerator = [input_devices objectEnumerator];
115        id anObject;
116        QTCaptureDevice *devBuilt = nil;
117        QTCaptureDevice *devUSB = nil;
118        while(anObject = [enumerator nextObject]){
119                NSString *localizedDisplayName = [anObject localizedDisplayName];
120                if([localizedDisplayName compare:@"Built-in iSight"] == NSOrderedSame){
121                        devBuilt = anObject;
122                }else if([localizedDisplayName compare:@"USB2.0 1.3MP UVC Camera"] == NSOrderedSame){
123                        devUSB = anObject;
124                }
125        }
126        QTCaptureDevice *device = nil;
127        if(devUSB != nil){
128                device = devUSB;
129        }else if(devBuilt != nil){
130                device = devBuilt;
131        }
132        [device open:&error];
133        // Add the video device to the session as device input
134        mCaptureDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice:device];
135        success = [mCaptureSession addInput:mCaptureDeviceInput error:&error];
136        if (!success) {
137                // Handle error
138        }
139               
140        rootLayer = [[[QTCaptureLayer alloc] initWithSession:mCaptureSession] retain];
141        // Start the capture session running
142        [mCaptureSession startRunning];
143
144        [self setLayer:rootLayer];
145        [self setWantsLayer:YES];
146
147        //////////////////////////////////////
148        CGRect rect = [rootLayer frame];
149
150        countLayer=[CATextLayer layer];
151        countLayer.string=@"9999";
152        countLayer.font=@"Lucida-Grande";
153        countLayer.fontSize=fontSize;
154        countLayer.foregroundColor=CGColorCreateGenericRGB(1.0,1.0,1.0,1.0);
155        countLayer.frame=CGRectMake(0.0, 0.0, rect.size.width, rect.size.height/4);
156        countLayer.backgroundColor=CGColorCreateGenericRGB(0.0,0.0,0.0,0.5);
157        [rootLayer addSublayer:countLayer];
158        //////////////////////////////////////
159       
160        CGRect rectCurrentLocation = [rootLayer frame];
161        currentLocationLayer=[CATextLayer layer];
162        currentLocationLayer.string=@"現在地";
163        currentLocationLayer.font=@"Lucida-Grande";
164        currentLocationLayer.fontSize=fontSize;
165        currentLocationLayer.foregroundColor=CGColorCreateGenericRGB(1.0,1.0,1.0,1.0);
166        currentLocationLayer.frame=CGRectMake(0.0, 0.0, rect.size.width, rect.size.height/4);
167        currentLocationLayer.backgroundColor=CGColorCreateGenericRGB(0.0,0.0,0.0,0.5);
168        [rootLayer addSublayer:currentLocationLayer];
169       
170        // selectionLayerの最初の位置を設定し、次に最初のselectedIndexを0に設定
171
172        int idx = 0;
173        geoclips[idx].title = @"東京都現代美術館";
174        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/1992.jpg";
175        geoclips[idx].latitude = 35.679667;
176        geoclips[idx].longitude = 139.807350;
177
178        idx++;
179        geoclips[idx].title = @"[080202]両国なう!";
180        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/1902.jpg";
181        geoclips[idx].latitude = 35.699852;
182        geoclips[idx].longitude = 139.799511;
183
184        idx++;
185        geoclips[idx].title = @"Spaceforyourfuture";
186        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/1716.jpg";
187        geoclips[idx].latitude = 35.680728;
188        geoclips[idx].longitude = 139.800703;
189
190        idx++;
191        geoclips[idx].title = @"門前仲町『ディデアン』";
192        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/1212.jpg";
193        geoclips[idx].latitude = 35.672700;
194        geoclips[idx].longitude = 139.796881;
195       
196        idx++;
197        geoclips[idx].title = @"深川八幡";
198        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/1210.jpg";
199        geoclips[idx].latitude = 35.671603;
200        geoclips[idx].longitude = 139.799633;
201       
202        idx++;
203        geoclips[idx].title = @"でかいし";
204        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/869.jpg";
205        geoclips[idx].latitude = 35.695278;
206        geoclips[idx].longitude = 139.814158;
207
208        idx++;
209        geoclips[idx].title = @"いちごジュース";
210        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/868.jpg";
211        geoclips[idx].latitude = 35.696261;
212        geoclips[idx].longitude = 139.814256;
213       
214        idx++;
215        geoclips[idx].title = @"くじら丼";
216        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/733.jpg";
217        geoclips[idx].latitude = 35.685403;
218        geoclips[idx].longitude = 139.780119;
219       
220        idx++;
221        geoclips[idx].title = @"相撲でもちゃんこでもない";
222        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/720.jpg";
223        geoclips[idx].latitude = 35.696000;
224        geoclips[idx].longitude = 139.791508;
225       
226        idx++;
227        geoclips[idx].title = @"錦糸町不二家";
228        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/621.jpg";
229        geoclips[idx].latitude = 35.695561;
230        geoclips[idx].longitude = 139.814869;
231       
232        idx++;
233        geoclips[idx].title = @"仕事場所その1";
234        geoclips[idx].filename = "/Users/user/Desktop/20080420MAKE/geoclip/552.jpg";
235        geoclips[idx].latitude = 35.698372;
236        geoclips[idx].longitude = 139.781203;
237       
238        int i;
239        for(i = 0; i < GEOCLIP_COUNT; i++){
240                CALayer *imageLayer = [CALayer layer];
241                geoclips[i].layer = imageLayer;
242                CGDataProviderRef imageDataProvider = CGDataProviderCreateWithFilename(geoclips[i].filename);
243                CGImageRef image = CGImageCreateWithJPEGDataProvider(imageDataProvider, NULL, NO, kCGRenderingIntentDefault);
244                imageLayer.contents = (id)image;
245                imageLayer.frame = CGRectMake(10, rootLayer.bounds.size.height - 150 - 10, 200, 150);
246                imageLayer.opacity = 0.5f;
247                [rootLayer addSublayer:imageLayer];
248                CGImageRelease(image);
249                CGDataProviderRelease(imageDataProvider);
250        }
251}
252
253-(void)dealloc
254{
255    [mCaptureSession stopRunning];
256    [[mCaptureDeviceInput device] close];
257
258    [mCaptureSession release];
259    [mCaptureDeviceInput release];
260
261        [rootLayer autorelease];
262        [countLayer autorelease];
263        [currentLocationLayer autorelease];
264        [names autorelease];
265        [super dealloc];
266}
267
268- (void)MyInstanceThreadMethod:(id)param
269{
270        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
271       
272        int i;
273        for(i=0; i<60*60*24; i++){
274                [NSThread sleepForTimeInterval:1.0];
275                selectedIndex = i%5;
276        }
277       
278       
279        [pool release];
280}
281
282- (void)MyInstanceThreadMethod2:(id)param
283{
284        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
285        ////////////////////////////////////////////////////////////////////////////////////
286        /* MacでUSB経由にてTDS01Vの値を取得する */
287
288// nanosleepをつっこむ事で受信を待つという、暫定的な対応をしている。
289// 同期・非カノニカルにする事で、ちゃんとした作りにできるはず。
290
291    int fd, res;                 /* fd:ファイルディスクリプタ res:受け取った文字数 */
292    struct termios oldtio, newtio;  /* 通信ポートを制御するためのインターフェイス */
293    char buf[255];                 /* 受信文字を格納 */
294    char crlf[2];
295
296    char modem_device[4096];
297
298    char send_command[4096];
299
300    int i;
301
302    struct timespec treq, trem;
303
304    char dir_dig_str[5];
305    short dir_dig;
306
307    treq.tv_sec = (time_t)0;
308    treq.tv_nsec = SLEEP_NSEC;
309
310    strcpy(modem_device, "/dev/tty.usbserial-0000101D");
311       
312
313   if((fd = open(modem_device, O_RDWR | O_NOCTTY ))==-1){
314   /* O_RDWR:読み書き両用 O_NOCTTY:tty制御をしない */
315       perror(modem_device);
316                mDirDig = 0.0;
317           while(1){
318                   nanosleep(&treq, &trem);
319                   mDirDig += 1.0;
320                   if(mDirDig>=360.0){
321                        mDirDig -= 360.0;
322                        }
323                }
324    }
325
326   tcgetattr(fd, &oldtio);         /* 現在のシリアルポートの設定を待避させる*/
327   bzero(&newtio, sizeof(newtio)); /* 新しいポートの設定の構造体をクリアする */
328   newtio.c_cflag = (BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD);
329
330  /*
331    BAUDRATE: ボーレートの設定
332    CRTSCTS : 出力のハードウェアフロー制御
333    CS8     : 8n1 (8 ビット,ノンパリティ,ストップビット 1)
334    CLOCAL  : ローカル接続,モデム制御なし
335    CREAD   : 受信文字(receiving characters)を有効にする.
336  */
337
338   newtio.c_iflag = (IGNPAR | ICRNL);
339  /*
340    IGNPAR  : パリティエラーのデータは無視する
341    ICRNL   : CR を NL に対応させる(これを行わないと,他のコンピュータで
342              CR を入力しても,入力が終りにならない)
343    それ以外の設定では,デバイスは raw モードである(他の入力処理は行わない)
344  */
345
346   newtio.c_oflag = 0;
347  /*   Raw モードでの出力 */
348
349   newtio.c_lflag = ICANON;
350  /*
351    ICANON  : カノニカル入力を有効にする
352    全てのエコーを無効にし,プログラムに対してシグナルは送らせない
353  */
354
355   tcflush(fd, TCIFLUSH);
356   tcsetattr(fd,TCSANOW,&newtio);
357  /*  モデムラインをクリアし,ポートの設定を有効にする */
358
359  /*
360    端末の設定終了.
361    行の先頭に 'z' を入力することでプログラムを終了させる
362  */
363   puts("START");
364
365   // センサ情報項目設定 全センサ全データ
366   // (ベクトルデータ+計測データ)
367  sprintf(send_command, "%s", "0DF7");
368  sprintf(buf, "%s\r\n", send_command);
369   write(fd,buf,strlen(buf));
370
371   // ACK センサ情報項目設定応答
372   nanosleep(&treq, &trem);
373   res = read(fd,buf,2);
374   read(fd,crlf,2);
375   buf[res]=0;
376
377   // 計測条件設定
378  sprintf(send_command, "%s", "050027950000");
379  sprintf(buf, "%s\r\n", send_command);
380   write(fd,buf,strlen(buf));
381
382   // [ACK 計測条件設定応答]を読み取ってみる
383   nanosleep(&treq, &trem);
384   res = read(fd,buf,2);
385   buf[res]=0;
386   read(fd,crlf,2);
387
388   // 地磁気センサ初期化要求
389  sprintf(send_command, "%s", "27");
390  sprintf(buf, "%s\r\n", send_command);
391   write(fd,buf,strlen(buf));
392
393   // ACK 地磁気センサ初期化要求応答
394   nanosleep(&treq, &trem);
395   res = read(fd,buf,2);
396   buf[res]=0;
397   read(fd,crlf,2);
398
399   for(i = 0; i < 10*60; i ++){
400
401  // 計測開始
402  sprintf(send_command, "%s", "21");
403  sprintf(buf, "%s\r\n", send_command);
404   write(fd,buf,strlen(buf));
405
406   // ACK 計測開始応答
407   nanosleep(&treq, &trem);
408   res = read(fd,buf,2);
409   buf[res]=0;
410   read(fd,crlf,2);
411
412  // センサ情報要求
413  sprintf(send_command, "%s", "29");
414  sprintf(buf, "%s\r\n", send_command);
415   write(fd,buf,strlen(buf));
416
417   // センサ情報 全センサ全データ(ベクトルデータ+計測データ)
418   nanosleep(&treq, &trem);
419   res = read(fd,buf,52);
420   buf[res]=0;
421   read(fd,crlf,2);
422
423   memcpy(dir_dig_str, &buf[12], 4);
424   dir_dig_str[4] = '\0';
425   dir_dig = (short)strtol(dir_dig_str,NULL,16);
426
427        mDirDig = dir_dig/10.0;
428
429   }
430
431  tcsetattr(fd, TCSANOW, &oldtio);  /* 退避させた設定に戻す */
432  close(fd);                       /* シリアルポートを閉じる */
433
434        ////////////////////////////////////////////////////////////////////////////////////
435        [pool release];
436}
437
438- (void)MyInstanceThreadMethod3:(id)param
439{
440        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
441       
442        while(1){
443                [[WifiGetter alloc] initWifiWithDelegate:self];
444                struct timespec treq, trem;
445                treq.tv_sec = (time_t)10;
446                treq.tv_nsec = 0;
447                nanosleep(&treq, &trem);
448        }
449
450        [pool release];
451}
452
453- (void)timerAdjustWindowSize:( NSTimer *)aTimer
454{
455        NSString *direction;
456        if(0.0 <= mDirDig && mDirDig <= 45.0)
457                direction = @"北";
458        else if(45.0 <= mDirDig && mDirDig <= 135.0)
459                direction = @"東";
460        else if(135.0 <= mDirDig && mDirDig <= 225.0)
461                direction = @"南";
462        else if(225.0 <= mDirDig && mDirDig <= 315.0)
463                direction = @"西";
464        else
465                direction = @"北";
466
467        Landmark presentPlace;
468       
469        presentPlace.caption = @"東京都江東区白河1-5-15";
470        presentPlace.latitude = 35.682708 ; // 緯度
471        presentPlace.longitude = 139.800612; // 経度
472       
473        const int LANDMARK_COUNT = 9;
474        Landmark landmarks[LANDMARK_COUNT];
475        landmarks[0].caption = @"森下駅";
476        landmarks[0].latitude = 35.688324; // 緯度
477        landmarks[0].longitude = 139.797034; // 経度
478
479        landmarks[1].caption = @"菊川駅";
480        landmarks[1].latitude = 35.688742; // 緯度
481        landmarks[1].longitude = 139.806025; // 経度
482
483        landmarks[2].caption = @"住吉駅";
484        landmarks[2].latitude = 35.689439; // 緯度
485        landmarks[2].longitude = 139.81566; // 経度
486
487        landmarks[3].caption = @"東陽町駅";
488        landmarks[3].latitude = 35.669988; // 緯度
489        landmarks[3].longitude = 139.817591; // 経度
490
491        landmarks[4].caption = @"木場駅";
492        landmarks[4].latitude = 35.669709; // 緯度
493        landmarks[4].longitude = 139.807034; // 経度
494
495        landmarks[5].caption = @"門前仲町駅";
496        landmarks[5].latitude = 35.672219; // 緯度
497        landmarks[5].longitude = 139.796219; // 経度
498
499        landmarks[6].caption = @"水天宮前駅";
500        landmarks[6].latitude = 35.683043; // 緯度
501        landmarks[6].longitude = 139.785383; // 経度
502
503        landmarks[7].caption = @"東日本橋駅";
504        landmarks[7].latitude = 35.692489; // 緯度
505        landmarks[7].longitude = 139.784825; // 経度
506       
507        landmarks[8].caption = @"錦糸町駅";
508        landmarks[8].latitude = 35.696811; // 緯度
509        landmarks[8].longitude = 139.813943; // 経度
510
511        NSString *to_land = @"NONE";
512        int i;
513        for(i=0; i<LANDMARK_COUNT; i++){
514                double k = atan2(
515                        landmarks[i].latitude - presentPlace.latitude
516                        ,landmarks[i].longitude - presentPlace.longitude) * 180 / PI;
517                k = -k + 90; // 角度を合わせる
518                if(k < 0.0)     k += 360.0;
519                if(360.0 <= k)  k -= 360.0;
520                double diff = k - mDirDig;
521                if(diff < 0) diff *= -1;
522                if(diff < 20.0){
523                        to_land = landmarks[i].caption;
524                        break;
525                }
526        }
527
528        countLayer.string=[NSString stringWithFormat:@"%.1f %@ %@", mDirDig, direction, to_land];
529       
530        for(i = 0; i < GEOCLIP_COUNT; i++){
531                geoclips[i].layer.opacity = 0.0f;
532        }
533        for(i = 0; i < GEOCLIP_COUNT; i++){
534                double k = atan2(
535                        geoclips[i].latitude - presentPlace.latitude
536                        ,geoclips[i].longitude - presentPlace.longitude) * 180 / PI;
537                geoclips[i].layer.frame = CGRectMake(
538                        10, rootLayer.bounds.size.height - rootLayer.bounds.size.height/4.0 - 10,
539                        rootLayer.bounds.size.width/4.0, rootLayer.bounds.size.height/4.0);
540                k = -k + 90; // 角度を合わせる
541                if(k < 0.0)     k += 360.0;
542                if(360.0 <= k)  k -= 360.0;
543                double diff = k - mDirDig;
544                if(diff < 0) diff *= -1;
545                if(diff < 20.0){
546                        geoclips[i].layer.opacity = 1.0f;
547                        break;
548                }
549        }
550}
551
552- (void)timerAdjustWindowSize2:( NSTimer *)aTimer
553{
554        [[WifiGetter alloc] initWifiWithDelegate:self];
555}
556
557- (void)setText:(NSString*)addr aLongitude:(NSString*)longitude aLatitude:(NSString*)latitude aMessage:(NSString*)msg
558{
559        NSLog(@"addr = %@", addr);
560        NSLog(@"latitude = %@", latitude);
561        NSLog(@"longitude = %@", longitude);
562        NSLog(@"msg = %@", msg);
563        currentLocationLayer.string=[NSString stringWithFormat:@"%@\n%@ : %@", addr, latitude, longitude];
564}
565
566@end
Note: See TracBrowser for help on using the browser.