Statistics
| Revision:

root / Ports / PortsLCD.h

History | View | Annotate | Download (4.1 KB)

1
//>>> The latest version of this code can be found at https://github.com/jcw/ !!
2
3
// LiquidCrystal library, extended for use over I2C with the LCD Plug
4
// 2009-09-23 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
5
// $Id: PortsLCD.h 7763 2011-12-11 01:28:16Z jcw $
6
7
// see http://news.jeelabs.org/2009/09/26/generalized-liquidcrystal-library/
8
9
#ifndef LiquidCrystal_h
10
#define LiquidCrystal_h
11
12
#include <inttypes.h>
13
#include <Print.h>
14
#include <Ports.h>
15
16
// commands
17
#define LCD_CLEARDISPLAY 0x01
18
#define LCD_RETURNHOME 0x02
19
#define LCD_ENTRYMODESET 0x04
20
#define LCD_DISPLAYCONTROL 0x08
21
#define LCD_CURSORSHIFT 0x10
22
#define LCD_FUNCTIONSET 0x20
23
#define LCD_SETCGRAMADDR 0x40
24
#define LCD_SETDDRAMADDR 0x80
25
26
// flags for display entry mode
27
#define LCD_ENTRYRIGHT 0x00
28
#define LCD_ENTRYLEFT 0x02
29
#define LCD_ENTRYSHIFTINCREMENT 0x01
30
#define LCD_ENTRYSHIFTDECREMENT 0x00
31
32
// flags for display on/off control
33
#define LCD_DISPLAYON 0x04
34
#define LCD_DISPLAYOFF 0x00
35
#define LCD_CURSORON 0x02
36
#define LCD_CURSOROFF 0x00
37
#define LCD_BLINKON 0x01
38
#define LCD_BLINKOFF 0x00
39
40
// flags for display/cursor shift
41
#define LCD_DISPLAYMOVE 0x08
42
#define LCD_CURSORMOVE 0x00
43
#define LCD_MOVERIGHT 0x04
44
#define LCD_MOVELEFT 0x00
45
46
// flags for function set
47
#define LCD_8BITMODE 0x10
48
#define LCD_4BITMODE 0x00
49
#define LCD_2LINE 0x08
50
#define LCD_1LINE 0x00
51
#define LCD_5x10DOTS 0x04
52
#define LCD_5x8DOTS 0x00
53
54
// this class defines all the basic functionality needed to drive an LCD display
55
// it is an incomplete (abstract) base class, which needs to be extended for use
56
// see the LiquidCrystal and LiquidCrystalI2C classes for two usable versions
57
58
class LiquidCrystalBase : public Print {
59
public:
60
  LiquidCrystalBase () {}
61
  
62
  void begin(byte cols, byte rows, byte charsize = LCD_5x8DOTS);
63
64
  void clear();
65
  void home();
66
67
  void noDisplay();
68
  void display();
69
  void noBlink();
70
  void blink();
71
  void noCursor();
72
  void cursor();
73
  void scrollDisplayLeft();
74
  void scrollDisplayRight();
75
  void leftToRight();
76
  void rightToLeft();
77
  void autoscroll();
78
  void noAutoscroll();
79
80
  void createChar(byte, byte[]);
81
  void setCursor(byte, byte); 
82
  virtual void write(byte);
83
  void command(byte);
84
protected:
85
  virtual void config() =0;
86
  virtual void send(byte, byte) =0;
87
  virtual void write4bits(byte) =0;
88
89
  byte _displayfunction;
90
  byte _displaycontrol;
91
  byte _displaymode;
92
  byte _initialized;
93
  byte _numlines,_currline;
94
};
95
96
// This class can be used to create an object with drives an LCD through many
97
// different I/O pins, connected to the display in parallel - it is equivalent
98
// to the LiquidCrystal class defined in the Arduino, but has be adjusted to
99
// work with the above LiquidCrystalBase instead.
100
101
class LiquidCrystal : public LiquidCrystalBase {
102
public:
103
  LiquidCrystal(byte rs, byte enable,
104
                byte d0, byte d1, byte d2, byte d3, byte d4, byte d5, byte d6, byte d7);
105
  LiquidCrystal(byte rs, byte rw, byte enable,
106
                byte d0, byte d1, byte d2, byte d3, byte d4, byte d5, byte d6, byte d7);
107
  LiquidCrystal(byte rs, byte rw, byte enable,
108
                byte d0, byte d1, byte d2, byte d3);
109
  LiquidCrystal(byte rs, byte enable,
110
                byte d0, byte d1, byte d2, byte d3);
111
112
  void init(byte fourbitmode, byte rs, byte rw, byte enable,
113
            byte d0, byte d1, byte d2, byte d3, byte d4, byte d5, byte d6, byte d7);
114
  
115
  virtual void config();
116
  virtual void send(byte, byte);
117
  virtual void write4bits(byte);
118
119
  void write8bits(byte);
120
  void pulseEnable();
121
122
  byte _rs_pin; // LOW: command.  HIGH: character.
123
  byte _rw_pin; // LOW: write to LCD.  HIGH: read from LCD.
124
  byte _enable_pin; // activated by a HIGH pulse.
125
  byte _data_pins[8];
126
};
127
128
// This class allows driving an LCD connected via I2C using an LCD Plug, which
129
// is in turn based on an MCP23008 I2C I/O expander chip and some other parts.
130
// The available functions include all those of the LiquidCrystal class.
131
132
class LiquidCrystalI2C : public LiquidCrystalBase {
133
  DeviceI2C device;
134
public:
135
  LiquidCrystalI2C (const PortI2C& p, byte addr =0x24);
136
  
137
  // this display can also turn the back-light on or off
138
  void backlight();
139
  void noBacklight();
140
protected:
141
  virtual void config();
142
  virtual void send(byte, byte);
143
  virtual void write4bits(byte);
144
};
145
146
#endif