Skip to content

Commit

Permalink
support for v1.1 which changed backlight handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Bad-Assumptions committed Sep 28, 2023
1 parent 63525e0 commit 79009bc
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 62 deletions.
152 changes: 95 additions & 57 deletions DFRobot_RGBLCD1602.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@
#define LIB_TAG "DFRobot_RGBLCD1602"

const uint8_t color_define[4][3] =
{
{255, 255, 255}, // white
{255, 0, 0}, // red
{0, 255, 0}, // green
{0, 0, 255}, // blue, also monochrome
{
{255, 255, 255}, // white
{255, 0, 0}, // red
{0, 255, 0}, // green
{0, 0, 255}, // blue, also monochrome
};

// /*******************************public*******************************/
DFRobot_RGBLCD1602::DFRobot_RGBLCD1602(uint8_t lcdCols, uint8_t lcdRows, i2c_port_t i2c_num, uint8_t lcdAddr, uint8_t RGBAddr)
{
_lcdAddr = lcdAddr;
_RGBAddr = RGBAddr;
_cols = lcdCols;
_rows = lcdRows;
_i2c_num = i2c_num;
DFRobot_RGBLCD1602::DFRobot_RGBLCD1602(uint8_t lcdCols, uint8_t lcdRows, i2c_port_t i2c_num, uint8_t lcdAddr, uint8_t RGBAddr)
{
_lcdAddr = lcdAddr;
_RGBAddr = RGBAddr;
_cols = lcdCols;
_rows = lcdRows;
_i2c_num = i2c_num;
}
/**
* @brief scan an I2C address
*
*
*/
esp_err_t DFRobot_RGBLCD1602::i2c_scan(const uint8_t addr)
{
Expand Down Expand Up @@ -78,48 +78,59 @@ esp_err_t DFRobot_RGBLCD1602::init()
_RGBAddr = LCD_RGB_ADDRESS;
REG_RED = 0x04;
REG_GREEN = 0x03;
REG_BLUE = 0x02;
REG_BLUE = 0x02;
}
else
{
ret = i2c_scan(LCD_RGB_ADDRESS_ALT);
ret = i2c_scan(LCD_RGB_ADDRESS_ALT2);
if (ret == ESP_OK) // found it
{
_RGBAddr = LCD_RGB_ADDRESS_ALT;
_RGBAddr = LCD_RGB_ADDRESS_ALT2;
REG_RED = 0x06; // pwm2
REG_GREEN = 0x07; // pwm1
REG_BLUE = 0x08; // pwm0
REG_GREEN = 0x05; // pwm1
REG_BLUE = 0x04; // pwm0
}
else
{
ret = i2c_scan(LCD_RGB_ADDRESS_ALT);
if (ret == ESP_OK) // found it
{
_RGBAddr = LCD_RGB_ADDRESS_ALT;
REG_RED = 0x06; // pwm2
REG_GREEN = 0x07; // pwm1
REG_BLUE = 0x08; // pwm0
}
}
}
ESP_LOGD(_TAG, "backlight assigned to 0%02x", _RGBAddr);


_showFunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
begin(_rows);
_showFunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
begin(_rows);
return ret;
}

esp_err_t DFRobot_RGBLCD1602::clear()
{
esp_err_t ret = command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
vTaskDelay(pdMS_TO_TICKS(12)); // this command takes a long time!
esp_err_t ret = command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
vTaskDelay(pdMS_TO_TICKS(12)); // this command takes a long time!
home();
return ret;
}

esp_err_t DFRobot_RGBLCD1602::home()
{
esp_err_t ret = command(LCD_RETURNHOME); // set cursor position to zero
vTaskDelay(pdMS_TO_TICKS(12)); // this command takes a long time!
return ret;
}
esp_err_t DFRobot_RGBLCD1602::home()
{
esp_err_t ret = command(LCD_RETURNHOME); // set cursor position to zero
vTaskDelay(pdMS_TO_TICKS(12)); // this command takes a long time!
return ret;
}

esp_err_t DFRobot_RGBLCD1602::noDisplay()
{
_showControl &= ~LCD_DISPLAYON;
return command(LCD_DISPLAYCONTROL | _showControl);
}
esp_err_t DFRobot_RGBLCD1602::noDisplay()
{
_showControl &= ~LCD_DISPLAYON;
return command(LCD_DISPLAYCONTROL | _showControl);
}

esp_err_t DFRobot_RGBLCD1602::display()
esp_err_t DFRobot_RGBLCD1602::display()
{
_showControl |= LCD_DISPLAYON;
return command(LCD_DISPLAYCONTROL | _showControl);
Expand Down Expand Up @@ -192,9 +203,9 @@ esp_err_t DFRobot_RGBLCD1602::customSymbol(uint8_t location, uint8_t charmap[])

uint8_t data[9];
data[0] = 0x40;
for(int i=0; i<8; i++)
for (int i = 0; i < 8; i++)
{
data[i+1] = charmap[i];
data[i + 1] = charmap[i];
}
return i2c_master_write_to_device(_i2c_num, _lcdAddr,
data, 9,
Expand All @@ -203,18 +214,32 @@ esp_err_t DFRobot_RGBLCD1602::customSymbol(uint8_t location, uint8_t charmap[])
esp_err_t DFRobot_RGBLCD1602::setCursor(uint8_t col, uint8_t row)
{

col = (row == 0 ? col|0x80 : col|0xc0);
col = (row == 0 ? col | 0x80 : col | 0xc0);
const uint8_t data[] = {0x80, col};
return i2c_master_write_to_device(_i2c_num, _lcdAddr,
data, 2,
LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
data, 2,
LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
}

// this function generally doesn't work as expected. Better to use setRGB().
void DFRobot_RGBLCD1602::setPWM(uint8_t color, uint8_t pwm)
{
setReg(color, pwm);
if (_RGBAddr == 0x6b)
{
setReg(0x07, pwm);
}
}

esp_err_t DFRobot_RGBLCD1602::setRGB(uint8_t r, uint8_t g, uint8_t b)
{
ESP_RETURN_ON_ERROR(setReg(REG_RED, r), _TAG, "setReg(0x%2x, 0x%2x)", REG_RED, r);
ESP_RETURN_ON_ERROR(setReg(REG_GREEN, g), _TAG, "setReg(0x%2x, 0x%2x)", REG_GREEN, g);
ESP_RETURN_ON_ERROR(setReg(REG_BLUE, b), _TAG, "setReg(0x%2x, 0x%2x)", REG_BLUE, b);
if (_RGBAddr == 0x6b)
{
setReg(0x07, 0xFF);
}
return ESP_OK;
}

Expand All @@ -228,8 +253,8 @@ esp_err_t DFRobot_RGBLCD1602::blinkLED(void)
{
///< blink period in seconds = (<reg 7> + 1) / 24
///< on/off ratio = <reg 6> / 256
setReg(0x07, 0x17); // blink every second
return setReg(0x06, 0x7f); // half on, half off
setReg(0x07, 0x17); // blink every second
return setReg(0x06, 0x7f); // half on, half off
}

esp_err_t DFRobot_RGBLCD1602::noBlinkLED(void)
Expand Down Expand Up @@ -260,8 +285,8 @@ inline esp_err_t DFRobot_RGBLCD1602::command(uint8_t value)
{
const uint8_t data[] = {0x80, value};
return i2c_master_write_to_device(_i2c_num, _lcdAddr,
data, 2,
LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
data, 2,
LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
}

void DFRobot_RGBLCD1602::print(const char chr)
Expand Down Expand Up @@ -298,15 +323,17 @@ void DFRobot_RGBLCD1602::print(const float f, uint8_t decimalPlaces)
// }

// /*******************************private*******************************/
esp_err_t DFRobot_RGBLCD1602::begin( uint8_t rows, uint8_t charSize)
esp_err_t DFRobot_RGBLCD1602::begin(uint8_t rows, uint8_t charSize)
{
if (rows > 1) {
if (rows > 1)
{
_showFunction |= LCD_2LINE;
}
_numLines = rows;
_currLine = 0;
///< for some 1 line displays you can select a 10 pixel high font
if ((charSize != 0) && (rows == 1)) {
if ((charSize != 0) && (rows == 1))
{
_showFunction |= LCD_5x10DOTS;
}

Expand All @@ -320,9 +347,9 @@ esp_err_t DFRobot_RGBLCD1602::begin( uint8_t rows, uint8_t charSize)

///< Send function set command sequence
command(LCD_FUNCTIONSET | _showFunction);
vTaskDelay(pdMS_TO_TICKS(5)); // wait more than 4.1ms
///< second try
vTaskDelay(pdMS_TO_TICKS(5)); // wait more than 4.1ms

///< second try
command(LCD_FUNCTIONSET | _showFunction);
vTaskDelay(pdMS_TO_TICKS(5));

Expand All @@ -340,17 +367,28 @@ esp_err_t DFRobot_RGBLCD1602::begin( uint8_t rows, uint8_t charSize)
_showMode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
///< set the entry mode
command(LCD_ENTRYMODESET | _showMode);

if(_RGBAddr == LCD_RGB_ADDRESS){
///< backlight init

if (_RGBAddr == LCD_RGB_ADDRESS)
{
///< backlight init
setReg(LCD_REG_MODE1, 0);
///< set LEDs controllable by both PWM and GRPPWM registers
setReg(LCD_REG_OUTPUT, 0xFF);
///< set MODE2 values
///< 0010 0000 -> 0x20 (DMBLNK to 1, ie blinky mode)
setReg(LCD_REG_MODE2, 0x20);
}else{
setReg(0x04, 0x15);
}
else if (_RGBAddr == 0x6B)
{
setReg(0x2F, 0x00);
setReg(0x00, 0x20);
setReg(0x01, 0x00);
setReg(0x02, 0x01);
setReg(0x03, 4);
}
else
{
setReg(0x04, 0x15);
}
setColorWhite();
return ESP_OK;
Expand All @@ -374,6 +412,6 @@ esp_err_t DFRobot_RGBLCD1602::setReg(uint8_t addr, uint8_t data)
ESP_LOGD(_TAG, "i2c_master_write_to_device(port:%i, addr:0x%02x, reg:0x%02x, data:0x%02x)", _i2c_num, _RGBAddr, addr, data);
const uint8_t write_buffer[] = {addr, data};
return i2c_master_write_to_device(_i2c_num, _RGBAddr,
write_buffer, 2,
LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
write_buffer, 2,
LCD_COMMAND_DELAY_MS / portTICK_PERIOD_MS);
}
19 changes: 14 additions & 5 deletions DFRobot_RGBLCD1602.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*/
#define LCD_LCD_ADDRESS (0x7c >> 1)
#define LCD_RGB_ADDRESS_ALT (0x60 >> 1)
#define LCD_RGB_ADDRESS (0xc0 >> 1)
#define LCD_RGB_ADDRESS_ALT2 (0x6b ) // LCD1602 module V1.1
#define LCD_RGB_ADDRESS (0xc0 >> 1) // = 0x60. LCD1602 module V1.0 and LCD1602 RGB Module V1.0

/*!
* @brief color define
Expand Down Expand Up @@ -222,12 +223,20 @@ class DFRobot_RGBLCD1602 // : public Print --yeah don't know if I'll be able to
*/
esp_err_t setRGB(uint8_t r, uint8_t g, uint8_t b);

// /**
// * @brief set backlight PWM output
// * @param color backlight color Preferences:REG_RED\REG_GREEN\REG_BLUE
// * @param pwm color intensity range(0-255)
// */
// esp_err_t setPWM(uint8_t color, uint8_t pwm) {return setReg(color, pwm);} // set pwm

/**
* @brief set backlight PWM output
* @param color backlight color Preferences:REG_RED\REG_GREEN\REG_BLUE
* @param pwm color intensity range(0-255)
* @fn setPWM
* @brief set backlight PWM output
* @param color backlight color Preferences:REG_RED\REG_GREEN\REG_BLUE
* @param pwm color intensity range(0-255)
*/
esp_err_t setPWM(uint8_t color, uint8_t pwm) {return setReg(color, pwm);} // set pwm
void setPWM(uint8_t color, uint8_t pwm);

/**
* @brief backlight color
Expand Down

0 comments on commit 79009bc

Please sign in to comment.