<phyphox version="1.7">
    <title>HID Mouse: Illuminance vs. distance (SensorTag)</title>
    <category>Bluetooth</category>
    <description>
        Measure illuminance (SensorTag) as a function of the distance tracked by a mouse.
    </description>
    <data-containers>
        <!-- mouse -->
        <container size="1">last</container>
        <container size="0">tempAll</container>
        <container size="0">-tempAll</container>
        <container size="0">new</container>
        <container size="1">count</container>
        <container size="1000">dx</container>
        <container size="1000">dy</container>
        <container size="0">t</container>
        <container size="0" init="0">x</container>
        <container size="0" init="0">y</container>

        <container size="1">x2</container>
        <container size="1">y2</container>
        <container size="1">d2</container>
        <container size="1">d</container>

        <container size="0">dHist</container>
        
        <!-- sensor tag -->
        <container size="0" static="false">RawInt</container>
        <container size="0" static="false">e</container>
        <container size="0" static="false">m</container>
        <container size="0" static="false">LUX</container> <!-- size 0 for time -->
        <container size="1" static="false">countI</container>
        <container size="1" static="false">tmax</container>
        <container size="0" static="false">time</container>
        <container size="0" static="false">factor</container>
        
        <container size="1">illum</container>
        <container size="0">illumHist</container>
        
        <!-- log-log -->
        <container size="1">illum_log</container>
        <container size="1">d_log</container>
        <container size="0">dHist_log</container>
        <container size="0">illumHist_log</container>
        
    </data-containers>
    <input>
        <!-- mouse -->
        <bluetooth uuid="00001812-0000-1000-8000-00805f9b34fb" mode="notification">
            <config char="00002A4e-0000-1000-8000-00805F9B34FB"  conversion="hexadecimal">00</config>
            <output char="00002A33-0000-1000-8000-00805F9B34FB" offset="1" length="1" conversion="int8">dx</output>
            <output char="00002A33-0000-1000-8000-00805F9B34FB" offset="2" length="1" conversion="int8">dy</output>
             <output char="00002A33-0000-1000-8000-00805F9B34FB" extra="time">t</output>
        </bluetooth>
        <!-- sensor tag -->
        <bluetooth name="SensorTag"  mode="notification">
            <config char="F000AA73-0451-4000-B000-000000000000" conversion="hexadecimal">50</config>
            <config char="F000AA72-0451-4000-B000-000000000000" conversion="hexadecimal">01</config>
            <output char="F000AA71-0451-4000-B000-000000000000" conversion="uInt16LittleEndian" offset="0" length="2">RawInt</output>
        </bluetooth>
    </input>
    <views>
        <!-- illum - distance -->
        <view label="Illuminance (distance)">
            <graph label="Illuminance" labelX="d" unitX="a.u." labelY="Ev" unitY="lx">
                <input axis="y">illumHist</input>
                <input axis="x">dHist</input>
            </graph>
            <info label="Avoid rotating the mouse! Rotations cannot be recognized and it is impossible to calculate the total distance when axes change due to the rotation." />
            <info label="A new data point is generated every 800ms by the sensortag. The refresh rate of the mouse is considerably higher. This leads to multiple assignments of different distance values to one and the same illuminance value. A sleep rate of 800ms can synchronize SensorTag and mouse. However, data may be lost in this case." />
        </view>
      <!-- log-log -->
        <view label="log-log">
            <graph label="log-log" labelX="log(d)" unitX="a.u." labelY="log(Ev/lx)" unitY="a.u." precision="2">
                <input axis="y">illumHist_log</input>
                <input axis="x">dHist_log</input>
            </graph>
            <separator height="1"/>
            <info label="Avoid rotating the mouse! Rotations cannot be recognized and it is impossible to calculate the total distance when axes change due to the rotation." />
        </view>
        <!-- illum-time, distance-time -->
        <view label="Illuminance, distance (time)">
            <graph label="Illuminance" labelX="t" unitX="s" labelY="Ev" unitY="lx" precision="2">
                    <input axis="y">LUX</input>
                    <input axis="x">time</input>
            </graph>
            <graph label="Distance-time" labelX="y" unitX="a.u." labelY="d" unitY="(a.u.)" precision="2">
                <input axis="y">y</input>
                <input axis="x">t</input>
            </graph>
                <info label="Avoid rotating the mouse! Rotations cannot be recognized and it is impossible to calculate the total distance when axes change due to the rotation." />
        </view>
        <view label="Mouse">
            <graph label="Integrated Movement" labelX="x" unitX="a.u." labelY="y" unitY="a.u." aspectRatio="1">
                <input axis="y">y</input>
                <input axis="x">x</input>
            </graph>
        </view>
    </views>
        <!-- to link data mouse-light: sleep="0.8" because cc2650 provides optical sensor data each 800 milliseconds. You can use no-sleep, then you might have multiple distance values for each illuminance value or you could use interpolation for the missing illuminance data -->
    <analysis optimization="true" sleep="0.8">
        <append>
            <input clear="false">x</input>
            <output>last</output>
        </append>
        <integrate>
            <input>dx</input>
            <output>tempAll</output>
        </integrate>
        <count>
            <input clear="false">tempAll</input>
            <output>count</output>
        </count>
        <add>
            <input>tempAll</input>
            <input>last</input>
            <output>new</output>
        </add>
        <if greater="true">
            <input>count</input>
            <input type="value">0</input>
            <input>new</input>
            <output clear="false">x</output>
        </if>

        <append>
            <input clear="false">y</input>
            <output>last</output>
        </append>
        <integrate>
            <input>dy</input>
            <output>tempAll</output>
        </integrate>
        <multiply>
            <input clear="false">tempAll</input>
            <input type="value">-1</input>
            <output>-tempAll</output>
        </multiply>
        <count>
            <input clear="false">tempAll</input>
            <output>count</output>
        </count>
        <add>
            <input>-tempAll</input>
            <input>last</input>
            <output>new</output>
        </add>
        <if greater="true">
            <input>count</input>
            <input type="value">0</input>
            <input>new</input>
            <output clear="false">y</output>
        </if>

        <multiply>
            <input clear="false">x</input>
            <input clear="false">x</input>
            <output>x2</output>
        </multiply>
        <multiply>
            <input clear="false">y</input>
            <input clear="false">y</input>
            <output>y2</output>
        </multiply>
        <add>
            <input clear="false">x2</input>
            <input clear="false">y2</input>
            <output>d2</output>
        </add>
        <power>
            <input clear="false" as="base">d2</input>
            <input type="value" as="exponent">0.5</input>
            <output>d</output>
        </power>
        
        <count>
            <input clear="false">illum</input>
            <output>count</output>
        </count>
        <if greater="true">
            <input clear="false">count</input>
            <input type="value">0</input>
            <input clear="false">illum</input>
            <output clear="false">illumHist</output>
        </if>
        <if greater="true">
            <input clear="false">count</input>
            <input type="value">0</input>
            <input clear="false">d</input>
            <output clear="false">dHist</output>
        </if>
        
        <!-- log-log -->
        <formula formula="log([1])">
            <input>d</input>
            <output clear="false">d_log</output>
        </formula>
        <formula formula="log([1])">
            <input>illum</input>
            <output clear="false">illum_log</output>
        </formula>
        <if greater="true">
            <input clear="false">count</input>
            <input type="value">0</input>
            <input>illum_log</input>
            <output clear="false">illumHist_log</output>
        </if>
        <if greater="true">
            <input>count</input>
            <input type="value">0</input>
            <input>d_log</input>
            <output clear="false">dHist_log</output>
        </if>

    <!-- sensor tag -->
        <formula formula="floor([1_]/4096)">
            <input as="in" clear="false">RawInt</input>
            <output as="out">e</output>
        </formula>
        <formula formula="[2_]-[1_]*4096">
            <input as="in" clear="false">e</input>
            <input as="in" clear="false">RawInt</input>
            <output as="out">m</output>
        </formula>
        <power>
            <input as="base" type="value">2</input>
            <input as="exponent" clear="false">e</input>
            <output as="power">factor</output>
        </power>
        <formula formula="[2_]*(0.01*[1_])">
            <input as="in" clear="false">factor</input>
            <input as="in" clear="false">m</input>
            <output as="out">illum</output>
        </formula>
        <formula formula="[2_]*(0.01*[1_])">
            <input as="in" clear="false">factor</input>
            <input as="in" clear="false">m</input>
            <output as="out">LUX</output>
        </formula>
        <count>
            <input as="buffer" clear="false">RawInt</input>
            <output as="count">countI</output>
        </count>
        <formula formula="([1]-1)*0.8">
            <input as="in" clear="false">countI</input>
            <output as="out">tmax</output>
        </formula>
        <ramp>
            <input as="start" type="value">0</input>
            <input as="stop">tmax</input>
            <input as="length">countI</input>
            <output as="out">time</output>
        </ramp>

    </analysis>

    <export>
        <set name="Illuminance-Distance">
            <data name="d (a.u.)">dHist</data>
            <data name="Ev (lx)">illumHist</data>
            <data name="ln(d) (a.u.)">dHist_log</data>
            <data name="ln(Ev/lux)">illumHist_log</data> 
        </set>
    </export>

</phyphox>
