/*
  Verwarming.cpp
  Copyright (c) 2014 Kenneth van Ewijk.  All right reserved.
*/

// include this library's description file
#include "Verwarming.h"
#include "Arduino.h"
#include "Wire.h"

Verwarming::Verwarming(void)
{
  // initialize this instance's variables
  temp = 0;
  wattage = 0;
  state = false;
  volume = 0;
  led = 0;
}


//Set Heater Properties
void Verwarming::setTemp(double givenTemp)
{
  temp = givenTemp;
}

void Verwarming::setState(bool givenState){
  state = givenState;
}

void Verwarming::setWattage(int givenWattage){
  wattage = givenWattage;
  deltaTemp = (wattage / ( (volume * 1.293) * 1000) ) * 60;
}

void Verwarming::setVolume(int givenVolume){
  volume = givenVolume;
  deltaTemp = (wattage / ( (volume * 1.293) * 1000) ) * 60;
}

void Verwarming::setLed(int givenLed){
  led = givenLed;
  pinMode(led, OUTPUT);
}


//Calc Heater Properties
void Verwarming::toggleLed(void)
{
  if(state){
    analogWrite(led, 255);
  }
  else {
    analogWrite(led, 0);
  }
}


//Calc Heater Properties
double Verwarming::calcTemp(void)
{
  if(state){
    double newTemp = temp + deltaTemp;

    return newTemp;
  }
  else {
    return temp;
  }
}

void Verwarming::keepTemp(double givenTemp)
{

  if(temp >= givenTemp){
    state = false;
  }
  else if(temp < givenTemp){
    state = true;
  }

  toggleLed();
  temp = calcTemp();
}

void Verwarming::gotoTemp(double givenTemp)
{

  if(temp >= givenTemp){
    state = false;
  }

  double newTemp = calcTemp();

  if(newTemp <= givenTemp){
    temp = newTemp;
  }
  else {
    state = false;
  }

  toggleLed();
}


//Get Heater Properties
double Verwarming::getTemp(void)
{
	return temp;
}

int Verwarming::getWattage(void)
{
	return wattage;
}

bool Verwarming::getState(void){
  return state;
}

double Verwarming::getDeltaTemp(void){
  return deltaTemp;
}

int Verwarming::getTimeToTemp(double givenHeatFlow, double givenToTemp){
  int timer = 0;

  if(state){
    double deltaTemp2 = givenToTemp - temp;
    double neededHeat = (volume * 1.293) * 1000 * deltaTemp2;

    double heatFlow = wattage - givenHeatFlow;
    timer = neededHeat / heatFlow;
    timer = timer / 60;

    if(timer < 0){
      timer = 0;
    }
  }
  else{
    timer = 0;
  }

  return timer;
}