//LCD functions
//*************************************
//Comment unneeded out as required 
//Select interface
#define lcdi4           //4 bit interface
//#define lcdi8         //8 bit interface

//Select amount of lines
//#define lcd1l         //1 line display
#define lcd2l           //2 line display
//#define lcd4l         //4 line display

//Select RW connection
#define lcd_use_rw    //use RW, ie not tied to ground

//Define hardware below to match your circuit
#define lcd_db7   pin_b7
#define lcd_db6   pin_b6
#define lcd_db5   pin_b5
#define lcd_db4   pin_b4

#ifdef lcdi8
   #define lcd_db3   pin_b3
   #define lcd_db2   pin_b2
   #define lcd_db1   pin_b1
   #define lcd_db0   pin_b0
#endif
   
#define lcd_e     pin_b3
#define lcd_rs    pin_b1

#ifdef lcd_use_rw
   #define lcd_rw   pin_b2
#endif
//*************************************

#define lcd_4i_1line    0x20     //1 line, 4 bit interface
#define lcd_8i_1line    0x30     //1 line, 8 bit interface
#define lcd_4i_2line    0x28     //2/4 line, 4 bit interface
#define lcd_8i_2line    0x38     //2/4 line, 8 bit interface
#define lcd_line1       0x80     //line 1 position 1
#define lcd_line2       0xc0     //line 2 position 1
#define lcd_line3       0x94     //line 3 position 1
#define lcd_line4       0xd4     //line 4 position 1
#define lcd_cls         0x01     //clear display, go home, no shift
#define lcd_home        0x02     //go home, noshift
#define lcd_doff        0x08     //display off
#define lcd_donnc       0x0c     //display on, cursor off
#define lcd_donb        0x0d     //display on, cursor off, blink on
#define lcd_doco        0x0e     //display on, cursor on
#define lcd_docb        0x0f     //display on, cursor on, blink on 
#define lcd_sfdc        0x04     //decrement DD RAM address
#define lcd_shdl        0x05     //shift display left
#define lcd_sfic        0x06     //increment DD RAM address
#define lcd_shdr        0x07     //shifts entire display right
#define lcd_cleft       0x10     //decrement cursor
#define lcd_cright      0x14     //increment cursor
#define lcd_dleft       0x18     //shift whole display left
#define lcd_dright      0x1c     //shift whole display right
#define lcd_cgb         0x40     //CG RAM base address
#define lcd_ddb         0x80     //DD RAM base address

int defchar1[8]={0x04,0x0e,0x15,0x15,0x04,0,0,0};        //Up arrow
int defchar2[8]={0,0,0x04,0x04,0x15,0x0e,0x04,0};        //Down arrow
int defchar3[8]={0x1b,0x1b,0,0x04,0,0x11,0x0e,0};        //Face
int defchar4[8]={0x1C,0x14,0x1C,0,0,0,0,0};              //Degree symbol
int defchar5[8]={0x08,0x14,0x14,0x08,0x05,0x06,0x05,0};  //OK
int defchar6[8]={0x11,0x12,0x17,0x09,0x13,0x04,0x07,0};  //1/2
int defchar7[8]={0x0e,0x11,0x11,0x1f,0x1b,0x1b,0x1f,0};  //close lock
int defchar8[8]={0x0e,0x10,0x10,0x1f,0x1b,0x1b,0x1f,0};  //open lock

#define arrowup   8  //Arrow up char \0 is NULL!!!!!
#define arrowdwn  1  //Arrow down char
#define aface     2  //Face (that smile?)
#define degsym    3  //Degree sign
#define okchar    4  //OK char
#define oneontwo  5  //1 on top of 2 char
#define lockclose 6  //Closed padlock
#define lockopen  7  //Opened padlock

int lcd_ln;    //line number of current character
int lcd_data;  //data to and from LCD
#bit lcdd7=lcd_data.7
#bit lcdd6=lcd_data.6
#bit lcdd5=lcd_data.5
#bit lcdd4=lcd_data.4
#bit lcdd3=lcd_data.3
#bit lcdd2=lcd_data.2
#bit lcdd1=lcd_data.1
#bit lcdd0=lcd_data.0
#bit lcdbf=lcd_data.7

//Send a byte to the LCD module
//rs=0=command rs=1=character rs=2=4bit command
void lcd_send_byte(int rs, int data) {
   lcd_data=data;
   output_low(lcd_rs); 
   delay_us(80);  
   if(rs==1)output_high(lcd_rs); 
      else output_low(lcd_rs); 
   delay_us(1); 
#ifdef lcd_use_rw
   output_low(lcd_rw); 
   delay_us(1); 
#endif
   output_low(lcd_e);
   output_bit(lcd_db7,lcdd7);
   output_bit(lcd_db6,lcdd6);
   output_bit(lcd_db5,lcdd5);
   output_bit(lcd_db4,lcdd4);
   if(rs==2)goto nibble_only;
#ifdef lcdi4  
   delay_us(1); 
       output_high(lcd_e); 
delay_us(2); 
       output_low(lcd_e);
   output_bit(lcd_db7,lcdd3);
   output_bit(lcd_db6,lcdd2);
   output_bit(lcd_db5,lcdd1);
   output_bit(lcd_db4,lcdd0);
#endif
#ifdef lcdi8
   output_bit(lcd_db3,lcdd3);
   output_bit(lcd_db2,lcdd2);
   output_bit(lcd_db1,lcdd1);
   output_bit(lcd_db0,lcdd0);
#endif
nibble_only:   
   delay_us(1); 
       output_high(lcd_e); 
       delay_us(2); 
       output_low(lcd_e); 
}

