#pragma once
#include <PID_v1.h>
#include "Arduino.h"

// ***** CONSTANTS *****
#define TEMPERATURE_ROOM        50
#define TEMPERATURE_SOAK_MIN    150
#define TEMPERATURE_SOAK_MAX    185
#define TEMPERATURE_REFLOW_MAX  220
#define TEMPERATURE_COOL_MIN    100
#define SENSOR_SAMPLING_TIME    1000
#define SOAK_TEMPERATURE_STEP   5
#define SOAK_MICRO_PERIOD       9000
#define DEBOUNCE_PERIOD_MIN     50
#define PREHEAT_PERIOD          12000
#define REFLOW_PERIOD           12000
#define COOLDOWN_PERIOD         30000

// ***** PID PARAMETERS *****
// ***** PRE-HEAT STAGE *****
#define PID_KP_PREHEAT 100
#define PID_KI_PREHEAT 0.025
#define PID_KD_PREHEAT 20
// ***** SOAKING STAGE *****
#define PID_KP_SOAK 300
#define PID_KI_SOAK 0.05
#define PID_KD_SOAK 250
// ***** REFLOW STAGE *****
#define PID_KP_REFLOW 300
#define PID_KI_REFLOW 0.05
#define PID_KD_REFLOW 350
#define PID_SAMPLE_TIME 1000
// This is for testing on different board
// #define LCD_PIN 14
// #define ODROID


// ***** TYPE DEFINITIONS *****
typedef enum REFLOW_STATE
{
  REFLOW_STATE_IDLE,
  REFLOW_STATE_PREHEAT,
  REFLOW_STATE_SOAK,
  REFLOW_STATE_REFLOW,
  REFLOW_STATE_COOL,
  REFLOW_STATE_COMPLETE,
  REFLOW_STATE_TOO_HOT,
  REFLOW_STATE_ERROR
} reflowState_t;

typedef enum REFLOW_STATUS
{
  REFLOW_STATUS_OFF,
  REFLOW_STATUS_ON
} reflowStatus_t;

typedef enum SWITCH
{
  SWITCH_NONE,
  SWITCH_1,
  SWITCH_2
} switch_t;

typedef enum DEBOUNCE_STATE
{
  DEBOUNCE_STATE_IDLE,
  DEBOUNCE_STATE_CHECK,
  DEBOUNCE_STATE_RELEASE
} debounceState_t;

void initControlLoop(void);
void handleControlLoop(void);

String getReflowStatus_str(void);
String getReflowState_str(void);
reflowStatus_t getReflowStatus(void);
void setReflowStatus(bool newStatus);
double getReflowTargetTemp(void);
bool getOutputState(void);
float getProcessTime(void);
bool getOverheating(void);