Skip to content

Commit e6c974a

Browse files
committed
Allow for setting rows & columns when initialising driver.
1 parent aeb9af3 commit e6c974a

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

i2c-lcd1602.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,17 @@ void i2c_lcd1602_free(i2c_lcd1602_info_t ** i2c_lcd1602_info)
278278
}
279279
}
280280

281-
esp_err_t i2c_lcd1602_init(i2c_lcd1602_info_t * i2c_lcd1602_info, smbus_info_t * smbus_info, bool backlight)
281+
esp_err_t i2c_lcd1602_init(i2c_lcd1602_info_t * i2c_lcd1602_info, smbus_info_t * smbus_info,
282+
bool backlight, uint8_t num_rows, uint8_t num_columns, uint8_t num_visible_columns)
282283
{
283284
esp_err_t err = ESP_FAIL;
284285
if (i2c_lcd1602_info != NULL)
285286
{
286287
i2c_lcd1602_info->smbus_info = smbus_info;
287288
i2c_lcd1602_info->backlight_flag = backlight ? FLAG_BACKLIGHT_ON : FLAG_BACKLIGHT_OFF;
289+
i2c_lcd1602_info->num_rows = num_rows;
290+
i2c_lcd1602_info->num_columns = num_columns;
291+
i2c_lcd1602_info->num_visible_columns = num_visible_columns;
288292

289293
// display on, no cursor, no blinking
290294
i2c_lcd1602_info->display_control_flags = FLAG_DISPLAY_CONTROL_DISPLAY_ON | FLAG_DISPLAY_CONTROL_CURSOR_OFF | FLAG_DISPLAY_CONTROL_BLINK_OFF;
@@ -435,13 +439,13 @@ esp_err_t i2c_lcd1602_move_cursor(const i2c_lcd1602_info_t * i2c_lcd1602_info, u
435439
if (_is_init(i2c_lcd1602_info))
436440
{
437441
const int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
438-
if (row > I2C_LCD1602_NUM_ROWS)
442+
if (row > i2c_lcd1602_info->num_rows)
439443
{
440-
row = I2C_LCD1602_NUM_ROWS - 1;
444+
row = i2c_lcd1602_info->num_rows - 1;
441445
}
442-
if (col > I2C_LCD1602_NUM_COLUMNS)
446+
if (col > i2c_lcd1602_info->num_columns)
443447
{
444-
col = I2C_LCD1602_NUM_COLUMNS - 1;
448+
col = i2c_lcd1602_info->num_columns - 1;
445449
}
446450
err = _write_command(i2c_lcd1602_info, COMMAND_SET_DDRAM_ADDR | (col + row_offsets[row]));
447451
}

include/i2c-lcd1602.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ typedef struct
5050
bool init; ///< True if struct has been initialised, otherwise false
5151
smbus_info_t * smbus_info; ///< Pointer to associated SMBus info
5252
uint8_t backlight_flag; ///< Non-zero if backlight is to be enabled, otherwise zero
53+
uint8_t num_rows; ///< Number of configured columns
54+
uint8_t num_columns; ///< Number of configured columns, including offscreen columns
55+
uint8_t num_visible_columns; ///< Number of visible columns
5356
uint8_t display_control_flags; ///< Currently active display control flags
5457
uint8_t entry_mode_flags; ///< Currently active entry mode flags
5558
} i2c_lcd1602_info_t;
5659

57-
#define I2C_LCD1602_NUM_ROWS 2 ///< Maximum number of supported rows for this device
58-
#define I2C_LCD1602_NUM_COLUMNS 40 ///< Maximum number of supported columns for this device
59-
#define I2C_LCD1602_NUM_VISIBLE_COLUMNS 16 ///< Number of columns visible at any one time
6060

6161
// Special characters for ROM Code A00
6262

@@ -127,9 +127,14 @@ void i2c_lcd1602_free(i2c_lcd1602_info_t ** tsl2561_info);
127127
*
128128
* @param[in] i2c_lcd1602_info Pointer to I2C-LCD1602 info instance.
129129
* @param[in] smbus_info Pointer to SMBus info instance.
130+
* @param[in] backlight Initial backlight state.
131+
* @param[in] num_rows Maximum number of supported rows for this device. Typical values include 2 (1602) or 4 (2004).
132+
* @param[in] num_columns Maximum number of supported columns for this device. Typical values include 40 (1602, 2004).
133+
* @param[in] num_visible_columns Number of columns visible at any one time. Typical values include 16 (1602) or 20 (2004).
130134
* @return ESP_OK if successful, otherwise an error constant.
131135
*/
132-
esp_err_t i2c_lcd1602_init(i2c_lcd1602_info_t * i2c_lcd1602_info, smbus_info_t * smbus_info, bool backlight);
136+
esp_err_t i2c_lcd1602_init(i2c_lcd1602_info_t * i2c_lcd1602_info, smbus_info_t * smbus_info,
137+
bool backlight, uint8_t num_rows, uint8_t num_columns, uint8_t num_visible_columns);
133138

134139
/**
135140
* @brief Reset the display. Custom characters will be cleared.
@@ -142,6 +147,7 @@ esp_err_t i2c_lcd1602_reset(const i2c_lcd1602_info_t * i2c_lcd1602_info);
142147
/**
143148
* @brief Clears entire display (clears DDRAM) and returns cursor to home position.
144149
* DDRAM content is cleared, CGRAM content is not changed.
150+
*
145151
* @param[in] i2c_lcd1602_info Pointer to initialised I2C-LCD1602 info instance.
146152
* @return ESP_OK if successful, otherwise an error constant.
147153
*/

0 commit comments

Comments
 (0)