package fietsv1;
import stamp.core.*;
import stamp.math.*;
import fietsv1.*;

/**
 * Processes
 *
 * komt nog
 *
 * @version 1.0 28-05-06
 * @author Johnny Kuiper
 */

public class Processor {

  private boolean pulsePresent;
  private boolean pulseValid;
  private int circumference; // in dm
  private int spd;
  private int avg;
  private int dstm;
  private int dstKm;
  private Sensor sen;
  private int chrMS,chrS,chrM,chrH;

  public Processor() {
    sen = new Sensor();
    pulsePresent = false;
    pulseValid = false;
  }

  public void setCircumference(int c){
    circumference = c;
  }

  public void setPulsePresent(){
    pulsePresent = true;
  }

  public void run(){
    if (pulsePresent){
      if (sen.getPulseTime() <= 2000){
        pulseValid = true;
      }
      if (pulseValid){
        dstm = dstm + circumference;
        if (dstm == 10000) {
          dstKm++;
          dstm = 0;
          if (dstKm == 10000) dstKm = 0; //wrap from 9999 to 0000
        }
        chrMS = chrMS + sen.getPulseTime();
        if (chrMS >= 2000) {
          chrS = chrS + 2;
          chrMS -= 2000;
        }
        if (chrS > 59) {
          chrM++;
          chrS -= 60;
        }
        if (chrM > 59) {
          chrH++;
          chrM -= 60;
        }
        if (chrH > 23) {
          chrH -= 60;
        }
      }
    }
    pulsePresent = false;
    pulseValid = false;
  }

  public int calculateSpeed(){
    spd = UnsignedIntMath.umulf((short)36000,UnsignedIntMath.ufrac(circumference,sen.getPulseTime()));
    spd = UnsignedIntMath.ufrac(spd,10); //dm>m
    return spd;
  }

  public int calculateAverage(){
    //hier average calculation
    return avg;
  }

  public int getDistanceM(){
  return dstm;
  }

  public int getDistanceKm(){
  return dstKm;
  }

  public int getChronoSeconds(){
    return chrS;
  }

  public int getChronoMinutes(){
    return chrM;
  }

  public int getChronoHours(){
    return chrH;
  }


} 