【iOS与EV3混合机器人编程系列之三】编写EV3 Port Viewer 应用监

连接上之后,其实App就已经开始接收从EV3那边传过来的各个端口的数据了,不过我们现在还看不到。

要断开连接也很简单,再点击一下已连接的ip地址,就会弹出是否断开连接的提示。

==Step 8: 显示端口数据==

这是这个App的功能所在,就是类似于在EV3上直接可以看到的Port View,只不过我们把这个功能搬到了iPhone上。

由于我对于这个代码库的封装使得现在要获取端口数据变得易如反掌。大家马上可以看到。这个程序最大的问题可能在于这个Ev3TableViewController的编写上。

对于对iOS开发完全不了解的童鞋,这里我也无法讲得再细使大家都能理解。建议不了解的童鞋还是先看看iOS开发的基础教程。

下面介绍每一步的实现方法。

Step 7.1 :编写 Ev3Cell

我们要显示每个端口的信息,包含以下几个方面:

1)端口连接的传感器,上图片

2)端口的名称

3)端口连接的传感器的名称

4)端口连接的传感器发送过来的实时数据

因此,打开Ev3Cell.h,添加如下代码:

@property (nonatomic,strong) UIImageView *sensorImage;@property (nonatomic,strong) UILabel *portLabel;@property (nonatomic,strong) UILabel *nameLabel;@property (nonatomic,strong) UILabel *dataLabel;

接下来,打开Ev3Cell.h,添加如下代码到初始化Method中:

– (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {// Initialization codeself.frame = CGRectMake(0, 0, 320, 120);self.sensorImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];self.portLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 15, 180, 20)];self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 50, 180, 20)];self.dataLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 85, 180, 20)];[self addSubview:self.sensorImage];[self addSubview:self.portLabel];[self addSubview:self.nameLabel];[self addSubview:self.dataLabel];}return self;}

这里我们采用纯手写的方式来编写Ev3Cell,我们还可以使用storyboard或xib通过图形界面来设置,这里不多讲。

OK,我们编写好了Ev3Cell,接下来是Ev3TableViewController

Step 7.2 完善Ev3TableViewController

1)打开Ev3TableViewController.m,添加头文件

#import "EV3WifiBrowserViewController.h"#import "EV3WifiManager.h"#import “Ev3Cell.h"

2)添加property

@property (nonatomic,strong) EV3WifiManager *ev3WifiManager;@property (nonatomic,strong) EV3Device *device;

3)添加初始化代码在ViewDidLoad:方法中

– (void)viewDidLoad{[super viewDidLoad];self.ev3WifiManager = [EV3WifiManager sharedInstance];[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refreshData) userInfo:nil repeats:YES];}

这里因为考虑到我们需要实时更新数据,所以弄了一个定时器NSTimer,让其每0.1s更新一次。

4)添加refreshData方法

– (void)refreshData{self.device = self.ev3WifiManager.devices.allValues.lastObject;[self.tableView reloadData];}

5)更改tableView的设置

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{if (self.device) {return 8;} else return 1;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return 120;}

6)更改Cell的内容

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{Ev3Cell *cell = [[Ev3Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];cell.userInteractionEnabled = NO;if (self.device) {switch (indexPath.row) {case 0:cell.portLabel.text = @"PORTA";cell.nameLabel.text = self.device.sensorPortA.typeString;cell.imageView.image = self.device.sensorPortA.image;cell.dataLabel.text = [NSString stringWithFormat:@"Raw Data:%d",self.device.sensorPortA.data];break;case 1:cell.portLabel.text = @"PORTB";cell.nameLabel.text = self.device.sensorPortB.typeString;cell.imageView.image = self.device.sensorPortB.image;cell.dataLabel.text = [NSString stringWithFormat:@"Raw Data:%d",self.device.sensorPortB.data];break;case 2:cell.portLabel.text = @"PORTC";cell.nameLabel.text = self.device.sensorPortC.typeString;cell.imageView.image = self.device.sensorPortC.image;cell.dataLabel.text = [NSString stringWithFormat:@"Raw Data:%d",self.device.sensorPortC.data];break;case 3:cell.portLabel.text = @"PORTD";cell.nameLabel.text = self.device.sensorPortD.typeString;cell.imageView.image = self.device.sensorPortD.image;cell.dataLabel.text = [NSString stringWithFormat:@"Raw Data:%d",self.device.sensorPortD.data];break;case 4:cell.portLabel.text = @"PORT1";cell.nameLabel.text = self.device.sensorPort1.typeString;cell.imageView.image = self.device.sensorPort1.image;cell.dataLabel.text = [NSString stringWithFormat:@"Raw Data:%d",self.device.sensorPort1.data];break;case 5:cell.portLabel.text = @"PORT2";cell.nameLabel.text = self.device.sensorPort2.typeString;cell.imageView.image = self.device.sensorPort2.image;cell.dataLabel.text = [NSString stringWithFormat:@"Raw Data:%d",self.device.sensorPort2.data];break;case 6:cell.portLabel.text = @"PORT3";cell.nameLabel.text = self.device.sensorPort3.typeString;cell.imageView.image = self.device.sensorPort3.image;cell.dataLabel.text = [NSString stringWithFormat:@"Raw Data:%d",self.device.sensorPort3.data];break;case 7:cell.portLabel.text = @"PORT4";cell.nameLabel.text = self.device.sensorPort4.typeString;cell.imageView.image = self.device.sensorPort4.image;cell.dataLabel.text = [NSString stringWithFormat:@"Raw Data:%d",self.device.sensorPort4.data];break;default:break;}} else {cell.textLabel.text = @"Waiting For Connection Of EV3!";cell.textLabel.textAlignment = NSTextAlignmentCenter;}return cell;}

一切OK!

解释一下:

1)self.device = self.ev3WifiManager.devices.allValues.lastObject;

看看花儿冲破北疆漫漫寒冬,妖娆绽放;

【iOS与EV3混合机器人编程系列之三】编写EV3 Port Viewer 应用监

相关文章:

你感兴趣的文章:

标签云: