best practice to record sub-second data to fit file

I'd like to record rr-intervals (raw data for later HRV calculation) to a fit file from my datafield. This data obviously when the heart rate is above 60 bpm will be sub-second, meaning there will be more than one datapoint per second.

So I was thinking, what's the best practice to do this? I guess that technically I'll have something like:

mRRIntervalField = dataField.createField("rr_int", 13, Fit.DATA_TYPE_UINT32, {:count=> 5, mesgType=>Fit.MESG_TYPE_RECORD});

And I'll collect the data to an rrBuffer as Array<Number> and send it "every second" and "empty it"

But when do I "empty" it? I mean there are numerous independent cycles in the app:

1. every second DataField.compute(info as Activity.Info) is called

2. my ANT+ chest sensor broadcast (calls onMessage(msg as Ant.Message)) (depending on the channel settings, but for this question let's assume 4 times a second)

3. I call mRRIntervalField.setData(rrBuffer);

4. FitContributor writes out the data sent by the last call of setData

5. I need to empty rrBuffer - ideally right after FitContributor wrote out the previous value sent via setData

But how do I know when to empty the rrBuffer? I need to know when the previously set data was written out, and then empty it, so the next datapoint will become the 1st value in the array. Ideally there should be a callback for onDataWritten() or something like that, but there isn't Disappointed

Possible problems I see:

Let's say I add the next value to rrBuffer, then call setData and then every second (every 4th time I called setData) I empty rrBuffer. Then these things would happen:

a) if I empty rrBuffer too early then I'll lose some datapoints from the fit file

b) if I empty rrBuffer "too late" then I'll lose some datapoints (because it's equivalent to empty it too early for the next write cycle)

So basically there's 3/4 chance to do it wrong.

  • This is a related topic I had with Garmin with no luck with them to add to CIQ. There is a Message Type called 'HRV', which is an array of rr intervals.  It is not supported in CIQ, however it is a ANT+ Message Type.    You can see this functionality by setting Enable HRV in supported watches.  This will post the HRV Message type as an array 16bit ints every second to the fit file.  3rd party apps as in Kubios uses this message in their HRV software.  This HRV message type is updated a once a second with an array of rr interval in milliseconds.      

    Once a second, I would think you would copy rrBuffer to temporary buffer, clear the original rrBuffer, then write your temp rrBuffer to the FIT.