11//
22// FILE: I2C_LCD.cpp
334- // VERSION: 0.1.3
4+ // VERSION: 0.1.4
55// DATE: 2023-12-16
66// PUPROSE: Arduino library for I2C_LCD
77// URL: https://github.com/RobTillaart/I2C_LCD
@@ -71,19 +71,22 @@ void I2C_LCD::config (uint8_t address, uint8_t enable, uint8_t readWrite, uint8_
7171}
7272
7373
74- void I2C_LCD::begin (uint8_t cols, uint8_t rows)
74+ bool I2C_LCD::begin (uint8_t cols, uint8_t rows)
7575{
7676 _cols = cols;
7777 _rows = rows;
7878
79+ if (isConnected () == false ) return false ;
80+
7981 // ALL LINES LOW.
8082 _wire->beginTransmission (_address);
8183 _wire->write (0x00 );
8284 _wire->endTransmission ();
8385
8486 // Figure 24 for procedure on 4-bit initialization
8587 // wait for more than 15 ms
86- delay (100 ); // no need to optimize as this is called only once.
88+ // if other objects initialize earlier there will be less blocking time.
89+ while (millis () < 100 ) delay (1 );
8790
8891 // Force 4 bit mode
8992 write4bits (0x03 );
@@ -102,6 +105,7 @@ void I2C_LCD::begin(uint8_t cols, uint8_t rows)
102105 // default enable display
103106 display ();
104107 clear ();
108+ return true ;
105109}
106110
107111
@@ -118,7 +122,7 @@ bool I2C_LCD::isConnected()
118122//
119123void I2C_LCD::setBacklightPin (uint8_t pin, uint8_t polarity)
120124{
121- _backLightPin = ( 1 << pin);
125+ _backLightPin = (1 << pin);
122126 _backLightPol = polarity;
123127}
124128
@@ -163,7 +167,7 @@ void I2C_LCD::clear()
163167
164168void I2C_LCD::clearEOL ()
165169{
166- for ( int i = _pos; i < _cols; i++ )
170+ while ( _pos < _cols)
167171 {
168172 print (' ' );
169173 }
@@ -346,7 +350,7 @@ size_t I2C_LCD::write(uint8_t c)
346350size_t I2C_LCD::center (uint8_t row, const char * message)
347351{
348352 uint8_t len = strlen (message) + 1 ;
349- setCursor (10 - len/ 2 , row);
353+ setCursor ((_cols - len) / 2 , row);
350354 return print (message);
351355}
352356
@@ -359,6 +363,17 @@ size_t I2C_LCD::right(uint8_t col, uint8_t row, const char * message)
359363}
360364
361365
366+ size_t I2C_LCD::repeat (uint8_t c, uint8_t times)
367+ {
368+ size_t n = 0 ;
369+ while ((times--) && (_pos < _cols))
370+ {
371+ n += write (c);
372+ }
373+ return n;
374+ }
375+
376+
362377// ////////////////////////////////////////////////////////
363378//
364379// PRIVATE
0 commit comments