Grove LCD Library
Embed:
(wiki syntax)
Show/hide line numbers
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 void Grove_LCD_RGB_Backlight::write(char data1) 00072 { 00073 char data[2]; 00074 data[0]=0x40; 00075 data[1]=(data1 >>4)+0x30; 00076 i2c.write(LCD_ADDRESS,data,2); 00077 data[1]=(data1&0x0f)+0x30; 00078 i2c.write(LCD_ADDRESS,data,2); 00079 } 00080 00081 void Grove_LCD_RGB_Backlight::writech(char data2) 00082 { 00083 char data[2]; 00084 data[0]=0x40; 00085 data[1]=data2; 00086 i2c.write(LCD_ADDRESS,data,2); 00087 00088 } 00089 00090 /*This function sets where on the screen the text will be written next. It 00091 takes in two values which indicate the row and column on the display that 00092 the cursor should be moved to*/ 00093 void Grove_LCD_RGB_Backlight::locate(char col, char row) 00094 { 00095 if(row == 0) 00096 { 00097 col = col | 0x80; 00098 } 00099 else 00100 { 00101 col = col | 0xc0; 00102 } 00103 00104 char data[2]; 00105 data[0] = 0x80; 00106 data[1] = col; 00107 i2c.write(LCD_ADDRESS, data, 2); 00108 } 00109 00110 /*This function sends an instruction to the LCD display using the 00111 built in instruction codes from the device datasheet using the mbed 00112 I2C library*/ 00113 void Grove_LCD_RGB_Backlight::sendCommand(char value) 00114 { 00115 char data[2] = {0x80, value}; 00116 i2c.write(LCD_ADDRESS, data, 2); 00117 } 00118 00119 void Grove_LCD_RGB_Backlight::init() 00120 { 00121 //Initialize displayfunction parameter for setting up LCD display 00122 _displayfunction |= LCD_2LINE; 00123 _displayfunction |= LCD_5x10DOTS; 00124 00125 //Wait for more than 30 ms after power rises above 4.5V per the data sheet 00126 wait_ms(50); 00127 00128 00129 // Send first function set command. Wait longer that 39 us per the data sheet 00130 sendCommand(LCD_FUNCTIONSET | _displayfunction); 00131 wait_us(45); 00132 00133 // turn the display on 00134 displayOn(); 00135 00136 // clear the display 00137 clear(); 00138 00139 // Initialize backlight 00140 setReg(0, 0); 00141 setReg(1, 0); 00142 setReg(0x08, 0xAA); 00143 00144 } 00145
Generated on Tue Jul 12 2022 11:17:42 by
1.7.2
David Ross