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