From the repository of Chandler Matz, I add a fix to be compliant with mbed6 (wait_ms doesn't exist any more)
Revision 0:86fab34f85f1, committed 2021-11-10
- Comitter:
- Sebastien Prouff
- Date:
- Wed Nov 10 18:47:43 2021 +0100
- Commit message:
- initial commit
Changed in this revision
Grove_LCD_RGB_Backlight.cpp | Show annotated file Show diff for this revision Revisions of this file |
Grove_LCD_RGB_Backlight.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 86fab34f85f1 Grove_LCD_RGB_Backlight.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Grove_LCD_RGB_Backlight.cpp Wed Nov 10 18:47:43 2021 +0100 @@ -0,0 +1,132 @@ +#include "mbed.h" +#include "Grove_LCD_RGB_Backlight.h" +char _displayfunction; +char _displaycontrol; + +Grove_LCD_RGB_Backlight::Grove_LCD_RGB_Backlight(PinName sda, PinName scl):i2c(sda, scl) +{ + this->init(); +} + +/*This function is used to turn on the display on the LCD. It sends the +hexadecimal code for the display on setting to the device in order to +turn on the display*/ + +void Grove_LCD_RGB_Backlight::displayOn() +{ + _displaycontrol |= LCD_DISPLAYON; + this->sendCommand(LCD_DISPLAYCONTROL | _displaycontrol); +} + +/*This function is used to clear display on the LCD. It sends the +hexadecimal code for the clear instruction to the device in order to +clear the display*/ +void Grove_LCD_RGB_Backlight::clear() +{ + this->sendCommand(LCD_CLEARDISPLAY); + wait_us(2000); +} + + +/*This function is used to set the backlight color of the dispaly. +It writes the provided r, g, and b values to the registers for the red +value, the green value, and the blue value respectively.*/ +void Grove_LCD_RGB_Backlight::setRGB(char r, char g, char b) +{ + + this->setReg(RED_REG, r); + this->setReg(GREEN_REG, g); + this->setReg(BLUE_REG, b); + +} + +/*This function is used to write to one of the registers for the backlight +of the LCD display. The function takes in the address of which register to +write to and the value to be written to that register and then sends it to the +LCD display via the mbed I2C library*/ +void Grove_LCD_RGB_Backlight::setReg(char addr, char val) +{ + char data[2]; + data[0] = addr; + data[1] = val; + i2c.write(RGB_ADDRESS, data, 2); +} + +/*This function is used to write to the LCD screen. It takes in a string of +characters and writes them to the 0x40 register of the display.*/ +void Grove_LCD_RGB_Backlight::print(char *str) +{ + char data[2]; + data[0] = 0x40; + while(*str) + { + data[1] = *str; + i2c.write(LCD_ADDRESS, data, 2); + str++; + + } + + +} + + +/*This function sets where on the screen the text will be written next. It +takes in two values which indicate the row and column on the display that +the cursor should be moved to*/ +void Grove_LCD_RGB_Backlight::locate(char col, char row) +{ + if(row == 0) + { + col = col | 0x80; + } + else + { + col = col | 0xc0; + } + + char data[2]; + data[0] = 0x80; + data[1] = col; + i2c.write(LCD_ADDRESS, data, 2); +} + +/*This function sends an instruction to the LCD display using the + built in instruction codes from the device datasheet using the mbed + I2C library*/ +void Grove_LCD_RGB_Backlight::sendCommand(char value) +{ + char data[2] = {0x80, value}; + i2c.write(LCD_ADDRESS, data, 2); +} + +void Grove_LCD_RGB_Backlight::init() +{ + //Initialize displayfunction parameter for setting up LCD display + _displayfunction |= LCD_2LINE; + _displayfunction |= LCD_5x10DOTS; + + //Wait for more than 30 ms after power rises above 4.5V per the data sheet + //wait_ms(50); + wait_us(50000); + + // Send first function set command. Wait longer that 39 us per the data sheet + sendCommand(LCD_FUNCTIONSET | _displayfunction); + wait_us(45); + + // turn the display on + displayOn(); + + // clear the display + clear(); + + // Initialize backlight + setReg(0, 0); + setReg(1, 0); + setReg(0x08, 0xAA); + + + + + +} +
diff -r 000000000000 -r 86fab34f85f1 Grove_LCD_RGB_Backlight.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Grove_LCD_RGB_Backlight.h Wed Nov 10 18:47:43 2021 +0100 @@ -0,0 +1,85 @@ +#include "mbed.h" + +// I2C addresses for LCD and RGB +#define LCD_ADDRESS (0x7c) +#define RGB_ADDRESS (0xc4) + +#define RED_REG 0x04 +#define GREEN_REG 0x03 +#define BLUE_REG 0x02 + +// commands +#define LCD_CLEARDISPLAY 0x01 +#define LCD_DISPLAYCONTROL 0x08 +#define LCD_FUNCTIONSET 0x20 + +// flags for display on/off control +#define LCD_DISPLAYON 0x04 +#define LCD_DISPLAYOFF 0x00 + +// flag for entry mode +#define LCD_ENTRYLEFT 0x02 + +// flags for function set +#define LCD_8BITMODE 0x10 +#define LCD_2LINE 0x08 +#define LCD_5x10DOTS 0x04 + +/** Grove_LCD_RGB_Backlight Class. +* Used for connecting a Grove LCD RGB Backlit display +* to an mbed microcontroller via an I2C interface. +*/ +class Grove_LCD_RGB_Backlight +{ +public: + + /** Contructor. Creates an instance of the Grove_LCD_RGB_Backlight class. + * @param sda SDA pin on the mbed microcontroller which will be used to communicate with the display. + * @param scl SCL pin on the mbed microcontroller which will be used to communicate with the display. + */ + Grove_LCD_RGB_Backlight(PinName sda, PinName scl); + + + + /** Set RGB color of backlight + * @param r Value for the red component of the RGB backlight (Between 0 and 255). + * @param g Value for the green component of the RGB backlight (Between 0 and 255). + * @param b Value for the blue component of the RGB backlight (Between 0 and 255). + */ + void setRGB(char r, char g, char b); + + + /** Removes all of the text from the display. + */ + void clear(); + + /**Prints text to the LCD display. + * @param *str Pointer to an array of characters which will be printed to the LCD screen. + */ + void print(char *str); + + /**Move cursor to specified location on the LCD screen. + * @param col Value for which column on the display the next text being printed will start at. + * @param row Value for which row on the display the next text being printed will be printed on. + */ + void locate(char col, char row); + + +private: + + //Initialize device + void init(); + + //Turn on display + void displayOn(); + + //Send command to display + void sendCommand(char value); + + //Set register value + void setReg(char addr, char val); + + //MBED I2C object used to transfer data to LCD + I2C i2c; + +}; \ No newline at end of file