Page 1 of 1

Weird Values from BT LE

PostPosted: July 30th, 2015, 7:34 am
by JamesRocheUK
I am getting weird number from my PM5 via Bluetooth, i am getting an update on 0x0031 but the duration never goes up, the elapsed time and distance are very odd also. Sample below.

Elapsed Time lo:234
Elapsed Time Mid:123
Elapsed Time High:0

Distance lo: 44
distance Mid: 23
Distance High: 0

Duration low: 0
Duration Mid: 0
duration High: 0

While the PM5 screen shows the following
Time 5:17
Distance 593

Everything else looks to be coming through correctly... Am i missing the point here? Should for example Elapsed time high/mid/low be hours:minutes:seconds? What do the values coming from tend vice actually mean? do they look correct?

Much appreciated!

Weird Values from PM5 over BT:LE

PostPosted: July 30th, 2015, 7:50 am
by JamesRocheUK
I am getting strange values from Bluetooth LE when reading 0x0031, The packets gives me this for Time Elapsed

Lo: 234
Mid 123
high: 0

When my PM5 screen shows duration of 5:17

What do the Low, Med, High value represent? Do i need to :normalise" them in some way?

Re: Weird Values from BT LE

PostPosted: July 31st, 2015, 12:50 pm
by Piboo
Hi,

You receive a byte array from PM5 so Lo, Mid and Hi corresponds to a byte hence 8 bits.

So you must rebuild your elapsed Time value like this:
elapseTime = Hi * 65536 + Mid * 256 + Lo

The results is in 0.01s

You can find some good explanation about byte / bits conversion.

Regards

Re: Weird Values from BT LE

PostPosted: August 5th, 2015, 11:46 am
by horseshoe7
You can also read the 3 bytes into a uint32, where

Byte 4 3 2 1
-- hi mid lo

In Objective-C, this was done by:

Code: Select all
NSData *data = ... // the bytes coming from the characteristic
uint32_t time = 0;  // initialize to zero so that all bits of a unit32_t are initialized to 0
[data getBytes:&time range:NSMakeRange(0, 3)];  // 3 because the spec says 3 bytes for elapsed time
NSTimeInterval elapsed = (NSTimeInterval)time * 0.01;  // 0.01 because the spec says this uint32_t value has a unit of 0.01s

Re: Weird Values from BT LE

PostPosted: August 9th, 2015, 12:13 pm
by wivku
The Hi * 65536 + Mid * 256 + Lo is fine, but specific for hi-mid-lo. In another situation it could be you have 2 bytes (LSB,MSB).

Similar to horseshoe7's answer, in Swift:

Code: Select all
var testdata = NSData(bytes: [234,123,0, 33,11,0] as [UInt8], length: 6) // example: first 6 bytes from 0x0031

var elapsed: Int = 0
testdata.getBytes(&elapsed, range: NSMakeRange(0, 3)) // elapsed time starts at byte 0 and is 3 bytes long

print( Double(elapsed) * 0.01 ) // documentation states it is in 0.01 seconds, so multiply by 0.01 to get seconds
// = 317.22 seconds = 5:17


Or a more generic solution (this time in JavaScript)

Code: Select all
function bytesToNumber(bytes) {
   var total=0;
   for (var i=0; i< bytes.length; i++) {
      total += (bytes[i] * Math.pow(256, i));
   }
   return total;
}

var elapsed = bytesToNumber( [234,123,0] );
console.log( elapsed * 0.01 ); // 317.22