
/*****************************************************
*           Keyboard action                          *
*****************************************************/

void readKeys(){
  
    enterKey  = digitalRead(enter);
    rightKey  = digitalRead(right);
    leftKey   = digitalRead(left);
    upKey     = digitalRead(up);
    downKey   = digitalRead(down);
    
    if (enterKey == pressed){
      keyIn = true;
        if (scrNr == 0 && enterKeyblk == false){
          enterKeyblk = true;
          enter0Script();
        }
        if (scrNr == 1 && enterKeyblk == false){
          enterKeyblk = true;
          enter1Script();
        }
        if (scrNr == 2 && enterKeyblk == false){
          enterKeyblk = true;
          enter2Script();
        }
        if (scrNr == 3 && enterKeyblk == false){
          enterKeyblk = true;
          enter3Script();
        }
    }
    if (enterKey == released){
      if (enterKeyblk == true){
        enterKeyblk = false;
      }
    }
    
    if (rightKey == pressed){
      keyIn = true;
      if (rightKeyblk == false){
        if(repeatRightLeftKeyblk == false) rightKeyblk = true;        
        if (scrNr == 1){
          if (recipeNr0 == 0){
            right1Script();
          }
        }
        if (scrNr == 2){
          right2Script();
        }
      }
    }
    if (rightKey == released){
      if (rightKeyblk == true){
        rightKeyblk = false;
      }
    }
    
    if (leftKey == pressed){
      keyIn = true;
      if (leftKeyblk == false){
        if(repeatRightLeftKeyblk == false) leftKeyblk = true;        
        if (scrNr == 1){
          if (recipeNr0 == 0){
            left1Script();
          }
        }
        if (scrNr == 2){
          left2Script();
        }
        if (scrNr == 3){
          left3Script();
        }
      }
    }
    if (leftKey == released){
      if (leftKeyblk == true){
        leftKeyblk = false;
      }
    }
    
    if (upKey == pressed ){
      keyIn = true;
      if (upKeyblk == false){
        if(repeatUpdownKeyblk == false) upKeyblk = true;
        if (scrNr == 0){
          up0Script();
        }
        if (scrNr == 1){
          up1Script();
        }
        if (scrNr == 2){
          up2Script();
        }
        if (scrNr == 3){
          up3Script();
        }
      }
    }
    if (upKey == released){
      if (upKeyblk == true){
        upKeyblk = false;
      }
    }
    
    if (downKey == pressed){
      keyIn = true;
      if (downKeyblk == false){
        if(repeatUpdownKeyblk == false) downKeyblk = true;
        if (scrNr == 0){
          down0Script();
        }
        if (scrNr == 1){
          down1Script();
        }
        if (scrNr == 2){
          down2Script();
        }
        if (scrNr == 3){
          down3Script();
        }
      }
    }
    if (downKey == released){
     if(downKeyblk == true){
        downKeyblk = false;
       }
    }
    
    if ((enterKey & rightKey & leftKey & upKey & downKey) == released){keyIn = false; start_scr_timer = true;} else {start_scr_timer = false;}
    if ((upKey == pressed || downKey == pressed) && enter1Pos==0){blocktemp=true;} else {blocktemp=false;}
    if (upKey == pressed & downKey == pressed){/*Enter reset code here*/}  
}

/*****************************************************
*           Place a value on x,y position            *
*****************************************************/

void placeValue(int row, int col, int len, int dec, float oldvalue, float value)
{
    
  String svalue = String(oldvalue,dec);
  int lvalue = svalue.length();             // length of value to send to the display
  for( int i=0;i<len-lvalue;i++){
    svalue =  " " + svalue;
  }
  oled.setTextColor(SSD1322_BLACK);
  oled.setCursor(col*font_with, row*16+15);
  oled.print(svalue);
  
  //oled.fillRect(col*font_with, row*16, len*font_with, 15, SSD1322_BLACK);

  svalue = String(value,dec);
  lvalue = svalue.length();                 // length of value to send to the display
  for( int i=0;i<len-lvalue;i++){
    svalue =  " " + svalue;
  }
  oled.setTextColor(SSD1322_WHITE);
  oled.setCursor(col*font_with, row*16+15);
  oled.print(svalue);
  oled.display();
}


