forked from vansatchen/OPiZero2Plus-ssd1306-i2c
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssd1306.cpp
208 lines (187 loc) · 5.51 KB
/
ssd1306.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
#include <ctype.h>
#include "charmap.h"
#include "ssd1306.h"
#ifdef DEBUG
#define D if(1)
#else
#define D if(0)
#endif
SSD1306::SSD1306() {
for(int i=0; i<8; i++)
{
for(int j=0; j<128; j++)
{
displayLines[i][j] = 0;
}
}
}
void SSD1306::initDisplay() {
D printf("initDisplay\n");
writeI2C(initSequence, 26);
i2cInitialised = TRUE;
}
void SSD1306::textDisplay(const char *message) {
if(!i2cInitialised)
{
initDisplay();
}
// and scroll up if needed (in SCROLL mode)
if((displayMode == SSD1306::Mode::SCROLL) && needScroll)
{
scrollUp(1);
}
D printf("textDisplay - %s\n", message);
int currByteCount = 0;
for(int i=0; i<strlen(message); i++)
{
int bytesAdded = addFontBytes(currByteCount, message[i]);
if(bytesAdded > 0)
{
currByteCount += bytesAdded;
}
else
{
if(wordWrap)
{
// fill the rest of this line with 0s
while(currByteCount < 128)
{
displayLines[currentLine][currByteCount++] = 0x00;
}
// move it onto the next line
currentLine++;
// and scroll up if needed (in SCROLL mode)
if((displayMode == SSD1306::Mode::SCROLL) && needScroll)
{
scrollUp(1);
}
if (currentLine > 7)
{
currentLine = 0;
needScroll = TRUE;
}
currByteCount=0;
i--; // move back one so we try it again
}
}
}
// now fill up any left over with 0x00s on the current line
while(currByteCount < 128)
{
displayLines[currentLine][currByteCount++] = 0x00;
}
// now point to next line and set to first char
currByteCount = 0;
currentLine++;
if(currentLine > 7)
{
currentLine = 0;
needScroll = TRUE;
}
updateDisplayFull();
}
void SSD1306::clearDisplay() {
// blank out the line buffers
for(int i=0; i<8; i++)
{
for(int j=0; j<128; j++)
{
displayLines[i][j] = 0;
}
}
//reset the scroll pointer and line pointer
currentScrollLine = 0;
currentLine = 0;
// write it out
updateDisplayFull();
}
void SSD1306::setWordWrap(int b) {
wordWrap = b;
}
void SSD1306::setDisplayMode(SSD1306::Mode mode) {
displayMode = mode;
}
// private functions
void SSD1306::scrollUp(int lines) {
if(!i2cInitialised)
{
initDisplay();
}
currentScrollLine += lines;
if (currentScrollLine > 7) { currentScrollLine -= 8; }
scrollUpSequence[2] = currentScrollLine * 8;
D printf("scrollUp\n");
writeI2C(scrollUpSequence, 3);
}
int SSD1306::addFontBytes(int curr, unsigned char c) {
D printf("addFontBytes - %i - ", c);
c = toupper(c); // we only support UPPERCASE letters
int letterIdx = (c - ' ');
if(letterIdx > 64)
{
letterIdx = 65;
}
int letterBytes = fontData[letterIdx][0];
if((curr + letterBytes + 1) > 127 )
{
return 0;
D printf("\n");
}
else
{
for(int i=0; i<letterBytes; i++)
{
D printf("%x ", fontData[letterIdx][1 + i]);
displayLines[currentLine][curr + i] = fontData[letterIdx][1 + i];
}
displayLines[currentLine][curr + letterBytes++] = 0x00; // single byte space / seperator
D printf("\n");
return letterBytes;
}
}
void SSD1306::setDisplayRange(int line = -1) {
// -1 = full range
// 0..7 = line
D printf("setDisplayRange (7 bytes)\n");
if(line == -1)
{
writeI2C(setFullRange, 7);
}
}
void SSD1306::updateDisplayFull() {
setDisplayRange(-1);
for(int line=0; line<8; line++)
{
unsigned char buffer[129] = {0};
buffer[0] = 0x40;
for(int i=0; i<128; i++) {
buffer[1 + i] = displayLines[line][i];
}
writeI2C(buffer, 129);
}
}
void SSD1306::writeI2C(unsigned char* data, int bytes) {
char *deviceName = (char*)"/dev/i2c-0";
if ((i2cHandle = open(deviceName, O_RDWR)) < 0)
{
printf("error opening I2C\n");
}
else
{
D printf("Opened I2C bus (Handle = %i)\n", i2cHandle);
if (ioctl(i2cHandle, I2C_SLAVE, i2cAddress) < 0)
{
printf("Error at ioctl\n");
}
else
{
D printf("writeI2C : ");
for(int i=0; i<bytes; i++) { D printf("%x ", data[i]); }
write(i2cHandle, data, bytes);
D printf("\n");
}
// Close the i2c device bus
char *deviceName = (char*)"dev/i2c-0";
close(*deviceName);
}
}