AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Public Types | Public Member Functions | List of all members
I2cLcd Class Reference

This class provides a high-level interface via I2C to an LCD such as those offered by AdaFruit and SparkFun. Specifically, it communicates via I2C with an MCP23017 that drives an HD44780U controlling an LCD. It also lets you detect button presses on the 5-button keypad generally assocaited with such devices. More...

#include <I2cLcd.h>

Inheritance diagram for I2cLcd:
Inheritance graph
[legend]
Collaboration diagram for I2cLcd:
Collaboration graph
[legend]

Public Types

enum  {
  kButton_Select, kButton_Right, kButton_Down, kButton_Up,
  kButton_Left
}
 
enum  {
  kBacklight_Red, kBacklight_Yellow, kBacklight_Green, kBacklight_Teal,
  kBacklight_Blue, kBacklight_Violet, kBacklight_White
}
 
enum  IntegerOutputBase { kBin = 2, kOct = 8, kDec = 10, kHex = 16 }
 An enumeration that defines the number that will be used as the base for representing integer quantities as a string of characters. More...
 

Public Member Functions

 I2cLcd ()
 Constructor simply initializes some internal bookkeeping.
 
int init ()
 Initialize the I2cLcd object. This must be called before using the I2cLcd, or calling any of the other member functions. The I2C system must be initialized before calling this function (by calling I2cMaster::start() from I2cMaster.h). More...
 
void clear ()
 Clear the display (all rows, all columns).
 
void home ()
 Move the cursor home (the top row, left column).
 
void displayTopRow (const char *str)
 Display a C-string on the top row. More...
 
void displayBottomRow (const char *str)
 Display a C-string on the bottom row. More...
 
void clearTopRow ()
 Clear the top row.
 
void clearBottomRow ()
 Clear the bottom row.
 
void displayOff ()
 Turn the display off.
 
void displayOn ()
 Turn the display on.
 
void blinkOff ()
 Do not blink the cursor.
 
void blinkOn ()
 Blink the cursor.
 
void cursorOff ()
 Hide the cursor.
 
void cursorOn ()
 Display the cursor.
 
void scrollDisplayLeft ()
 Scroll the display to the left.
 
void scrollDisplayRight ()
 Scroll the display to the right.
 
void autoscrollOn ()
 Turn on automatic scrolling of the display.
 
void autoscrollOff ()
 Turn off automatic scrolling of the display.
 
void setCursor (uint8_t row, uint8_t col)
 Move the cursor the a particular row and column. More...
 
int setBacklight (uint8_t color)
 Set the backlight to a given color. Set a black-and-white LCD display to White if you want to have a backlight. More...
 
void command (uint8_t cmd)
 Pass a command to the LCD. More...
 
uint8_t readButtons ()
 Read the state of the buttons associated with the LCD display. More...
 
virtual size_t write (char c)
 Write a single character to the LCD at the current cursor location. This implements the pure virtual function Writer::write( char c ). More...
 
virtual size_t write (const char *str)
 Write a C-string to the LCD at the current cursor location. This implements the pure virtual function Writer::write( const char* str ). More...
 
virtual size_t write (const char *buffer, size_t size)
 Write a given number of characters from a buffer to the LCD at the current cursor location. This implements the pure virtual function Writer::write( const char* buffer, size_t size ). More...
 
virtual size_t write (const uint8_t *buffer, size_t size)
 Write a given number of bytes from a buffer to the LCD at the current cursor location. This implements the pure virtual function Writer::write( const uint8_t* buffer, size_t size ). More...
 
virtual void flush ()
 This function does nothing. It simply implements the pure virtual function Writer::flush().
 
size_t print (const char *str, bool addLn=false)
 Print a null-terminated string to the output stream, with or without adding a new line character at the end. More...
 
size_t print (const uint8_t *buf, size_t size, bool addLn=false)
 Print a number of bytes to the output stream, with or without adding a new line character at the end. More...
 
size_t print (char c, bool addLn=false)
 Print a single character to the output stream, with or without adding a new line character at the end. More...
 
size_t print (int8_t n, int base=kDec, bool addLn=false)
 Print an 8-bit integer to the output stream, with or without adding a new line character at the end. More...
 
size_t print (uint8_t n, int base=kDec, bool addLn=false)
 Print an 8-bit unsigned integer to the output stream, with or without adding a new line character at the end. More...
 