/*****************************************************
*      display a layout from the LAYOUT array        *
*****************************************************/

void layoutput(int laynr)
{
    
  oled.clearDisplay();
  oled.setTextColor(SSD1322_WHITE);
  for( int i=0;i<4;i++){
    oled.setCursor(0,15+i*16);
    oled.println(LAYOUT[laynr][i]);
  }
  oled.display();
}


/*****************************************************
*         Change mode heat run stop standby          *
*****************************************************/

void changeMode(int oldmode, int mode)
{
  oled.setCursor(0,15);
  oled.setTextColor(SSD1322_BLACK);
  oled.println(MODE[oldmode]);
  
  oled.setCursor(0,15);
  oled.setTextColor(SSD1322_WHITE);
  oled.println(MODE[mode]);
  
  oled.display();
}

void outbar(int outVal, int xOutPos, int OutLen)
{
  oled.drawRect(xOutPos, 16, OutLen, 4, bordercolor);
  oled.drawRect(xOutPos+1, 17, OutLen-2, 2, 0);
  oled.drawRect(xOutPos+1, 17, outVal, 2, 15);

}



/****************************************************
*              Status Message Scroll                *
****************************************************/

void statusMessage(int stextNr)
{
  oled.setCursor(0,63);
  oled.setTextColor(SSD1322_BLACK);
  oled.print(STATUSLINE[statusMsgOld]);
  if(statusMsgOld == 4) oled.drawCircle(239, 55, 2, SSD1322_BLACK);    // degrees Celsius 'o'
  
  oled.setCursor(0,63);
  oled.setTextColor(SSD1322_WHITE);
  oled.print(STATUSLINE[stextNr]);
  if(stextNr == 4) oled.drawCircle(239, 55, 2, SSD1322_WHITE);    // degrees Celsius 'o'
  
  statusMsgOld = stextNr;
  
  oled.display();
}


/*****************************************************
*           Place cursor in new position             *
*****************************************************/

void changePos(int var, bool vis)
{
  oled.setCursor(POSX[scrNr][var+1]*font_with, POSY[scrNr][var+1]); 
  if (vis == 0){
    oled.setTextColor(SSD1322_BLACK);
  } else {
    oled.setTextColor(SSD1322_WHITE);
  }
  oled.print(">");   
  oled.display();
}

/*****************************************************
*               Thermcouple errors                   *
*****************************************************/

void thermocoupleErrors(uint8_t fault) {
    if (fault & MAX31856_FAULT_CJRANGE) statusMessage(10);
    if (fault & MAX31856_FAULT_TCRANGE) statusMessage(11);
    if (fault & MAX31856_FAULT_CJHIGH)  statusMessage(12);
    if (fault & MAX31856_FAULT_CJLOW)   statusMessage(13);
    if (fault & MAX31856_FAULT_TCHIGH)  statusMessage(14);
    if (fault & MAX31856_FAULT_TCLOW)   statusMessage(15);
    if (fault & MAX31856_FAULT_OVUV)    statusMessage(16);
    if (fault & MAX31856_FAULT_OPEN)    statusMessage(17);
    setAlert = true;
}

/*****************************************************
*          Place elements on main screen             *
*****************************************************/

void mainScreen(){
  scrNr = 0;
  layoutput(0);
   
  // Draw border
  oled.drawLine(9*font_with+2, 0, 9*font_with+2, 19, bordercolor);
  oled.drawLine(20*font_with+4, 0, 20*font_with+4, 19, bordercolor);
  oled.drawLine(0, 0, 0, 48, bordercolor);
  oled.drawLine(255, 0, 255, 48, bordercolor);
  oled.drawLine(0, 0, 255, 0, bordercolor);
  oled.drawLine(0, 49, 255, 49, bordercolor);
  oled.drawLine(0, 19, 255, 19, bordercolor);
  outbar(30, 83, 102);

  oled.drawCircle(15*font_with+5, 7, 2, SSD1322_WHITE);    // degrees Celsius 'o'
  
  tempControl(POSX[1][2]+1);
  placeValue(0, POSX[1][3]+1, 3, 0, timeCounter0, timeCounter0); //  place actual procestime on screen
 
  changeMode(oldenter1Pos, enter1Pos);
  curpos = 0;
  changePos(curpos, true);                                     //  Place the cursor
  //oled.display();
}

