#include <SSD1322_for_Adafruit_GFX.h>   // SPI 256x64 oled display
#include "fonts/DejaVu_Sans_Mono_14.h"  // Adafruit GFX font
#include "fonts/DejaVu_Sans_Mono_9.h"   // Adafruit GFX font

#include <EEPROM.h>

#include <PID_v1.h>                     // PID controller
#include <Adafruit_MAX31856.h>

#include "VarInit.h"                    // All variables used


void setup()
{
  Serial.begin(115200);

  maxthermo.begin();                    // Activate the MAX31856 temperature sensormodule

  maxthermo.setThermocoupleType(MAX31856_TCTYPE_K); // Choose type K thermocouple sensor

  //maxthermo.setConversionMode(MAX31856_ONESHOT_NOWAIT);

  oled.setFont(&DejaVu_Sans_Mono_14); 

  myPID.SetOutputLimits(0, 4095) ;

  //turn the PID controller on
  myPID.SetMode(AUTOMATIC);             // Activate the temperature controller

  setupGPIO();                          // Setup i/o pins

  if ( ! oled.begin(0x3D) ) {        // Activate the 256x64 oled display
    Serial.println("Unable to initialize OLED");
    while (1) yield();
  }

  oled.clearDisplay();
  oled.setContrast(255);             // 0 = dark  255 = bright
  oled.setRotation(2);               // landscape mode

  layoutput(4);                         // Show start screen
  delay(2000);
  layoutput(5);                         // Show version screen
  delay(2000);
  mainScreen();                         // Show main screen

  TestEepromEmpty();
  GetEeprom();
}


void loop()
{
  // Check alerts
  unsigned long alertnow = millis();
  if (alertnow - alerttime > alertwindow) {
    alerttime = alertnow;
    readKlixon();                     // Check if the internal temperature is higher than 50 degrees celcius
  }

  // Read the buttons
  unsigned long keynow = millis();
  if (keynow - keytime > keywindow) {
    keytime = keynow;
    readKeys();                       // Check if a key is pressed

    // if the key is a repeatable key, the repeat will go faster if you keep the key pushed longer.
    if (keyIn == true && (repeatUpdownKeyblk == true || repeatRightLeftKeyblk == true || repeatEnterKeyblk == true)) {
      if (first==0){ first=1; keywindow = initrepeat;}
      keytottime = keytottime + keywindow;
      if (keytottime > keytimecnt) {
        keywindow = keywindow - 10;
        if (keywindow < 0) {
          keywindow = 0;
        }
        keytottime = 0;
      }
    } else {
      first = 0;
      keytottime = 0;
      keytimecntinit = keytimecnt;
      keywindow = initkeywindow;
    }
  }


  // Update actual temperature
  unsigned long tempnow = millis();
  if (tempnow - temptime > tempwindow && blocktemp==false) {
    temptime = tempnow;
    tempControl(POSX[1][2] + 1);        //refresh temperature and place the value at POSX[1][2]+1
  }

  // Lifting pins on delay
  if (pins_up_with_delay > 0) {
    unsigned long pindelaynow = millis();
    if (pins_up_with_delay == 1) {
      pindelaytime = pindelaynow;
      pins_up_with_delay = 2;
    }
    if (pindelaynow - pindelaytime > pindelaywindow) {
      pindelaytime = pindelaynow;
      pins_up_with_delay = 0;

      digitalWrite(pins, true);

    }
  }

  // Run process timer
  if (process_start == true) {
    unsigned long prctimernow = millis();
    if (prctimernow - prctimertime > prctimerwindow) {
      prctimertime = prctimernow;
      processTime();
    }
  }

  // Run process timer Curve
  if (process_startC == true) {
    unsigned long prctimernowC = millis();
    if (prctimernowC - prctimertimeC > prctimerwindowC) {
      prctimertimeC = prctimernowC;
      processTimeC();
    }
  }

  unsigned long scrsavernow = millis();
  if (start_scr_timer == true) {
    if (scrsavernow - scrsavertime > scrsaverwindow) {
      scrsavertime = scrsavernow;    
      scrsvrcnt++;
      if (scrsvrcnt>=60) {oled.setContrast(30); scrsvrcnt = 0;}
    }
  } else {
    oled.setContrast(255); scrsvrcnt = 0;
  }
}
