// Called by push devices (e.g. ANT+)
void RealtimeWindow::updateData(RealtimeData &rtData)
{
        displayPower = rtData.getWatts();
        displayCadence = rtData.getCadence();
        displayHeartRate = rtData.getHr();
        displaySpeed = rtData.getSpeed();
        displayLoad = rtData.getLoad();
    // Gradient not supported
        return;
}
void RealtimeWindow::guiUpdate()           // refreshes the telemetry
{
    RealtimeData rtData;

    // get latest telemetry from device (if it is a pull device e.g. Computrainer //
    if (status&RT_RUNNING && deviceController->doesPull() == true) {
        deviceController->getRealtimeData(rtData);
        displayPower = rtData.getWatts();
        displayCadence = rtData.getCadence();
        displayHeartRate = rtData.getHr();
        displaySpeed = rtData.getSpeed();
        displayLoad = rtData.getLoad();
    }

    // Distance assumes current speed for the last second. from km/h to km/sec
    displayDistance += displaySpeed / (5 * 3600); // XXX assumes 200ms refreshrate
    displayWorkoutDistance += displaySpeed / (5 * 3600); // XXX assumes 200ms refreshrate

    total_msecs = session_elapsed_msec + session_time.elapsed();
    lap_msecs = lap_elapsed_msec + lap_time.elapsed();

    // update those LCDs!
    timeLCD->display(QString("%1:%2:%3.%4").arg(total_msecs/3600000)
                                           .arg((total_msecs%3600000)/60000,2,10,QLatin1Char('0'))
                                           .arg((total_msecs%60000)/1000,2,10,QLatin1Char('0'))
                                           .arg((total_msecs%1000)/100));

    laptimeLCD->display(QString("%1:%2:%3.%4").arg(lap_msecs/3600000,2)
                                              .arg((lap_msecs%3600000)/60000,2,10,QLatin1Char('0'))
                                              .arg((lap_msecs%60000)/1000,2,10,QLatin1Char('0'))
                                              .arg((lap_msecs%1000)/100));

    // Cadence, HR and Power needs to be rounded to 0 decimal places
    powerLCD->display(round(displayPower));
    double val = round(displaySpeed * (useMetricUnits ? 1.0 : MILES_PER_KM) * 10.00)/10.00;
    speedLCD->display(QString::number(val, 'f', 1)); // always show 1 decimal point
    cadenceLCD->display(round(displayCadence));
    heartrateLCD->display(round(displayHeartRate));
    lapLCD->display(displayWorkoutLap+displayLap);

    // load or gradient depending on mode we are running
    if (status&RT_MODE_ERGO)
        loadLCD->display(displayLoad);
    else
    {
        val = round(displayGradient*10)/10.00;
        loadLCD->display(QString::number(val, 'f', 1)); // always show 1 decimal point
    }

    // distance
    val = round(displayDistance*(useMetricUnits ? 1.0 : MILES_PER_KM) *10.00) /10.00;
    distanceLCD->display(QString::number(val, 'f', 1)); // always show 1 decimal point

    // NZ Averages.....
    if (displayPower) { //NZAP is bogus - make it configurable!!!
        pwrcount++; if (pwrcount ==1) avgPower = displayPower;
        avgPower = ((avgPower * (double)pwrcount) + displayPower) /(double) (pwrcount+1);
    }
    if (displayCadence) {
        cadcount++; if (cadcount ==1) avgCadence = displayCadence;
        avgCadence = ((avgCadence * (double)cadcount) + displayCadence) /(double) (cadcount+1);
    }
    if (displayHeartRate) {
        hrcount++; if (hrcount ==1) avgHeartRate = displayHeartRate;
        avgHeartRate = ((avgHeartRate * (double)hrcount) + displayHeartRate) /(double) (hrcount+1);
    }
    if (displaySpeed) {
        spdcount++; if (spdcount ==1) avgSpeed = displaySpeed;
        avgSpeed = ((avgSpeed * (double)spdcount) + displaySpeed) /(double) (spdcount+1);
    }
    if (displayLoad && status&RT_MODE_ERGO) {
        lodcount++; if (lodcount ==1) avgLoad = displayLoad;
        avgLoad = ((avgLoad * (double)lodcount) + displayLoad) /(double) (lodcount+1);
        avgloadLCD->display((int)avgLoad);
    }
    if (status&RT_MODE_SPIN) {
        grdcount++; if (grdcount ==1) avgGradient = displayGradient;
        avgGradient = ((avgGradient * (double)grdcount) + displayGradient) /(double) (grdcount+1);
        avgloadLCD->display((int)avgGradient);
    }

    avgpowerLCD->display((int)avgPower);
    val = round(avgSpeed * (useMetricUnits ? 1.0 : MILES_PER_KM) * 10.00)/10.00;
    avgspeedLCD->display(QString::number(val, 'f', 1)); // always show 1 decimal point
    avgcadenceLCD->display((int)avgCadence);
    avgheartrateLCD->display((int)avgHeartRate);


    // now that plot....
    rtPlot->pwrData.addData(displayPower); // add new data point
    rtPlot->cadData.addData(displayCadence); // add new data point
    rtPlot->spdData.addData(displaySpeed); // add new data point
    rtPlot->hrData.addData(displayHeartRate); // add new data point
    //rtPlot->lodData.addData(displayLoad); // add new Load point
    rtPlot->replot();                // redraw

    this->update();

}
void
RealtimeController::processRealtimeData(RealtimeData &rtData)
{
    if (!dc) return; // no config

    // setup the algorithm or lookup tables
    // for the device postprocessing type
    switch(dc->postProcess) {
    case 0 : // nothing!
        break;
    case 1 : // Kurt Kinetic - Cyclone
        {
        double mph = rtData.getSpeed() * MILES_PER_KM;
        // using the algorithm from http://www.kurtkinetic.com/powercurve.php
        rtData.setWatts((6.481090) * mph + (0.020106) * (mph*mph*mph));
        }
        break;
    case 2 : // Kurt Kinetic - Road Machine
        {
        double mph = rtData.getSpeed() * MILES_PER_KM;
        // using the algorithm from http://www.kurtkinetic.com/powercurve.php
        rtData.setWatts((5.244820) * mph + (0.019168) * (mph*mph*mph));
        }
        break;
    case 3 : // Cyclops Fluid 2
        {
        double mph = rtData.getSpeed() * MILES_PER_KM;
        // using the algorithm from:
        // http://thebikegeek.blogspot.com/2009/12/while-we-wait-for-better-and-better.html
        rtData.setWatts((0.0115*(mph*mph*mph)) - ((0.0137)*(mph*mph)) + ((8.9788)*(mph)));
        }
        break;
    case 4 : // BT-ATS - BT Advanced Training System
        {
        //        v is expressed in revs/second
        double v = rtData.getWheelRpm()/60.0;
        // using the algorithm from Steven Sansonetti of BT:
        //  This is a 3rd order polynomial, where P = av3 + bv2 + cv + d
        //  where:
                double a =       2.90390167E-01; // ( 0.290390167)
                double b =     - 4.61311774E-02; // ( -0.0461311774)
                double c =       5.92125507E-01; // (0.592125507)
                double d =       0.0;
        rtData.setWatts(a*v*v*v + b*v*v +c*v + d);
        }
        break;

    case 5 : // Lemond Revolution
        {
        double V = rtData.getSpeed() * 0.277777778;
        // Tom Anhalt spent a lot of time working this all out
        // for the data / analysis see: http://wattagetraining.com/forum/viewtopic.php?f=2&t=335
        rtData.setWatts((0.21*pow(V,3))+(4.25*V));
        }
        break;

    case 6 : // 1UP USA
        {
        double V = rtData.getSpeed() * MILES_PER_KM;
        // Power curve provided by extraction from SportsTracks plugin
        rtData.setWatts(25.00 + (2.65f*V) - (0.42f*pow(V,2)) + (0.058f*pow(V,3)));
        }
        break;

    // MINOURA - Has many gears
    case 7 : //MINOURA V100 on H
        {
        double V = rtData.getSpeed();
        // 7 = V100 on H: y = -0.0036x^3 + 0.2815x^2 + 3.4978x - 9.7857 
        rtData.setWatts(pow(-0.0036*V, 3) + pow(0.2815*V,2) + (3.4978*V) - 9.7857);
        }
        break;

    case 8 : //MINOURA V100 on 5
        {
        double V = rtData.getSpeed();
        // 8 = V100 on 5: y = -0.0023x^3 + 0.2067x^2 + 3.8906x - 11.214 
        rtData.setWatts(pow(-0.0023*V, 3) + pow(0.2067*V,2) + (3.8906*V) - 11.214);
        }
        break;

    case 9 : //MINOURA V100 on 4
        {
        double V = rtData.getSpeed();
        // 9 = V100 on 4: y = -0.00173x^3 + 0.1825x^2 + 3.4036x - 10 
        rtData.setWatts(pow(-0.00173*V, 3) + pow(0.1825*V,2) + (3.4036*V) - 10.00);
        }
        break;

    case 10 : //MINOURA V100 on 3
        {
        double V = rtData.getSpeed();
        // 10 = V100 on 3: y = -0.0011x^3 + 0.1433x^2 + 2.8808x - 8.1429 
        rtData.setWatts(pow(-0.0011*V, 3) + pow(0.1433*V,2) + (2.8808*V) - 8.1429);
        }
        break;

    case 11 : //MINOURA V100 on 2
        {
        double V = rtData.getSpeed();
        // 11 = V100 on 2: y = -0.0007x^3 + 0.1348x^2 + 1.581x - 3.3571 
        rtData.setWatts(pow(-0.0007*V, 3) + pow(0.1348*V,2) + (1.581*V) - 3.3571);
        }
        break;

    case 12 : //MINOURA V100 on 1
        {
        double V = rtData.getSpeed();
        // 12 = V100 on 1: y = 0.0004x^3 + 0.057x^2 + 1.7797x - 5.0714 
        rtData.setWatts(pow(0.0004*V, 3) + pow(0.057*V,2) + (1.7797*V) - 5.0714);
        }
        break;

    case 13 : //MINOURA V100 on L
        {
        double V = rtData.getSpeed();
        // 13 = V100 on L: y = 0.0557x^2 + 1.231x - 3.7143 
        rtData.setWatts(pow(0.0557*V, 2) + (1.231*V) - 3.7143);
        }
        break;

    case 14 : //SARIS POWERBEAM PRO
        {
        double V = rtData.getSpeed();
        // 14 = 0.0008x^3 + 0.145x^2 + 2.5299x + 14.641 where x = speed in kph
        rtData.setWatts(pow(0.0008*V, 3) + pow(0.145*V, 2) + (2.5299*V) + 14.641);
        }
        break;

    case 15 : //  TACX SATORI SETTING 2
        {
        double V = rtData.getSpeed();
        double slope = 5.33;
        double intercept = -36.67;
        rtData.setWatts((slope * V) + intercept);
        }
        break;

    case 16 : //  TACX SATORI SETTING 4
        {
        double V = rtData.getSpeed();
        double slope = 8.27;
        double intercept = -47.33;
        rtData.setWatts((slope * V) + intercept);
        }
        break;

    case 17 : //  TACX SATORI SETTING 6
        {
        double V = rtData.getSpeed();
        double slope = 11.400;
        double intercept = -67.00;
        rtData.setWatts((slope * V) + intercept);
        }
        break;

    case 18 : //  TACX SATORI SETTING 8
        {
        double V = rtData.getSpeed();
        double slope = 14.40;
        double intercept = -82.00;
        rtData.setWatts((slope * V) + intercept);
        }
        break;

    case 19 : //  TACX SATORI SETTING 10
        {
        double V = rtData.getSpeed();
        double slope = 17.73;
        double intercept = -114.67;
        rtData.setWatts((slope * V) + intercept);
        }
        break;

    case 20 : //  TACX FLOW SETTING 0
        {
        double V = rtData.getSpeed();
        double slope = 7.75;
        double intercept = -47.27;
        rtData.setWatts((slope * V) + intercept);
        }
        break;

    case 21 : //  TACX FLOW SETTING 2
        {
        double V = rtData.getSpeed();
        double slope = 9.51;
        double intercept = -66.69;
        rtData.setWatts((slope * V) + intercept);
        }
        break;

    case 22 : //  TACX FLOW SETTING 4
        {
        double V = rtData.getSpeed();
        double slope = 11.03;
        double intercept = -71.59;
        rtData.setWatts((slope * V) + intercept);
        }
        break;

    case 23 : //  TACX FLOW SETTING 6
        {
        double V = rtData.getSpeed();
        double slope = 12.81;
        double intercept = -95.05;
        rtData.setWatts((slope * V) + intercept);
        }
        break;

    case 24 : //  TACX FLOW SETTING 8
        {
        double V = rtData.getSpeed();
        double slope = 14.37;
        double intercept = -102.43;
        rtData.setWatts((slope * V) + intercept);
        }
        break;

    default : // unknown - do nothing
        break;
    }
}