/*****************************************************
*          Place elements on single screen           *
*****************************************************/

void singleScreen(){
  scrNr = 1;
  layoutput(1);
   
  // Draw border
  oled.drawLine(9*font_with+2, 0, 9*font_with+2, 48, bordercolor);
  oled.drawLine(20*font_with+4, 0, 20*font_with+4, 48, bordercolor);
  oled.drawLine(0, 0, 0, 48, bordercolor);
  oled.drawLine(255, 0, 255, 48, bordercolor);
  oled.drawLine(0, 0, 255, 0, bordercolor);
  oled.drawLine(0, 49, 255, 49, bordercolor);
  oled.drawLine(0, 19, 255, 19, bordercolor);

  oled.drawCircle(15*font_with+5, 7, 2, SSD1322_WHITE);     // degrees Celsius 'o'
  oled.drawCircle(15*font_with+5, 39, 2, SSD1322_WHITE);    // degrees Celsius 'o'
  
  tempControl(POSX[1][2]+1);

  tempValue0 = RECIPES[recipeNr0][0];
  oldtempValue0 = tempValue0;
  timeValue0 =  RECIPES[recipeNr0][1];
  oldtimeValue0 = timeValue0;

  placeValue(0, POSX[1][3]+1, 3, 0, timeCounter0, timeCounter0); //  place actual procestime on screen
  placeValue(2, POSX[1][1]+1, 2, 0, recipeNr0, recipeNr0);     //  place set recipe number on screen
  placeValue(2, POSX[1][2]+1, 3, 0, tempValue0, tempValue0);   //  place set temperature on screen
  placeValue(2, POSX[1][3]+1, 3, 0, timeValue0, timeValue0);   //  place set procestime on screen

  changeMode(enter1Pos, enter1Pos);                            //  Change to STOP mode
  statusMessage(enter1Pos);                                    //  Show the STOP message
  digitalWrite(vacuum, false);                                 //  Vacuum off
  //pins_up_with_delay = 1;                                      //  Pins up after a delay
  curpos = 0;
  changePos(curpos, true);                                     //  Place the cursor

  //oled.display();
}

/*****************************************************
*         Place elements on Recipe screen            *
*****************************************************/

void curveSetupScreen(){
  scrNr = 2;
  layoutput(2);
   
  // Draw border
  oled.drawLine(9*font_with+2, 0, 9*font_with+2, 19, bordercolor);
  oled.drawLine(20*font_with+4, 0, 20*font_with+4, 19, bordercolor);
  oled.drawLine(7*font_with+4, 19, 7*font_with+4, 49, bordercolor);
  oled.drawLine(14*font_with, 19, 14*font_with, 49, bordercolor);
  oled.drawLine(21*font_with+4, 19, 21*font_with+4, 49, bordercolor);
  oled.drawLine(0, 0, 0, 48, bordercolor);
  oled.drawLine(255, 0, 255, 48, bordercolor);
  oled.drawLine(0, 0, 255, 0, bordercolor);
  oled.drawLine(0, 49, 255, 49, bordercolor);
  oled.drawLine(0, 19, 255, 19, bordercolor);

  oled.drawCircle(15*font_with+5, 7, 2, SSD1322_WHITE);    // degrees Celsius 'o'
  oled.drawCircle(19*font_with+5, 39, 2, SSD1322_WHITE);   // degrees Celsius 'o'
  
  tempControl(POSX[1][2]+1);

  tempValue1 = CURVES[curveNr1][pointNr1][0];
  oldtempValue1 = tempValue1;
  timeValue1 =  CURVES[curveNr1][pointNr1][1];
  oldtimeValue1 = timeValue1;
  
  placeValue(0, POSX[1][3]+1, 3, 0, timeCounter1, timeCounter1); //  place actual procestime on screen
  placeValue(2, POSX[2][1]+1, 2, 0, curveNr1, curveNr1);       //  place set curve number on screen
  placeValue(2, POSX[2][2]+1, 2, 0, pointNr1, pointNr1);       //  place set point number on screen
  placeValue(2, POSX[2][3]+1, 3, 0, oldtempValue1, tempValue1);   //  place set temperature on screen
  placeValue(2, POSX[2][4]+1, 3, 0, oldtimeValue1, timeValue1);   //  place set procestime on screen

 
  changeMode(oldenter1Pos, enter1Pos);
  statusMessage(enter1Pos);                                    //  Show the status message
  curpos = 0;
  changePos(curpos, true);                                     //  Place the cursor
  //oled.display();
}

