-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniversallcd.cpp
More file actions
216 lines (173 loc) · 4.57 KB
/
universallcd.cpp
File metadata and controls
216 lines (173 loc) · 4.57 KB
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
#include "universallcd.hpp"
namespace universalLCD{
const uint8_t UniversalLCD::addrs[] = {0x80, 0xC0};
void UniversalLCD::send8Bits(uint8_t data, uint8_t RS_state){
bridge.send(RS_state, data);
bridge.send(RS_state | enable_set, data);
delay(1);
bridge.send(RS_state, data);
}
void UniversalLCD::send4Bits(uint8_t data, uint8_t RS_state){
uint8_t high_nibble = data & 0xF0;
uint8_t low_nibble = data << 4;
send8Bits(high_nibble, RS_state);
delay(1);
send8Bits(low_nibble, RS_state);
}
UniversalLCD& UniversalLCD::send(uint8_t data, uint8_t RS_state){
if(bus == Bus8Bits){
send8Bits(data, RS_state);
}else{
send4Bits(data, RS_state);
}
if(RS_state == 1){
delay(2);
}
else{
delay(40);
}
return *this;
}
UniversalLCD& UniversalLCD::begin(){
delay(55000); //change 2024-01-10 (add power on wait time + init time) (datasheet: power up time = mora than 40ms| begin time = more than 15ms)
send8Bits(0x30, 0);
delay(4100);
send8Bits(0x30, 0);
delay(100);
send8Bits(0x30, 0);
delay(100);
if(bus == Bus4Bits){
send8Bits(0x20, 0);
}
send(function_set, 0);
clear();
reset();
send(entry_mode, 0);
send(display_control, 0);
selectLcd(0);
return *this;
}
/* ----------------- DISPLAY COMMANDS ---------------------*/
UniversalLCD& UniversalLCD::command(Command command){
send(command, 0);
return *this;
}
UniversalLCD& UniversalLCD::clear(){
command(LCDClear);
delay(1600); //clear e reset demoram 1.52ms para executar
return *this;
}
UniversalLCD& UniversalLCD::reset(){
command(LCDReset);
delay(1600); //clear e reset demoram 1.52ms para executar
return *this;
}
UniversalLCD& UniversalLCD::moveCursorLeft(){
command(LCDShiftCursotLeft);
return *this;
}
UniversalLCD& UniversalLCD::moveCursorRight(){
command(LCDShiftCursotRight);
return *this;
}
UniversalLCD& UniversalLCD::moveDisplayLeft(){
command(LCDShiftDisplayLeft);
return *this;
}
UniversalLCD& UniversalLCD::moveDisplayRight(){
command(LCDShiftDisplayRight);
return *this;
}
/* -----------------END OF DISPLAY COMMANDS ---------------------*/
/* ------------------ DISPLAY CONFIGURATION ------------------------*/
UniversalLCD& UniversalLCD::shiftOn(){
entry_mode |= LCDShiftMode;
send(entry_mode, 0);
return *this;
}
UniversalLCD& UniversalLCD::shiftOff(){
entry_mode &= ~LCDShiftMode;
send(entry_mode, 0);
return *this;
}
UniversalLCD& UniversalLCD::increment(){
entry_mode |= LCDDirection;
send(entry_mode,0);
return *this;
}
UniversalLCD& UniversalLCD::decrement(){
entry_mode &= ~LCDDirection;
send(entry_mode,0);
return *this;
}
UniversalLCD& UniversalLCD::cursorBlinkOn(){
display_control |= LCDBlink;
send(display_control,0);
return *this;
}
UniversalLCD& UniversalLCD::cursorBlinkOff(){
display_control &= ~LCDBlink;
send(display_control,0);
return *this;
}
UniversalLCD& UniversalLCD::cursorOn(){
display_control |= LCDCursor;
send(display_control,0);
return *this;
}
UniversalLCD& UniversalLCD::cursorOff(){
display_control &= ~LCDCursor;
send(display_control,0);
return *this;
}
UniversalLCD& UniversalLCD::displayOn(){
display_control |= LCDDisplay;
send(display_control,0);
return *this;
}
UniversalLCD& UniversalLCD::displayOff(){
display_control &= ~LCDDisplay;
send(display_control,0);
return *this;
}
/*------------------- END DISPLAY CONFIGURATION ---------------------*/
UniversalLCD& UniversalLCD::setCursor(uint8_t line, uint8_t col){
if((line < 2) && (col < 40)){
send(addrs[line]+col, 0);
}
return *this;
}
UniversalLCD& UniversalLCD::createChar(uint8_t c[8], uint8_t pos){
pos &= 0b00000111;
send(0x40 | (pos<<3), 0);
for(int i = 0; i < 8; i++){
send(c[i], 1);
}
return *this;
}
UniversalLCD& UniversalLCD::echo(){
enable_set = 0b1100;
return *this;
}
UniversalLCD& UniversalLCD::selectLcd(uint8_t enable){
if(enable < 2){
enable_set = 0b100<<enable;
}
return *this;
}
UniversalLCD& UniversalLCD::write_char_delimiter(const char text[], char delimiter){
for(int i = 0; text[i] != delimiter ; i++){
send(text[i], 1);
}
return *this;
}
UniversalLCD& UniversalLCD::write_limit(const char text[], unsigned int size){
for(int i = 0; i < size ; i++){
send(text[i], 1);
}
return *this;
}
UniversalLCD& UniversalLCD::write(const char text[]){
return write_char_delimiter(text, '\0');
}
}