Skip to content

Commit 1be19f2

Browse files
committed
simplified _row_offsets initialization
1 parent 9af188e commit 1be19f2

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

src/LiquidCrystal_PCF8574.cpp

+13-27
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ LiquidCrystal_PCF8574::LiquidCrystal_PCF8574(int i2cAddr)
3333

3434
void LiquidCrystal_PCF8574::begin(int cols, int lines)
3535
{
36-
_cols = cols;
37-
_lines = lines;
36+
_cols = min(cols, 80);
37+
_lines = min(lines, 4);
3838

3939
int functionFlags = 0;
4040

41+
_row_offsets[0] = 0x00;
42+
_row_offsets[1] = 0x40;
43+
_row_offsets[2] = 0x00 + cols;
44+
_row_offsets[3] = 0x40 + cols;
45+
4146
if (lines > 1) {
4247
functionFlags |= 0x08;
4348
}
@@ -58,16 +63,14 @@ void LiquidCrystal_PCF8574::begin(int cols, int lines)
5863
delayMicroseconds(200);
5964
_sendNibble(0x03);
6065
delayMicroseconds(200);
61-
_sendNibble(0x02); // finally, set to 4-bit interface
66+
_sendNibble(0x02); // finally, set to 4-bit interface
6267

6368
// Instruction: Function set = 0x20
6469
_send(0x20 | functionFlags);
6570

6671
display();
6772
clear();
6873
leftToRight();
69-
70-
_row_offsets = _getRowOffsets();
7174
} // begin()
7275

7376

@@ -96,8 +99,11 @@ void LiquidCrystal_PCF8574::home()
9699
/// Set the cursor to a new position.
97100
void LiquidCrystal_PCF8574::setCursor(int col, int row)
98101
{
99-
// Instruction: Set DDRAM address = 0x80
100-
_send(0x80 | (_row_offsets[row] + col));
102+
// check boundaries
103+
if ((col >= 0) && (col < _cols) && (row >= 0) && (row < _lines)) {
104+
// Instruction: Set DDRAM address = 0x80
105+
_send(0x80 | (_row_offsets[row] + col));
106+
}
101107
} // setCursor()
102108

103109

@@ -279,24 +285,4 @@ void LiquidCrystal_PCF8574::_write2Wire(int halfByte, bool isData, bool enable)
279285
Wire.endTransmission();
280286
} // write2Wire
281287

282-
// Prepare row_offsets
283-
int* LiquidCrystal_PCF8574::_getRowOffsets()
284-
{
285-
int *row_offsets = new int[this->_lines];
286-
287-
if (this->_lines > 0)
288-
row_offsets[0] = 0x00;
289-
else if (this->_lines > 1)
290-
row_offsets[1] = 0x40;
291-
else if (this->_lines > 2)
292-
{
293-
for (int i = 0, j = 2; j < this->_lines; i++, j++)
294-
{
295-
row_offsets[j] = row_offsets[i] + _cols;
296-
}
297-
}
298-
299-
return row_offsets;
300-
}
301-
302288
// The End.

src/LiquidCrystal_PCF8574.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ class LiquidCrystal_PCF8574 : public Print
7171
// instance variables
7272
int _i2cAddr; ///< Wire Address of the LCD
7373
int _backlight; ///< the backlight intensity
74+
int _cols; ///< number of cols of the display
7475
int _lines; ///< number of lines of the display
7576
int _entrymode; ///<flags from entrymode
7677
int _displaycontrol; ///<flags from displaycontrol
77-
int* _row_offsets;
78+
int _row_offsets[4];
7879

7980
// low level functions
8081
void _send(int value, bool isData = false);
8182
void _sendNibble(int halfByte, bool isData = false);
8283
void _write2Wire(int halfByte, bool isData, bool enable);
83-
int* _getRowOffsets();
8484
};
8585

8686
#endif

0 commit comments

Comments
 (0)