Now lets write some methods to drive the LCD controller, first of all the JHD1313 datasheet (Grove for some reason puts a link to the datasheet of JHD1214) is not too clear, but after some time you will realize that to init the device you can write to register 0x00 if you want to write to CGRAM AD you need to write to register 0x40 and to write information to DDRAM you need to write to register 0x80 (take a look to the JHD1214 data sheet).
So lets start adding some defines we will need:
// commands#defineLCD_CLEARDISPLAY0x01#defineLCD_RETURNHOME0x02#defineLCD_ENTRYMODESET0x04#defineLCD_DISPLAYCONTROL0x08#defineLCD_CURSORSHIFT0x10#defineLCD_FUNCTIONSET0x20#defineLCD_SETCGRAMADDR0x40#defineLCD_SETDDRAMADDR0x80// flags for display entry mode#defineLCD_ENTRYRIGHT0x00#defineLCD_ENTRYLEFT0x02#defineLCD_ENTRYSHIFTINCREMENT0x01#defineLCD_ENTRYSHIFTDECREMENT0x00// flags for display on/off control#defineLCD_DISPLAYON0x04#defineLCD_DISPLAYOFF0x00#defineLCD_CURSORON0x02#defineLCD_CURSOROFF0x00#defineLCD_BLINKON0x01#defineLCD_BLINKOFF0x00// flags for display/cursor shift#defineLCD_DISPLAYMOVE0x08#defineLCD_CURSORMOVE0x00#defineLCD_MOVERIGHT0x04#defineLCD_MOVELEFT0x00// flags for function set#defineLCD_8BITMODE0x10#defineLCD_4BITMODE0x00#defineLCD_2LINE0x08#defineLCD_5x8DOTS0x00//////////////////////////////////////////////////////////uint8_t _displayfunction =0;uint8_t _displaycontrol =0;uint8_t _displaymode =0;uint8_t lines =2;
Now add an init method to setup our LCD:
voidinitLCD(I2CCONTEXT *lcd){//set the number of lines of our Display ////////////////////////////////////////////////////////////////////writeByteRegister(lcd->file,0x00, LCD_FUNCTIONSET | LCD_2LINE );usleep(100);//enable display, show cursor, show a blinking box cursor ////////////////////////////////////////////////////////////////////writeByteRegister(lcd->file,0x00,LCD_DISPLAYCONTROL | LCD_DISPLAYON | LCD_CURSORON | LCD_BLINKON );usleep(100);//clear display and set cursor at 0 position ////////////////////////////////////////////////////////////////////writeByteRegister(lcd->file,0x00, LCD_CLEARDISPLAY );usleep(4000);//set how the cursor will move everytime a letter is written to the LCD ////////////////////////////////////////////////////////////////////////writeByteRegister(lcd->file,0x00,LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT );}
Of course we need a function we can pass a c string to it , then iterate it and pass each letter to the LCD. for that lets add the following method:
voidwriteToLCD(I2CCONTEXT *lcd,char*c){uint ascii_val=0;int i;int s =strlen(c);for(i =0 ; i < s; i++) { ascii_val =toascii(c[i]); /////////////////////////////////////////////////////////////sending a 0x40 tell the LCD we will send a character code//to CGRAM ///////////////////////////////////////////////////////////writeByteRegister(lcd->file,0x40, ascii_val); }}
Alright! now what we have left is to update our main method:
//Grove LCD has two devices one that drives LCD//and a second that drives RGBinitContext(&rgb, RGB_SLAVE , BUS);initContext(&lcd, LCD_SLAVE , BUS); //////////////////////////////////////////////////////////////init RGB device & set White colorinitLCD(&lcd);initRGB(&rgb); writeToLCD(&lcd,":) HOLO!"); /*sleep for 5 secs before turning off*/sleep(5); /*turn off RGB LEDS*/turnOffRGB(&rgb);printf("\nDONE!\n");