<?xml version="1.0" encoding="UTF-8"?>
<phyphox version="1.7">
    <title>Logger HR+RR for Polar H9+H10</title>
    <category>Bluetooth</category>
    <color>3366ff</color>

    <description>
        Reads data from Polar H9 or H10 (Bluetooth Heart Rate Service)
	Probably it also works with other BLE HR sensors that provide RR-intervals.
	Data: Heartrate, RR-intervals (RR1 .. RR4)
		Packages of 4 RR intervals are accepted, works up to an HR of 240 BPM
	Reads status of battery as percentage
	Shows values on screen:
		Timer Unit seconds
		HR (unit BPM)
		RR1 .. RR4 (unit ms)
		Flags (status of HR sensor - 16 means that valid RR packages are sent)
		Battery status
        Graph:
        	HR as function of time
		RR1 as function of time
		Note 1: The graph will sometimes show points with RR-intervals of zero. This happens
		  when the HR is below 60 because not every second an RR-interval is transmitted.
		Note 2: If you want, you can add also the RR2..RR4 intervals, but that will make
		  the graph more messy.
        Export:
        	t, HR, RR1, RR2, RR3, RR4, Flags, Battery
	Written by Peter Lambooy
    </description>

    <data-containers>
        <container size="0">t</container>
        <container size="0">flags</container>
        <container size="0">hr</container>
        <container size="0">rr1_raw</container>
        <container size="0">rr2_raw</container>
        <container size="0">rr3_raw</container>
        <container size="0">rr4_raw</container>
        <container size="0">rr1</container>
        <container size="0">rr2</container>
        <container size="0">rr3</container>
        <container size="0">rr4</container>
        <container size="0"> count_t</container>
        <container size="0"> count_rr1</container>
        <container size="0"> count_rr2</container>
        <container size="0"> count_rr3</container>
        <container size="0"> count_rr4</container>
        <container size="0"> difference</container>
        <container size="0"> battery</container>
    </data-containers>

    <input>
            <bluetooth uuid="0000180d-0000-1000-8000-00805f9b34fb" mode="notification">

            <!-- Time -->
            <output
                char="00002A37-0000-1000-8000-00805F9B34FB"
                extra="time">t</output>
            <!-- flags (UInt8) an Offset 0 -->
            <output
                char="00002A37-0000-1000-8000-00805F9B34FB"
                conversion="uInt8"
                offset="0"
                length="1">flags</output>
            <!-- HR (UInt8) an Offset 1 -->
            <output
                char="00002A37-0000-1000-8000-00805F9B34FB"
                conversion="uInt8"
                offset="1"
                length="1">hr</output>
            <!-- RR1 (UInt16 LE) ab Offset 2 -->
            <output
                char="00002A37-0000-1000-8000-00805F9B34FB"
                conversion="uInt16LittleEndian"
                offset="2"
                length="2">rr1_raw</output>
            <!-- RR2 (UInt16 LE) ab Offset 4 -->
            <output
                char="00002A37-0000-1000-8000-00805F9B34FB"
                conversion="uInt16LittleEndian"
                offset="4"
                length="2">rr2_raw</output>
            <!-- RR3 (UInt16 LE) ab Offset 4 -->
            <output
                char="00002A37-0000-1000-8000-00805F9B34FB"
                conversion="uInt16LittleEndian"
                offset="6"
                length="2">rr3_raw</output>
            <!-- RR4 (UInt16 LE) ab Offset 4 -->
            <output
                char="00002A37-0000-1000-8000-00805F9B34FB"
                conversion="uInt16LittleEndian"
                offset="8"
                length="2">rr4_raw</output>
            <output
                char="00002A19-0000-1000-8000-00805F9B34FB"
                conversion="uInt8"
                offset="0"
                length="1">battery</output>

        </bluetooth>
    </input>

    <analysis sleep="0.5">

      <count>
        <input keep="true">t</input>
        <output append="true">count_t</output>
      </count>
      <count>
        <input keep="true">rr1_raw</input>
        <output append="true">count_rr1</output>
      </count>
      <subtract>
        <input keep="true">count_t</input>
        <input keep="true">count_rr1</input>
        <output>difference</output>
      </subtract>
      <const>
        <input as="value" type="value">0</input>
        <input as="length" keep="true">difference</input>
        <output append="true">rr1_raw</output>
      </const>

      <count>
        <input keep="true">rr2_raw</input>
        <output>count_rr2</output>
      </count>
      <subtract>
        <input keep="true">count_t</input>
        <input>count_rr2</input>
        <output>difference</output>
      </subtract>
      <const>
        <input as="value" type="value">0</input>
        <input as="length">difference</input>
        <output append="true">rr2_raw</output>
      </const>

      <count>
        <input keep="true">rr3_raw</input>
        <output>count_rr3</output>
      </count>
       <subtract>
        <input keep="true">count_t</input>
        <input>count_rr3</input>
        <output>difference</output>
      </subtract>
      <const>
        <input as="value" type="value">0</input>
        <input as="length">difference</input>
        <output append="true">rr3_raw</output>
      </const>

      <count>
        <input keep="true">rr4_raw</input>
        <output>count_rr4</output>
       </count>
      <subtract>
        <input keep="true">count_t</input>
        <input>count_rr4</input>
        <output>difference</output>
      </subtract>
      <const>
        <input as="value" type="value">0</input>
        <input as="length">difference</input>
        <output append="true">rr4_raw</output>
      </const>

      <formula formula="[1_]/1024*1000">
        <input keep="true">rr1_raw</input>
        <output>rr1</output>
      </formula>
      <formula formula="[1_]/1024*1000">
        <input keep="true">rr2_raw</input>
        <output>rr2</output>
      </formula>
      <formula formula="[1_]/1024*1000">
        <input keep="true">rr3_raw</input>
        <output>rr3</output>
      </formula>
        <formula formula="[1_]/1024*1000">
        <input keep="true">rr4_raw</input>
        <output>rr4</output>
      </formula>

    </analysis>

    <views>
        <view label="Live">
            <info label="Logger HR + RR-intervals" />

            <value label="Time" unit="s" precision="0" size="1.5">
                <input>t</input>
            </value>
            <value label="Heartrate" unit="bpm" precision="0" size="1.5">
                <input>hr</input>
            </value>

            <value label="RR1" unit="(ms)" precision="0">
                <input>rr1</input>
            </value>
            <value label="RR2" unit="(ms)" precision="0">
                <input>rr2</input>
            </value>
            <value label="RR3" unit="(ms)" precision="0">
                <input>rr3</input>
            </value>
            <value label="RR4" unit="(ms)" precision="0">
                <input>rr4</input>
            </value>

            <value label="Flags" precision="0">
                <input>flags</input>
            </value>
	    <value label="Battery" precision="0">
                <input>battery</input>
            </value>
 
            <separator height="0.3" />

            <graph
                label="HR"
                labelX="Time"
                unitX="s"
                labelY="BPM"
                unitY="(bpm)"
                timeOnX="true"
                partialUpdate="true">

                <input axis="x">t</input>
                <input axis="y">hr</input>
            </graph>

            <graph
                label="RR1"
                style="dots"
		lineWidth="2"
                labelX="Time"
                unitX="s"
                labelY="RR1"
                unitY="(ms)"
                timeOnX="true"
                partialUpdate="true">

                <input axis="x">t</input>
                <input axis="y">rr1</input>
            </graph>

        </view>
    </views>

    <export>
        <set name="Polar H9 HR + RR (ms)">
            <data name="Time (s)">t</data>
            <data name="Heart Rate (bpm)">hr</data>
            <data name="RR1 (ms)">rr1</data>
            <data name="RR2 (ms)">rr2</data>
            <data name="RR3 (ms)">rr3</data>
            <data name="RR4 (ms)">rr4</data>
            <data name="Flags">flags</data>
            <data name="battery">battery</data>
        </set>
    </export>

</phyphox>
