-
Notifications
You must be signed in to change notification settings - Fork 22
/
ACROBOTIC_SSD1306.cpp
executable file
·343 lines (308 loc) · 8.24 KB
/
ACROBOTIC_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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
06/01/2016
Author: Makerbro
Platforms: ESP8266
Language: C++
File: ACROBOTIC_SSD1306.cpp
------------------------------------------------------------------------
Description:
SSD1306 OLED Driver Library.
------------------------------------------------------------------------
Please consider buying products from ACROBOTIC to help fund future
Open-Source projects like this! We'll always put our best effort in every
project, and release all our design files and code for you to use.
https://acrobotic.com/
------------------------------------------------------------------------
License:
Released under the MIT license. Please check LICENSE.txt for more
information. All text above must be included in any redistribution.
*/
#include "ACROBOTIC_SSD1306.h"
void ACROBOTIC_SSD1306::init(TwoWire& wire)
{
m_wire = &wire;
displayOff(); //display off
sendCommand(0xA6); //Set Normal Display (default)
displayOff(); //display off
sendCommand(0xD5); //SETDISPLAYCLOCKDIV
sendCommand(0x80); // the suggested ratio 0x80
sendCommand(0xA8); //SSD1306_SETMULTIPLEX
sendCommand(SSD1306_Max_Y);
sendCommand(0xD3); //SETDISPLAYOFFSET
sendCommand(0x0); //no offset
sendCommand(0x40|0x0); //SETSTARTLINE
sendCommand(0x8D); //CHARGEPUMP
sendCommand(0x14);
sendCommand(0x20); //MEMORYMODE
sendCommand(0x00); //0x0 act like ks0108
sendCommand(0xA1); //SEGREMAP Mirror screen horizontally (A0)
sendCommand(0xC8); //COMSCANDEC Rotate screen vertically (C0)
sendCommand(0xDA); //0xDA
sendCommand(0x12); //COMSCANDEC
sendCommand(0x81); //SETCONTRAST
sendCommand(0xCF); //
sendCommand(0xd9); //SETPRECHARGE
sendCommand(0xF1);
sendCommand(0xDB); //SETVCOMDETECT
sendCommand(0x40);
sendCommand(0xA4); //DISPLAYALLON_RESUME
sendCommand(0xA6); //NORMALDISPLAY
clearDisplay();
sendCommand(0x2E); //Stop scroll
sendCommand(0x20); //Set Memory Addressing Mode
sendCommand(0x00); //Set Memory Addressing Mode ab Horizontal addressing mode
setFont(font8x8);
}
void ACROBOTIC_SSD1306::displayOn()
{
sendCommand(0xAF);
}
void ACROBOTIC_SSD1306::displayOff()
{
sendCommand(0xAE);
}
void ACROBOTIC_SSD1306::setFont(const uint8_t* font, bool inverse)
{
m_font = font;
m_inverse=inverse;
m_font_width = pgm_read_byte(&m_font[0]);
}
void ACROBOTIC_SSD1306::sendCommand(unsigned char command)
{
m_wire->beginTransmission(SSD1306_Address); // begin I2C communication
m_wire->write(SSD1306_Command_Mode); // Set OLED Command mode
m_wire->write(command);
m_wire->endTransmission(); // End I2C communication
}
void ACROBOTIC_SSD1306::setBrightness(unsigned char Brightness)
{
sendCommand(SSD1306_Set_Brightness_Cmd);
sendCommand(Brightness);
}
void ACROBOTIC_SSD1306::setHorizontalMode()
{
addressingMode = HORIZONTAL_MODE;
sendCommand(0x20); //set addressing mode
sendCommand(0x00); //set horizontal addressing mode
}
void ACROBOTIC_SSD1306::setPageMode()
{
addressingMode = PAGE_MODE;
sendCommand(0x20); //set addressing mode
sendCommand(0x02); //set page addressing mode
}
void ACROBOTIC_SSD1306::setTextXY(unsigned char row, unsigned char col)
{
sendCommand(0xB0 + row); //set page address
sendCommand(0x00 + (m_font_width*col & 0x0F)); //set column lower addr
sendCommand(0x10 + ((m_font_width*col>>4)&0x0F)); //set column higher addr
}
void ACROBOTIC_SSD1306::clearDisplay()
{
unsigned char i,j;
sendCommand(SSD1306_Display_Off_Cmd); //display off
for(j=0;j<8;j++)
{
setTextXY(j,0);
{
for(i=0;i<16;i++) //clear all columns
{
putChar(' ');
}
}
}
sendCommand(SSD1306_Display_On_Cmd); //display on
setTextXY(0,0);
}
void ACROBOTIC_SSD1306::sendData(unsigned char Data)
{
m_wire->beginTransmission(SSD1306_Address); // begin I2C transmission
m_wire->write(SSD1306_Data_Mode); // data mode
m_wire->write(m_inverse?~Data:Data);
m_wire->endTransmission(); // stop I2C transmission
}
bool ACROBOTIC_SSD1306::putChar(unsigned char ch)
{
if (!m_font) return 0;
//Ignore non-printable ASCII characters. This can be modified for
//multilingual font.
if(ch < 32 || ch > 127)
{
ch = ' ';
}
for(unsigned char i=0;i<m_font_width;i++)
{
// Font array starts at 0, ASCII starts at 32
sendData(pgm_read_byte(&m_font[(ch-32)*m_font_width+m_font_offset+i]));
}
return 1;
}
void ACROBOTIC_SSD1306::putString(const char *string)
{
unsigned char i=0;
while(string[i])
{
putChar(string[i]);
i++;
}
}
void ACROBOTIC_SSD1306::putString(String string)
{
char char_array[string.length()+1];
string.toCharArray(char_array, sizeof(char_array));
putString(char_array);
}
unsigned char ACROBOTIC_SSD1306::putNumber(long long_num)
{
unsigned char char_buffer[10]="";
unsigned char i = 0;
unsigned char f = 0;
if (long_num < 0)
{
f=1;
putChar('-');
long_num = -long_num;
}
else if (long_num == 0)
{
f=1;
putChar('0');
return f;
}
while (long_num > 0)
{
char_buffer[i++] = long_num % 10;
long_num /= 10;
}
f=f+i;
for(; i > 0; i--)
{
putChar('0'+ char_buffer[i - 1]);
}
return f;
}
unsigned char ACROBOTIC_SSD1306::putFloat(float floatNumber,unsigned char decimal)
{
unsigned int temp=0;
float decy=0.0;
float rounding = 0.5;
unsigned char f=0;
if(floatNumber<0.0)
{
putString("-");
floatNumber = -floatNumber;
f +=1;
}
for (unsigned char i=0; i<decimal; ++i)
{
rounding /= 10.0;
}
floatNumber += rounding;
temp = floatNumber;
f += putNumber(temp);
if(decimal>0)
{
putChar('.');
f +=1;
}
decy = floatNumber-temp;//decimal part,
for(unsigned char i=0;i<decimal;i++)//4
{
decy *=10;// for the next decimal
temp = decy;//get the decimal
putNumber(temp);
decy -= temp;
}
f +=decimal;
return f;
}
unsigned char ACROBOTIC_SSD1306::putFloat(float floatNumber)
{
unsigned char decimal=2;
unsigned int temp=0;
float decy=0.0;
float rounding = 0.5;
unsigned char f=0;
if(floatNumber<0.0)
{
putString("-");
floatNumber = -floatNumber;
f +=1;
}
for (unsigned char i=0; i<decimal; ++i)
{
rounding /= 10.0;
}
floatNumber += rounding;
temp = floatNumber;
f += putNumber(temp);
if(decimal>0)
{
putChar('.');
f +=1;
}
decy = floatNumber-temp;//decimal part,
for(unsigned char i=0;i<decimal;i++)//4
{
decy *=10;// for the next decimal
temp = decy;//get the decimal
putNumber(temp);
decy -= temp;
}
f +=decimal;
return f;
}
void ACROBOTIC_SSD1306::drawBitmap(unsigned char *bitmaparray,int bytes)
{
char localAddressMode = addressingMode;
if(addressingMode != HORIZONTAL_MODE)
{
//Bitmap is drawn in horizontal mode
setHorizontalMode();
}
for(int i=0;i<bytes;i++)
{
sendData(pgm_read_byte(&bitmaparray[i]));
}
if(localAddressMode == PAGE_MODE)
{
//If pageMode was used earlier, restore it.
setPageMode();
}
}
void ACROBOTIC_SSD1306::setHorizontalScrollProperties(bool direction,unsigned char startPage, unsigned char endPage, unsigned char scrollSpeed)
{
if(Scroll_Right == direction)
{
//Scroll right
sendCommand(0x26);
}
else
{
//Scroll left
sendCommand(0x27);
}
sendCommand(0x00);
sendCommand(startPage);
sendCommand(scrollSpeed);
sendCommand(endPage);
sendCommand(0x00);
sendCommand(0xFF);
}
void ACROBOTIC_SSD1306::activateScroll()
{
sendCommand(SSD1306_Activate_Scroll_Cmd);
}
void ACROBOTIC_SSD1306::deactivateScroll()
{
sendCommand(SSD1306_Dectivate_Scroll_Cmd);
}
void ACROBOTIC_SSD1306::setNormalDisplay()
{
sendCommand(SSD1306_Normal_Display_Cmd);
}
void ACROBOTIC_SSD1306::setInverseDisplay()
{
sendCommand(SSD1306_Inverse_Display_Cmd);
}
ACROBOTIC_SSD1306 oled; // Pre-instantiate object