Programme BUS CAN

Dependents:   Prg_TP_CAN

Fork of Grove_LCD_RGB_Backlight by Chandler Matz

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Grove_LCD_RGB_Backlight.cpp Source File

Grove_LCD_RGB_Backlight.cpp

00001 #include "mbed.h"
00002 #include "Grove_LCD_RGB_Backlight.h"
00003 char _displayfunction;
00004 char _displaycontrol;
00005 
00006 Grove_LCD_RGB_Backlight::Grove_LCD_RGB_Backlight(PinName sda, PinName scl):i2c(sda, scl)
00007 {
00008     this->init();
00009 }
00010 
00011 /*This function is used to turn on the display on the LCD.  It sends the 
00012 hexadecimal code for the display on setting to the device in order to 
00013 turn on the display*/
00014 
00015 void Grove_LCD_RGB_Backlight::displayOn() 
00016 {
00017     _displaycontrol |= LCD_DISPLAYON;
00018     this->sendCommand(LCD_DISPLAYCONTROL | _displaycontrol);
00019 }
00020 
00021 /*This function is used to clear display on the LCD.  It sends the 
00022 hexadecimal code for the clear instruction to the device in order to 
00023 clear the display*/
00024 void Grove_LCD_RGB_Backlight::clear()
00025 {
00026     this->sendCommand(LCD_CLEARDISPLAY);        
00027     wait_us(2000);         
00028 }
00029 
00030 
00031 /*This function is used to set the backlight color of the dispaly.  
00032 It writes the provided r, g, and b values to the registers for the red
00033 value, the green value, and the blue value respectively.*/
00034 void Grove_LCD_RGB_Backlight::setRGB(char r, char g, char b)
00035 {
00036    
00037     this->setReg(RED_REG, r);
00038     this->setReg(GREEN_REG, g);
00039     this->setReg(BLUE_REG, b);  
00040     
00041 }
00042 
00043 /*This function is used to write to one of the registers for the backlight
00044 of the LCD display.  The function takes in the address of which register to
00045 write to and the value to be written to that register and then sends it to the
00046 LCD display via the mbed I2C library*/
00047 void Grove_LCD_RGB_Backlight::setReg(char addr, char val)
00048 {
00049     char data[2];
00050     data[0] = addr;
00051     data[1] = val;
00052     i2c.write(RGB_ADDRESS, data, 2);
00053 }
00054 
00055 /*This function is used to write to the LCD screen.  It takes in a string of
00056 characters and writes them to the 0x40 register of the display.*/
00057 void Grove_LCD_RGB_Backlight::print(char *str)
00058 {   
00059     char data[2];
00060     data[0] = 0x40;
00061     while(*str)
00062     {
00063             data[1] = *str;
00064             i2c.write(LCD_ADDRESS, data, 2);
00065             str++;
00066             
00067     }
00068 
00069 
00070 }
00071 
00072 
00073 /*This function sets where on the screen the text will be written next.  It 
00074 takes in two values which indicate the row and column on the display that 
00075 the cursor should be moved to*/
00076 void Grove_LCD_RGB_Backlight::locate(char col, char row)
00077 {
00078     if(row == 0)
00079     {
00080         col = col | 0x80;
00081     }
00082     else
00083     {   
00084         col = col | 0xc0;
00085     }
00086  
00087     char data[2];
00088     data[0] = 0x80;
00089     data[1] = col;
00090     i2c.write(LCD_ADDRESS, data, 2);
00091 }
00092 
00093 /*This function sends an instruction to the LCD display using the
00094  built in instruction codes from the device  datasheet using the mbed
00095  I2C library*/
00096 void Grove_LCD_RGB_Backlight::sendCommand(char value)
00097 {
00098     char data[2] = {0x80, value};
00099     i2c.write(LCD_ADDRESS, data, 2);
00100 }
00101 
00102 void Grove_LCD_RGB_Backlight::init() 
00103 {   
00104     //Initialize displayfunction parameter for setting up LCD display
00105    _displayfunction |= LCD_2LINE;
00106    _displayfunction |= LCD_5x10DOTS;
00107  
00108    //Wait for more than 30 ms after power rises above 4.5V per the data sheet
00109     wait_ms(50);
00110 
00111 
00112     // Send first function set command. Wait longer that 39 us per the data sheet
00113     sendCommand(LCD_FUNCTIONSET | _displayfunction);
00114     wait_us(45);  
00115     
00116     // turn the display on
00117     displayOn();
00118 
00119     // clear the display
00120     clear();
00121     
00122     // Initialize backlight
00123     setReg(0, 0);
00124     setReg(1, 0);
00125     setReg(0x08, 0xAA);   
00126     
00127 
00128 
00129     
00130 
00131 }
00132