Library to operate Grove-LCD RGB Backlight v4.0

Files at this revision

API Documentation at this revision

Comitter:
nono411
Date:
Mon Apr 15 07:44:45 2019 +0000
Commit message:
RGB LCD Library

Changed in this revision

rgb_lcd.cpp Show annotated file Show diff for this revision Revisions of this file
rgb_lcd.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rgb_lcd.cpp	Mon Apr 15 07:44:45 2019 +0000
@@ -0,0 +1,135 @@
+#include "rgb_lcd.h"
+/*
+    rgb_lcd library
+    Created by: Isaac Maximiliano Martinez Govea
+    15/04/2019
+*/
+
+
+/*
+        ////////////////////////////////////////////////////////////////////
+                         Local comunication fonctions
+        ////////////////////////////////////////////////////////////////////
+*/
+/*
+     Communication with the LCD driver
+*/
+void rgb_lcd::command(uint8_t comm){
+    uint8_t data[2] = {0x80, comm};     //  0x80 : Configuration register
+    i2c_send_byteS(data,2);
+}
+
+/*
+     Communication with the RGB driver
+*/
+void rgb_lcd::setReg(uint8_t reg, uint8_t value){
+    uint8_t data[2] = {reg,value};
+    i2c->write(RGB_ADDRESS, (char*)data, 2);
+}
+/*
+     Communication with the print register of the LCD
+*/
+void rgb_lcd::write(uint8_t value){
+    uint8_t data[2] = {0x40, value};    // 0x40 : writing register
+    i2c_send_byteS(data, 2);
+}
+/*
+     Communication I2C
+*/
+void rgb_lcd::i2c_send_byteS(uint8_t *data, uint8_t len){
+    i2c->write(LCD_ADDRESS, (char*) data, len);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/*
+        ////////////////////////////////////////////////////////////////////
+                                User Fonctions
+        ////////////////////////////////////////////////////////////////////
+*/
+/*
+    Constructeur
+*/
+rgb_lcd::rgb_lcd(I2C* i2c_bus){
+    i2c = i2c_bus;
+    _displayfunction = 0x00;
+    _displaycontrol = 0x00;
+    _displaymode = 0x00;
+}
+/*
+    Initialize the LCD
+*/
+void rgb_lcd::begin(){
+    
+    _displayfunction |= LCD_2LINE;  // We use a 2 line display
+    _displayfunction |= LCD_5X8DOTS;   //  We set the size of characters 
+    
+    wait_ms(50);    //  We wait for the power rise
+    
+    //We send many times the command to guarantee the proper fonctioning of the LCD
+    command(LCD_FUNCTIONSET | _displayfunction);
+    wait_us(4500);
+    command(LCD_FUNCTIONSET | _displayfunction);
+    wait_us(150);
+    command(LCD_FUNCTIONSET | _displayfunction);
+    command(LCD_FUNCTIONSET | _displayfunction);
+    
+    // We turn on the display without cursor
+    _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
+    display();
+    clear();
+    
+    // We set up the text direction ( From left to right )
+    _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
+    command(LCD_ENTRYMODESET | _displaymode );
+    //We set up the backlight
+    setReg( REG_MODE1, 0);  // We turn on the backlight
+    setReg( REG_OUTPUT, 0xFF);  //  We allow the control of the RGB
+    setReg( REG_MODE2,   0x20);  //  Blinky Mode
+    setRGB(255,255,255);    //We set the white color
+}
+/*
+    Set backlight-color
+*/
+void rgb_lcd::setRGB(uint8_t r, uint8_t g, uint8_t b){
+    setReg(REG_RED, r);
+    setReg(REG_GREEN, g);
+    setReg(REG_BLUE, b);
+}
+/*
+    Set cursor position
+*/
+void rgb_lcd::setCursor(uint8_t col, uint8_t row){
+    col = (row == 0 ? col|0x80 : col|0xC0);
+    uint8_t data[2] = {0x80 , col};
+    i2c_send_byteS(data, 2);
+}
+/*
+    Display text
+*/
+void rgb_lcd::display(){
+    _displaycontrol |= LCD_DISPLAYON;
+    command(LCD_DISPLAYCONTROL | _displaycontrol);
+}
+/*
+    Clear display
+*/
+void rgb_lcd::clear(){
+    command(LCD_CLEARDISPLAY);  // Clear and set cursor to position zero
+    wait_ms(2);
+}
+////////////////////////////////////////////////////////////////////////////////
+
+/*
+        ////////////////////////////////////////////////////////////////////
+                     FONCTIONS TO ENABLE PRINTF FROM STREAM
+        ////////////////////////////////////////////////////////////////////
+*/
+
+int rgb_lcd::_putc(int c){
+    write((uint8_t)c);
+    return 0;
+}
+int rgb_lcd::_getc(){
+    return -1;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rgb_lcd.h	Mon Apr 15 07:44:45 2019 +0000
@@ -0,0 +1,78 @@
+/*
+    rgb_lcd library
+    Created by: Isaac Maximiliano Martinez Govea
+    15/04/2019
+*/
+
+#ifndef RGB_LCD_H
+#define RGB_LCD_H
+
+/*  Constants */
+//Addresses I2C
+#define LCD_ADDRESS (0x7C)
+#define RGB_ADDRESS (0xC4)
+
+//Registres de la couleur
+#define REG_MODE1   0x00
+#define REG_MODE2   0X01
+#define REG_OUTPUT  0x08
+
+#define REG_RED 0x04
+#define REG_GREEN   0x03
+#define REG_BLUE    0x02
+
+//COMMANDES LCD
+#define LCD_FUNCTIONSET 0x20
+#define LCD_ENTRYMODESET    0x04
+#define LCD_DISPLAYCONTROL  0x08
+#define LCD_CLEARDISPLAY    0x01
+
+//Flags
+#define LCD_2LINE   0x08
+#define LCD_5X10DOTS    0x04
+#define LCD_5X8DOTS    0x00
+#define LCD_DISPLAYON   0x04
+#define LCD_CURSOROFF   0x00
+#define LCD_BLINKOFF    0x00
+#define LCD_ENTRYLEFT   0x02
+#define LCD_ENTRYSHIFTDECREMENT 0x00
+
+#include "mbed.h"
+
+class rgb_lcd : public Stream{
+    private:
+        I2C *i2c;
+        uint8_t _displayfunction;
+        uint8_t _displaycontrol;
+        uint8_t _displaymode;
+        
+        /*
+            Local comunication fonctions
+        */
+        void setReg(uint8_t reg, uint8_t value);    // Communication with the RGB driver
+        void command(uint8_t comm); // Communication with the LCD driver
+    
+        void write(uint8_t value);                  // Communication with the print register of the LCD
+        void i2c_send_byteS(uint8_t *data, uint8_t len);    // Communication I2C
+    protected:
+        /* 
+            We overdrive these functions to get acces to the function printf
+        */
+        virtual int _putc(int c);
+        virtual int _getc();
+    public:
+    rgb_lcd(I2C* i2c_bus);
+    
+    /* 
+        User Fonctions
+    */
+    void begin();       // Initialize the LCD
+    void setRGB(uint8_t r, uint8_t g, uint8_t b);   // Set backlight-color
+    void setCursor(uint8_t col, uint8_t row);       // Set cursor position
+    void display();     //  Display text
+    void clear();       //  Clear display
+    
+    void test_rgb();
+};
+
+#endif
\ No newline at end of file