-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathWaveViewController.m
354 lines (313 loc) · 14 KB
/
WaveViewController.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
//
// WaveViewController.m
// XingAi02
//
// Created by Lihui on 14/11/14.
// Copyright (c) 2014年 Lihui. All rights reserved.
//
#import "WaveViewController.h"
#import "MainTabViewController.h"
#import "SliderViewController.h"
#import "UserScoreDAO.h"
@interface WaveViewController ()
{
NSTimer *timer;
NSTimer *setZeroTimer;
}
@property(nonatomic,retain)UILabel *sportsTime;
@property(nonatomic,retain)UILabel *sportsIntensity;
@property(nonatomic,retain)UILabel *sportsHz;
@property(nonatomic,retain)UILabel *sportsCount;
@property(nonatomic,retain)UILabel *sportsTimeData;
@property(nonatomic,retain)UILabel *sportsIntensityData;
@property(nonatomic,retain)UILabel *sportsHzData;
@property(nonatomic,retain)UILabel *sportsCountData;
@property(nonatomic,retain)UIButton *pauseButton;
@property(nonatomic,retain)UIButton *finishButton;
@end
@implementation WaveViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.navigationController.interactivePopGestureRecognizer.enabled = NO;
// self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickReturnButton:)];
_returnButton.userInteractionEnabled=NO;
[self addObserver:[SliderViewController sharedSliderController].MainVC forKeyPath:@"isEndGetReciveData" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"Pass Contex"];
_isEndGetReciveData=NO;
// Do any additional setup after loading the view from its nib.
_wave = [[DrawWave alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 36+360)];
_wave.clipsToBounds = YES;
_wave.backgroundColor=[UIColor clearColor];
[self.view addSubview:_wave];
UIImageView *scaleImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 36)];
scaleImageView.image=[UIImage imageNamed:@"主页-上滑标尺"];
[_wave addSubview:scaleImageView];
_wave1=[[DrawData alloc] initWithFrame:CGRectMake(0, 90+36, SCREEN_WIDTH, 360)];
_wave1.clipsToBounds = YES;
_wave1.backgroundColor=[UIColor clearColor];
[self.view addSubview:_wave1];
timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(detectionVoice) userInfo:nil repeats:YES];
//[self setTimerFire];
// setZeroTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(detectionVoice) userInfo:nil repeats:YES];
_bleControl=[BLEControl sharedBLEControl];
_bleControl.delegate=self;
_label=[[UILabel alloc]initWithFrame:CGRectMake(20, 450, 280, 100)];
//[self.view addSubview:_label];
_label.backgroundColor=[UIColor clearColor];
_label.text=@"0";
_label.textAlignment=NSTextAlignmentCenter;
_label.font=[UIFont systemFontOfSize:80];
_label.textColor=[UIColor whiteColor];
[self initLabel];
[self initDataLabel];
//开启一个后台线程,调用执行startReciveRealTimeData方法
[self performSelectorInBackground:@selector(startReciveRealTimeData) withObject:nil];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:YES];
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
-(void) initLabel
{
_sportsTime=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2+30, 165, SCREEN_WIDTH/2-70, 30)];
_sportsTime.backgroundColor=[UIColor clearColor];
_sportsTime.text=@"运动时间";
_sportsTime.textAlignment=NSTextAlignmentLeft;
_sportsTime.font=[UIFont systemFontOfSize:15];
_sportsTime.textColor=[UIColor whiteColor];
[self.view addSubview:_sportsTime];
_sportsCount=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2+30, 230, SCREEN_WIDTH/2-70, 30)];
_sportsCount.backgroundColor=[UIColor clearColor];
_sportsCount.text=@"运动次数";
_sportsCount.textAlignment=NSTextAlignmentLeft;
_sportsCount.font=[UIFont systemFontOfSize:15];
_sportsCount.textColor=[UIColor whiteColor];
[self.view addSubview:_sportsCount];
_sportsIntensity=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2+30, 305, SCREEN_WIDTH/2-70, 30)];
_sportsIntensity.backgroundColor=[UIColor clearColor];
_sportsIntensity.text=@"实时强度";
_sportsIntensity.textAlignment=NSTextAlignmentLeft;
_sportsIntensity.font=[UIFont systemFontOfSize:15];
_sportsIntensity.textColor=[UIColor whiteColor];
[self.view addSubview:_sportsIntensity];
_sportsHz=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2+30, 370, SCREEN_WIDTH/2-70, 30)];
_sportsHz.backgroundColor=[UIColor clearColor];
_sportsHz.text=@"实时频率";
_sportsHz.textAlignment=NSTextAlignmentLeft;
_sportsHz.font=[UIFont systemFontOfSize:15];
_sportsHz.textColor=[UIColor whiteColor];
[self.view addSubview:_sportsHz];
}
-(void)initDataLabel
{
_sportsTimeData=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2+30, 185, SCREEN_WIDTH/2-70, 30)];
_sportsTimeData.backgroundColor=[UIColor clearColor];
_sportsTimeData.text=[self transformTimeFormat:_bleControl.sportsSeconds];
_sportsTimeData.textAlignment=NSTextAlignmentLeft;
_sportsTimeData.font=[UIFont systemFontOfSize:20];
_sportsTimeData.textColor=[UIColor colorWithRed:224.0/255 green:184.0/255 blue:25.0/255 alpha:1.0];
[self.view addSubview:_sportsTimeData];
_sportsCountData=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2+30, 250, 60, 30)];
_sportsCountData.backgroundColor=[UIColor clearColor];
_sportsCountData.text=@"0";
_sportsCountData.textAlignment=NSTextAlignmentLeft;
_sportsCountData.font=[UIFont systemFontOfSize:20];
_sportsCountData.textColor=[UIColor colorWithRed:224.0/255 green:184.0/255 blue:25.0/255 alpha:1.0];
[self.view addSubview:_sportsCountData];
_sportsIntensityData=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2+30, 325, 60, 30)];
_sportsIntensityData.backgroundColor=[UIColor clearColor];
_sportsIntensityData.text=@"0";
_sportsIntensityData.textAlignment=NSTextAlignmentLeft;
_sportsIntensityData.font=[UIFont systemFontOfSize:20];
_sportsIntensityData.textColor=[UIColor colorWithRed:224.0/255 green:184.0/255 blue:25.0/255 alpha:1.0];
[self.view addSubview:_sportsIntensityData];
_sportsHzData=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2+30, 390, SCREEN_WIDTH/2-70, 30)];
_sportsHzData.textColor=[UIColor colorWithRed:224.0/255 green:184.0/255 blue:25.0/255 alpha:1.0];
_sportsHzData.text=@"0";
_sportsHzData.textAlignment=NSTextAlignmentLeft;
_sportsHzData.font=[UIFont systemFontOfSize:18];
_sportsHzData.backgroundColor=[UIColor clearColor];
[self.view addSubview:_sportsHzData];
_pauseButton=[[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-66)/2, 425+36-3-40, 66, 66)];
// [StopButton setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
[self.view addSubview:_pauseButton];
[_pauseButton setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
// [_pauseButton setTitle:@"暂 停" forState:UIControlStateNormal];
// [_pauseButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[_pauseButton addTarget:self action:@selector(clickPauseButton) forControlEvents:UIControlEventTouchUpInside];
_finishButton=[[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-60)/2+80, 425+36-3, 60, 33)];
// [StopButton setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
[self.view addSubview:_finishButton];
[_finishButton setTitle:@"完 成" forState:UIControlStateNormal];
[_finishButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_finishButton addTarget:self action:@selector(clickFinishButton) forControlEvents:UIControlEventTouchUpInside];
}
-(void)clickPauseButton {
if ([_pauseButton.imageView.image isEqual:[UIImage imageNamed:@"暂停"]]) {
[_pauseButton setImage:[UIImage imageNamed:@"开始"] forState:UIControlStateNormal];
[_bleControl endReciveRealTimeData];
}
else {
[_pauseButton setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
[_bleControl getRealTimeData];
}
// if ([_pauseButton.titleLabel.text isEqualToString:@"暂 停"]) {
// [_bleControl endReciveRealTimeData];
// [_pauseButton setTitle:@"开 始" forState:UIControlStateNormal];
// [_pauseButton setImage:[UIImage imageNamed:@"开始"] forState:UIControlStateNormal];
// }
// else if([_pauseButton.titleLabel.text isEqualToString:@"开 始"])
// {
// [_bleControl getRealTimeData];
// [_pauseButton setTitle:@"暂 停" forState:UIControlStateNormal];
// [_pauseButton setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
//
// }
}
-(void)clickFinishButton
{
[_bleControl finishRealTimeSports];
_pauseButton.userInteractionEnabled=NO;
_returnButton.userInteractionEnabled=YES;
_finishButton.userInteractionEnabled=NO;
self.isEndGetReciveData=YES;
[self.navigationController popToRootViewControllerAnimated:YES];
_bleControl.delegate=nil;
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"请您对本次结果打分"message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
//先存个默认的分数0分
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
[self saveZeroInUserScoreCoreDate];
});
}
-(void)addCountNum:(NSInteger)num
Intensity:(float)intensity
Hz:(float)hz
{
_sportsCountData.text=[NSString stringWithFormat:@"%ld",(long)num];
_sportsIntensityData.text
=[NSString stringWithFormat:@"%0.1f",intensity];
_sportsHzData.text=[NSString stringWithFormat:@"%0.1f",hz];
}
-(void)renewSportsTime {
NSString *timeStr=[self transformTimeFormat:_bleControl.sportsSeconds];
_sportsTimeData.text=timeStr;
}
-(void)detectionVoice
{
//[_wave callDraw:1];
[_wave1 addZeroNum];
}
//清零
-(void)setDataLabelZero
{
double delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
_sportsHzData.text=@"0";
_sportsCountData.text=@"0";
_sportsIntensityData.text=@"0";
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//关闭定时器
-(void)setTimerFire {
[timer setFireDate:[NSDate distantFuture]];
}
//开启定时器
-(void)setTimerRun {
[timer setFireDate:[NSDate distantPast]];
}
//关闭定时器
-(void)setZeroTimerFire {
[setZeroTimer setFireDate:[NSDate distantFuture]];
}
//开启定时器
-(void)setZeroTimerRun {
[setZeroTimer setFireDate:[NSDate distantPast]];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)clickStartButton:(id)sender {
if(_bleControl.activePeripheral) {
//CBPeripheral *xx=_bleControl.activePeripheral;
//NSLog(@"%@",xx.name);
// [self.bleControl getRealTimeData];
[self.bleControl endReciveRealTimeData];
// if ([self.startButton.titleLabel.text isEqualToString:@"开始"]) {
// [self.startButton setTitle:@"结束" forState:UIControlStateNormal];
// [self.bleControl getRealTimeData];
// }
// else {
// [self.startButton setTitle:@"开始" forState:UIControlStateNormal];
// [self.bleControl endReciveRealTimeData];
// }
//
}
}
//开始接收蓝牙实时数据
-(void) startReciveRealTimeData
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue,^{
[_bleControl getRealTimeData];
});
}
- (IBAction)clickReturnButton:(id)sender {
//[_bleControl endReciveRealTimeData];
self.isEndGetReciveData=YES;
[self.navigationController popToRootViewControllerAnimated:YES];
_bleControl.delegate=nil;
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"请您对本次结果打分"message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
//先存个默认的分数0分
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
[self saveZeroInUserScoreCoreDate];
});
}
//时间转换器
-(NSString*)transformTimeFormat:(NSInteger)seconds
{
NSInteger hour=seconds/3600;
NSInteger minute=(seconds%3600)/60;
NSInteger second=seconds%60;
NSString *hourStr=hour<10?[NSString stringWithFormat:@"0%ld",(long)hour]:[NSString stringWithFormat:@"%ld",(long)hour];
NSString *minuteStr=minute<10?[NSString stringWithFormat:@"0%ld",(long)minute]:[NSString stringWithFormat:@"%ld",(long)minute];
NSString *secondStr=second<10?[NSString stringWithFormat:@"0%ld",(long)second]:[NSString stringWithFormat:@"%ld",(long)second];
NSString *text=[NSString stringWithFormat:@"%@:%@:%@",hourStr,minuteStr,secondStr];
return text;
}
-(BOOL)saveZeroInUserScoreCoreDate
{
UserScoreDAO *DAO=[UserScoreDAO shareManager];
UserScore *data=[[UserScore alloc]init];
data.score=[NSNumber numberWithInt:0];
data.date=[NSDate date];
if([DAO create:data]==0) {
NSLog(@"这个默认的分数0保存了");
return YES;
}
else return NO;
}
-(void)autoPopCurrentVC
{
[self.navigationController popToRootViewControllerAnimated:YES];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"请您对本次结果打分"message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
_bleControl.delegate=nil;
}
@end