Library to operate Grove-LCD RGB Backlight v4.0

Committer:
nono411
Date:
Mon Apr 15 07:44:45 2019 +0000
Revision:
0:fb21134ce1b9
RGB LCD Library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nono411 0:fb21134ce1b9 1 #include "rgb_lcd.h"
nono411 0:fb21134ce1b9 2 /*
nono411 0:fb21134ce1b9 3 rgb_lcd library
nono411 0:fb21134ce1b9 4 Created by: Isaac Maximiliano Martinez Govea
nono411 0:fb21134ce1b9 5 15/04/2019
nono411 0:fb21134ce1b9 6 */
nono411 0:fb21134ce1b9 7
nono411 0:fb21134ce1b9 8
nono411 0:fb21134ce1b9 9 /*
nono411 0:fb21134ce1b9 10 ////////////////////////////////////////////////////////////////////
nono411 0:fb21134ce1b9 11 Local comunication fonctions
nono411 0:fb21134ce1b9 12 ////////////////////////////////////////////////////////////////////
nono411 0:fb21134ce1b9 13 */
nono411 0:fb21134ce1b9 14 /*
nono411 0:fb21134ce1b9 15 Communication with the LCD driver
nono411 0:fb21134ce1b9 16 */
nono411 0:fb21134ce1b9 17 void rgb_lcd::command(uint8_t comm){
nono411 0:fb21134ce1b9 18 uint8_t data[2] = {0x80, comm}; // 0x80 : Configuration register
nono411 0:fb21134ce1b9 19 i2c_send_byteS(data,2);
nono411 0:fb21134ce1b9 20 }
nono411 0:fb21134ce1b9 21
nono411 0:fb21134ce1b9 22 /*
nono411 0:fb21134ce1b9 23 Communication with the RGB driver
nono411 0:fb21134ce1b9 24 */
nono411 0:fb21134ce1b9 25 void rgb_lcd::setReg(uint8_t reg, uint8_t value){
nono411 0:fb21134ce1b9 26 uint8_t data[2] = {reg,value};
nono411 0:fb21134ce1b9 27 i2c->write(RGB_ADDRESS, (char*)data, 2);
nono411 0:fb21134ce1b9 28 }
nono411 0:fb21134ce1b9 29 /*
nono411 0:fb21134ce1b9 30 Communication with the print register of the LCD
nono411 0:fb21134ce1b9 31 */
nono411 0:fb21134ce1b9 32 void rgb_lcd::write(uint8_t value){
nono411 0:fb21134ce1b9 33 uint8_t data[2] = {0x40, value}; // 0x40 : writing register
nono411 0:fb21134ce1b9 34 i2c_send_byteS(data, 2);
nono411 0:fb21134ce1b9 35 }
nono411 0:fb21134ce1b9 36 /*
nono411 0:fb21134ce1b9 37 Communication I2C
nono411 0:fb21134ce1b9 38 */
nono411 0:fb21134ce1b9 39 void rgb_lcd::i2c_send_byteS(uint8_t *data, uint8_t len){
nono411 0:fb21134ce1b9 40 i2c->write(LCD_ADDRESS, (char*) data, len);
nono411 0:fb21134ce1b9 41 }
nono411 0:fb21134ce1b9 42
nono411 0:fb21134ce1b9 43 ////////////////////////////////////////////////////////////////////////////////
nono411 0:fb21134ce1b9 44
nono411 0:fb21134ce1b9 45 /*
nono411 0:fb21134ce1b9 46 ////////////////////////////////////////////////////////////////////
nono411 0:fb21134ce1b9 47 User Fonctions
nono411 0:fb21134ce1b9 48 ////////////////////////////////////////////////////////////////////
nono411 0:fb21134ce1b9 49 */
nono411 0:fb21134ce1b9 50 /*
nono411 0:fb21134ce1b9 51 Constructeur
nono411 0:fb21134ce1b9 52 */
nono411 0:fb21134ce1b9 53 rgb_lcd::rgb_lcd(I2C* i2c_bus){
nono411 0:fb21134ce1b9 54 i2c = i2c_bus;
nono411 0:fb21134ce1b9 55 _displayfunction = 0x00;
nono411 0:fb21134ce1b9 56 _displaycontrol = 0x00;
nono411 0:fb21134ce1b9 57 _displaymode = 0x00;
nono411 0:fb21134ce1b9 58 }
nono411 0:fb21134ce1b9 59 /*
nono411 0:fb21134ce1b9 60 Initialize the LCD
nono411 0:fb21134ce1b9 61 */
nono411 0:fb21134ce1b9 62 void rgb_lcd::begin(){
nono411 0:fb21134ce1b9 63
nono411 0:fb21134ce1b9 64 _displayfunction |= LCD_2LINE; // We use a 2 line display
nono411 0:fb21134ce1b9 65 _displayfunction |= LCD_5X8DOTS; // We set the size of characters
nono411 0:fb21134ce1b9 66
nono411 0:fb21134ce1b9 67 wait_ms(50); // We wait for the power rise
nono411 0:fb21134ce1b9 68
nono411 0:fb21134ce1b9 69 //We send many times the command to guarantee the proper fonctioning of the LCD
nono411 0:fb21134ce1b9 70 command(LCD_FUNCTIONSET | _displayfunction);
nono411 0:fb21134ce1b9 71 wait_us(4500);
nono411 0:fb21134ce1b9 72 command(LCD_FUNCTIONSET | _displayfunction);
nono411 0:fb21134ce1b9 73 wait_us(150);
nono411 0:fb21134ce1b9 74 command(LCD_FUNCTIONSET | _displayfunction);
nono411 0:fb21134ce1b9 75 command(LCD_FUNCTIONSET | _displayfunction);
nono411 0:fb21134ce1b9 76
nono411 0:fb21134ce1b9 77 // We turn on the display without cursor
nono411 0:fb21134ce1b9 78 _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
nono411 0:fb21134ce1b9 79 display();
nono411 0:fb21134ce1b9 80 clear();
nono411 0:fb21134ce1b9 81
nono411 0:fb21134ce1b9 82 // We set up the text direction ( From left to right )
nono411 0:fb21134ce1b9 83 _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
nono411 0:fb21134ce1b9 84 command(LCD_ENTRYMODESET | _displaymode );
nono411 0:fb21134ce1b9 85 //We set up the backlight
nono411 0:fb21134ce1b9 86 setReg( REG_MODE1, 0); // We turn on the backlight
nono411 0:fb21134ce1b9 87 setReg( REG_OUTPUT, 0xFF); // We allow the control of the RGB
nono411 0:fb21134ce1b9 88 setReg( REG_MODE2, 0x20); // Blinky Mode
nono411 0:fb21134ce1b9 89 setRGB(255,255,255); //We set the white color
nono411 0:fb21134ce1b9 90 }
nono411 0:fb21134ce1b9 91 /*
nono411 0:fb21134ce1b9 92 Set backlight-color
nono411 0:fb21134ce1b9 93 */
nono411 0:fb21134ce1b9 94 void rgb_lcd::setRGB(uint8_t r, uint8_t g, uint8_t b){
nono411 0:fb21134ce1b9 95 setReg(REG_RED, r);
nono411 0:fb21134ce1b9 96 setReg(REG_GREEN, g);
nono411 0:fb21134ce1b9 97 setReg(REG_BLUE, b);
nono411 0:fb21134ce1b9 98 }
nono411 0:fb21134ce1b9 99 /*
nono411 0:fb21134ce1b9 100 Set cursor position
nono411 0:fb21134ce1b9 101 */
nono411 0:fb21134ce1b9 102 void rgb_lcd::setCursor(uint8_t col, uint8_t row){
nono411 0:fb21134ce1b9 103 col = (row == 0 ? col|0x80 : col|0xC0);
nono411 0:fb21134ce1b9 104 uint8_t data[2] = {0x80 , col};
nono411 0:fb21134ce1b9 105 i2c_send_byteS(data, 2);
nono411 0:fb21134ce1b9 106 }
nono411 0:fb21134ce1b9 107 /*
nono411 0:fb21134ce1b9 108 Display text
nono411 0:fb21134ce1b9 109 */
nono411 0:fb21134ce1b9 110 void rgb_lcd::display(){
nono411 0:fb21134ce1b9 111 _displaycontrol |= LCD_DISPLAYON;
nono411 0:fb21134ce1b9 112 command(LCD_DISPLAYCONTROL | _displaycontrol);
nono411 0:fb21134ce1b9 113 }
nono411 0:fb21134ce1b9 114 /*
nono411 0:fb21134ce1b9 115 Clear display
nono411 0:fb21134ce1b9 116 */
nono411 0:fb21134ce1b9 117 void rgb_lcd::clear(){
nono411 0:fb21134ce1b9 118 command(LCD_CLEARDISPLAY); // Clear and set cursor to position zero
nono411 0:fb21134ce1b9 119 wait_ms(2);
nono411 0:fb21134ce1b9 120 }
nono411 0:fb21134ce1b9 121 ////////////////////////////////////////////////////////////////////////////////
nono411 0:fb21134ce1b9 122
nono411 0:fb21134ce1b9 123 /*
nono411 0:fb21134ce1b9 124 ////////////////////////////////////////////////////////////////////
nono411 0:fb21134ce1b9 125 FONCTIONS TO ENABLE PRINTF FROM STREAM
nono411 0:fb21134ce1b9 126 ////////////////////////////////////////////////////////////////////
nono411 0:fb21134ce1b9 127 */
nono411 0:fb21134ce1b9 128
nono411 0:fb21134ce1b9 129 int rgb_lcd::_putc(int c){
nono411 0:fb21134ce1b9 130 write((uint8_t)c);
nono411 0:fb21134ce1b9 131 return 0;
nono411 0:fb21134ce1b9 132 }
nono411 0:fb21134ce1b9 133 int rgb_lcd::_getc(){
nono411 0:fb21134ce1b9 134 return -1;
nono411 0:fb21134ce1b9 135 }