/*****************************************************
*         Place elements on Curve screen            *
*****************************************************/

void curveScreen(){
  
  pointcnt = 1;
  scrNr = 3;
  layoutput(3);
   
  // Draw border
  oled.drawLine(7*font_with+2, 0, 7*font_with+2, 19, bordercolor);
  oled.drawLine(15*font_with+4, 0, 15*font_with+4, 19, bordercolor);
  oled.drawLine(22*font_with+4, 0, 22*font_with+4, 19, bordercolor);
  oled.drawLine(0, 0, 0, 63, bordercolor);
  oled.drawLine(255, 0, 255, 63, bordercolor);
  oled.drawLine(0, 0, 255, 0, bordercolor);
  oled.drawLine(0, 63, 255, 63, bordercolor);
  oled.drawLine(0, 19, 255, 19, bordercolor);

  oled.drawCircle(19*font_with+5, 7, 2, SSD1322_WHITE);    // degrees Celsius 'o'
  oled.drawCircle(12*font_with+5, 7, 2, SSD1322_WHITE);    // degrees Celsius 'o'
  
  placeValue(0, POSX[1][2] -2, 3, 0, oldSetpoint, Setpoint);   

  tempControl(POSX[1][2]+1);
  placeValue(0, POSX[1][3]+1, 3, 0, timeCounter1, timeCounter1); //  place actual procestime on screen


  oled.setFont(&DejaVu_Sans_Mono_9);
  
  oled.setTextColor(SSD1322_WHITE);
  oled.setCursor(7*font_with+3, 12); oled.print("sp");

  curvetimescale = 0;
  for (int i = 0; i <= 10; i++) {
    if (CURVES[curveNr1][i][1] > curvetimescale){
      curvetimescale = CURVES[curveNr1][i][1];
    }
  }
  float timedev = curvetimescale/10;          
  curvetempscale = 0;
  for (int i = 0; i <= 10; i++) {
    if (CURVES[curveNr1][i][0] > curvetempscale){
      curvetempscale = CURVES[curveNr1][i][0];
    }
  }
  float tempdev = curvetempscale/50;
            
  oled.setTextColor(SSD1322_WHITE);
  oled.setCursor(2, 28); oled.print(curvetempscale,0);
  oled.drawCircle(12, 32, 1, SSD1322_WHITE);    // degrees Celsius 'o'
  oled.setCursor(14, 38); oled.print("C");
  oled.setCursor(14, 62); oled.print(0);
  oled.setCursor(235, 55); oled.print(curvetimescale,0);
  oled.setCursor(235, 62); oled.print("sec");
  oled.drawLine(22, 60, 232, 60, bordercolor);
  //for (int i = 0; i <= 10; i++) {
  //  oled.drawLine(22+i*21, 59, 22+i*21, 61, bordercolor);
  //}
  for (int i = 0; i <= timedev; i++) {
    oled.drawLine(22+i*210/timedev, 59, 22+i*210/timedev, 61, bordercolor);
  }
  oled.drawLine(22, 22, 22, 60, bordercolor);
  //for (int i = 0; i <= 2; i++) {
  //  oled.drawLine(21, 22+i*19, 23, 22+i*19, bordercolor);
  //}
  for (int i = 0; i <= tempdev; i++) {
    oled.drawLine(21, 60-i*38/tempdev, 23, 60-i*38/tempdev, bordercolor);
  }
  for (int i = 1; i <= 10; i++) {
    int x1 = 22+(CURVES[curveNr1][i-1][1]/curvetimescale)*210;
    int x2 = 22+(CURVES[curveNr1][i][1]/curvetimescale)*210;
    int y1 = 60-(CURVES[curveNr1][i-1][0]/curvetempscale)*38;
    int y2 = 60-(CURVES[curveNr1][i][0]/curvetempscale)*38;
    oled.drawLine(x1, y1, x2 , y2, curvecolor);
  }          
  oled.setFont(&DejaVu_Sans_Mono_14);
  changeMode(oldenter1Pos, enter1Pos);
  //curpos = 0;
  //changePos(curpos, true);//  Place the cursor
  oled.display();
}