size_t print (int n, int base=kDec, bool addLn=false)
 Print an integer to the output stream, with or without adding a new line character at the end. More...
 
size_t print (unsigned int n, int base=kDec, bool addLn=false)
 Print an unsigned integer to the output stream, with or without adding a new line character at the end. More...
 
size_t print (long n, int base=kDec, bool addLn=false)
 Print a long integer to the output stream, with or without adding a new line character at the end. More...
 
size_t print (unsigned long n, int base=kDec, bool addLn=false)
 Print an unsigned long integer to the output stream, with or without adding a new line character at the end. More...
 
size_t print (double d, int digits=2, bool addLn=false)
 Print a floating point number to the output stream, with or without adding a new line character at the end. More...
 
size_t println (const char *str)
 Print a null-terminated string to the output stream, adding a new line character at the end. More...
 
size_t println (const uint8_t *buf, size_t size)
 Print a number of bytes to the output stream, adding a new line character at the end. More...
 
size_t println (char c)
 Print a single character to the output stream, adding a new line character at the end. More...
 
size_t println (int8_t n, int base=kDec)
 Print an 8-bit integer to the output stream, adding a new line character at the end. More...
 
size_t println (uint8_t n, int base=kDec)
 Print an 8-bit unsigned integer to the output stream, adding a new line character at the end. More...
 
size_t println (int n, int base=kDec)
 Print an integer to the output stream, adding a new line character at the end. More...
 
size_t println (unsigned int n, int base=kDec)
 Print an unsigned integer to the output stream, adding a new line character at the end. More...
 
size_t println (long n, int base=kDec)
 Print a long integer to the output stream, adding a new line character at the end. More...
 
size_t println (unsigned long n, int base=kDec)
 Print an unsigned long integer to the output stream, adding a new line character at the end. More...
 
size_t println (double d, int digits=2)
 Print a floating point number to the output stream, adding a new line character at the end. More...
 
size_t println ()
 Print a new line to the output stream.
 

Detailed Description

This class provides a high-level interface via I2C to an LCD such as those offered by AdaFruit and SparkFun. Specifically, it communicates via I2C with an MCP23017 that drives an HD44780U controlling an LCD. It also lets you detect button presses on the 5-button keypad generally assocaited with such devices.

This class derives from Writer, allowing you to write to the LCD much as it if were a serial device.

To use these features, include I2cLcd.h in your source code and link against I2cLcd.cpp and I2cMaster.cpp, and initialize the I2C hardware by calling I2cMaster::start().

Member Enumeration Documentation

§ anonymous enum

anonymous enum

These constants are used to identify the five buttons.

Enumerator
kButton_Select 

the Select button

kButton_Right 

the Right button

kButton_Down 

the Down button

kButton_Up 

the Up button

kButton_Left 

the Left button

§ anonymous enum

anonymous enum

These constants are used to set the backlight color on the LCD.

Enumerator
kBacklight_Red 

Backlight red.

kBacklight_Yellow 

Backlight yellow.

kBacklight_Green 

Backlight green.

kBacklight_Teal 

Backlight teal.

kBacklight_Blue 

Backlight blue.

kBacklight_Violet 

Backlight violet.

kBacklight_White 

Backlight white.

§ IntegerOutputBase

enum Writer::IntegerOutputBase
inherited

An enumeration that defines the number that will be used as the base for representing integer quantities as a string of characters.

Enumerator
kBin 

Produce a binary representation of integers (e.g., 11 is output as 0b1011)

kOct 

Produce an octal representation of integers (e.g, 11 is output as 013)

kDec 

