背景:
科技在不断进步,越来越多的智能家居产品和服务进入到人们的日常生活中。智能灯是一款常见的智能设施,装置智能灯后,用户能够应用手机 App 轻松调整室内色彩和亮度,设置不同的照明场景来轻松发明出和煦、放松、乏味或者鼓舞人心的室内空间。
Android版请参考:《如何基于SDK疾速开发一款IoT App管制智能灯泡(Android版)》
RGB 5 路智能灯介绍
蕴含性能:
五路灯有白光和彩光是 2 种模式切换,不可同时启用。
RGB 色调模型
RGB(Red、Green、Blue)色调模式是一种面向硬件的色调模型,显示零碎都采纳RGB色彩模型来进行图像显示。RGB色调模型属于加法混色原理,每种原色的数值越高,色调就越亮,RGB都是0时是彩色,都是255时是红色。
RGB色调模型表示间接,然而RGB数值和色调的三属性没有间接的分割,不能揭示色调之间的关系。
HSV 色调模型
HSV 色调模式是一种面向用户感官的色调模型,偏重与色调示意。这是依据人的视觉系统对亮度的敏感度要强于色调值这个生理个性而提出的色彩模型。它比 RGB 更靠近人们对黑白的感知教训。十分直观地表白色彩的色调、娇艳水平和明暗水平,不便进行色彩的比照。
HSV(Hue、Saturation、Value)别离对应色相、饱和度、明度。
- 色相 H:用角度度量,取值范畴为0°~360°,从红色开始依照逆时针方向计算,红色为0°,绿色为120°,蓝色为240°,黄色为60°,青色为180°,品红为300°,对应于绕圆柱的核心轴的角度。
- 饱和度 S:示意色调的纯度,对应于离圆柱的核心轴的间隔。数值越高色彩则深而艳,数值越低则色彩逐步变灰,取值范畴为0.0~1.0,红色的S=0。
- 明度 V:示意色彩的亮堂水平。取值范畴为0.0(彩色)~1.0(红色)。对应于绕圆柱的核心轴的高度,轴取值为自底部的彩色V=0到顶部的红色V=1。
根底筹备
基于 SDK 开发一个智能灯 App 的时候,须要做上面筹备:
- 创立 App SDK
- 集成 SDK
- 注册和登录
- 创立家庭
- 设施配网
- 设施管制
具体参考
性能点介绍
性能点是对产品性能的形象示意,是具体智能设施性能的形象,用于形容产品性能及其参数。
- 性能点 ID:性能点的编码。设施与云端的性能数据通过性能点 ID 进行传输。
- 性能点名称:自定义的性能名称。
- 标识名:性能点 Code 值,用于 App 显示性能名称的多语言治理。反对字母、数字和下划线,以字母结尾。
- 数据类型:
数据传输类型:
- 可下发可上报:指令数据能够发送给设施,设施数据能够传输给云端。
- 只上报:数据只反对从设施传输给云端。
- 只下发:数据只反对从云端发送给设施。
产品 pid : "ylr9R01cMWnMRqEB" 的性能点如下图所示:
Demo 介绍
Demo 地址
demo 地址:https://github.com/tuya/tuya-home-ios-sdk 性能分支:main
SDK 文档地址:https://tuyainc.github.io/tuyasmart_home_ios_sdk_doc/zh-hans/
Demo 下载
在终端的命令行中输出:
git clone https:_//github.com/tuya/tuya-home-ios-sdk.git_
下载 demo 到本地。
环境配置
1、关上我的项目设置,Target => General,批改 Bundle Identifier
为涂鸦开发者平台对应的 iOS 包名
2、导入平安图片到工程根目录,重命名为 t_s.bmp
,并退出「我的项目设置 => Target => Build Phases => Copy Bundle Resources」中。
3、在我的项目的PrefixHeader.pch
文件增加以下内容:
#import <TuyaSmartHomeKit/TuyaSmartKit.h>
4、关上AppDelegate.m
文件,在[AppDelegate application:didFinishLaunchingWithOptions:]
办法中初始化SDK:
[[TuyaSmartSDK sharedInstance] startWithAppKey:<#your_app_key#> secretKey:<#your_secret_key#>];
5、关上 Debug 模式
在开发的过程中能够开启 Debug 模式,打印一些日志用于剖析问题。
Objc:
#ifdef DEBUG [[TuyaSmartSDK sharedInstance] setDebugMode:YES];#else#endif
Demo 内容
大家在开发的过程中能够参考的 demo 的代码。
在 demo 中咱们曾经实现了基于 dp 点管制实现智能灯泡的开关,亮度,冷暖,色彩管制。
智能灯管制页面的如果下图所示:
性能点 ID
NSString * const kLightSwtichDpId = @"1";/* 管制灯开关的 dp 点 */NSString * const kLightColorTypeDpId = @"2";/* 管制灯类型的 dp 点 */NSString * const kLightColorBrightDpId = @"3";/* 管制彩灯亮度的 dp 点 */NSString * const kLightColorTempDpId = @"4";/* 管制彩灯 temp 的 dp 点 */NSString * const kLightColorDpId = @"5";/* 管制灯色调的 dp 点 */
开关操作
- (void)switchAction:(UISwitch *)sender { // 开关操作 WEAKSELF_AT [TPDemoProgressUtils showMessag:TYSDKDemoLocalizedString(@"loading", @"") toView:self.view]; [self.device publishDps:@{kLightSwtichDpId:@(sender.isOn)} success:^{ [TPDemoProgressUtils hideHUDForView:weakSelf_AT.view animated:NO]; } failure:^(NSError *error) { [TPDemoProgressUtils hideHUDForView:weakSelf_AT.view animated:NO]; [TPDemoProgressUtils showError:error.localizedDescription]; }];}
亮度调节(白光和彩光模式下都反对)
分两种状况:在白光模式和彩光模式两种状况
// BrightNSDictionary *dps = self.device.deviceModel.dps;if ([[dps objectForKey:kLightColorTypeDpId] isEqualToString:@"colour"]) { int ir,ig,ib; _hsvValue.v = value; HSVToRGB(_hsvValue.h, _hsvValue.s, _hsvValue.v, &ir, &ig, &ib); UIColor *resColor = RGBCOLOR(ir, ig, ib); NSString *dpsString = [NSString stringWithFormat:@"%@%@", [[[resColor hexStringFromColor] lowercaseString] substringFromIndex:1], [self getHexStringFromHSV:_hsvValue]]; publishDps = @{ kLightSwtichDpId:@(YES), kLightColorDpId:dpsString, kLightColorTypeDpId:@"colour" };} else { CGFloat tempV = (value * 100 - 1)/(100.0-1.0) * (self.maxValue - self.minValue) + self.minValue; int val = [self round:tempV]; publishDps = @{ kLightSwtichDpId:@(YES), kLightColorBrightDpId:@(val), kLightColorTypeDpId:@"white", };}[self.device publishDps:publishDps success:^{ } failure:^(NSError *error) {}];
冷暖调节(只有白光模式反对)
// Temperatureint dpsInt = (int)(value * (self.tempMaxValue - self.tempMinValue) + self.tempMinValue);publishDps = @{ kLightColorTypeDpId:@"white", kLightColorTempDpId:@(dpsInt)};[self.device publishDps:publishDps success:^{ } failure:^(NSError *error) {}];
色彩调节(只有彩光模式反对)
#pragma mark - RSColorPickerViewDelegate- (void)colorPickerDidChangeSelection:(RSColorPickerView *)colorPicker { NSDictionary *dps = self.device.deviceModel.dps; BOOL isSwitch = [[dps objectForKey:kLightSwtichDpId] tysdk_toBool]; if (!isSwitch) { return; } HSVType hsv = [self.class getHSVFromUIColor:colorPicker.selectionColor]; _hsvValue.h = hsv.h; _hsvValue.s = hsv.s; _currentH = hsv.h; if (_hsvValue.v == 0) { _hsvValue.v = 1; } int r, g, b; HSVToRGB(_hsvValue.h, _hsvValue.s, _hsvValue.v, &r, &g, &b); NSString *dpsString = [NSString stringWithFormat:@"%02x%02x%02x%@", (unsigned int)r, (unsigned int)g, (unsigned int)b, [self getHexStringFromHSV:_hsvValue]]; NSDictionary *publishDps = @{ kLightColorDpId:dpsString, kLightColorTypeDpId:@"colour", }; [self.device publishDps:publishDps success:^{ } failure:^(NSError *error) { }];}
饱和度调节(只有彩光模式反对)
// Saturation_hsvValue.s = value;if (_hsvValue.v == 0) { _hsvValue.v = 1;}int r, g, b;HSVToRGB(_hsvValue.h, _hsvValue.s, _hsvValue.v, &r, &g, &b);NSString *dpsString = [NSString stringWithFormat:@"%02x%02x%02x%@", (unsigned int)r, (unsigned int)g, (unsigned int)b, [self getHexStringFromHSV:_hsvValue]];publishDps = @{ kLightColorDpId:dpsString, kLightColorTypeDpId:@"colour", };[self.device publishDps:publishDps success:^{ } failure:^(NSError *error) {}];
智能灯状态变动回调刷新
#pragma mark - TuyaSmartDeviceDelegate/// dp数据更新- (void)device:(TuyaSmartDevice *)device dpsUpdate:(NSDictionary *)dps { [self reloadData];}- (void)reloadData { _hsvValue = [self getHSVFromDpId:kLightColorDpId]; NSDictionary *dps = self.device.deviceModel.dps; BOOL isSwitch = [[dps objectForKey:kLightSwtichDpId] boolValue]; [self.switchButton setOn:isSwitch]; double brightnessValue = [self getBrightness:dps]; [self.brightSliderView setSliderValue:brightnessValue]; double tempValue = [self getTempValue:[[dps objectForKey:kLightColorTempDpId] doubleValue]]; [self.tempSliderView setSliderValue:tempValue]; HSVType hsv = [self getHSVFromDpId:kLightColorDpId]; UIColor *color = [self getColorFromDpId:kLightColorDpId]; NSLog(@" h : %f, s : %f, v : %f, color : %@", hsv.h, hsv.s, hsv.v, color); if (fabs(roundf(hsv.h) - roundf(_currentH)) > 1) { _currentH = hsv.h; self.colorPicker.selectionColor = color; self.colorPicker.brightness = 1; self.colorPicker.opaque = YES; } [self.saturationSliderView setSliderValue:hsv.s];}