]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - sensortag-ios-source-code-example/sensortag-ios-source-code-example.git/blob - SensorTag_iOS/SensorTag2-Example/sensorTagLightService.m
V1 Release
[sensortag-ios-source-code-example/sensortag-ios-source-code-example.git] / SensorTag_iOS / SensorTag2-Example / sensorTagLightService.m
1 /*!
2  *  \author         Ole A. Torvmark <o.a.torvmark@ti.com , torvmark@stalliance.no>
3  *  \brief          SensorTag Light Service handler for SensorTag2-Example iOS application
4  *  \copyright      Copyright (c) 2015 Texas Instruments Incorporated
5  *  \file           sensorTagLightService.m
6  */
8 /*
9  * Copyright (c) 2015 Texas Instruments Incorporated
10  *
11  * All rights reserved not granted herein.
12  * Limited License.
13  *
14  * Texas Instruments Incorporated grants a world-wide, royalty-free,
15  * non-exclusive license under copyrights and patents it now or hereafter
16  * owns or controls to make, have made, use, import, offer to sell and sell ("Utilize")
17  * this software subject to the terms herein.  With respect to the foregoing patent
18  *license, such license is granted  solely to the extent that any such patent is necessary
19  * to Utilize the software alone.  The patent license shall not apply to any combinations which
20  * include this software, other than combinations with devices manufactured by or for TI (“TI Devices”).
21  * No hardware patent is licensed hereunder.
22  *
23  * Redistributions must preserve existing copyright notices and reproduce this license (including the
24  * above copyright notice and the disclaimer and (if applicable) source code license limitations below)
25  * in the documentation and/or other materials provided with the distribution
26  *
27  * Redistribution and use in binary form, without modification, are permitted provided that the following
28  * conditions are met:
29  *
30  *   * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any
31  *     software provided in binary form.
32  *   * any redistribution and use are licensed by TI for use only with TI Devices.
33  *   * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code.
34  *
35  * If software source code is provided to you, modification and redistribution of the source code are permitted
36  * provided that the following conditions are met:
37  *
38  *   * any redistribution and use of the source code, including any resulting derivative works, are licensed by
39  *     TI for use only with TI Devices.
40  *   * any redistribution and use of any object code compiled from the source code and any resulting derivative
41  *     works, are licensed by TI for use only with TI Devices.
42  *
43  * Neither the name of Texas Instruments Incorporated nor the names of its suppliers may be used to endorse or
44  * promote products derived from this software without specific prior written permission.
45  *
46  * DISCLAIMER.
47  *
48  * THIS SOFTWARE IS PROVIDED BY TI AND TI’S LICENSORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
49  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL TI AND TI’S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
52  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54  * POSSIBILITY OF SUCH DAMAGE.
55  */
58 #import "sensorTagLightService.h"
59 #import "sensorFunctions.h"
60 #import "masterUUIDList.h"
61 #import "masterMQTTResourceList.h"
62 #import "math.h"
64 @implementation sensorTagLightService
66 +(BOOL) isCorrectService:(CBService *)service {
67     if ([service.UUID.UUIDString isEqualToString:TI_SENSORTAG_TWO_LIGHT_SENSOR_SERVICE]) {
68         return YES;
69     }
70     return NO;
71 }
74 -(instancetype) initWithService:(CBService *)service {
75     self = [super initWithService:service];
76     if (self) {
77         self.btHandle = [bluetoothHandler sharedInstance];
78         self.service = service;
79         
80         for (CBCharacteristic *c in service.characteristics) {
81             if ([c.UUID.UUIDString isEqualToString:TI_SENSORTAG_TWO_LIGHT_SENSOR_CONFIG]) {
82                 self.config = c;
83             }
84             else if ([c.UUID.UUIDString isEqualToString:TI_SENSORTAG_TWO_LIGHT_SENSOR_DATA]) {
85                 self.data = c;
86             }
87             else if ([c.UUID.UUIDString isEqualToString:TI_SENSORTAG_TWO_LIGHT_SENSOR_PERIOD]) {
88                 self.period = c;
89             }
90         }
91         if (!(self.config && self.data && self.period)) {
92             NSLog(@"Some characteristics are missing from this service, might not work correctly !");
93         }
94         
95         self.tile.origin = CGPointMake(4, 3);
96         self.tile.size = CGSizeMake(4, 2);
97         self.tile.title.text = @"Light Level (OPT3001)";
98     }
99     return self;
104 -(BOOL) dataUpdate:(CBCharacteristic *)c {
105     if ([self.data isEqual:c]) {
106         NSLog(@"sensorTagLightService: Recieved value : %@",c.value);
107         oneValueCell *tile = (oneValueCell *)self.tile;
108         tile.value.text = [NSString stringWithFormat:@"%@",[self calcValue:c.value]];
109         return YES;
110     }
111     return NO;
114 -(NSArray *) getCloudData {
115     NSArray *ar = [[NSArray alloc]initWithObjects:
116           [NSDictionary dictionaryWithObjectsAndKeys:
117            //Value 1
118            [NSString stringWithFormat:@"%0.1f",self.lightLevel],@"value",
119            //Name 1
120            MQTT_RESOURCE_NAME_LIGHT_LEVEL,@"name", nil], nil];
121     return ar;
124 -(NSString *) calcValue:(NSData *) value {
125     unsigned char tmp[value.length];
126     [value getBytes:tmp length:value.length];
127     uint16_t dat;
128     dat = ((uint16_t)tmp[1] & 0xFF) << 8;
129     dat |= (uint16_t)(tmp[0] & 0xFF);
130     
131     self.lightLevel = (float)[sensorTagLightService sfloatExp2ToDouble:dat];
132     
133     
134     
135     return [NSString stringWithFormat:@"Amb: %0.1f Lux",(float)self.lightLevel];
138 +(double) sfloatExp2ToDouble:(uint16_t) sfloat {
139     uint16_t mantissa;
140     uint8_t exponent;
141     
142     mantissa = sfloat & 0x0FFF;
143     exponent = sfloat >> 12;
144 #ifdef SIGNED
145     if (exponent >= 0x0008) {
146         exponent = -((0x000F + 1) - exponent);
147     }
148 #endif
149 #ifdef SIGNED
150     if (mantissa >= 0x0800) {
151         mantissa = -((0x0FFF + 1) - mantissa);
152     }
153 #endif
154     double output;
155     double magnitude = pow(2.0f, exponent);
156     output = (mantissa * magnitude);
157     
158     return output / 100.0f;
162 @end