libraries / Visuino_LiquidCrystal / Visuino_LiquidCrystal_I2C.hon commit Added link to project report (97a3ba0)
   1// ---------------------------------------------------------------------------
   2// Created by Francisco Malpartida on 20/08/11.
   3// Copyright 2011 - Under creative commons license 3.0:
   4//        Attribution-ShareAlike CC BY-SA
   5//
   6// This software is furnished "as is", without technical support, and with no 
   7// warranty, express or implied, as to its usefulness for any purpose.
   8//
   9// Thread Safe: No
  10// Extendable: Yes
  11//
  12// @file LiquidCrystal_I2C.h
  13// This file implements a basic liquid crystal library that comes as standard
  14// in the Arduino SDK but using an I2C IO extension board.
  15// 
  16// @brief 
  17// This is a basic implementation of the LiquidCrystal library of the
  18// Arduino SDK. The original library has been reworked in such a way that 
  19// this class implements the all methods to command an LCD based
  20// on the Hitachi HD44780 and compatible chipsets using I2C extension
  21// backpacks such as the I2CLCDextraIO with the PCF8574* I2C IO Expander ASIC.
  22//
  23// The functionality provided by this class and its base class is identical
  24// to the original functionality of the Arduino LiquidCrystal library.
  25//
  26//
  27// @author F. Malpartida - fmalpartida@gmail.com
  28// ---------------------------------------------------------------------------
  29#ifndef LiquidCrystal_I2C_h
  30#define LiquidCrystal_I2C_h
  31#include <inttypes.h>
  32#include <Print.h>
  33
  34#include "Visuino_I2CIO.h"
  35#include "Visuino_LCD.h"
  36
  37
  38class LiquidCrystal_I2C : public LCD 
  39{
  40public:
  41   
  42   /*!
  43    @method     
  44    @abstract   Class constructor. 
  45    @discussion Initializes class variables and defines the I2C address of the
  46    LCD. The constructor does not initialize the LCD.
  47    
  48    @param      lcd_Addr[in] I2C address of the IO expansion module. For I2CLCDextraIO,
  49    the address can be configured using the on board jumpers.
  50    */
  51   LiquidCrystal_I2C (uint8_t lcd_Addr);
  52   // Constructor with backlight control
  53   LiquidCrystal_I2C (uint8_t lcd_Addr, uint8_t backlighPin, t_backlighPol pol);
  54   
  55   /*!
  56    @method     
  57    @abstract   Class constructor. 
  58    @discussion Initializes class variables and defines the I2C address of the
  59    LCD. The constructor does not initialize the LCD.
  60    
  61    @param      lcd_Addr[in] I2C address of the IO expansion module. For I2CLCDextraIO,
  62    the address can be configured using the on board jumpers.
  63    @param      En[in] LCD En (Enable) pin connected to the IO extender module
  64    @param      Rw[in] LCD Rw (Read/write) pin connected to the IO extender module
  65    @param      Rs[in] LCD Rs (Reset) pin connected to the IO extender module
  66    */
  67   LiquidCrystal_I2C( uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs);
  68   // Constructor with backlight control
  69   LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs,
  70                     uint8_t backlighPin, t_backlighPol pol);   
  71   
  72   /*!
  73    @method     
  74    @abstract   Class constructor. 
  75    @discussion Initializes class variables and defines the I2C address of the
  76    LCD. The constructor does not initialize the LCD.
  77    
  78    @param      lcd_Addr[in] I2C address of the IO expansion module. For I2CLCDextraIO,
  79    the address can be configured using the on board jumpers.
  80    @param      En[in] LCD En (Enable) pin connected to the IO extender module
  81    @param      Rw[in] LCD Rw (Read/write) pin connected to the IO extender module
  82    @param      Rs[in] LCD Rs (Reset) pin connected to the IO extender module
  83    @param      d4[in] LCD data 0 pin map on IO extender module
  84    @param      d5[in] LCD data 1 pin map on IO extender module
  85    @param      d6[in] LCD data 2 pin map on IO extender module
  86    @param      d7[in] LCD data 3 pin map on IO extender module
  87    */
  88   LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs, 
  89                     uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7 );
  90   // Constructor with backlight control
  91   LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs, 
  92                     uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7,
  93                     uint8_t backlighPin, t_backlighPol pol);
  94   /*!
  95    @function
  96    @abstract   LCD initialization and associated HW.
  97    @discussion Initializes the LCD to a given size (col, row). This methods
  98    initializes the LCD, therefore, it MUST be called prior to using any other
  99    method from this class or parent class.
 100    
 101    The begin method can be overloaded if necessary to initialize any HW that 
 102    is implemented by a library and can't be done during construction, here
 103    we use the Wire class.
 104    
 105    @param      cols[in] the number of columns that the display has
 106    @param      rows[in] the number of rows that the display has
 107    @param      charsize[in] size of the characters of the LCD: LCD_5x8DOTS or
 108    LCD_5x10DOTS.
 109    */
 110   virtual void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);   
 111   
 112   /*!
 113    @function
 114    @abstract   Send a particular value to the LCD.
 115    @discussion Sends a particular value to the LCD for writing to the LCD or
 116    as an LCD command.
 117    
 118    Users should never call this method.
 119    
 120    @param      value[in] Value to send to the LCD.
 121    @param      mode[in] DATA - write to the LCD CGRAM, COMMAND - write a 
 122    command to the LCD.
 123    */
 124   virtual void send(uint8_t value, uint8_t mode);
 125   
 126   /*!
 127    @function
 128    @abstract   Sets the pin to control the backlight.
 129    @discussion Sets the pin in the device to control the backlight. This device
 130    doesn't support dimming backlight capability.
 131    
 132    @param      0: backlight off, 1..255: backlight on.
 133    */
 134   void setBacklightPin ( uint8_t value, t_backlighPol pol );
 135   
 136   /*!
 137    @function
 138    @abstract   Switch-on/off the LCD backlight.
 139    @discussion Switch-on/off the LCD backlight.
 140    The setBacklightPin has to be called before setting the backlight for
 141    this method to work. @see setBacklightPin.
 142    
 143    @param      value: backlight mode (HIGH|LOW)
 144    */
 145   void setBacklight ( uint8_t value );
 146   
 147private:
 148   
 149   /*!
 150    @method     
 151    @abstract   Initializes the LCD class
 152    @discussion Initializes the LCD class and IO expansion module.
 153    */
 154   int  init();
 155   
 156   /*!
 157    @function
 158    @abstract   Initialises class private variables
 159    @discussion This is the class single point for initialising private variables.
 160    
 161    @param      lcd_Addr[in] I2C address of the IO expansion module. For I2CLCDextraIO,
 162    the address can be configured using the on board jumpers.
 163    @param      En[in] LCD En (Enable) pin connected to the IO extender module
 164    @param      Rw[in] LCD Rw (Read/write) pin connected to the IO extender module
 165    @param      Rs[in] LCD Rs (Reset) pin connected to the IO extender module
 166    @param      d4[in] LCD data 0 pin map on IO extender module
 167    @param      d5[in] LCD data 1 pin map on IO extender module
 168    @param      d6[in] LCD data 2 pin map on IO extender module
 169    @param      d7[in] LCD data 3 pin map on IO extender module
 170    */
 171   void config (uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs, 
 172                uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7 );
 173   
 174   /*!
 175    @method     
 176    @abstract   Writes an 4 bit value to the LCD.
 177    @discussion Writes 4 bits (the least significant) to the LCD control data lines.
 178    @param      value[in] Value to write to the LCD
 179    @param      more[in]  Value to distinguish between command and data.
 180    COMMAND == command, DATA == data.
 181    */
 182   void write4bits(uint8_t value, uint8_t mode);
 183   
 184   /*!
 185    @method     
 186    @abstract   Pulse the LCD enable line (En).
 187    @discussion Sends a pulse of 1 uS to the Enable pin to execute an command
 188    or write operation.
 189    */
 190   void pulseEnable(uint8_t);
 191   
 192   
 193   uint8_t _Addr;             // I2C Address of the IO expander
 194   uint8_t _backlightPinMask; // Backlight IO pin mask
 195   uint8_t _backlightStsMask; // Backlight status mask
 196   I2CIO   _i2cio;            // I2CIO PCF8574* expansion module driver I2CLCDextraIO
 197   uint8_t _En;               // LCD expander word for enable pin
 198   uint8_t _Rw;               // LCD expander word for R/W pin
 199   uint8_t _Rs;               // LCD expander word for Register Select pin
 200   uint8_t _data_pins[4];     // LCD data lines
 201   
 202};
 203
 204#endif