forked from gadjet/1602-OLED-Arduino-Library
-
Notifications
You must be signed in to change notification settings - Fork 2
/
US2066.cpp
216 lines (173 loc) · 3.71 KB
/
US2066.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
/*
* The US2066 OLED driver library via i2c interface
* Written by Pedro Pacheco
* Modified from gadjet(Phil Grant)'s 1602 OLED Arduino Library on github
*
* Functions documented on the US2066.h file
*/
/* Notes:
* SD on 78, off 79
*
*/
#include "US2066.h"
#include "Wire.h"
#include "Math.h"
#define DEFAULTAddress 0x3c
#define OLED_Command_Mode 0x80
#define OLED_Data_Mode 0x40
#define BAR_Beggining 0xDA //Works only in ROM-A
US2066::US2066() {}
US2066::US2066(uint8_t address) {
this->addr = address;
}
US2066::~US2066() {}
void US2066::init() {
// *** I2C initialization from the OLEDM1602 datasheet *** //
delay(100);
Wire.begin();
//Disable internal regulator
//sendCommand(0x2a); //RE=1
//sendCommand(0x71);
//sendCommand(0x00);
//sendCommand(0x28); //RE=0, IS=0
//set display to OFF
sendCommand(0x08);
//Set display clock devide ratio, oscillator freq
sendCommand(0x2a); //RE=1
sendCommand(0x79); //SD=1
sendCommand(0xd5);
sendCommand(0x70);
sendCommand(0x78); //SD=0
//Set display mode
sendCommand(0x08);
//Set remap
sendCommand(0x06);
//CGROM/CGRAM Management
sendCommand(0x72);
sendData(0x01); //ROM A
//Set OLED Characterization
sendCommand(0x2a); //RE=1
sendCommand(0x79); //SD=1
//Set SEG pins Hardware configuration
sendCommand(0xda);
sendCommand(0x10);
//Set contrast control
sendCommand(0x81);
sendCommand(0xff);
//Set precharge period
sendCommand(0xd9);
sendCommand(0xf1);
//Set VCOMH Deselect level
sendCommand(0xdb);
sendCommand(0x30);
//Exiting Set OLED Characterization
sendCommand(0x78); //SD=0
sendCommand(0x28); //RE=0, IS=0
//Clear display
sendCommand(0x01);
//Set DDRAM Address
sendCommand(0x80);
delay(100);
//Set display to ON
sendCommand(0x0c);
}
void US2066::cursor(uint8_t row, uint8_t col)
{
int row_offsets[] = { 0x00, 0x40 };
sendCommand(0x80 | (col + row_offsets[row]));
}
void US2066::clear()
{
sendCommand(0x01);
}
void US2066::off()
{
state &= ~ON;
updateState();
}
void US2066::on()
{
state |= ON;
updateState();
}
void US2066::home()
{
cursor(0,0);
}
void US2066::blinkingCursor(uint8_t state)
{
if(state)
{
this->state |= BLINKING_CURSOR;
}
else
{
this->state &= ~BLINKING_CURSOR;
}
updateState();
}
void US2066::print(uint8_t row, uint8_t col, const char *String)
{
cursor(row, col);
print(String);
}
void US2066::print(const char *String)
{
int i = 0;
while (String[i])
{
sendData(String[i]);
i++;
}
}
void US2066::print(char c)
{
sendData(c);
}
void US2066::bar(uint8_t row, uint8_t col, int size, int value){
char bar[size+2];
int i=0;
for(;i < size*value/100;i++){
bar[i] = BAR_Beginning - 4;//Full bar char
}
int v = (size*value)%100/20;
if(!v)
bar[i]=' ';
else
bar[i] = BAR_Beginning-(v-1);
bar[++i]=0;
print(row, col, bar);
}
void US2066::updateState(){
sendCommand( 0x08 | state );
}
void US2066::sendCommand(unsigned char command)
{
Wire.beginTransmission(addr);
Wire.write(OLED_Command_Mode);
Wire.write(command);
Wire.endTransmission();
}
void US2066::sendData(unsigned char data)
{
Wire.beginTransmission(addr);
Wire.write(OLED_Data_Mode);
Wire.write(data);
Wire.endTransmission();
}
void US2066::sendDataContinuation(unsigned char data)
{
Wire.beginTransmission(addr);
Wire.write(OLED_Data_Mode | OLED_Command_Mode);
Wire.write(data);
Wire.endTransmission();
}
void US2066::contrast(unsigned char contrast)
{
sendCommand(0x2A);
sendCommand(0x79); //Set OLED Command set
sendCommand(0x81); // Set Contrast
sendCommand(contrast); // send contrast value
sendCommand(0x78); // Exiting Set OLED Command set
sendCommand(0x28);
}