From 79009bcb9ea630a07a09010f259390de46d0a3cc Mon Sep 17 00:00:00 2001 From: Brian Alano <111384732+Bad-Assumptions@users.noreply.github.com> Date: Thu, 28 Sep 2023 10:50:27 -0400 Subject: [PATCH] support for v1.1 which changed backlight handling --- DFRobot_RGBLCD1602.cpp | 152 +++++++++++++++++++++++++---------------- DFRobot_RGBLCD1602.h | 19 ++++-- 2 files changed, 109 insertions(+), 62 deletions(-) diff --git a/DFRobot_RGBLCD1602.cpp b/DFRobot_RGBLCD1602.cpp index e58c9e6..d126427 100644 --- a/DFRobot_RGBLCD1602.cpp +++ b/DFRobot_RGBLCD1602.cpp @@ -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) { @@ -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); @@ -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, @@ -203,11 +214,21 @@ 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) @@ -215,6 +236,10 @@ 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; } @@ -228,8 +253,8 @@ esp_err_t DFRobot_RGBLCD1602::blinkLED(void) { ///< blink period in seconds = ( + 1) / 24 ///< on/off ratio = / 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) @@ -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) @@ -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; } @@ -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)); @@ -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; @@ -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); } diff --git a/DFRobot_RGBLCD1602.h b/DFRobot_RGBLCD1602.h index d69addd..da03a9d 100644 --- a/DFRobot_RGBLCD1602.h +++ b/DFRobot_RGBLCD1602.h @@ -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 @@ -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