I2CTextLCD.h

Committer:
Wimpie
Date:
2010-12-19
Revision:
1:0eb3365ec819
Parent:
0:9218cf335f9b
Child:
2:1c5dea5d8783

File content as of revision 1:0eb3365ec819:

/* mbed I2CTextLCD Library
 * Copyright (c) 2007-2009 sford
 * Copyright (c) 2010 Wim De Roeve  changed to work with I2C PCF8575
 * Released under the MIT License: http://mbed.org/license/mit
 */

#ifndef MBED_I2CTextLCD_H
#define MBED_I2CTextLCD_H

#include "mbed.h"
#include "Stream.h"

/* The code assumes the following connections between the PCF8575
* and the LCD
*
* nc  - D0
* nc  - D1
* nc  - D2
* nc  - D3
* P0  - D4
* P1  - D5
* P2  - D6
* P3  - D7
* P4  - E
* P5  - nc
* P6  - nc
* P7  - RS
* gnd - R/W

*/

#define E_ON  0x10  //P4
#define BACKLIGHT_ON  0x20  //P5
#define RS_ON 0x80  //P7

namespace mbed {

/* Class: I2CTextLCD
 * A 16x2 Text LCD controller
 *
 * Allows you to print to a Text LCD screen, and locate/cls. Could be
 * turned in to a more generic libray.
 *
 * If you are connecting multiple displays, you can connect them all in
 * parallel except for the enable (e) pin, which must be unique for each
 * display.
 *
 * Example:
 * > #include "mbed.h"
 * > #include "I2CTextLCD.h"
 * >
 * > I2CTextLCD lcd(p9, P10, 0x40); // sda scl, address
 * >
 * > int main() {
 * >     lcd.printf("Hello World!");
 * > }
 */
class I2CTextLCD : public Stream {

public:
    /* Constructor: I2CTextLCD
     * Create a I2CTextLCD object, connected to the specified pins
     *
     * All signals must be connected to DigitalIn compatible pins.
     *
     * Variables:
     *  rs -  Used to specify data or command
     *  rw - Used to determine read or write
     *  e - enable
     *  d0..d3 - The data lines
     */

    I2CTextLCD(PinName sda, PinName scl, int address, int columns = 16, int rows = 2);

#if 0 // Inhereted from Stream, for documentation only
    /* Function: putc
     *  Write a character
     *
     * Variables:
     *  c - The character to write to the serial port
     */
    int putc(int c);

    /* Function: printf
     *  Write a formated string
     *
     * Variables:
     *  format - A printf-style format string, followed by the
     *      variables to use in formating the string.
     */
    int printf(const char* format, ...);
#endif

    /* Function: locate
     * Locate to a certian position
     *
     * Variables:
     *  column - the column to locate to, from 0..15
     *  row - the row to locate to, from 0..1
     */
    virtual void locate(int column, int row);

    /* Function: cls
     * Clear the screen, and locate to 0,0
     */
    virtual void cls();

    virtual void reset();
    virtual void backlight(bool on);

protected:

    void clock();
    void writeData(int data);
    void writeCommand(int command);
    void writeByte(int value, bool rs);
    void writeNibble(int value, bool rs);
    void writeI2CByte(int data);
    int readI2C();
    virtual int _putc(int c);
    virtual int _getc();
    virtual void newline();

    int _row;
    int _column;
    int _columns;
    int _rows;

    I2C _i2c;
    int _i2cAddress;
    bool _backlight;
        
private:

   

};

}

#endif