I2CTextLCD.h

Committer:
Wimpie
Date:
2010-12-23
Revision:
2:1c5dea5d8783
Parent:
1:0eb3365ec819
Child:
3:704f87be7993

File content as of revision 2:1c5dea5d8783:

/* 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"

#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, the address of the PCF8575 or PCF8574 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
     *
     * wiring
     *
     * PCF8575/PCF8574  to  LCD
     * ----------------------
     * P0              -  D4
     * P1              -  D5
     * P2              -  D6
     * P3              -  D7
     * P4              -  E
     * P5              -  backlight (connected to a relay)
     * P6              -  nc
     * P7              -  RS
     * gnd             -  R/W
     *
     */

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

#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();

    /* Function: backlight
    * Sets the backlight on or off
    *
    * Variables:
    *  on (true or false)
    */
    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