/*
Gestion du LCD:
-Initialisation
-Dessins et calcul des graphs
*/


void LCD_init(){
  lcd.begin(20, 4);
  lcd.createChar(0, degree);
#ifdef LCD_GRAPH
  currentTime = millis();
  loopTime = currentTime;
  clearchar(); // clear graph character array
  clearcustomchar(1, 6); // write the cleared grafchar array to custom LCD memory poitions
  graph_compute();
  countcol = 0;
  mempos = 1; // defines first screen position for graph
  glcdpos = 10; // first graph character position
#endif
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(software);//"Pregs Control");
  lcd.setCursor(0, 1);
  lcd.print(version);

}

#ifdef LCD_GRAPH
// function to get graph point height
int getheight(float tempnow, float minT, float maxT) { // takes measurements , minimum and maximum temperature and reurns a value from 0 to 7 
  if (tempnow <= minT) return 0;  // lowest graph point
  if (tempnow >= maxT) return 7; // highest graph point
  if (tempnow > minT & tempnow < maxT) {
    return (tempnow - minT) * 8 / (maxtemp - minT); // here is the way values from 1 to 6 are calculated  
  }
}

// function to make graph point in hc 77480 special characters
int createpoint(int row, int col, int hcchar) {  // takes as input the row(height) , the column of custom char that it must be set, and custom character memory position at LCD.
  row = 7 - row; // lcd character 0 is top left so invert coordinates
  col = 4 - col; // lcd character 0 is top left so invert coordinates
  while (row < 8){
    bitSet(grafChar[row], col); // this loop sets 1 the bit of the needed pixel and all below that at graphChar bitarray so as to display a bar!
    row++;
  }
  lcd.createChar((uint8_t)hcchar, grafChar); // write to custom character the contents of grafChar array. 
  return 1; // not used
}

// function to set clear custom graph chararacter matrix (not an independent function, just to make loop code neater
void clearchar() { // a quick way to set all grafChar bits back to zero so as to creat a new one.
  int countj = 0;
  while (countj < 8) {
    grafChar[countj] = B00000;
    countj++;
  }
  return;
} // end clear char
// function to clear range of LCD custom characters. Needs graph char to be set to nothing first (clearchar)
void clearcustomchar(int first, int second) { // clear the custom character LCD memory at the range given at function input.
  int counter = first;
  while (counter < second + 1) {
    lcd.createChar(counter, grafChar);
    counter++;
  }
  return;
} // end clearcustomchar

// function to find average of temparray (not an independent function  , just to clear the loop code) 
void averagecalc() {
  temparraycounter = 0; // initialize counter
  average = 0; // set average variable back to zero
  while (temparraycounter < 10) { // add the 10 temparray elements and store sum to average
    average = average + temparray[temparraycounter];
    temparraycounter++; // increase temparrayposition
  }
  average = (average / 10); // get average of 10 measurements
  temparraycounter = 0; // needs to be set again to zero because it's used in various places.
} // end averagecalc() 

// function to update min and max temp values whenever average is calculated. (not an independent function  , just to clear the loop code) 
void minmaxupdate() {
  if (average > maxtemp) { // update maximum temperature
    maxtemp = average;
  }
  if (average < mintemp) { // update minimum temperature
    mintemp = average;
  }
} // end minmaxupdate



void lcd_graph_compute(){

  tempC = int(input);
  if (currentTime >= (loopTime + updatetime)){
    temparray[temparraycounter] = tempC; // store current temperature to temp array
    temparraycounter++; // increase temparrayposition
    loopTime = currentTime;  // Updates loopTime
  }

  if (temparraycounter == 10) {
    averagecalc(); // calculate average
    minmaxupdate(); // update min and max values from average

      temp = getheight(average, mintemp, maxtemp);  // call my getheight function to calculate new point's heght value (0-7)

    createpoint(temp, countcol, mempos); //gives graph column height,column and custom LCD character number to function (mempos-9 makes custom character 1 the first custom graph character).
    countcol++; // increase column counter (0-4)
    if (countcol == 5) { // if lcd element column is exceeded (5) then
      mempos++; // go to next custom lcd char memory position
      glcdpos++; // increase lcd element position 
      countcol = 0; // go to new characters first column.
      clearchar(); // clear the gradChar array
    }
    if (glcdpos == 16) { // if last graph elements is completed then 
      mempos = 1; //go to first custom lcd character
      glcdpos = 10; // set the graph char position back to (10,1)
      clearcustomchar(1, 6); // erase all custom chars in LCD'S memory..
    }
  }
  graph_compute();
}

void graph_compute(){
  lcd.setCursor(10, 1); // set cursor to first graph LCD text elements.
  lcd.write((uint8_t)1); // write to screen the first custom graph character.
  lcd.setCursor(11, 1);
  lcd.write((uint8_t)2);
  lcd.setCursor(12, 1);
  lcd.write((uint8_t)3);
  lcd.setCursor(13, 1);
  lcd.write((uint8_t)4);
  lcd.setCursor(14, 1);
  lcd.write((uint8_t)5);
  lcd.setCursor(15, 1);
  lcd.write((uint8_t)6);
}

#endif
