#include <Arduino.h>
#include <config.h>
#include <SoftwareSerial.h>
extern SoftwareSerial nexSerial;
extern uint8_t Mode; 
extern uint8_t old_Mode;
extern char line0[line_len];
extern char line1[line_len];
extern char line2[line_len];
extern char line3[line_len];
extern char line0_old[line_len];
extern char line1_old[line_len];
extern char line2_old[line_len];
extern char line3_old[line_len];
extern bool old_load;
extern char temperature_val[5];
extern char temperature_buf[5];
void writeNum2(char * command, uint16_t value){ 
  nexSerial.print(command);  
  if(command !="cmd"){
    nexSerial.print("=");
    nexSerial.print(value);
  }
  nexSerial.print("\xFF\xFF\xFF");
}
void writeStr(char *  command, char * txt){ 

  nexSerial.print(command);
    
  if(command != "cmd"){
    nexSerial.print(F(".txt=\""));
    nexSerial.print(txt);
    nexSerial.print("\"");
  }
  nexSerial.print("\xFF\xFF\xFF");
}

void screen_clear(char empty){
  memset(line0, empty, 20);
  memset(line1, empty, 20);
  memset(line2, empty, 20);
  memset(line3, empty, 20);
  writeStr("t0",(char *)line0);
  writeStr("t1",(char *)line1);
  writeStr("t2",(char *)line2);
  writeStr("t3",(char *)line3);
}

void print_temp(void){
    uint8_t compare=0;
    compare=memcmp(temperature_val,temperature_buf,5);
    if (compare!=0){
        writeStr("ttemp",temperature_val);
         memcpy(temperature_buf,temperature_val,5);
    }
    
}
void print_chars2(int x1,int len){
      uint8_t compare=0;
      switch(x1){
        case 0: 
          line0[21]=0x00;        
          compare=memcmp(line0_old,line0,20);
          if (compare!=0){
            writeStr("t0",line0);
          }
          line0[21]=0x00;
          memcpy(line0_old,line0,21);
          break;
        case 1:
          line1[21]=0x00;
          compare=memcmp(line1_old,line1,20);
          if (compare!=0){
            writeStr("t1",line1);
          }
          line1[21]=0x00;
          memcpy(line1_old,line1,21);    
          break;
        case 2:
          line2[21]=0x00;
          compare=memcmp(line2_old,line2,20);
          if (compare!=0)
            writeStr("t2",line2);
          line2[21]=0x00;
          memcpy(line2_old,line2,21);
          break;
        case 3:
          line3[21]=0x00;
          compare=memcmp(line3_old,line3,20);
          if (compare!=0)
            writeStr("t3",line3);
          line3[21]=0x00;
          memcpy(line3_old,line3,21); 
          break;         
      }
      delay(1);
}
void print_chars(int x1,int z, char * text,int len){
      switch(x1){
        case 0:
          memcpy(line0+z,text,len);         
          break;
        case 1:
          memcpy(line1+z,text,len);
          break;
        case 2:
          memcpy(line2+z,text,len);
          break;
        case 3:         
          memcpy(line3+z,text,len);
          break;          

      }
      print_chars2(x1,20);
}
void print_char(int x1,int z, char text){
      switch(x1){
        case 0:  
          line0[z]=text;
          break;
        case 1:
          line1[z]=text;
          break;
        case 2:
          line2[z]=text;
          break;
        case 3:
          line3[z]=text;
          break;          

      }
      print_chars2(x1,20);
}
void setmode(){
  if (Mode!=old_Mode){
    if (Mode == MODE_TC || Mode == MODE_TP || Mode == MODE_TT || Mode == MODE_TL ) {
          writeNum2("bi.bco",(int) col_gray);
          writeNum2("bw.bco",(int) col_gray);
          writeNum2("br.bco",(int) col_gray);  
          writeNum2("bbat.bco",(int) col_gray); 
          writeNum2("btrans.bco",(int) col_blue);
    }
    if (Mode == MODE_CC ) {
          writeNum2("bi.bco",(int) col_blue);
          writeNum2("bw.bco",(int) col_gray);
          writeNum2("br.bco",(int) col_gray);  
          writeNum2("bbat.bco",(int) col_gray); 
          writeNum2("btrans.bco",(int) col_gray);
    }
    if (Mode == MODE_CP ) {
          writeNum2("bi.bco",(int) col_gray);
          writeNum2("bw.bco",(int) col_blue);
          writeNum2("br.bco",(int) col_gray);  
          writeNum2("bbat.bco",(int) col_gray); 
          writeNum2("btrans.bco",(int) col_gray);
    }
    if (Mode == MODE_CR ) {
          writeNum2("bi.bco",(int) col_gray);
          writeNum2("bw.bco",(int) col_gray);
          writeNum2("br.bco",(int) col_blue);  
          writeNum2("bbat.bco",(int) col_gray); 
          writeNum2("btrans.bco",(int) col_gray);
    }
    if (Mode == MODE_BC ) {
          writeNum2("bi.bco",(int) col_gray);
          writeNum2("bw.bco",(int) col_gray);
          writeNum2("br.bco",(int) col_gray);  
          writeNum2("bbat.bco",(int) col_blue); 
          writeNum2("btrans.bco",(int) col_gray);
    }
    old_Mode=Mode;
  }
}
void color_load (bool load){

  if (old_load != load){
    if (load){
      writeNum2("bload.bco",(int) col_green); 
    }else{
      writeNum2("bload.bco",(int) col_red); 
    }
  }
  old_load=load;
  
}