/*****************************************************
*                   Setup I/O                        *
*****************************************************/

void setupGPIO(){
  pinMode(enter, INPUT_PULLUP);
  pinMode(up,    INPUT_PULLUP);
  pinMode(down,  INPUT_PULLUP);
  pinMode(right, INPUT_PULLUP);
  pinMode(left,  INPUT_PULLUP);

  pinMode(klixon,  INPUT_PULLUP); // Declaring high temperature alert as INPUT

  pinMode(pins, OUTPUT);          // Declaring lifting pins pin as OUTPUT
  pinMode(vacuum, OUTPUT);        // Delcaring vacuumpump pin as OUTPUT
  pinMode(heat, OUTPUT);          // Declaring heater pin as OUTPUT

  digitalWrite(pins, false);
  digitalWrite(vacuum, false);
  digitalWrite(heat, false);

  pinMode(CS, OUTPUT);            // Declaring CS as OUTPUT
  
  // configure LED PWM functionalitites
  analogWriteResolution(resolution);
  analogWriteFrequency(heat, freq);
  //ledcSetup(ledChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  //ledcAttachPin(heat, ledChannel);
}

/*********************************************
*           eeprom empty test                * 
**********************************************/
void TestEepromEmpty(){
  short eetest = 0;
  EEPROM.get(0, eetest);
  if(eetest != 12345){
    EEPROM.put(0, 12345);
    PutEeprom();
    //Serial.println(EEPROM.get(0, eetest));
  }
}

  

/*********************************************
*           Put recipes in eeprom            *
**********************************************/
void PutEeprom(){
  for (int i = 0; i < recipeMax0+1; i++) {
    for (int j = 0; j < 2; j++) {
      EEPROM.put((i*2+j)*2+10, RECIPES[i][j]);
      //Serial.print((i*2+j)*2+10); Serial.print(" "); Serial.print(RECIPES[i][j]);  Serial.print(" ");
    }
    //Serial.println();
  }

  for (int h = 0; h < curveMax1+1;h++) {
    for (int i = 0; i < pointMax1+1;i++) {
      for (int j = 0; j < 2; j++) {
         EEPROM.put(((h*(pointMax1+1)+i)*2+j)*2+100, CURVES[h][i][j]);
         //Serial.print(((h*(pointMax1+1)+i)*2+j)*2+100); Serial.print(" "); Serial.print(CURVES[h][i][j]);  Serial.print(" ");
      }
    }
    //Serial.println();
  }
}

/***********************************************
*           Get recipes from eeprom            *
************************************************/
void GetEeprom(){
  for (int i = 0; i < recipeMax0+1; i++) {
    for (int j = 0; j < 2; j++) {
      EEPROM.get((i*2+j)*2+10, RECIPES[i][j]);
      //Serial.print((i*2+j)*2+10); Serial.print(" "); Serial.print(RECIPES[i][j]);  Serial.print(" ");
    }
    //Serial.println();
  }

  for (int h = 0; h < curveMax1+1;h++) { // 0 - 10
    for (int i = 0; i < pointMax1+1;i++) { // 0 - 10
      for (int j = 0; j < 2; j++) { //0 - 1
        EEPROM.get(((h*(pointMax1+1)+i)*2+j)*2+100, CURVES[h][i][j]);
        //Serial.print(((h*(pointMax1+1)+i)*2+j)*2+100); Serial.print(" "); Serial.print(CURVES[h][i][j]);  Serial.print(" ");
      }
    }
    //Serial.println();
  }
}
