-
Notifications
You must be signed in to change notification settings - Fork 90
/
LiquidCrystal_I2C.cpp
executable file
·303 lines (262 loc) · 8.43 KB
/
LiquidCrystal_I2C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// ---------------------------------------------------------------------------
// Created by Francisco Malpartida on 20/08/11.
// Copyright (C) - 2018
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License v3.0
// along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.en.html>.
//
// ---------------------------------------------------------------------------
//
// Thread Safe: No
// Extendable: Yes
//
// @file LiquidCrystal_I2C.c
// This file implements a basic liquid crystal library that comes as standard
// in the Arduino SDK but using an I2C IO extension board.
//
// @brief
// This is a basic implementation of the LiquidCrystal library of the
// Arduino SDK. The original library has been reworked in such a way that
// this class implements the all methods to command an LCD based
// on the Hitachi HD44780 and compatible chipsets using I2C extension
// backpacks such as the I2CLCDextraIO with the PCF8574* I2C IO Expander ASIC.
//
// The functionality provided by this class and its base class is identical
// to the original functionality of the Arduino LiquidCrystal library.
//
//
//
// @author F. Malpartida - [email protected]
// ---------------------------------------------------------------------------
#if (ARDUINO < 100)
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
#include <inttypes.h>
#include "I2CIO.h"
#include "LiquidCrystal_I2C.h"
// CONSTANT definitions
// ---------------------------------------------------------------------------
// flags for backlight control
/*!
@defined
@abstract LCD_NOBACKLIGHT
@discussion NO BACKLIGHT MASK
*/
#define LCD_NOBACKLIGHT 0x00
/*!
@defined
@abstract LCD_BACKLIGHT
@discussion BACKLIGHT MASK used when backlight is on
*/
#define LCD_BACKLIGHT 0xFF
// Default library configuration parameters used by class constructor with
// only the I2C address field.
// ---------------------------------------------------------------------------
/*!
@defined
@abstract Enable bit of the LCD
@discussion Defines the IO of the expander connected to the LCD Enable
*/
#define EN 6 // Enable bit
/*!
@defined
@abstract Read/Write bit of the LCD
@discussion Defines the IO of the expander connected to the LCD Rw pin
*/
#define RW 5 // Read/Write bit
/*!
@defined
@abstract Register bit of the LCD
@discussion Defines the IO of the expander connected to the LCD Register select pin
*/
#define RS 4 // Register select bit
/*!
@defined
@abstract LCD dataline allocation this library only supports 4 bit LCD control
mode.
@discussion D4, D5, D6, D7 LCD data lines pin mapping of the extender module
*/
#define D4 0
#define D5 1
#define D6 2
#define D7 3
// CONSTRUCTORS
// ---------------------------------------------------------------------------
LiquidCrystal_I2C::LiquidCrystal_I2C( uint8_t lcd_Addr )
{
config(lcd_Addr, EN, RW, RS, D4, D5, D6, D7);
}
LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t backlighPin,
t_backlightPol pol = POSITIVE)
{
config(lcd_Addr, EN, RW, RS, D4, D5, D6, D7);
setBacklightPin(backlighPin, pol);
}
LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw,
uint8_t Rs)
{
config(lcd_Addr, En, Rw, Rs, D4, D5, D6, D7);
}
LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw,
uint8_t Rs, uint8_t backlighPin,
t_backlightPol pol = POSITIVE)
{
config(lcd_Addr, En, Rw, Rs, D4, D5, D6, D7);
setBacklightPin(backlighPin, pol);
}
LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw,
uint8_t Rs, uint8_t d4, uint8_t d5,
uint8_t d6, uint8_t d7 )
{
config(lcd_Addr, En, Rw, Rs, d4, d5, d6, d7);
}
LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw,
uint8_t Rs, uint8_t d4, uint8_t d5,
uint8_t d6, uint8_t d7, uint8_t backlighPin,
t_backlightPol pol = POSITIVE )
{
config(lcd_Addr, En, Rw, Rs, d4, d5, d6, d7);
setBacklightPin(backlighPin, pol);
}
// PUBLIC METHODS
// ---------------------------------------------------------------------------
//
// begin
void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize)
{
init(); // Initialise the I2C expander interface
LCD::begin ( cols, lines, dotsize );
}
// User commands - users can expand this section
//----------------------------------------------------------------------------
// Turn the (optional) backlight off/on
//
// setBacklightPin
void LiquidCrystal_I2C::setBacklightPin ( uint8_t value, t_backlightPol pol = POSITIVE )
{
_backlightPinMask = ( 1 << value );
_polarity = pol;
setBacklight(BACKLIGHT_OFF);
}
//
// setBacklight
void LiquidCrystal_I2C::setBacklight( uint8_t value )
{
// Check if backlight is available
// ----------------------------------------------------
if ( _backlightPinMask != 0x0 )
{
// Check for polarity to configure mask accordingly
// ----------------------------------------------------------
if (((_polarity == POSITIVE) && (value > 0)) ||
((_polarity == NEGATIVE ) && ( value == 0 )))
{
_backlightStsMask = _backlightPinMask & LCD_BACKLIGHT;
}
else
{
_backlightStsMask = _backlightPinMask & LCD_NOBACKLIGHT;
}
_i2cio.write( _backlightStsMask );
}
}
// PRIVATE METHODS
// ---------------------------------------------------------------------------
//
// init
int LiquidCrystal_I2C::init()
{
int status = 0;
// initialize the backpack IO expander
// and display functions.
// ------------------------------------------------------------------------
if ( _i2cio.begin ( _Addr ) == 1 )
{
_i2cio.portMode ( OUTPUT ); // Set the entire IO extender to OUTPUT
_displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
status = 1;
_i2cio.write(0); // Set the entire port to LOW
}
return ( status );
}
//
// config
void LiquidCrystal_I2C::config (uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7 )
{
_Addr = lcd_Addr;
_backlightPinMask = 0;
_backlightStsMask = LCD_NOBACKLIGHT;
_polarity = POSITIVE;
_En = ( 1 << En );
_Rw = ( 1 << Rw );
_Rs = ( 1 << Rs );
// Initialise pin mapping
_data_pins[0] = ( 1 << d4 );
_data_pins[1] = ( 1 << d5 );
_data_pins[2] = ( 1 << d6 );
_data_pins[3] = ( 1 << d7 );
}
// low level data pushing commands
//----------------------------------------------------------------------------
//
// send - write either command or data
void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode)
{
// No need to use the delay routines since the time taken to write takes
// longer that what is needed both for toggling and enable pin an to execute
// the command.
if ( mode == FOUR_BITS )
{
write4bits( (value & 0x0F), COMMAND );
}
else
{
write4bits( (value >> 4), mode );
write4bits( (value & 0x0F), mode);
}
}
//
// write4bits
void LiquidCrystal_I2C::write4bits ( uint8_t value, uint8_t mode )
{
uint8_t pinMapValue = 0;
// Map the value to LCD pin mapping
// --------------------------------
for ( uint8_t i = 0; i < 4; i++ )
{
if ( ( value & 0x1 ) == 1 )
{
pinMapValue |= _data_pins[i];
}
value = ( value >> 1 );
}
// Is it a command or data
// -----------------------
if ( mode == LCD_DATA )
{
mode = _Rs;
}
pinMapValue |= mode | _backlightStsMask;
pulseEnable ( pinMapValue );
}
//
// pulseEnable
void LiquidCrystal_I2C::pulseEnable (uint8_t data)
{
_i2cio.write (data | _En); // En HIGH
_i2cio.write (data & ~_En); // En LOW
}