This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
forked from GTCMTold/Vertices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetupViewController.m
512 lines (406 loc) · 17.6 KB
/
SetupViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//
// SetupViewController.m
// Vertices
//
// Created by James O'Neill on 10/12/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "SetupViewController.h"
#import "SetupView.h"
@implementation SetupViewController
@synthesize verticesSession;
@synthesize receivedViewController;
@synthesize sentViewController;
@synthesize mySelfPeerID;
@synthesize CLController;
@synthesize connectionRequestsIn;
@synthesize acceptancesOut;
@synthesize connectionRequestsOut;
- (id)init {
self = [super init];
if (self) {
connectionRequestsIn = [[NSMutableArray alloc] init];
connectionRequestsOut = [[NSMutableArray alloc] init];
acceptancesOut = [[NSMutableArray alloc] init];
}
return self;
}
- (void)postInit {
[self setReceivedViewController: RECEIVEDVIEWCONTROLLER];
[self setSentViewController: SENTVIEWCONTROLLER];
err = [[NSError alloc] init];
[NSThread detachNewThreadSelector:@selector(btTimer)
toTarget:self
withObject:nil];
}
- (void)btTimer {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BOOL check;
while (TRUE) {
[NSThread sleepForTimeInterval:2];
[peerAvailable setArray:[verticesSession peersWithConnectionState:GKPeerStateAvailable]];
[peerConnected setArray:[verticesSession peersWithConnectionState:GKPeerStateConnected]];
//tableArray = [[NSMutableArray alloc] initWithObjects:peerConnected,peerAvailable,nil];
[self performSelectorOnMainThread:@selector(refreshTableView) withObject:nil waitUntilDone:NO];
//[self.view setNeedsDisplay];
//for(int i = 0; i<[[self.view subviews] count]; i+=1) {
// [[[self.view subviews] objectAtIndex:i] setNeedsDisplay];
// //[self.view bringSubviewToFront:[[self.view subviews] objectAtIndex:i] ];
//
// }
//peerTable = [[UITableView alloc]
// initWithFrame:[[UIScreen mainScreen] applicationFrame]
// style:UITableViewStyleGrouped];
// peerTable.delegate = self;
// peerTable.dataSource = self;
// [self.view addSubview:peerTable];
//update contingent lists
if([connectionRequestsIn count] > 0) {
check = [verticesSession acceptConnectionFromPeer:[connectionRequestsIn objectAtIndex:0] error:&err];
if(!check) {
NSLog(@"connectionRequestError %@", [err code]);
}
else {
[acceptancesOut addObject:[connectionRequestsIn objectAtIndex:0]];
[connectionRequestsIn removeObjectAtIndex:0];
}
}
else if([connectionRequestsOut count] > 0) {
[verticesSession connectToPeer:[connectionRequestsOut objectAtIndex:0] withTimeout:0];
[connectionRequestsOut removeObjectAtIndex:0];
}
}
[pool release];
}
- (void) refreshTableView
{
[peerTable reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
// This creates a peer mode session named "verticesSession", session ID is "Vertices", display name is the iphone name
[self setVerticesSession:[[GKSession alloc] initWithSessionID:@"Vertices"
displayName:nil
sessionMode:GKSessionModePeer]];
[[self verticesSession]setDelegate:self];
[[self verticesSession]setAvailable:YES];
[[self verticesSession] setDisconnectTimeout:0];
[[self verticesSession] setDataReceiveHandler:self withContext:nil];
[self setMySelfPeerID:[verticesSession displayNameForPeer:verticesSession.peerID]];
//Create a list to store peers list
peerAvailable=[[NSMutableArray alloc] init];
peerConnected=[[NSMutableArray alloc] init];
positionList=[[NSMutableArray alloc] init];
for (int i = 0; i < MAX_PEERS; i+=1)
{
[positionList addObject:[NSNumber numberWithFloat:-1.0]];
}
[peerAvailable setArray:[verticesSession peersWithConnectionState:GKPeerStateAvailable]];
[peerConnected setArray:[verticesSession peersWithConnectionState:GKPeerStateConnected]];
//NSLog(@"%@", [verticesSession displayNameForPeer:[peerAvailable objectAtIndex:0]]);
// NSLog(@"connected%@", peerConnected);
// NSLog(@"available%@", peerAvailable);
tableArray = [[NSMutableArray alloc] initWithObjects:peerConnected,peerAvailable,nil];
//NSLog(@"tableArray initial %@", tableArray);
//draw the list in NStable
peerTable = [[UITableView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]
style:UITableViewStyleGrouped];
peerTable.delegate = self;
peerTable.dataSource = self;
[self.view addSubview:peerTable];
//Instruction Alert View
UIAlertView *instructions1 = [[UIAlertView alloc] initWithTitle:@"How To - Step 1" message:@"In SETUP view, choose available peers to connect with. To calibrate, click the name of each connected peer with your phone pointed at that phone." delegate:self cancelButtonTitle:@"Next" otherButtonTitles:nil];
instructions1.tag=1;
[instructions1 show];
[instructions1 release];
//Toss View
tossImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"toss.png"]];
[tossImageView setAlpha:0];
//[self.view addSubview:tossImageView];
[[APPDELEGATE window] addSubview:tossImageView];
UIButton *btnRefresh = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnRefresh addTarget:self action:@selector(refreshButtonCB) forControlEvents:UIControlEventTouchUpInside];
btnRefresh.frame = CGRectMake(20, 300, 280, 30);
//[btnRefresh setImage:[UIImage imageNamed:@"refresh.jpg"] forState:UIControlStateNormal];
[btnRefresh setTitle:@"refresh" forState:UIControlStateNormal];
[self.view addSubview:btnRefresh];
//Create the buttons "connect"
// UIButton *btnConnect = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// [btnConnect addTarget:self action:@selector(showList) forControlEvents:UIControlEventTouchUpInside];
// [btnConnect setTitle:@"Search" forState:UIControlStateNormal];
// btnConnect.frame = CGRectMake(20, 100, 280, 30);
// //btnConnect.tag = 12;
// [self.view addSubview:btnConnect];
//ACCELEROMETER
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
accWaitTime = 0;
//LOCATION
CLController = [[CoreLocationController alloc] init];
CLController.delegate = self;
[CLController.locMgr startUpdatingLocation];
//Compass initialization
peerPosition=[[Position alloc] init];
[peerPosition initialize];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if(buttonIndex==0){
if(actionSheet.tag==1)
{
[APPDELEGATE tabBarController].selectedViewController = [[[APPDELEGATE tabBarController] viewControllers]objectAtIndex:1];
NSLog(@"tag=1");
UIAlertView *instructions2 = [[UIAlertView alloc] initWithTitle:@"How To - Step 2" message:@"In EDIT view, draw vertices with fingers. Press the bottom left button to start/stop recording and bottom right button to switch loop mode on/off." delegate:self cancelButtonTitle:@"Next" otherButtonTitles:nil];
[instructions2 show];
instructions2.tag=2;
[instructions2 release];
}
if (actionSheet.tag==2) {
[APPDELEGATE tabBarController].selectedViewController = [[[APPDELEGATE tabBarController] viewControllers]objectAtIndex:2];
NSLog(@"tag=2");
UIAlertView *instructions3 = [[UIAlertView alloc] initWithTitle:@"How To - Step 3" message:@"In RECEIVED view, select which sound objects to playback or edit. Press one of the numbers to play audio back. Hold one of the numbers to edit object." delegate:self cancelButtonTitle:@"Next" otherButtonTitles:nil];
//UIAlertView *instructions3 = [[UIAlertView alloc] initWithTitle:@"Finish" message:@"Start your VERTICES!" delegate:self cancelButtonTitle:@"GO!" otherButtonTitles:nil];
instructions3.tag=3;
[instructions3 show];
[instructions3 release];
}
if (actionSheet.tag==3){
[APPDELEGATE tabBarController].selectedViewController = [[[APPDELEGATE tabBarController] viewControllers]objectAtIndex:0];
UIAlertView *instructions4 = [[UIAlertView alloc] initWithTitle:@"Finish" message:@"Start your VERTICES!" delegate:self cancelButtonTitle:@"GO!" otherButtonTitles:nil];
[instructions4 show];
[instructions4 release];
}
}
}
// UIAccelerometerDelegate method, called when the device accelerates.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// Update the accelerometer graph view
//deviceTilt0.x = deviceTilt1.x;
//deviceTilt0.y = deviceTilt1.y;
z0 = z1;
//deviceTilt1.x = acceleration.x;
//deviceTilt1.y = acceleration.y;
z1 = acceleration.z;
//NSLog([NSString stringWithFormat:@"%f", acceleration.z]);
if(accWaitTime > 0)
accWaitTime -= 1;
//else if(deviceTilt1.y - deviceTilt0.y > 0.9) {
else if( z1 - z0 < -1.5 ) {
//this is where a toss is detected
//NSLog([NSString stringWithFormat:@"%f", deviceTilt1.y - deviceTilt0.y]);
//NSLog([NSString stringWithFormat:@"%f", z1 - z0]);
accWaitTime = 0;
//Diretion it points to
//NSLog(@"direction %f", [peerPosition magneticHeading]);
//send data
if ([[sentViewController sentSoundObjects] count] > 0) {
NSData* data = [[[sentViewController sentSoundObjects] lastObject] dataToSend];
//[self sendData:data toPeers:kAllPeers];
//NSLog([peerPosition compassAvailable]);
//NSLog(@"compassaAvailable %@", [peerPosition compassAvailable]?@"YES":@"NO");
if ([peerPosition compassAvailable]==TRUE){
//Compare the heading with all the peer position
//NSLog(@"magneticHeading %f",[peerPosition magneticHeading]);
NSEnumerator *e1 = [positionList objectEnumerator];
NSNumber * object1;
while (object1 = [e1 nextObject]) {
if ([object1 floatValue]!=-1){
if ((fabs([peerPosition magneticHeading]-[object1 floatValue])<30)||(fabs([peerPosition magneticHeading]+360-[object1 floatValue])<30)||(fabs([peerPosition magneticHeading]-360-[object1 floatValue])<30)) {
//NSLog(@"magneticHeading coming %f",[peerPosition magneticHeading]);
int index=[positionList indexOfObject:object1];
NSArray *peerToReceive = [NSArray arrayWithObject:[peerConnected objectAtIndex:index]];
//[verticesSession sendData:data toPeers:[peerConnected objectAtIndex:index]];
[verticesSession sendData:data toPeers:peerToReceive withDataMode:GKSendDataReliable error:nil];
//NSLog(@"senddata to %@", [peerConnected objectAtIndex:index]);
}
}
}
}
else{
[self sendData:data toPeers:kAllPeers];
}
//make animation
[tossImageView setAlpha:1];
[UIView beginAnimations:nil context:NULL]; // animate the following:
[UIView setAnimationDuration:1.0];
[tossImageView setAlpha:0];
[UIView commitAnimations];
}
}
}
//LOCATION STUFF
- (void)locationUpdate:(CLLocation *)location {
//NSLog([NSString stringWithFormat:@"%f", [location course]]);
}
- (void)locationError:(NSError *)error {
}
- (void)showList
{//get the list of available peersID
[peerAvailable setArray:[verticesSession peersWithConnectionState:GKPeerStateAvailable]];
[peerConnected setArray:[verticesSession peersWithConnectionState:GKPeerStateConnected]];
NSLog(@"%@", [verticesSession displayNameForPeer:[peerAvailable objectAtIndex:0]]);
NSLog(@"connected%@", peerConnected);
NSLog(@"available%@", peerAvailable);
tableArray = [[NSMutableArray alloc] initWithObjects:peerConnected,peerAvailable,nil];
//NSLog(@"tableArray initial %@", tableArray);
//draw the list in NStable
peerTable = [[UITableView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]
style:UITableViewStyleGrouped];
peerTable.delegate = self;
peerTable.dataSource = self;
[self.view addSubview:peerTable];
UIButton *btnRefresh = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnRefresh addTarget:self action:@selector(refreshButtonCB) forControlEvents:UIControlEventTouchUpInside];
btnRefresh.frame = CGRectMake(20, 300, 280, 30);
//[btnRefresh setImage:[UIImage imageNamed:@"refresh.jpg"] forState:UIControlStateNormal];
[btnRefresh setTitle:@"refresh" forState:UIControlStateNormal];
[self.view addSubview:btnRefresh];
}
-(void)refreshButtonCB {
tableArray = [[NSMutableArray alloc] initWithObjects:peerConnected,peerAvailable,nil];
peerTable = [[UITableView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]
style:UITableViewStyleGrouped];
peerTable.delegate = self;
peerTable.dataSource = self;
[self.view addSubview:peerTable];
UIButton *btnRefresh = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnRefresh addTarget:self action:@selector(refreshButtonCB) forControlEvents:UIControlEventTouchUpInside];
btnRefresh.frame = CGRectMake(20, 300, 280, 30);
//[btnRefresh setImage:[UIImage imageNamed:@"refresh.jpg"] forState:UIControlStateNormal];
[btnRefresh setTitle:@"refresh" forState:UIControlStateNormal];
[self.view addSubview:btnRefresh];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger sections = [tableArray count];
//NSLog(@"tableArray in section %@", tableArray);
//NSLog(@"number of sections%d", sections);
return sections;
}
- (NSInteger)tableView:(UITableView *)peerTable numberOfRowsInSection:(NSInteger)section {
//NSLog(@"tableArray%@",tableArray);
NSArray *sectionContents = [tableArray objectAtIndex:section];
NSInteger rows = [sectionContents count];
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)peerTable
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [peerTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSArray *sectionContents = [tableArray objectAtIndex:[indexPath section]];
NSString *contentForThisRow = [sectionContents objectAtIndex:[indexPath row]];
//cell.font=[UIFont fontWithName:@"Arial" size:16.0];
//cell.imageView.image = [UIImage imageNamed:@"peer2.jpg"];
cell.textLabel.text = [verticesSession displayNameForPeer:contentForThisRow];
return cell;
}
- (NSString *)tableView:(UITableView *)peerTable
titleForHeaderInSection:(NSInteger)section{
switch (section){
case 0:
return @"Connected Peers";
break;
case 1:
return @"Available Peers";
break;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%d was pressed", indexPath.row);
if ([indexPath section] == 0) {
//NSLog(@"section %d", [indexPath section]);
//if([positionList objectAtIndex:[indexPath row]]==-1.0){
//[positionList replaceObject:[NSNumber numberWithFloat:[peerPosition magneticHeading]] atIndex:indexPath.row];
[positionList replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithFloat:[peerPosition magneticHeading]]];
NSLog(@"positinList %@",positionList);
//}
//else{
// [positionList replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithFloat:[peerPosition magneticHeading]]];
//}
}
else if ([indexPath section] == 1) {
//NSLog(@"section %d", [indexPath section]);
[connectionRequestsOut addObject:[peerAvailable objectAtIndex:indexPath.row]];
//[self connect:[peerAvailable objectAtIndex:indexPath.row]];
}
}
//check constants in .pch for 'peers' argument
- (void)sendData:(NSData*)data toPeers:(int)peers {
if(peers == kAllPeers) {
[verticesSession sendDataToAllPeers:data
withDataMode:GKSendDataReliable
error:nil];
}
}
- (void)dealloc {
[peerAvailable release];
[peerConnected release];
[tableArray release];
[super dealloc];
}
#pragma mark -
#pragma mark GKSessionDelegate
//send request to connect
- (void)connect:(NSString *)peerID
{
[verticesSession connectToPeer:peerID withTimeout:0];
NSLog(@"connection requested to %@",peerID);
}
//print peerID, When receiving connection request from peers. Then automatically receive! JUTS FOR NOW!!!
- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID{
NSLog(@":%s", peerID);
//[NSError errorWithDomain:GKSessionErrorDomain code:<#(NSInteger)code#> userInfo:<#(NSDictionary *)dict#>
[connectionRequestsIn addObject:[[NSString alloc] initWithString:peerID]];
}
- (void)session:(GKSession *)session didFailWithError:(NSError *)error {
}
//When peers change states
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{
if(state == GKPeerStateConnected){
// Add the peer to the Array
[peerConnected addObject:peerID];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connected" message:peerID delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
if(state == GKPeerStateAvailable){
// Add the peer to the Array
NSLog([verticesSession displayNameForPeer:peerID]);
NSLog(@"available");
}
if(state == GKPeerStateUnavailable){
// Add the peer to the Array
NSLog([verticesSession displayNameForPeer:peerID]);
}
if(state == GKPeerStateDisconnected){
//[peerConnected removeObject:peerID];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Disconnected" message:peerID delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)denyConnectionFromPeer:(NSString *)peerID {
}
- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
NSUInteger peerIndex = [peerConnected indexOfObject: peer];
[receivedViewController addSoundObjectForData:data fromPeer:peerIndex];
//Convert received NSData to NSString to display
// NSString *whatDidIget = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
//
// //Display as a UIAlertView
// UIAlertView *r = [[UIAlertView alloc] initWithTitle:@"Received" message:whatDidIget delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
// [r show];
// [r release];
// [whatDidIget release];
//Display what others are drawing??
}
@end