-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReportViewController.m
502 lines (415 loc) · 15 KB
/
ReportViewController.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
//
// ReportViewController.m
// AccelerometerGraph
//
// Created by Liangjun Jiang on 7/2/12.
// Copyright (c) 2012 ByPass Lane. All rights reserved.
//
#import "ReportViewController.h"
#import "InAppPurchaseViewController.h"
//#import <MessageUI/MessageUI.h>
//#import <MessageUI/MFMailComposeViewController.h>
#define THRESHOLD 0.90 // this is used to collect the peak value +/- 90% of the max swing speed detected.
#define EFFECTIVE_POINTS 180
#define NOISE_FLOOR 0.1
#define GRAVITY 9.8 // m/s^2
#define kUpdateFrequency 60
//#pragma mark - Help class to show the name prompt
//@interface NameAlertPrompt : UIAlertView {
// UITextField *textField;
//
//}
//
//@property (nonatomic, retain) UITextField *textField;
//@property (readonly) NSString *enteredText;
//- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
//
//@end
//
//
//@implementation NameAlertPrompt
//@synthesize textField, enteredText;
//
//- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle {
//
// self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil];
//
// if (self) {
// UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
// [theTextField setBackgroundColor:[UIColor whiteColor]];
// [theTextField setSecureTextEntry:YES];
// [theTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
// [theTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
// [self addSubview:theTextField];
// self.textField = theTextField;
// }
// return self;
//}
//
//- (void)show {
// [textField becomeFirstResponder];
// [super show];
//}
//
//- (NSString *)enteredText {
// return textField.text;
//}
//
//@end
@interface ReportViewController ()<InAppPurchaseViewControllerDelegate>
{
int count;
float max;
float mean;
float standardDeviation;
float maxAccX;
float meanAccX;
float sdAccx;
BOOL right_handed;
}
@property (nonatomic, strong) IBOutlet UITableView *mTableView;
@property (nonatomic, strong) IBOutlet UINavigationBar *navBar;
@property (nonatomic, strong) NSMutableArray *countableSwings;
@property (nonatomic, strong) NSMutableArray *countableSwingTempos;
@property (nonatomic, strong) NSDictionary *calculatedSwingTempo;
@property (nonatomic, strong) NSMutableArray *swingVelocity;
@end
@implementation ReportViewController
@synthesize delegate = _delegate, mTableView = _mTableView, navBar;
@synthesize rawData = _rawData, accelormeterData = _accelormeterData;
@synthesize countableSwings, countableSwingTempos, calculatedSwingTempo, swingVelocity;
- (IBAction)done:(id)sender
{
[self.delegate reportViewControllerDidFinish:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *title;
if ([defaults objectForKey:CLUB]) {
title = [[defaults objectForKey:CLUB] objectForKey:@"name"];
} else {
title = @"Driver";
}
self.navBar.topItem.title = title;
// We determine if the user is left-handed or right handed
right_handed = [[[NSUserDefaults standardUserDefaults] objectForKey:HANDED] isEqualToString:@"Right-handed"]?YES:NO;
// We do some simple math
max = 0.0;
mean = 0.0;
standardDeviation = 0.0;
max = [self calculateMax:self.rawData];
countableSwings = [self calculateCountableFromRawData:self.rawData withThreshold:max];
mean = [self calculateMean:countableSwings];
standardDeviation = sqrtf([self calculateVariance:countableSwings withMean:mean]);
maxAccX = 0.0;
meanAccX = 0.0;
sdAccx = 0.0;
NSMutableArray *cacluatingTempo = [NSMutableArray array];
[self.accelormeterData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[cacluatingTempo addObject:[(NSDictionary *)obj objectForKey:@"accX"]];
}];
swingVelocity = [self calculateSwingVelocity:cacluatingTempo];
maxAccX = [self calculateSwingTempoMax:swingVelocity];
// NSLog(@"max accX: %.2f",maxAccX);
countableSwingTempos = [self calculateCountableFromRawData:swingVelocity withThreshold:maxAccX];
meanAccX = [self calculateMean:countableSwingTempos];
sdAccx = sqrtf([self calculateVariance:swingVelocity withMean:meanAccX]);
calculatedSwingTempo = [NSDictionary dictionaryWithDictionary:[self calculateSwingTempo:swingVelocity withMax:maxAccX]];
}
- (void)viewDidUnload
{
[super viewDidUnload];
_mTableView = nil;
_rawData = nil;
countableSwings = nil;
_accelormeterData = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 4;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch (section) {
case 0:
return @"Number of Swing Counted";
break;
case 1:
return @"Distribution [MPH]";
break;
case 2:
return @"Counted Club Speed [MPH]";
break;
case 3:
return @"Calculated Swing Tempo [SECONDS]";
break;
default:
break;
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
switch (section) {
case 0:
return 1;
break;
case 1:
return 3;
break;
case 2:
return [countableSwings count];
break;
case 3:
return 2;
break;
default:
break;
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
switch (section) {
case 0:
switch (row) {
case 0:
cell.textLabel.text = [NSString stringWithFormat:@"%d", [countableSwings count]];
break;
default:
break;
}
break;
case 1:
switch (row) {
case 0:
cell.textLabel.text = [NSString stringWithFormat:@"Max: %.2f",max];
break;
case 1:
cell.textLabel.text = [NSString stringWithFormat:@"Mean: %.2f",mean];
break;
case 2:
cell.textLabel.text = [NSString stringWithFormat:@"Standard Deviation: %.2f",standardDeviation];
break;
default:
break;
}
break;
case 2:
cell.textLabel.text = [NSString stringWithFormat:@"%.2f",[[countableSwings objectAtIndex:row] floatValue]];
break;
case 3:
{
cell.textLabel.text = (row == 0)?[NSString stringWithFormat:@"Backswing: %.2f sec",[[calculatedSwingTempo objectForKey:@"backSwing"] floatValue] * 1.0/60.0]:[NSString stringWithFormat:@"Downswing: %.2f sec",[[calculatedSwingTempo objectForKey:@"downSwing"] floatValue]* 1.0/60.0];
break;
}
default:
break;
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark - help methods
- (float )calculateMax:(NSMutableArray *)mData{
float localMax = 0.0;
for (NSNumber *number in mData){
if ([number floatValue] > 0) {
if ([number floatValue] > localMax) {
localMax = [number floatValue];
}
}
else {
if (-[number floatValue] > localMax) {
localMax = -[number floatValue];
}
return -localMax;
}
}
return localMax;
}
// to measure swing tempo's max
- (float)calculateSwingTempoMax:(NSArray *)mData
{
NSNumber* min = [mData valueForKeyPath:@"@min.self"];
NSNumber* maxV = [mData valueForKeyPath:@"@max.self"];
if (right_handed) {
//for right handed, the max speed should be negative
return (fabs([min floatValue]) > fabs([maxV floatValue]))?[min floatValue]:[maxV floatValue];
} else
// for left-handed, the max speed should be positive
return (fabs([maxV floatValue]) > fabs([min floatValue]))?[maxV floatValue]:[min floatValue];
}
// we use +/- 20% of the max to get other peak values
- (NSMutableArray *)calculateCountableFromRawData:(NSMutableArray *)mData withThreshold:(float)mMax
{
NSMutableArray *localArray = [NSMutableArray array];
for (NSNumber *number in mData) {
if ([number floatValue] > THRESHOLD*mMax)
[localArray addObject:number];
}
return localArray;
}
- (float)calculateMean:(NSMutableArray *)mData
{
float sum = 0.0;
for (NSNumber *number in mData){
sum += [number floatValue];
}
return sum / [mData count];
}
- (float)calculateVariance:(NSMutableArray *)mData withMean:(float)mMean
{
float sum = 0.0;
for (NSNumber *number in mData) {
sum += fabsf(mMean - [number floatValue])*fabsf(mMean - [number floatValue]);
}
return sum / [mData count];
}
// we use the simple area method to calculate the swing velocity
- (NSMutableArray *)calculateSwingVelocity:(NSMutableArray *)mData {
NSMutableArray *velocityArray = [[NSMutableArray alloc] initWithCapacity:0];
float sum = 0.0;
for (int i = 0; i < [mData count]; i++) {
sum += [[mData objectAtIndex:i] floatValue] * GRAVITY * 1/kUpdateFrequency * METER_TO_MILE;
[velocityArray addObject:[NSNumber numberWithFloat:sum]];
}
return velocityArray;
}
- (NSDictionary *)calculateSwingTempo:(NSMutableArray *)mData withMax:(float)mMax{
NSUInteger maxIndex = 0;
int backTiming = 0.0;
int downTiming = 0.0;
if ([mData containsObject:[NSNumber numberWithFloat:mMax]]) {
maxIndex = [mData indexOfObject:[NSNumber numberWithFloat:mMax]];
}
for (int i = 0; i< [mData count]; i++) {
float temp = [[mData objectAtIndex:i] floatValue];
if (i < maxIndex && fabs(temp)> NOISE_FLOOR) {
if (right_handed){
if (temp < 0) backTiming++;
} else if(temp > 0) backTiming++;
}
}
// It's better that we starts from the end of backswing
for (int j = maxIndex * 0.3; j < [mData count]; j++) {
float temp = [[mData objectAtIndex:j] floatValue];
if ( fabs(temp) > NOISE_FLOOR) {
if (right_handed) {
if (temp > 0) downTiming++;
} else if (temp < 0) downTiming++;
}
}
// NSLog(@"down timing :%d, finished downswing %d, finished backswing, %d", downTiming, finishedDownSwing, finishedBackSwing);
return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:backTiming],@"backSwing",[NSNumber numberWithInt:downTiming],@"downSwing", nil];
}
#pragma mark - Remove Ads
- (void)purchaseControllerDidFinish:(InAppPurchaseViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)removeAds:(id)sender
{
[self loadingInAppPurchaseItems];
}
- (void)loadingInAppPurchaseItems{
InAppPurchaseViewController *inAppViewController = [[InAppPurchaseViewController alloc] initWithNibName:@"InAppPurchaseViewController" bundle:nil];
inAppViewController.delegate = self;
[self presentModalViewController:inAppViewController animated:YES];
}
#pragma mark - Mail Delegate
//- (IBAction)sendFeedback:(id)sender
//{
// Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
// if (mailClass != nil)
// {
// // We must always check whether the current device is configured for sending emails
// if ([mailClass canSendMail])
// {
// [self displayComposerSheet];
// }
// }
//
//}
//
//
//- (void)displayComposerSheet{
//
// MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
// picker.mailComposeDelegate = self;
//
// [picker setSubject:@"I'd like to hear from you!"];
//
// [picker setToRecipients:[NSArray arrayWithObject:@"[email protected]"]];
//
// NSString *emailBody = @"Do you think if the data make sense? Send me your comment and suggestion";
//
// [picker setMessageBody:emailBody isHTML:NO];
//
// [self presentModalViewController:picker animated:YES];
//
//}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
//- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
//{
// // message.hidden = NO;
// // Notifies users about errors associated with the interface
// switch (result)
// {
// case MFMailComposeResultCancelled:
// // message.text = @"Result: canceled";
// break;
// case MFMailComposeResultSaved:
// // message.text = @"Result: saved";
// break;
// case MFMailComposeResultSent:
// // message.text = @"Result: sent";
// break;
// case MFMailComposeResultFailed:
// // message.text = @"Result: failed";
// break;
// default:
// // message.text = @"Result: not sent";
// break;
// }
// [self dismissModalViewControllerAnimated:YES];
//}
#pragma mark - Alert View Delegate
//- (void)alertViewCancel:(UIAlertView *)alertView
//{
// NSLog(@"User Cancelled");
//}
//
//- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
// switch (buttonIndex) {
// case 0:
// break;
//
// default:
// NSLog(@"Save the user!");
// break;
// }
//
//}
@end