//Read a byte from the LCD module
//Returns 0 if RW=0 (connected to ground)
int lcd_read_byte(void){ 
   lcd_data=0;
#ifdef lcd_use_rw
   output_high(lcd_rw); 
   delay_us(1); 
   output_high(lcd_e); 
   delay_us(1); 
   lcdd4 = input(lcd_db4); 
   lcdd5 = input(lcd_db5); 
   lcdd6 = input(lcd_db6); 
   lcdd7 = input(lcd_db7); 
#ifdef lcdi4
   output_low(lcd_e);
   delay_us(1); 
   output_high(lcd_e); 
   delay_us(1);   
   lcdd0 = input(lcd_db4); 
   lcdd1 = input(lcd_db5); 
   lcdd2 = input(lcd_db6); 
   lcdd3 = input(lcd_db7); 
#endif
#ifdef lcdi8
   lcdd3 = input(lcd_db3); 
   lcdd2 = input(lcd_db2); 
   lcdd1 = input(lcd_db1); 
   lcdd0 = input(lcd_db0); 
   #endif
   output_low(lcd_e); 
#endif
return(lcd_data); 
} 

//Initialize LCD module
//!!! Must be executed before using the CLCD !!!
void lcd_init(void){
   output_low(lcd_rs); 
   output_low(lcd_e); 
#ifdef lcd_use_rw
   output_low(lcd_rw); 
#endif
   delay_ms(15); 
#ifdef lcdi4
   lcd_send_byte(2,0x03);
   delay_ms(5);
   lcd_send_byte(2,0x03);
   delay_us(250); 
   lcd_send_byte(2,0x03);
   delay_ms(250); 
   lcd_send_byte(2,0x02);
#ifdef lcd_1l
   lcd_send_byte(0,lcd_4i_1line);
#endif
#ifdef lcd_2l
   lcd_send_byte(0,lcd_4i_2line);
#endif
#ifdef lcd_4l
   lcd_send_byte(0,lcd_4i_2line);
#endif
#endif
#ifdef lcdi8
   lcd_send_byte(0,0x30);
   delay_ms(5);
   lcd_send_byte(0,0x30);
   delay_us(250); 
   lcd_send_byte(0,0x30);
#ifdef lcd_1l
   lcd_send_byte(0,lcd_8i_1line);
#endif
#ifdef lcd_2l
   lcd_send_byte(lcd_8i_2line);
#endif
#ifdef lcd_4l
   lcd_send_byte(lcd_8i_2line);
#endif
#endif
   lcd_send_byte(0,lcd_donnc);      //display on, no cursor
   lcd_send_byte(0,lcd_sfic);       //auto incr cursor
   lcd_send_byte(0,lcd_cls);        //clear screen and go home
   delay_ms(5); 
}

//Write user chars to CGRAM
void lcd_send_user(){
   int i;
   lcd_send_byte(0,0x40);     //CGRAM start = user defined char 1
   for(i=0;i<=7;i++){
      lcd_send_byte(1,defchar1[i]);}
   for(i=0;i<=7;i++){
      lcd_send_byte(1,defchar2[i]);}
   for(i=0;i<=7;i++){
      lcd_send_byte(1,defchar3[i]);}
   for(i=0;i<=7;i++){
      lcd_send_byte(1,defchar4[i]);}
   for(i=0;i<=7;i++){
      lcd_send_byte(1,defchar5[i]);}
   for(i=0;i<=7;i++){
      lcd_send_byte(1,defchar6[i]);}
   for(i=0;i<=7;i++){
      lcd_send_byte(1,defchar7[i]);}
   for(i=0;i<=7;i++){
      lcd_send_byte(1,defchar8[i]);}
}

//Define on-the-fly user character 0
//Cursor (row7) defaults to 0
//Used for animation
void lcd_def_char(int r0,int r1,int r2,int r3,int r4,int r5,int r6){
   lcd_send_byte(0,0x40);     //CGRAM start
   lcd_send_byte(1,r0);
   lcd_send_byte(1,r1);
   lcd_send_byte(1,r2);
   lcd_send_byte(1,r3);
   lcd_send_byte(1,r4);
   lcd_send_byte(1,r5);
   lcd_send_byte(1,r6);
   lcd_send_byte(1,0);     //Row 7 is for cursor
}

//Change display position for next character to be written
//x=position in line y
void lcd_goto_pos(int x, int y){ 
   int pos; 
   switch(y){ 
#ifdef lcd1l
      case 1:pos=lcd_line1; break; 
#endif      
#ifdef lcd2l
      case 1:pos=lcd_line1; break;
      case 2:pos=lcd_line2; break;
#endif
#ifdef lcd4l
      case 1:pos=lcd_line1; break;
      case 2:pos=lcd_line2; break;
      case 3:pos=lcd_line3; break;
      case 4:pos=lcd_line4; break; 
#endif
   }
   pos+=(x-1); 
   lcd_send_byte(0,lcd_ddb|pos); 
}

//Finally, the routine to write to the display
//Usage: lcd_putc('X') or lcd_putc("\fLine 1\nLine 2\nLine 3\nLine 4")
void lcd_putc(int x){ 
   switch(x) { 
      case '\f':lcd_send_byte(0,lcd_cls); lcd_ln=1; delay_ms(2); break; 
      case '\n':lcd_goto_pos(1,++lcd_ln); break; 
      case '\b':lcd_send_byte(0,lcd_cleft); break;
      default:lcd_send_byte(1,x); break; 
   } 
} 

