Grove LCD Library

Committer:
DavidElmoRoss
Date:
Thu Dec 31 21:15:50 2020 +0000
Revision:
3:3946b6e87adc
Parent:
0:253885b1f364
Bluefruit Color Picker

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cmatz3 0:253885b1f364 1 #include "mbed.h"
cmatz3 0:253885b1f364 2 #include "Grove_LCD_RGB_Backlight.h"
cmatz3 0:253885b1f364 3 char _displayfunction;
cmatz3 0:253885b1f364 4 char _displaycontrol;
cmatz3 0:253885b1f364 5
cmatz3 0:253885b1f364 6 Grove_LCD_RGB_Backlight::Grove_LCD_RGB_Backlight(PinName sda, PinName scl):i2c(sda, scl)
cmatz3 0:253885b1f364 7 {
cmatz3 0:253885b1f364 8 this->init();
cmatz3 0:253885b1f364 9 }
cmatz3 0:253885b1f364 10
cmatz3 0:253885b1f364 11 /*This function is used to turn on the display on the LCD. It sends the
cmatz3 0:253885b1f364 12 hexadecimal code for the display on setting to the device in order to
cmatz3 0:253885b1f364 13 turn on the display*/
cmatz3 0:253885b1f364 14
cmatz3 0:253885b1f364 15 void Grove_LCD_RGB_Backlight::displayOn()
cmatz3 0:253885b1f364 16 {
cmatz3 0:253885b1f364 17 _displaycontrol |= LCD_DISPLAYON;
cmatz3 0:253885b1f364 18 this->sendCommand(LCD_DISPLAYCONTROL | _displaycontrol);
cmatz3 0:253885b1f364 19 }
cmatz3 0:253885b1f364 20
cmatz3 0:253885b1f364 21 /*This function is used to clear display on the LCD. It sends the
cmatz3 0:253885b1f364 22 hexadecimal code for the clear instruction to the device in order to
cmatz3 0:253885b1f364 23 clear the display*/
cmatz3 0:253885b1f364 24 void Grove_LCD_RGB_Backlight::clear()
cmatz3 0:253885b1f364 25 {
cmatz3 0:253885b1f364 26 this->sendCommand(LCD_CLEARDISPLAY);
cmatz3 0:253885b1f364 27 wait_us(2000);
cmatz3 0:253885b1f364 28 }
cmatz3 0:253885b1f364 29
cmatz3 0:253885b1f364 30
cmatz3 0:253885b1f364 31 /*This function is used to set the backlight color of the dispaly.
cmatz3 0:253885b1f364 32 It writes the provided r, g, and b values to the registers for the red
cmatz3 0:253885b1f364 33 value, the green value, and the blue value respectively.*/
cmatz3 0:253885b1f364 34 void Grove_LCD_RGB_Backlight::setRGB(char r, char g, char b)
cmatz3 0:253885b1f364 35 {
cmatz3 0:253885b1f364 36
cmatz3 0:253885b1f364 37 this->setReg(RED_REG, r);
cmatz3 0:253885b1f364 38 this->setReg(GREEN_REG, g);
cmatz3 0:253885b1f364 39 this->setReg(BLUE_REG, b);
cmatz3 0:253885b1f364 40
cmatz3 0:253885b1f364 41 }
cmatz3 0:253885b1f364 42
cmatz3 0:253885b1f364 43 /*This function is used to write to one of the registers for the backlight
cmatz3 0:253885b1f364 44 of the LCD display. The function takes in the address of which register to
cmatz3 0:253885b1f364 45 write to and the value to be written to that register and then sends it to the
cmatz3 0:253885b1f364 46 LCD display via the mbed I2C library*/
cmatz3 0:253885b1f364 47 void Grove_LCD_RGB_Backlight::setReg(char addr, char val)
cmatz3 0:253885b1f364 48 {
cmatz3 0:253885b1f364 49 char data[2];
cmatz3 0:253885b1f364 50 data[0] = addr;
cmatz3 0:253885b1f364 51 data[1] = val;
cmatz3 0:253885b1f364 52 i2c.write(RGB_ADDRESS, data, 2);
cmatz3 0:253885b1f364 53 }
cmatz3 0:253885b1f364 54
cmatz3 0:253885b1f364 55 /*This function is used to write to the LCD screen. It takes in a string of
cmatz3 0:253885b1f364 56 characters and writes them to the 0x40 register of the display.*/
cmatz3 0:253885b1f364 57 void Grove_LCD_RGB_Backlight::print(char *str)
cmatz3 0:253885b1f364 58 {
cmatz3 0:253885b1f364 59 char data[2];
cmatz3 0:253885b1f364 60 data[0] = 0x40;
cmatz3 0:253885b1f364 61 while(*str)
cmatz3 0:253885b1f364 62 {
cmatz3 0:253885b1f364 63 data[1] = *str;
cmatz3 0:253885b1f364 64 i2c.write(LCD_ADDRESS, data, 2);
cmatz3 0:253885b1f364 65 str++;
cmatz3 0:253885b1f364 66
cmatz3 0:253885b1f364 67 }
cmatz3 0:253885b1f364 68
cmatz3 0:253885b1f364 69
cmatz3 0:253885b1f364 70 }
DavidElmoRoss 3:3946b6e87adc 71 void Grove_LCD_RGB_Backlight::write(char data1)
DavidElmoRoss 3:3946b6e87adc 72 {
DavidElmoRoss 3:3946b6e87adc 73 char data[2];
DavidElmoRoss 3:3946b6e87adc 74 data[0]=0x40;
DavidElmoRoss 3:3946b6e87adc 75 data[1]=(data1 >>4)+0x30;
DavidElmoRoss 3:3946b6e87adc 76 i2c.write(LCD_ADDRESS,data,2);
DavidElmoRoss 3:3946b6e87adc 77 data[1]=(data1&0x0f)+0x30;
DavidElmoRoss 3:3946b6e87adc 78 i2c.write(LCD_ADDRESS,data,2);
DavidElmoRoss 3:3946b6e87adc 79 }
cmatz3 0:253885b1f364 80
DavidElmoRoss 3:3946b6e87adc 81 void Grove_LCD_RGB_Backlight::writech(char data2)
DavidElmoRoss 3:3946b6e87adc 82 {
DavidElmoRoss 3:3946b6e87adc 83 char data[2];
DavidElmoRoss 3:3946b6e87adc 84 data[0]=0x40;
DavidElmoRoss 3:3946b6e87adc 85 data[1]=data2;
DavidElmoRoss 3:3946b6e87adc 86 i2c.write(LCD_ADDRESS,data,2);
DavidElmoRoss 3:3946b6e87adc 87
DavidElmoRoss 3:3946b6e87adc 88 }
cmatz3 0:253885b1f364 89
cmatz3 0:253885b1f364 90 /*This function sets where on the screen the text will be written next. It
cmatz3 0:253885b1f364 91 takes in two values which indicate the row and column on the display that
cmatz3 0:253885b1f364 92 the cursor should be moved to*/
cmatz3 0:253885b1f364 93 void Grove_LCD_RGB_Backlight::locate(char col, char row)
cmatz3 0:253885b1f364 94 {
cmatz3 0:253885b1f364 95 if(row == 0)
cmatz3 0:253885b1f364 96 {
cmatz3 0:253885b1f364 97 col = col | 0x80;
cmatz3 0:253885b1f364 98 }
cmatz3 0:253885b1f364 99 else
cmatz3 0:253885b1f364 100 {
cmatz3 0:253885b1f364 101 col = col | 0xc0;
cmatz3 0:253885b1f364 102 }
cmatz3 0:253885b1f364 103
cmatz3 0:253885b1f364 104 char data[2];
cmatz3 0:253885b1f364 105 data[0] = 0x80;
cmatz3 0:253885b1f364 106 data[1] = col;
cmatz3 0:253885b1f364 107 i2c.write(LCD_ADDRESS, data, 2);
cmatz3 0:253885b1f364 108 }
cmatz3 0:253885b1f364 109
cmatz3 0:253885b1f364 110 /*This function sends an instruction to the LCD display using the
cmatz3 0:253885b1f364 111 built in instruction codes from the device datasheet using the mbed
cmatz3 0:253885b1f364 112 I2C library*/
cmatz3 0:253885b1f364 113 void Grove_LCD_RGB_Backlight::sendCommand(char value)
cmatz3 0:253885b1f364 114 {
cmatz3 0:253885b1f364 115 char data[2] = {0x80, value};
cmatz3 0:253885b1f364 116 i2c.write(LCD_ADDRESS, data, 2);
cmatz3 0:253885b1f364 117 }
cmatz3 0:253885b1f364 118
cmatz3 0:253885b1f364 119 void Grove_LCD_RGB_Backlight::init()
cmatz3 0:253885b1f364 120 {
cmatz3 0:253885b1f364 121 //Initialize displayfunction parameter for setting up LCD display
cmatz3 0:253885b1f364 122 _displayfunction |= LCD_2LINE;
cmatz3 0:253885b1f364 123 _displayfunction |= LCD_5x10DOTS;
cmatz3 0:253885b1f364 124
cmatz3 0:253885b1f364 125 //Wait for more than 30 ms after power rises above 4.5V per the data sheet
cmatz3 0:253885b1f364 126 wait_ms(50);
cmatz3 0:253885b1f364 127
cmatz3 0:253885b1f364 128
cmatz3 0:253885b1f364 129 // Send first function set command. Wait longer that 39 us per the data sheet
cmatz3 0:253885b1f364 130 sendCommand(LCD_FUNCTIONSET | _displayfunction);
cmatz3 0:253885b1f364 131 wait_us(45);
cmatz3 0:253885b1f364 132
cmatz3 0:253885b1f364 133 // turn the display on
cmatz3 0:253885b1f364 134 displayOn();
cmatz3 0:253885b1f364 135
cmatz3 0:253885b1f364 136 // clear the display
cmatz3 0:253885b1f364 137 clear();
cmatz3 0:253885b1f364 138
cmatz3 0:253885b1f364 139 // Initialize backlight
cmatz3 0:253885b1f364 140 setReg(0, 0);
cmatz3 0:253885b1f364 141 setReg(1, 0);
cmatz3 0:253885b1f364 142 setReg(0x08, 0xAA);
DavidElmoRoss 3:3946b6e87adc 143
cmatz3 0:253885b1f364 144 }
cmatz3 0:253885b1f364 145