Commits

F Malpartida committed ef1ac6d

Adding backlight support to base classes.

Comments (0)

Files changed (2)

LiquidCrystal_I2C.cpp

 #include "LiquidCrystal_I2C.h"
 
 
+// flags for backlight control
+#define LCD_NOBACKLIGHT 0x00
+#define LCD_BACKLIGHT   0xFF
+
+
 // CONSTRUCTORS
 // ---------------------------------------------------------------------------
 LiquidCrystal_I2C::LiquidCrystal_I2C( uint8_t lcd_Addr )
 {
    _Addr = lcd_Addr;
    
-   _backlightval = LCD_NOBACKLIGHT;
+   _backlightPin  = 0x0;
+   _backlightMask = LCD_NOBACKLIGHT;
+   
    _En = EN;
    _Rw = RW;
    _Rs = RS;
 }
 
 LiquidCrystal_I2C::LiquidCrystal_I2C( uint8_t lcd_Addr, uint8_t En, uint8_t Rw,
-                                     uint8_t Rs)
+                                      uint8_t Rs)
 {
    _Addr = lcd_Addr;
    
-   _backlightval = LCD_NOBACKLIGHT;
+   _backlightPin  = 0;
+   _backlightMask = LCD_NOBACKLIGHT;
+   
    _En = ( 1 << En );
    _Rw = ( 1 << Rw );
    _Rs = ( 1 << Rs );
 {
    _Addr = lcd_Addr;
    
-   _backlightval = LCD_NOBACKLIGHT;
+   _backlightPin  = 0;
+   _backlightMask = LCD_NOBACKLIGHT;
+
    _En = ( 1 << En );
    _Rw = ( 1 << Rw );
    _Rs = ( 1 << Rs );
       _i2cio.portMode ( OUTPUT );  // Set the entire IO extender to OUTPUT
       _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
       status = 1;
+      _i2cio.write(0);  // Set the entire port to LOW
    }
    return ( status );
 }
 //----------------------------------------------------------------------------
 
 // Turn the (optional) backlight off/on
-void LiquidCrystal_I2C::noBacklight(void) 
+void LiquidCrystal_I2C::setBacklight( uint8_t mode ) 
 {
-   _backlightval=LCD_NOBACKLIGHT;
-   expanderWrite(0);
-}
 
-void LiquidCrystal_I2C::backlight(void) 
-{
-   _backlightval=LCD_BACKLIGHT;
-   expanderWrite(0);
+   if ( mode == HIGH )
+   {
+      _backlightMask = _backlightPin & LCD_BACKLIGHT;
+
+   }
+   else 
+   {
+      _backlightMask = _backlightPin & LCD_NOBACKLIGHT;
+   }
+   _i2cio.write( _backlightMask );
+
 }
 
 // PRIVATE METHODS
       mode = _Rs;
    }
    
-   expanderWrite ( pinMapValue | mode );
-   pulseEnable ( pinMapValue | mode );
+   pinMapValue |= mode | _backlightMask;
+   _i2cio.write ( pinMapValue );
+   pulseEnable ( pinMapValue );
 }
 
 //
 // write4bits
 void LiquidCrystal_I2C::pulseEnable (uint8_t _data)
 {
-   expanderWrite (_data | _En);   // En HIGH
-   expanderWrite(_data & ~_En);   // En LOW
-} 
-
-//
-// expanderWrite
-void LiquidCrystal_I2C::expanderWrite(uint8_t _data)
-{                                        
-   _i2cio.write ( _data );
-}
-
-
-
-
+   _i2cio.write (_data | _En);   // En HIGH
+   _i2cio.write (_data & ~_En);  // En LOW
+}
 #include "I2CIO.h"
 #include "LCD.h"
 
-// flags for backlight control
-#define LCD_BACKLIGHT   0x00
-#define LCD_NOBACKLIGHT 0x80
-
-/*!
- @warning These definitions will have to be changed should the IOExpander be 
- connected differently to this default.
- */
-
+// Default library configuration parameters used by class constructor with
+// only the I2C address field.
+// ---------------------------------------------------------------------------
 /*!
  @defined 
  @abstract   Enable bit of the LCD
     @discussion Switch-off the LCD backlight, this method is not supported by
     the I2CLCDextraIO, it needs an extra IO pin to drive the LCD backlight
     
-    @param      none
+    @param      mode: backlight mode (HIGH|LOW)
     */
-   void noBacklight();
-   
-   /*!
-    @function
-    @abstract   Switch-on the LCD backlight.
-    @discussion Switch-on the LCD backlight, this method is not supported by
-    the I2CLCDextraIO, it needs an extra IO pin to drive the LCD backlight
-    
-    @param      none
-    */
-   void backlight();
+   void setBacklight ( uint8_t mode );
    
    
 private:
     */
    void pulseEnable(uint8_t);
    
-   /*!
-    @method     
-    @abstract   Writes to the expansion module.
-    @discussion Writes a byte to the IO expansion module. This will be applied
-    to all its outputs.
-    */ 
-   void expanderWrite(uint8_t);
    
-   uint8_t _Addr;           // I2C Address of the IO expander
-   uint8_t _backlightval;   // Backlight shadow value
-   I2CIO   _i2cio;          // I2CIO PCF8574* expansion module driver I2CLCDextraIO
-   uint8_t _En;             // LCD expander word for enable pin
-   uint8_t _Rw;             // LCD expander word for R/W pin
-   uint8_t _Rs;             // LCD expander word for Register Select pin
-   uint8_t _data_pins[4];   // LCD data lines
+   uint8_t _Addr;             // I2C Address of the IO expander
+   uint8_t _backlightPin;     // Backlight IO pin
+   uint8_t _backlightMask;    // Backlight status mask
+   I2CIO   _i2cio;            // I2CIO PCF8574* expansion module driver I2CLCDextraIO
+   uint8_t _En;               // LCD expander word for enable pin
+   uint8_t _Rw;               // LCD expander word for R/W pin
+   uint8_t _Rs;               // LCD expander word for Register Select pin
+   uint8_t _data_pins[4];     // LCD data lines
    
 };