root/platform/DennouMegane/MenuView.m @ 10578

Revision 10578, 14.3 kB (checked in by akio0911, 5 years ago)

add DennouMegane?

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