-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCD.cpp
174 lines (154 loc) · 4.65 KB
/
LCD.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
#include "LCD.h"
#include "pico/stdlib.h"
#include "bitset"
using namespace Actinium;
std::string CharToLCDBin(char character) {
BYTE index = 0;
BYTE newLocation;
if(isalpha(character)) {
if(isupper(character)) {
index = character - (char)'A';
newLocation = LCD_UPPERCASE_START_INDEX + index;
} else {
index = character - (char)'a';
newLocation = LCD_LOWERCASE_START_INDEX + index;
}
}
else if(isdigit(character)) {
index = character - (char)'0';
newLocation = LCD_NUMERIC_START_INDEX + index;
}
else {
switch(character) {
case ' ':
newLocation = 0b10000000;
break;
case '#':
newLocation = 0b00100011;
break;
case '!':
newLocation = 0b00100001;
break;
}
}
return std::bitset<8>(newLocation).to_string();
}
void LCD::Apply() {
sleep_ms(2);
this->m_E_PIN.On();
sleep_ms(2);
this->m_E_PIN.Off();
}
void LCD::SetPins(BIT RS,
BIT RW,
BIT DB7,
BIT DB6,
BIT DB5,
BIT DB4,
BIT DB3,
BIT DB2,
BIT DB1,
BIT DB0) {
this->m_RS_PIN.Write(RS);
this->m_RW_PIN.Write(RW);
this->m_DB7_PIN.Write(DB7);
this->m_DB6_PIN.Write(DB6);
this->m_DB5_PIN.Write(DB5);
this->m_DB4_PIN.Write(DB4);
this->m_DB3_PIN.Write(DB3);
this->m_DB2_PIN.Write(DB2);
this->m_DB1_PIN.Write(DB1);
this->m_DB0_PIN.Write(DB0);
this->Apply();
}
void LCD::SetPins(std::string bits) {
if(bits.length() != 10)
return; // Data is wonky. No good!
this->m_RS_PIN.Write(bits[0] == '1');
this->m_RW_PIN.Write(bits[1] == '1');
this->m_DB7_PIN.Write(bits[2] == '1');
this->m_DB6_PIN.Write(bits[3] == '1');
this->m_DB5_PIN.Write(bits[4] == '1');
this->m_DB4_PIN.Write(bits[5] == '1');
this->m_DB3_PIN.Write(bits[6] == '1');
this->m_DB2_PIN.Write(bits[7] == '1');
this->m_DB1_PIN.Write(bits[8] == '1');
this->m_DB0_PIN.Write(bits[9] == '1');
this->Apply();
}
void LCD::ResetPins(bool ResetEnable) {
if(ResetEnable)
this->m_E_PIN.Off();
this->SetPins(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
void LCD::ResetCursor() {
this->SetPins(0, 0, 0, 0, 0, 0, 0, 0, 1, 0);
}
void LCD::CursorPos(BYTE position) {
this->ResetCursor();
for(int i = 0; i < position; ++i)
this->CursorRight();
}
void LCD::CursorEnd() {
this->CursorPos(16);
}
void LCD::CursorRight() {
this->SetPins(0, 0, 0, 0, 0, 1, 0, 1, 0, 0);
}
void LCD::CursorLeft() {
this->SetPins(0, 0, 0, 0, 0, 1, 0, 0, 0, 0);
}
void LCD::ClearDisplay() {
this->SetPins(0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
}
void LCD::WriteChar(char input) {
this->SetPins("10" + CharToLCDBin(input));
}
void LCD::WriteString(std::string input, unsigned int sleep) {
for(int i = 0; i < input.length(); ++i) {
this->WriteChar(input[i]);
sleep_ms(sleep);
}
}
void LCD::EntryMode(ShiftDirection Shift, bool ShiftDisplay) {
BIT ShiftRight = Shift == ShiftDirection::RIGHT;
this->SetPins(0, 0, 0, 0, 0, 0, 0, 1, ShiftRight, ShiftDisplay);
}
void LCD::FunctionSet(LineCount NumLines, FontSize dispSize) {
BIT TwoLines = NumLines == LineCount::TWO;
BIT Size = dispSize == FontSize::FiveByEleven;
this->SetPins(0, 0, 0, 0, 1, 1, TwoLines, Size, 0, 0);
}
void LCD::On(bool cursor, bool cursorBlink) {
this->SetPins(0, 0, 0, 0, 0, 0, 1, 1, cursor, cursorBlink);
}
void LCD::Off() {
this->SetPins(0, 0, 0, 0, 0, 0, 1, 1, 0, 0);
}
LCD::LCD(BYTE RS,
BYTE RW,
BYTE E,
BYTE DB7,
BYTE DB6,
BYTE DB5,
BYTE DB4,
BYTE DB3,
BYTE DB2,
BYTE DB1,
BYTE DB0)
{
this->m_RS_PIN = Pin(RS, PinMode::OUT);
this->m_RW_PIN = Pin(RW, PinMode::OUT);
this->m_E_PIN = Pin(E, PinMode::OUT);
this->m_DB7_PIN = Pin(DB7, PinMode::OUT);
this->m_DB6_PIN = Pin(DB6, PinMode::OUT);
this->m_DB5_PIN = Pin(DB5, PinMode::OUT);
this->m_DB4_PIN = Pin(DB4, PinMode::OUT);
this->m_DB3_PIN = Pin(DB3, PinMode::OUT);
this->m_DB2_PIN = Pin(DB2, PinMode::OUT);
this->m_DB1_PIN = Pin(DB1, PinMode::OUT);
this->m_DB0_PIN = Pin(DB0, PinMode::OUT);
this->ClearDisplay();
this->FunctionSet(LineCount::TWO, FontSize::FiveByEight);
this->EntryMode(ShiftDirection::RIGHT, false);
}