-
Notifications
You must be signed in to change notification settings - Fork 2
/
lcdDriver.cpp
312 lines (272 loc) · 8.73 KB
/
lcdDriver.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
304
305
306
307
308
309
310
311
312
// Copyright 2018 Chris Hamer
/* This file is part of RaspLCDDriver.
RaspLCDDriver 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
(at your option) any later version.
RaspLCDDriver 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
along with RaspLCDDriver. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "i2cControl.hpp"
#include "lcdDriver.hpp"
// Define some device constants
#define LCD_CHR 1 // Mode - Sending data
#define LCD_CMD 0 // Mode - Sending command
//LCD Available Commands
#define LCD_BLINK 0x0F
#define LCD_UNDERLINE_CURSOR 0x0E
#define LCD_CLEAR_DISPLAY_CLEAR_MEM 0x01
#define LCD_CLEAR_DISPLAY_KEEP_MEM 0x08
#define LCD_ENTRY_MODE 0x06 //04, 05, 06 & 07
#define LCD_DISPLAY_ON_CURSOR_OFF 0x0C
#define LCD_SET_4BIT_2LINE 0x28
#define LCD_SET_8BIT_2LINE 0x38
#define LCD_MOVE_CURSOR_RIGHT 0x14
#define LCD_MOVE_CURSOR_LEFT 0x10
#define LCD_RESET_CURSOR_POSITION 0x2
#define LCD_SCROLL_1_CHAR_RIGHT_ALL_LINES 0x1E
#define LCD_SCROLL_1_CHAR_LEFT_ALL_LINES 0x18
#define LCD_BACKLIGHT_ON 0x08 // On
#define LCD_BACKLIGHT_OFF 0x00 //Off
#define ENABLE 0b00000100 // Enable bit
#define READWRITE 0b00000010 // Read/Write bit
#define REGISTERSELECT 0b00000001 // Register select bit
#define LCD_SETCGRAMADDR 0x40 // Used for setting the custom chars
// Predefined Custom Char Addresses - Upper 4 bits come first from datasheet
#define LEFT_ARROW 0x7F
#define RIGHT_ARROW 0x7E
#define DEGREE_SYMBOL 0xDF
#define FULL_CHAR 0xFF
// Timing constants
#define E_PULSE 500 //micro Seconds
#define E_DELAY 500 //micro Seconds
LcdDriver::LcdDriver(unsigned char lcdAdd, I2cControl *i2c)
{
this->I2C_ADDR = lcdAdd;
this->i2c = i2c;
backlight = BACKLIGHT_ON;
lcdByte(0x33,LCD_CMD); // 110011 Initialise
lcdByte(0x32,LCD_CMD); // 110010 Initialise
lcdSendCommand(LCD_UNDERLINE_CURSOR);
lcdSendCommand(LCD_CLEAR_DISPLAY_CLEAR_MEM);
lcdSendCommand(LCD_ENTRY_MODE);
lcdSendCommand(LCD_SET_4BIT_2LINE);
usleep(E_DELAY);
}
void LcdDriver::blinkCursor() { lcdSendCommand(LCD_BLINK); }
void LcdDriver::enableUnderlineCursor() { lcdSendCommand(LCD_UNDERLINE_CURSOR); }
void LcdDriver::clearDisplayClearMem() { lcdSendCommand(LCD_CLEAR_DISPLAY_CLEAR_MEM); }
void LcdDriver::clearDisplayKeepMem() { lcdSendCommand(LCD_CLEAR_DISPLAY_KEEP_MEM); }
void LcdDriver::setEntryMode() { lcdSendCommand(LCD_ENTRY_MODE); }
void LcdDriver::setDisplayOnCursorOff() { lcdSendCommand(LCD_DISPLAY_ON_CURSOR_OFF); }
void LcdDriver::set4Bit2Line() { lcdSendCommand(LCD_SET_4BIT_2LINE); }
void LcdDriver::set8Bit2Line() { lcdSendCommand(LCD_SET_8BIT_2LINE); }
void LcdDriver::moveCursorRight() { lcdSendCommand(LCD_MOVE_CURSOR_RIGHT); }
void LcdDriver::moveCursorLeft() { lcdSendCommand(LCD_MOVE_CURSOR_LEFT); }
void LcdDriver::resetCursorPosition() { lcdSendCommand(LCD_RESET_CURSOR_POSITION); }
void LcdDriver::scroll1CharRightAllLines() { lcdSendCommand(LCD_SCROLL_1_CHAR_RIGHT_ALL_LINES); }
void LcdDriver::scroll1CharLeftAllLines() { lcdSendCommand(LCD_SCROLL_1_CHAR_LEFT_ALL_LINES); }
void LcdDriver::lcdToggleEnable(unsigned char bits)
{
// Toggle enable
usleep(E_DELAY);
i2c->writeByte(this->I2C_ADDR, (bits | ENABLE));
usleep(E_PULSE);
i2c->writeByte(this->I2C_ADDR,(bits & ~ENABLE));
usleep(E_DELAY);
}
void LcdDriver::error(const char *msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(1);
}
void LcdDriver::lcdByte(unsigned char bits, unsigned char mode)
{
unsigned char bits_high, bits_low;
// Send byte to data pins
// bits = the data
// mode = 1 for data
// 0 for command
if(backlight == BACKLIGHT_OFF)
{
bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT_OFF;
bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT_OFF;
}
else
{
bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT_ON;
bits_low = mode | ((bits << 4) & 0xF0) | LCD_BACKLIGHT_ON;
}
// High bits
i2c->writeByte(this->I2C_ADDR, bits_high);
lcdToggleEnable(bits_high);
// Low bits
i2c->writeByte(this->I2C_ADDR, bits_low);
lcdToggleEnable(bits_low);
}
void LcdDriver::setCursorPositionHex(unsigned char position)
{
if(position < LCD_BEG_LINE_1 || position > LCD_END_LINE_4)
{
char buffer[60];
sprintf(buffer, "Invalid hex position 0x%02x\nValid position is 0x%02x - 0x%02x", position, LCD_BEG_LINE_1, LCD_END_LINE_4);
error(buffer);
}
else
{
lcdSendCommand(position);
}
}
void LcdDriver::setCursorPositionRowCol(int row, int col)
{
if(row < 1 || row > LCD_ROWS)
{
char buffer[60];
sprintf(buffer, "Invalid position, no such row %i\nValid rows are 1 - %i", row, LCD_ROWS);
error(buffer);
}
else if(col < 0 || col > LCD_WIDTH-1)
{
char buffer[60];
sprintf(buffer, "Invalid position, no such col %i\nValid columns are 0 - %i", col, LCD_WIDTH-1);
error(buffer);
}
else
{
lcdSendCommand(convertRowColtoHex(row, col));
}
}
unsigned char LcdDriver::convertRowColtoHex(int row, int col)
{
unsigned char currentLineHex;
char buffer[70];
switch(row)
{
case 1:
currentLineHex = LCD_BEG_LINE_1;
break;
case 2:
currentLineHex = LCD_BEG_LINE_2;
break;
case 3:
currentLineHex = LCD_BEG_LINE_3;
break;
case 4:
currentLineHex = LCD_BEG_LINE_4;
break;
default:
sprintf(buffer, "No such row number %i\nValid rows are 1 - %i", row, LCD_ROWS);
error(buffer);
}
if(col < LCD_WIDTH & col >= 0)
{
return (currentLineHex+(col));
}
else
{
sprintf(buffer, "No such col number %i\n.Valid cols are 0 - %i", col, LCD_WIDTH-1);
error(buffer);
}
}
void LcdDriver::lcdSendCommand(unsigned char command)
{
lcdByte(command, LCD_CMD);
}
void LcdDriver::lcdSendCustomChar(char cgramAddress)
{
lcdSendChar(cgramAddress);
}
void LcdDriver::lcdSendChar(char singleChar)
{
lcdByte(singleChar, LCD_CHR);
}
void LcdDriver::lcdString(const char * message)
{
for(int i = 0; i<strlen(message); i++)
{
lcdByte(message[i],LCD_CHR);
}
}
void LcdDriver::clearColumnsHex(unsigned char positionToClearTo, unsigned char positionToClearFrom)
{
int numberOfColsToClear = positionToClearTo - positionToClearFrom;
if(numberOfColsToClear < LCD_WIDTH & numberOfColsToClear > 0)
{
setCursorPositionHex(positionToClearFrom);
for(int i = 0; i <= numberOfColsToClear; i++)
{
lcdString(" ");
}
}
else
{
char buffer[60];
sprintf(buffer, "Can only clear one line at a time. Exceeded LCD Width %i\n", LCD_WIDTH);
error(buffer);
}
}
void LcdDriver::clearColumnsRowCol(int row, int colToClearTo, int colToClearFrom)
{
unsigned char positionToClearTo = convertRowColtoHex(row, colToClearTo);
unsigned char positionToClearFrom = convertRowColtoHex(row, colToClearFrom);
clearColumnsHex(positionToClearTo, positionToClearFrom);
}
void LcdDriver::clearLine(int lineNo)
{
switch(lineNo)
{
case 1:
LcdDriver::clearColumnsHex(LCD_END_LINE_1,LCD_BEG_LINE_1);
break;
case 2:
clearColumnsHex(LCD_END_LINE_2,LCD_BEG_LINE_2);
break;
case 3:
clearColumnsHex(LCD_END_LINE_3,LCD_BEG_LINE_3);
break;
case 4:
clearColumnsHex(LCD_END_LINE_3,LCD_BEG_LINE_3);
break;
default:
error("No such line to clear!");
}
}
void LcdDriver::changeBacklight()
{
if(backlight == BACKLIGHT_ON)
backlight = BACKLIGHT_OFF;
else if(backlight == BACKLIGHT_OFF)
backlight = BACKLIGHT_ON;
}
void LcdDriver::createCustomChar(unsigned char location, unsigned char charMap[])
{
location &= 0x7; // we only have 8 locations 0-7. location = location & 0x7
lcdSendCommand(LCD_SETCGRAMADDR | (location << 3));
for (int i=0; i<8; i++) {
lcdByte(charMap[i], 0b00000001);
}
}
void LcdDriver::drawLeftArrow()
{
lcdSendChar(LEFT_ARROW);
}
void LcdDriver::drawRightArrow()
{
lcdSendChar(RIGHT_ARROW);
}
void LcdDriver::drawDegreeSymbol()
{
lcdSendChar(DEGREE_SYMBOL);
}
void LcdDriver::drawFullChar()
{
lcdSendChar(FULL_CHAR);
}