#ifndef _HEATER
#define _HEATER

#include "types.h"
//#define H_SUM_LIMIT	12800
#define HPWMSCALE    1024
#define MINHPWM 0
#define MAXHPWM 99
#define PWMSTART 0
#define MAXPPWM ((int32)MAXHPWM*(int32)HPWMSCALE)
#define MAXNPWM ((int32)MINHPWM*(int32)HPWMSCALE)
#define IRESETPWM ((int32)(50)*(int32)HPWMSCALE)
enum heaterStates{
heaterOn, heaterOff};

typedef struct
{
	uint16	dwell;		//secs?
	int16 	targetT;	//in hudredths of deg C
	uint16	errHoldoff;	//ticks before starting to check temp
	int16	minT,maxT;	//acceptable temp band
}thermalStep;
typedef struct {
   int16PID heating,cooling;  //PID gains for heating & cooling
} thermalCalibration;

enum CompletionCode { //causes of servo command aborts
  noError, invalidThermalProgram,hardwareFault, temperatureError
};

typedef struct
{
	thermalStep tProgram;
	thermalCalibration 	tCalib;
	int16		currentTemperature,thermoCouple;	//maybe calculated temp including adjustment
	uint16		elapsedTics;
	byte 		state;
	byte code;
	
	uint8 source;    // add these to hold who reguested the current move
	uint8 tag;         // if this field is 0 then no move in progress
    uint8 cmdType;   //rel or absolute

	int32		heaterSlope_num;
	int32		heaterSlope_den;
	int16		heaterPWM,coolerPWM;
	uint16		tempSense1,tempSense2;
	byte		tempSenseSwitch;
	int32		fixedPWM;
	int32 		sum;	 		//sum of error for integral term
	int32      intLimitP, intLimitN;
	int32       Pterm;
	int32        Iterm;
	int16       temp;	
}Heater;



void HeaterServoLoop(Heater* h);
void initHeater(Heater* h);
void startHeater(Heater* h);
void turnHeaterOff(Heater* h);
void calculateSlope(Heater* h);

#endif