-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSuperST7032.cpp
295 lines (251 loc) · 7.03 KB
/
SuperST7032.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
/*
*
* SuperST7032 - v1.0
* Code by Gavin Tryzbiak
* https://github.com/GavinTryz
*
* Arduino library built to communicate with any LCD using the ST7032 LCD controller (such as AQM0802A-RN-GBW, ERC1602FYG-4, and more.)
* Includes standard LCD functionality in addition to extra features not typically found in most LCD libraries.
*
*/
#include "SuperST7032.h"
#define DEFAULT_CONTRAST 25
// Constructors ///////////////////////////////////////////////////////////////////////
SuperST7032::SuperST7032() // No specified address (assume 0x3E)
{
_slaveAdr = 0x3E;
}
SuperST7032::SuperST7032(uint8_t addressInput) // Specific address
{
_slaveAdr = addressInput;
}
// Methods inherited from Print class /////////////////////////////////////////////////
size_t SuperST7032::write(uint8_t c)
{
Wire.beginTransmission(_slaveAdr);
Wire.write(0b01000000); // CO = 0, RS = 1
Wire.write(c);
Wire.endTransmission();
}
// Public Members /////////////////////////////////////////////////////////////////////
void SuperST7032::begin()
{
Wire.begin();
// Initialization sequence derived from Sitronix ST7032 datasheet page 33
command(0b00111000); // "Function set"
command(0b00111001); // "Function set"
command(0b00000100); // "EntryMode set (not listed on datasheet)
command(0b00010100); // "Internal OSC frequency"
command(0b01110000 | (DEFAULT_CONTRAST & 0xF)); // "Contrast set"
command(0b01011100 | ((DEFAULT_CONTRAST >> 4) & 0x3)); // "Power/ICON/Contrast control"
command(0b01101100); // "Follower control"
delay(200);
command(0b00111000); // "Function set" Sets interface length, double line, normal font, instruction table 0
command(0b00001100); // Display On
command(0b00000001); // Clear Display
delay(2);
home();
clear();
}
void SuperST7032::clear()
{
command(0b00000001);
}
void SuperST7032::scan(uint16_t stepTime, uint16_t stopTime, bool rollBack)
{
home();
setCursor(0, 0);
delay(stopTime-stepTime);
for(int i = 0; i < 32; i++)
{
delay(stepTime);
scrollDisplayLeft();
}
if(rollBack)
{
delay(stopTime-stepTime);
for(int i = 0; i < 32; i++)
{
delay(stepTime);
scrollDisplayRight();
}
}
}
void SuperST7032::scan()
{
scan(250, 750, true);
}
void SuperST7032::home()
{
command(0b00000010);
}
void SuperST7032::setCursor(uint8_t x, uint8_t y)
{
command(0b10000000 | ((y%2) * 0b01000000 + x));
}
void SuperST7032::cursor(bool x)
{
setState(0b00000010, x); // Sets bit 1 (cursor on/off) to x
updateDisplaySettings();
}
void SuperST7032::cursor()
{
cursor(true);
}
void SuperST7032::noCursor()
{
cursor(false);
}
void SuperST7032::blink(bool x)
{
setState(0b00000100, x); // Sets bit 2 (cursor blink on/off) to x
updateDisplaySettings();
}
void SuperST7032::blink()
{
blink(true);
}
void SuperST7032::noBlink()
{
blink(false);
}
void SuperST7032::display(bool x)
{
setState(0b00000001, x); // Sets bit 0 (display on/off) to x
updateDisplaySettings();
}
void SuperST7032::display()
{
display(true);
}
void SuperST7032::noDisplay()
{
display(false);
}
void SuperST7032::scrollDisplayLeft()
{
command(0b00011000);
}
void SuperST7032::scrollDisplayLeft(uint16_t count)
{
for(uint16_t i = 0; i < count; i++)
scrollDisplayLeft();
}
void SuperST7032::scrollDisplayRight()
{
command(0b00011100);
}
void SuperST7032::scrollDisplayRight(uint16_t count)
{
for(uint16_t i = 0; i < count; i++)
scrollDisplayRight();
}
void SuperST7032::autoscroll(bool x)
{
setState(0b00010000, x); // Sets bit 4 (autoscroll on/off) to x
updateWriteSettings();
}
void SuperST7032::autoscroll()
{
autoscroll(true);
}
void SuperST7032::noAutoscroll()
{
autoscroll(false);
}
void SuperST7032::leftToRight()
{
_states = _states | 0b00001000; // Sets bit 3 (write direction) to 1 (left to right)
updateWriteSettings();
}
void SuperST7032::rightToLeft()
{
_states = _states & 0b11110111; // Sets bit 3 (write direction) to 0 (right to left)
updateWriteSettings();
}
void SuperST7032::createChar(uint8_t charAddress, uint8_t charMap[])
{
if(charAddress > 7) // Verifies you only write to character addresses 0 - 7
return;
command(0b01000000 | (charAddress << 3));
for (byte i = 0; i < 8; i++)
write(charMap[i]);
setCursor(0, 0);
}
void SuperST7032::contrast(uint8_t x)
{
setState(0b10000000, 1); // Use instruction table 1
updateFunctionSettings();
command(0b01110000 | (x & 0xF)); // "Contrast set"
command(0b01011100 | ((x >> 4) & 0x3)); // "Power/ICON/Contrast control"
setState(0b10000000, 0); // Back to instruction table 0
updateFunctionSettings();
}
bool SuperST7032::doubleLine(bool x)
{
if(x)
return doubleLine();
return singleLine();
}
bool SuperST7032::doubleLine()
{
if(_states >> 6 & 0b00000001) // Verify the font isn't currently large
return false;
_states = _states | 0b00100000; // Set bit 5 (line count) to 1 (double)
updateFunctionSettings();
return true;
}
bool SuperST7032::singleLine()
{
_states = _states & 0b11011111;// Set bit 5 (line count) to 0 (single)
updateFunctionSettings();
return true;
}
bool SuperST7032::largeFont(bool x)
{
if(x)
return largeFont();
return normalFont();
}
bool SuperST7032::largeFont()
{
if(_states >> 5 & 0b00000001) // Verify the display isn't currently set to 2-line mode
return false;
_states = _states | 0b01000000; // Set bit 6 (font size) to 1 (double height)
updateFunctionSettings();
return true;
}
bool SuperST7032::normalFont()
{
_states = _states & 0b10111111; // Set bit 6 (font size) to 1 (single/normal height)
updateFunctionSettings();
return true;
}
// Private Members ////////////////////////////////////////////////////////////////////
void SuperST7032::command(uint8_t cmd)
{
Wire.beginTransmission(_slaveAdr);
Wire.write(0b00000000); // CO = 0, RS = 0, Declare that a command is about to be sent
Wire.write(cmd);
Wire.endTransmission();
delay(2); // The slowest instructions can take up to just over 1ms to execute
}
void SuperST7032::setState(uint8_t mask, bool x)
{
if(x)
_states = _states | mask;
else
_states = _states & ~mask;
}
void SuperST7032::updateDisplaySettings() // Updates "Display ON/OFF" [0 0 0 0 1 D C B] (display, cursor, blink)
{
command(0b00001000 | (0b00000100 & _states << 2) | (0b00000010 & _states) | (0b00000001 & _states >> 2));
}
void SuperST7032::updateWriteSettings() // Updates "Entry Mode Set" [0 0 0 0 0 1 I/D S] (write direction, autoscroll)
{
command(0b00000100 | (0b00000010 & _states >> 2) | (0b00000001 & _states >> 4));
}
void SuperST7032::updateFunctionSettings() // Updates "Function Set" [0 0 1 DL N DH 0 IS] (interface length (always high), line count, font size, instruction table set)
{
command(0b00110000 | (0b00001000 & _states >> 2) | (0b00000100 & _states >> 4) | (0b00000001 & _states >> 7));
}