IOS Swift TI SensorTag2 data conversion -


i write app ios using ti sensortag2. right i'm stuck conversion of data read on bluetooth float app.

following code released source code of ti objective c app.

-(nsstring *) calcvalue:(nsdata *) value {  char vals[value.length]; [value getbytes:vals length:value.length];  point3d gyropoint;  gyropoint.x = ((float)((int16_t)((vals[0] & 0xff) | (((int16_t)vals[1] << 8) & 0xff00)))/ (float) 32768) * 255 * 1; gyropoint.y = ((float)((int16_t)((vals[2] & 0xff) | (((int16_t)vals[3] << 8) & 0xff00)))/ (float) 32768) * 255 * 1; gyropoint.z = ((float)((int16_t)((vals[4] & 0xff) | (((int16_t)vals[5] << 8) & 0xff00)))/ (float) 32768) * 255 * 1;  self.gyro = gyropoint;  point3d accpoint;  accpoint.x = (((float)((int16_t)((vals[6] & 0xff) | (((int16_t)vals[7] << 8) & 0xff00)))/ (float) 32768) * 8) * 1; accpoint.y = (((float)((int16_t)((vals[8] & 0xff) | (((int16_t)vals[9] << 8) & 0xff00))) / (float) 32768) * 8) * 1; accpoint.z = (((float)((int16_t)((vals[10] & 0xff) | (((int16_t)vals[11] << 8) & 0xff00)))/ (float) 32768) * 8) * 1;  self.acc = accpoint;  point3d magpoint; magpoint.x = (((float)((int16_t)((vals[12] & 0xff) | (((int16_t)vals[13] << 8) & 0xff00))) / (float) 32768) * 4912); magpoint.y = (((float)((int16_t)((vals[14] & 0xff) | (((int16_t)vals[15] << 8) & 0xff00))) / (float) 32768) * 4912); magpoint.z = (((float)((int16_t)((vals[16] & 0xff) | (((int16_t)vals[17] << 8) & 0xff00))) / (float) 32768) * 4912);   self.mag = magpoint;   return [nsstring stringwithformat:@"acc : x: %+6.1f, y: %+6.1f, z: %+6.1f\nmag : x: %+6.1f, y: %+6.1f, z: %+6.1f\ngyr : x: %+6.1f, y: %+6.1f, z: %+6.1f",self.acc.x,self.acc.y,self.acc.z,self.mag.x,self.mag.y,self.mag.z,self.gyro.x,self.gyro.y,self.gyro.z]; } 

when try convert code swift, error "integer literal '65280' overflows when stored int16

let xf: float = ((float((int16(bytes[6]) & 0xff) | ((int16(bytes[7]) << 8) & 0xff00)) / float(32768)) * 8) * 1 

as understand that, combines 2 int8 variables single int16 , should work, don't find made error. part "& 0xff00" marked , when understand right here first 8 bits contain 1's , rest 0's

i had working code android app sensortag2, code crashes app time time, when read data gyroscope, wanted use ios code

let x = (int16(bytes[7]) << 8) + int16(bytes[6]) let xf = float(x) / (32768.0 / 8.0) 

maybe here can point me in right direction solve problem

one bad thing in line:

let xf: float = ((float((int16(bytes[6]) & 0xff) | ((int16(bytes[7]) << 8) & 0xff00)) / float(32768)) * 8) * 1 

is this: ((int16(bytes[7]) << 8) & 0xff00).

you know int16 can represent numbers -32768...32767, , value of 0xff00 65280. error message saying, large int16.

(remember swift no implicit conversions numeric types.)

with making bytes unsigned:

let bytes = unsafepointer<uint8>(data.bytes) 

you have no need use &.

but need pass signed value float.init, code needs this:

let xf: float = ((float(int16(bitpattern: uint16(bytes[6]) | (uint16(bytes[7]) << 8))) / float(32768)) * 8) * 1 

(or else, negative values in bytes make app crash.)


Comments