-
Notifications
You must be signed in to change notification settings - Fork 6
/
SeeedOLED.cpp
452 lines (403 loc) · 12.1 KB
/
SeeedOLED.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
SeeedOLED.cpp - SSD130x OLED Driver Library
2011 Copyright (c) Seeed Technology Inc. All right reserved.
Author: Visweswara R
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "i2c_t3.h"
#include "SeeedOLED.h"
#include <avr/pgmspace.h>
// 8x8 Font ASCII 32 - 127 Implemented
// Users can modify this to support more characters(glyphs)
// BasicFont is placed in code memory.
// This font be freely used without any restriction(It is placed in public domain)
const unsigned char BasicFont[][8] PROGMEM=
{
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00},
{0x00,0x14,0x7F,0x14,0x7F,0x14,0x00,0x00},
{0x00,0x24,0x2A,0x7F,0x2A,0x12,0x00,0x00},
{0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00},
{0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00},
{0x00,0x00,0x05,0x03,0x00,0x00,0x00,0x00},
{0x00,0x1C,0x22,0x41,0x00,0x00,0x00,0x00},
{0x00,0x41,0x22,0x1C,0x00,0x00,0x00,0x00},
{0x00,0x08,0x2A,0x1C,0x2A,0x08,0x00,0x00},
{0x00,0x08,0x08,0x3E,0x08,0x08,0x00,0x00},
{0x00,0xA0,0x60,0x00,0x00,0x00,0x00,0x00},
{0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00},
{0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00},
{0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00},
{0x00,0x3E,0x51,0x49,0x45,0x3E,0x00,0x00},
{0x00,0x00,0x42,0x7F,0x40,0x00,0x00,0x00},
{0x00,0x62,0x51,0x49,0x49,0x46,0x00,0x00},
{0x00,0x22,0x41,0x49,0x49,0x36,0x00,0x00},
{0x00,0x18,0x14,0x12,0x7F,0x10,0x00,0x00},
{0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00},
{0x00,0x3C,0x4A,0x49,0x49,0x30,0x00,0x00},
{0x00,0x01,0x71,0x09,0x05,0x03,0x00,0x00},
{0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00},
{0x00,0x06,0x49,0x49,0x29,0x1E,0x00,0x00},
{0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00},
{0x00,0x00,0xAC,0x6C,0x00,0x00,0x00,0x00},
{0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00},
{0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00},
{0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00},
{0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00},
{0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00},
{0x00,0x7E,0x09,0x09,0x09,0x7E,0x00,0x00},
{0x00,0x7F,0x49,0x49,0x49,0x36,0x00,0x00},
{0x00,0x3E,0x41,0x41,0x41,0x22,0x00,0x00},
{0x00,0x7F,0x41,0x41,0x22,0x1C,0x00,0x00},
{0x00,0x7F,0x49,0x49,0x49,0x41,0x00,0x00},
{0x00,0x7F,0x09,0x09,0x09,0x01,0x00,0x00},
{0x00,0x3E,0x41,0x41,0x51,0x72,0x00,0x00},
{0x00,0x7F,0x08,0x08,0x08,0x7F,0x00,0x00},
{0x00,0x41,0x7F,0x41,0x00,0x00,0x00,0x00},
{0x00,0x20,0x40,0x41,0x3F,0x01,0x00,0x00},
{0x00,0x7F,0x08,0x14,0x22,0x41,0x00,0x00},
{0x00,0x7F,0x40,0x40,0x40,0x40,0x00,0x00},
{0x00,0x7F,0x02,0x0C,0x02,0x7F,0x00,0x00},
{0x00,0x7F,0x04,0x08,0x10,0x7F,0x00,0x00},
{0x00,0x3E,0x41,0x41,0x41,0x3E,0x00,0x00},
{0x00,0x7F,0x09,0x09,0x09,0x06,0x00,0x00},
{0x00,0x3E,0x41,0x51,0x21,0x5E,0x00,0x00},
{0x00,0x7F,0x09,0x19,0x29,0x46,0x00,0x00},
{0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00},
{0x00,0x01,0x01,0x7F,0x01,0x01,0x00,0x00},
{0x00,0x3F,0x40,0x40,0x40,0x3F,0x00,0x00},
{0x00,0x1F,0x20,0x40,0x20,0x1F,0x00,0x00},
{0x00,0x3F,0x40,0x38,0x40,0x3F,0x00,0x00},
{0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00},
{0x00,0x03,0x04,0x78,0x04,0x03,0x00,0x00},
{0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00},
{0x00,0x7F,0x41,0x41,0x00,0x00,0x00,0x00},
{0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00},
{0x00,0x41,0x41,0x7F,0x00,0x00,0x00,0x00},
{0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x00},
{0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00},
{0x00,0x01,0x02,0x04,0x00,0x00,0x00,0x00},
{0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00},
{0x00,0x7F,0x48,0x44,0x44,0x38,0x00,0x00},
{0x00,0x38,0x44,0x44,0x28,0x00,0x00,0x00},
{0x00,0x38,0x44,0x44,0x48,0x7F,0x00,0x00},
{0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00},
{0x00,0x08,0x7E,0x09,0x02,0x00,0x00,0x00},
{0x00,0x18,0xA4,0xA4,0xA4,0x7C,0x00,0x00},
{0x00,0x7F,0x08,0x04,0x04,0x78,0x00,0x00},
{0x00,0x00,0x7D,0x00,0x00,0x00,0x00,0x00},
{0x00,0x80,0x84,0x7D,0x00,0x00,0x00,0x00},
{0x00,0x7F,0x10,0x28,0x44,0x00,0x00,0x00},
{0x00,0x41,0x7F,0x40,0x00,0x00,0x00,0x00},
{0x00,0x7C,0x04,0x18,0x04,0x78,0x00,0x00},
{0x00,0x7C,0x08,0x04,0x7C,0x00,0x00,0x00},
{0x00,0x38,0x44,0x44,0x38,0x00,0x00,0x00},
{0x00,0xFC,0x24,0x24,0x18,0x00,0x00,0x00},
{0x00,0x18,0x24,0x24,0xFC,0x00,0x00,0x00},
{0x00,0x00,0x7C,0x08,0x04,0x00,0x00,0x00},
{0x00,0x48,0x54,0x54,0x24,0x00,0x00,0x00},
{0x00,0x04,0x7F,0x44,0x00,0x00,0x00,0x00},
{0x00,0x3C,0x40,0x40,0x7C,0x00,0x00,0x00},
{0x00,0x1C,0x20,0x40,0x20,0x1C,0x00,0x00},
{0x00,0x3C,0x40,0x30,0x40,0x3C,0x00,0x00},
{0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00},
{0x00,0x1C,0xA0,0xA0,0x7C,0x00,0x00,0x00},
{0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00},
{0x00,0x08,0x36,0x41,0x00,0x00,0x00,0x00},
{0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00},
{0x00,0x41,0x36,0x08,0x00,0x00,0x00,0x00},
{0x00,0x02,0x01,0x01,0x02,0x01,0x00,0x00},
{0x00,0x02,0x05,0x05,0x02,0x00,0x00,0x00}
};
void SeeedOLED::init(void)
{
sendCommand(SeeedOLED_Display_Off_Cmd); //display off
delay(5);
sendCommand(SeeedOLED_Display_On_Cmd); //display on
delay(5);
sendCommand(SeeedOLED_Normal_Display_Cmd); //Set Normal Display (default)
}
void SeeedOLED::sendCommand(unsigned char command)
{
Wire.beginTransmission(SeeedOLED_Address); // begin I2C communication
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write(SeeedOLED_Command_Mode); // Set OLED Command mode
Wire.write(command);
#else
Wire.send(SeeedOLED_Command_Mode); // Set OLED Command mode
Wire.send(command);
#endif
Wire.endTransmission(); // End I2C communication
}
void SeeedOLED::setBrightness(unsigned char Brightness)
{
sendCommand(SeeedOLED_Set_Brightness_Cmd);
sendCommand(Brightness);
}
void SeeedOLED::setHorizontalMode()
{
addressingMode = HORIZONTAL_MODE;
sendCommand(0x20); //set addressing mode
sendCommand(0x00); //set horizontal addressing mode
}
void SeeedOLED::setPageMode()
{
addressingMode = PAGE_MODE;
sendCommand(0x20); //set addressing mode
sendCommand(0x02); //set page addressing mode
}
void SeeedOLED::setTextXY(unsigned char Row, unsigned char Column)
{
sendCommand(0xB0 + Row); //set page address
sendCommand(0x00 + (8*Column & 0x0F)); //set column lower address
sendCommand(0x10 + ((8*Column>>4)&0x0F)); //set column higher address
}
void SeeedOLED::clearDisplay()
{
unsigned char i,j;
sendCommand(SeeedOLED_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(SeeedOLED_Display_On_Cmd); //display on
setTextXY(0,0);
}
void SeeedOLED::displayOff(){
sendCommand(SeeedOLED_Display_Off_Cmd); //display off
}
void SeeedOLED::displayOn(){
sendCommand(SeeedOLED_Display_On_Cmd); //display off
}
void SeeedOLED::sendData(unsigned char Data)
{
Wire.beginTransmission(SeeedOLED_Address); // begin I2C transmission
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write(SeeedOLED_Data_Mode); // data mode
Wire.write(Data);
#else
Wire.send(SeeedOLED_Data_Mode); // data mode
Wire.send(Data);
#endif
Wire.endTransmission(); // stop I2C transmission
}
void SeeedOLED::putChar(unsigned char C)
{
if(C < 32 || C > 127) //Ignore non-printable ASCII characters. This can be modified for multilingual font.
{
C=' '; //Space
}
unsigned char i=0;
unsigned char CC = C-32;
const unsigned char* KK = BasicFont[CC];
for(i=0;i<8;i++)
{
//read bytes from code memory
sendData(pgm_read_byte(&KK[i])); //font array starts at 0, ASCII starts at 32. Hence the translation
}
}
void SeeedOLED::putCharLine(unsigned char C,unsigned char i)
{
if(C < 32 || C > 127) //Ignore non-printable ASCII characters. This can be modified for multilingual font.
{
C=' '; //Space
}
unsigned char CC = C-32;
const unsigned char* KK = BasicFont[CC];
//read bytes from code memory
sendData(pgm_read_byte(&KK[i])); //font array starts at 0, ASCII starts at 32. Hence the translation
}
void SeeedOLED::putString(const char *String)
{
unsigned char i=0;
while(String[i])
{
putChar(String[i]);
i++;
}
}
unsigned char SeeedOLED::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 SeeedOLED::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 SeeedOLED::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 SeeedOLED::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 SeeedOLED::setHorizontalScrollProperties(bool direction,unsigned char startPage, unsigned char endPage, unsigned char scrollSpeed)
{
/*
Use the following defines for 'direction' :
Scroll_Left
Scroll_Right
Use the following defines for 'scrollSpeed' :
Scroll_2Frames
Scroll_3Frames
Scroll_4Frames
Scroll_5Frames
Scroll_25Frames
Scroll_64Frames
Scroll_128Frames
Scroll_256Frames
*/
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 SeeedOLED::activateScroll()
{
sendCommand(SeeedOLED_Activate_Scroll_Cmd);
}
void SeeedOLED::deactivateScroll()
{
sendCommand(SeeedOLED_Dectivate_Scroll_Cmd);
}
void SeeedOLED::setNormalDisplay()
{
sendCommand(SeeedOLED_Normal_Display_Cmd);
}
void SeeedOLED::setInverseDisplay()
{
sendCommand(SeeedOLED_Inverse_Display_Cmd);
}
SeeedOLED SeeedOled; // Preinstantiate Objects