Produce a decimal representation of integers (e.g., 11 is output as 11.

kHex 

Produce a hexadecimal representation of integers (e.g., 11 is output as 0x0b)

Member Function Documentation

§ command()

void I2cLcd::command ( uint8_t  cmd)

Pass a command to the LCD.

  • cmd a valid command to send to the HD44780U.

§ displayBottomRow()

void I2cLcd::displayBottomRow ( const char *  str)

Display a C-string on the bottom row.

  • str the C-string to display.

§ displayTopRow()

void I2cLcd::displayTopRow ( const char *  str)

Display a C-string on the top row.

  • str the C-string to display.

§ init()

int I2cLcd::init ( )

Initialize the I2cLcd object. This must be called before using the I2cLcd, or calling any of the other member functions. The I2C system must be initialized before calling this function (by calling I2cMaster::start() from I2cMaster.h).

The LCD display is initialized in 16-column, 2-row mode.

§ print() [1/10]

size_t Writer::print ( const char *  str,
bool  addLn = false 
)
inherited

Print a null-terminated string to the output stream, with or without adding a new line character at the end.

  • str is the null-terminated string to output.
  • addLn if true, a new line character is added at the end of the output; the default is false.
Returns
the number of bytes sent to the output stream.

§ print() [2/10]

size_t Writer::print ( const uint8_t *  buf,
size_t  size,
bool  addLn = false 
)
inherited

Print a number of bytes to the output stream, with or without adding a new line character at the end.

  • buf is the buffer containing bytes to output.
  • size is the number of bytes from the buffer to output.
  • addLn if true, a new line character is added at the end of the output; the default is false.
Returns
the number of bytes sent to the output stream.

§ print() [3/10]

size_t Writer::print ( char  c,
bool  addLn = false 
)
inherited

Print a single character to the output stream, with or without adding a new line character at the end.

  • c is the character to output.
  • addLn if true, a new line character is added at the end of the output; the default is false.
Returns
the number of bytes sent to the output stream.

§ print() [4/10]

size_t Writer::print ( int8_t  n,
int  base = kDec,
bool  addLn = false 
)
inherited

Print an 8-bit integer to the output stream, with or without adding a new line character at the end.

  • n is the integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
  • addLn if true, a new line character is added at the end of the output; the default is false.
Returns
the number of bytes sent to the output stream.

§ print() [5/10]

size_t Writer::print ( uint8_t  n,
int  base = kDec,
bool  addLn = false 
)
inlineinherited

Print an 8-bit unsigned integer to the output stream, with or without adding a new line character at the end.

  • n is the integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
  • addLn if true, a new line character is added at the end of the output; the default is false.
Returns
the number of bytes sent to the output stream.

§ print() [6/10]

size_t Writer::print ( int  n,
int  base = kDec,
bool  addLn = false 
)
inherited

Print an integer to the output stream, with or without adding a new line character at the end.

  • n is the integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
  • addLn if true, a new line character is added at the end of the output; the default is false.
Returns
the number of bytes sent to the output stream.

§ print() [7/10]

size_t Writer::print ( unsigned int  n,
int  base = kDec,
bool  addLn = false 
)
inlineinherited

Print an unsigned integer to the output stream, with or without adding a new line character at the end.

  • n is the unsigned integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
  • addLn if true, a new line character is added at the end of the output; the default is false.
Returns
the number of bytes sent to the output stream.

§ print() [8/10]

size_t Writer::print ( long  n,
int  base = kDec,
bool  addLn = false 
)
inherited

Print a long integer to the output stream, with or without adding a new line character at the end.

  • n is the long integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
  • addLn if true, a new line character is added at the end of the output; the default is false.
Returns
the number of bytes sent to the output stream.

§ print() [9/10]

size_t Writer::print ( unsigned long  n,
int  base = kDec,
bool  addLn = false 
)
inherited

Print an unsigned long integer to the output stream, with or without adding a new line character at the end.

  • n is the unsigned long integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
  • addLn if true, a new line character is added at the end of the output; the default is false.
Returns
the number of bytes sent to the output stream.

§ print() [10/10]

size_t Writer::print ( double  d,
int  digits = 2,
bool  addLn = false 
)
inherited

Print a floating point number to the output stream, with or without adding a new line character at the end.

  • d is the floating point number to output.
  • digits is the number of decimal digits to output; the default is 2.
  • addLn if true, a new line character is added at the end of the output; the default is false.
Returns
the number of bytes sent to the output stream.

§ println() [1/10]

size_t Writer::println ( const char *  str)
inlineinherited

Print a null-terminated string to the output stream, adding a new line character at the end.

  • str is the null-terminated string to output.
Returns
the number of bytes sent to the output stream.

§ println() [2/10]

size_t Writer::println ( const uint8_t *  buf,
size_t  size 
)
inlineinherited

Print a number of bytes to the output stream, adding a new line character at the end.

  • buf is the buffer containing bytes to output.
  • size is the number of bytes from the buffer to output.
Returns
the number of bytes sent to the output stream.

§ println() [3/10]

size_t Writer::println ( char  c)
inlineinherited

Print a single character to the output stream, adding a new line character at the end.

  • c is the character to output.
Returns
the number of bytes sent to the output stream.

§ println() [4/10]

size_t Writer::println ( int8_t  n,
int  base = kDec 
)
inlineinherited

Print an 8-bit integer to the output stream, adding a new line character at the end.

  • n is the integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
Returns
the number of bytes sent to the output stream.

§ println() [5/10]

size_t Writer::println ( uint8_t  n,
int  base = kDec 
)
inlineinherited

Print an 8-bit unsigned integer to the output stream, adding a new line character at the end.

  • n is the unsigned integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
Returns
the number of bytes sent to the output stream.

§ println() [6/10]

size_t Writer::println ( int  n,
int  base = kDec 
)
inlineinherited

Print an integer to the output stream, adding a new line character at the end.

  • n is the integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
Returns
the number of bytes sent to the output stream.

§ println() [7/10]

size_t Writer::println ( unsigned int  n,
int  base = kDec 
)
inlineinherited

Print an unsigned integer to the output stream, adding a new line character at the end.

  • n is the unsigned integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
Returns
the number of bytes sent to the output stream.

§ println() [8/10]

size_t Writer::println ( long  n,
int  base = kDec 
)
inlineinherited

Print a long integer to the output stream, adding a new line character at the end.

  • n is the long integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
Returns
the number of bytes sent to the output stream.

§ println() [9/10]

size_t Writer::println ( unsigned long  n,
int  base = kDec 
)
inlineinherited

Print an unsigned long integer to the output stream, adding a new line character at the end.

  • n is the unsigned long integer to output.
  • base is the base used to represent the number; should be one of IntegerOutputBase; defaults to decimal representation (kDec).
Returns
the number of bytes sent to the output stream.

§ println() [10/10]

size_t Writer::println ( double  d,
int  digits = 2 
)
inlineinherited

Print a floating point number to the output stream, adding a new line character at the end.

  • d is the flaoting point number to output.
  • digits is the number of decimal digits to output; the default is 2.
Returns
the number of bytes sent to the output stream.

§ readButtons()

uint8_t I2cLcd::readButtons ( )

Read the state of the buttons associated with the LCD display.

Returns
a byte with flags set corresponding to the buttons that are depressed. You must "and" the return value with kButton_Right, kButton_Left, kButton_Down, kButton_Up, or kButton_Select to determine which buttons have been pressed.

§ setBacklight()

int I2cLcd::setBacklight ( uint8_t  color)

Set the backlight to a given color. Set a black-and-white LCD display to White if you want to have a backlight.

  • color the color to set the backlight. Pass one of kBacklight_Red, kBacklight_Yellow, kBacklight_Green, kBacklight_Teal, kBacklight_Blue, kBacklight_Violet, or kBacklight_White.

§ setCursor()

void I2cLcd::setCursor ( uint8_t  row,
uint8_t  col 
)

Move the cursor the a particular row and column.

  • row the row to move the cursor to (numbering starts at 0).
  • col the column to move the cursor to (numbering starts at 0).

§ write() [1/4]

size_t I2cLcd::write ( char  c)
virtual

Write a single character to the LCD at the current cursor location. This implements the pure virtual function Writer::write( char c ).

  • the character to be written.
Returns
the number of bytes written.

Implements Writer.

§ write() [2/4]

size_t I2cLcd::write ( const char *  str)
virtual

Write a C-string to the LCD at the current cursor location. This implements the pure virtual function Writer::write( const char* str ).

  • the C-string to be written.
Returns
the number of bytes written.

Implements Writer.

§ write() [3/4]

size_t I2cLcd::write ( const char *  buffer,
size_t  size 
)
virtual

Write a given number of characters from a buffer to the LCD at the current cursor location. This implements the pure virtual function Writer::write( const char* buffer, size_t size ).

  • buffer the buffer of characters to write.
  • size the number of characters to write
Returns
the number of bytes written.

Implements Writer.

§ write() [4/4]

size_t I2cLcd::write ( const uint8_t *  buffer,
size_t  size 
)
virtual

Write a given number of bytes from a buffer to the LCD at the current cursor location. This implements the pure virtual function Writer::write( const uint8_t* buffer, size_t size ).

  • buffer the buffer of bytes to write.
  • size the number of bytes to write
Returns
the number of bytes written.

Implements Writer.


The documentation for this class was generated from the following files: