@@ -33,11 +33,16 @@ LiquidCrystal_PCF8574::LiquidCrystal_PCF8574(int i2cAddr)
33
33
34
34
void LiquidCrystal_PCF8574::begin (int cols, int lines)
35
35
{
36
- _cols = cols;
37
- _lines = lines;
36
+ _cols = min ( cols, 80 ) ;
37
+ _lines = min ( lines, 4 ) ;
38
38
39
39
int functionFlags = 0 ;
40
40
41
+ _row_offsets[0 ] = 0x00 ;
42
+ _row_offsets[1 ] = 0x40 ;
43
+ _row_offsets[2 ] = 0x00 + cols;
44
+ _row_offsets[3 ] = 0x40 + cols;
45
+
41
46
if (lines > 1 ) {
42
47
functionFlags |= 0x08 ;
43
48
}
@@ -58,16 +63,14 @@ void LiquidCrystal_PCF8574::begin(int cols, int lines)
58
63
delayMicroseconds (200 );
59
64
_sendNibble (0x03 );
60
65
delayMicroseconds (200 );
61
- _sendNibble (0x02 ); // finally, set to 4-bit interface
66
+ _sendNibble (0x02 ); // finally, set to 4-bit interface
62
67
63
68
// Instruction: Function set = 0x20
64
69
_send (0x20 | functionFlags);
65
70
66
71
display ();
67
72
clear ();
68
73
leftToRight ();
69
-
70
- _row_offsets = _getRowOffsets ();
71
74
} // begin()
72
75
73
76
@@ -96,8 +99,11 @@ void LiquidCrystal_PCF8574::home()
96
99
// / Set the cursor to a new position.
97
100
void LiquidCrystal_PCF8574::setCursor (int col, int row)
98
101
{
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
+ }
101
107
} // setCursor()
102
108
103
109
@@ -279,24 +285,4 @@ void LiquidCrystal_PCF8574::_write2Wire(int halfByte, bool isData, bool enable)
279
285
Wire.endTransmission ();
280
286
} // write2Wire
281
287
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
-
302
288
// The End.
0 commit comments