source: rtos_arduino/trunk/examples/NCESIoT_RTOS/OLED_Display12864/SeeedOLED.cpp@ 242

Last change on this file since 242 was 242, checked in by ertl-honda, 8 years ago

RTOSのサンプルを追加

File size: 11.4 KB
Line 
1/*
2 SeeedOLED.cpp - SSD130x OLED Driver Library
3 2011 Copyright (c) Seeed Technology Inc. All right reserved.
4
5 Author: Visweswara R
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#include "Wire.h"
23#include "SeeedOLED.h"
24
25#include <avr/pgmspace.h>
26
27// 8x8 Font ASCII 32 - 127 Implemented
28// Users can modify this to support more characters(glyphs)
29// BasicFont is placed in code memory.
30
31// This font be freely used without any restriction(It is placed in public domain)
32const unsigned char BasicFont[][8] PROGMEM=
33{
34 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
35 {0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00},
36 {0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00},
37 {0x00,0x14,0x7F,0x14,0x7F,0x14,0x00,0x00},
38 {0x00,0x24,0x2A,0x7F,0x2A,0x12,0x00,0x00},
39 {0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00},
40 {0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00},
41 {0x00,0x00,0x05,0x03,0x00,0x00,0x00,0x00},
42 {0x00,0x1C,0x22,0x41,0x00,0x00,0x00,0x00},
43 {0x00,0x41,0x22,0x1C,0x00,0x00,0x00,0x00},
44 {0x00,0x08,0x2A,0x1C,0x2A,0x08,0x00,0x00},
45 {0x00,0x08,0x08,0x3E,0x08,0x08,0x00,0x00},
46 {0x00,0xA0,0x60,0x00,0x00,0x00,0x00,0x00},
47 {0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00},
48 {0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00},
49 {0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00},
50 {0x00,0x3E,0x51,0x49,0x45,0x3E,0x00,0x00},
51 {0x00,0x00,0x42,0x7F,0x40,0x00,0x00,0x00},
52 {0x00,0x62,0x51,0x49,0x49,0x46,0x00,0x00},
53 {0x00,0x22,0x41,0x49,0x49,0x36,0x00,0x00},
54 {0x00,0x18,0x14,0x12,0x7F,0x10,0x00,0x00},
55 {0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00},
56 {0x00,0x3C,0x4A,0x49,0x49,0x30,0x00,0x00},
57 {0x00,0x01,0x71,0x09,0x05,0x03,0x00,0x00},
58 {0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00},
59 {0x00,0x06,0x49,0x49,0x29,0x1E,0x00,0x00},
60 {0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00},
61 {0x00,0x00,0xAC,0x6C,0x00,0x00,0x00,0x00},
62 {0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00},
63 {0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00},
64 {0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00},
65 {0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00},
66 {0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00},
67 {0x00,0x7E,0x09,0x09,0x09,0x7E,0x00,0x00},
68 {0x00,0x7F,0x49,0x49,0x49,0x36,0x00,0x00},
69 {0x00,0x3E,0x41,0x41,0x41,0x22,0x00,0x00},
70 {0x00,0x7F,0x41,0x41,0x22,0x1C,0x00,0x00},
71 {0x00,0x7F,0x49,0x49,0x49,0x41,0x00,0x00},
72 {0x00,0x7F,0x09,0x09,0x09,0x01,0x00,0x00},
73 {0x00,0x3E,0x41,0x41,0x51,0x72,0x00,0x00},
74 {0x00,0x7F,0x08,0x08,0x08,0x7F,0x00,0x00},
75 {0x00,0x41,0x7F,0x41,0x00,0x00,0x00,0x00},
76 {0x00,0x20,0x40,0x41,0x3F,0x01,0x00,0x00},
77 {0x00,0x7F,0x08,0x14,0x22,0x41,0x00,0x00},
78 {0x00,0x7F,0x40,0x40,0x40,0x40,0x00,0x00},
79 {0x00,0x7F,0x02,0x0C,0x02,0x7F,0x00,0x00},
80 {0x00,0x7F,0x04,0x08,0x10,0x7F,0x00,0x00},
81 {0x00,0x3E,0x41,0x41,0x41,0x3E,0x00,0x00},
82 {0x00,0x7F,0x09,0x09,0x09,0x06,0x00,0x00},
83 {0x00,0x3E,0x41,0x51,0x21,0x5E,0x00,0x00},
84 {0x00,0x7F,0x09,0x19,0x29,0x46,0x00,0x00},
85 {0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00},
86 {0x00,0x01,0x01,0x7F,0x01,0x01,0x00,0x00},
87 {0x00,0x3F,0x40,0x40,0x40,0x3F,0x00,0x00},
88 {0x00,0x1F,0x20,0x40,0x20,0x1F,0x00,0x00},
89 {0x00,0x3F,0x40,0x38,0x40,0x3F,0x00,0x00},
90 {0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00},
91 {0x00,0x03,0x04,0x78,0x04,0x03,0x00,0x00},
92 {0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00},
93 {0x00,0x7F,0x41,0x41,0x00,0x00,0x00,0x00},
94 {0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00},
95 {0x00,0x41,0x41,0x7F,0x00,0x00,0x00,0x00},
96 {0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x00},
97 {0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00},
98 {0x00,0x01,0x02,0x04,0x00,0x00,0x00,0x00},
99 {0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00},
100 {0x00,0x7F,0x48,0x44,0x44,0x38,0x00,0x00},
101 {0x00,0x38,0x44,0x44,0x28,0x00,0x00,0x00},
102 {0x00,0x38,0x44,0x44,0x48,0x7F,0x00,0x00},
103 {0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00},
104 {0x00,0x08,0x7E,0x09,0x02,0x00,0x00,0x00},
105 {0x00,0x18,0xA4,0xA4,0xA4,0x7C,0x00,0x00},
106 {0x00,0x7F,0x08,0x04,0x04,0x78,0x00,0x00},
107 {0x00,0x00,0x7D,0x00,0x00,0x00,0x00,0x00},
108 {0x00,0x80,0x84,0x7D,0x00,0x00,0x00,0x00},
109 {0x00,0x7F,0x10,0x28,0x44,0x00,0x00,0x00},
110 {0x00,0x41,0x7F,0x40,0x00,0x00,0x00,0x00},
111 {0x00,0x7C,0x04,0x18,0x04,0x78,0x00,0x00},
112 {0x00,0x7C,0x08,0x04,0x7C,0x00,0x00,0x00},
113 {0x00,0x38,0x44,0x44,0x38,0x00,0x00,0x00},
114 {0x00,0xFC,0x24,0x24,0x18,0x00,0x00,0x00},
115 {0x00,0x18,0x24,0x24,0xFC,0x00,0x00,0x00},
116 {0x00,0x00,0x7C,0x08,0x04,0x00,0x00,0x00},
117 {0x00,0x48,0x54,0x54,0x24,0x00,0x00,0x00},
118 {0x00,0x04,0x7F,0x44,0x00,0x00,0x00,0x00},
119 {0x00,0x3C,0x40,0x40,0x7C,0x00,0x00,0x00},
120 {0x00,0x1C,0x20,0x40,0x20,0x1C,0x00,0x00},
121 {0x00,0x3C,0x40,0x30,0x40,0x3C,0x00,0x00},
122 {0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00},
123 {0x00,0x1C,0xA0,0xA0,0x7C,0x00,0x00,0x00},
124 {0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00},
125 {0x00,0x08,0x36,0x41,0x00,0x00,0x00,0x00},
126 {0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00},
127 {0x00,0x41,0x36,0x08,0x00,0x00,0x00,0x00},
128 {0x00,0x02,0x01,0x01,0x02,0x01,0x00,0x00},
129 {0x00,0x02,0x05,0x05,0x02,0x00,0x00,0x00}
130};
131
132void SeeedOLED::init(void)
133{
134 sendCommand(SeeedOLED_Display_Off_Cmd); //display off
135 delay(5);
136 sendCommand(SeeedOLED_Display_On_Cmd); //display on
137 delay(5);
138 sendCommand(SeeedOLED_Normal_Display_Cmd); //Set Normal Display (default)
139}
140
141void SeeedOLED::sendCommand(unsigned char command)
142{
143 Wire.beginTransmission(SeeedOLED_Address); // begin I2C communication
144#if defined(ARDUINO) && ARDUINO >= 100
145 Wire.write(SeeedOLED_Command_Mode); // Set OLED Command mode
146 Wire.write(command);
147#else
148 Wire.send(SeeedOLED_Command_Mode); // Set OLED Command mode
149 Wire.send(command);
150#endif
151 Wire.endTransmission(); // End I2C communication
152}
153
154void SeeedOLED::setBrightness(unsigned char Brightness)
155{
156 sendCommand(SeeedOLED_Set_Brightness_Cmd);
157 sendCommand(Brightness);
158}
159
160void SeeedOLED::setHorizontalMode()
161{
162 addressingMode = HORIZONTAL_MODE;
163 sendCommand(0x20); //set addressing mode
164 sendCommand(0x00); //set horizontal addressing mode
165}
166
167void SeeedOLED::setPageMode()
168{
169 addressingMode = PAGE_MODE;
170 sendCommand(0x20); //set addressing mode
171 sendCommand(0x02); //set page addressing mode
172}
173
174
175void SeeedOLED::setTextXY(unsigned char Row, unsigned char Column)
176{
177 sendCommand(0xB0 + Row); //set page address
178 sendCommand(0x00 + (8*Column & 0x0F)); //set column lower address
179 sendCommand(0x10 + ((8*Column>>4)&0x0F)); //set column higher address
180}
181
182
183void SeeedOLED::clearDisplay()
184{
185 unsigned char i,j;
186 sendCommand(SeeedOLED_Display_Off_Cmd); //display off
187 for(j=0;j<8;j++)
188 {
189 setTextXY(j,0);
190 {
191 for(i=0;i<16;i++) //clear all columns
192 {
193 putChar(' ');
194 }
195 }
196 }
197 sendCommand(SeeedOLED_Display_On_Cmd); //display on
198 setTextXY(0,0);
199}
200
201void SeeedOLED::sendData(unsigned char Data)
202{
203 Wire.beginTransmission(SeeedOLED_Address); // begin I2C transmission
204#if defined(ARDUINO) && ARDUINO >= 100
205 Wire.write(SeeedOLED_Data_Mode); // data mode
206 Wire.write(Data);
207#else
208 Wire.send(SeeedOLED_Data_Mode); // data mode
209 Wire.send(Data);
210#endif
211 Wire.endTransmission(); // stop I2C transmission
212}
213
214void SeeedOLED::putChar(unsigned char C)
215{
216 if(C < 32 || C > 127) //Ignore non-printable ASCII characters. This can be modified for multilingual font.
217 {
218 C=' '; //Space
219 }
220 unsigned char i=0;
221 for(i=0;i<8;i++)
222 {
223 //read bytes from code memory
224 sendData(pgm_read_byte(&BasicFont[C-32][i])); //font array starts at 0, ASCII starts at 32. Hence the translation
225 }
226}
227
228void SeeedOLED::putString(const char *String)
229{
230 unsigned char i=0;
231 while(String[i])
232 {
233 putChar(String[i]);
234 i++;
235 }
236}
237
238unsigned char SeeedOLED::putNumber(long long_num)
239{
240 unsigned char char_buffer[10]="";
241 unsigned char i = 0;
242 unsigned char f = 0;
243
244 if (long_num < 0)
245 {
246 f=1;
247 putChar('-');
248 long_num = -long_num;
249 }
250 else if (long_num == 0)
251 {
252 f=1;
253 putChar('0');
254 return f;
255 }
256
257 while (long_num > 0)
258 {
259 char_buffer[i++] = long_num % 10;
260 long_num /= 10;
261 }
262
263 f=f+i;
264 for(; i > 0; i--)
265 {
266 putChar('0'+ char_buffer[i - 1]);
267 }
268 return f;
269
270}
271
272unsigned char SeeedOLED::putFloat(float floatNumber,unsigned char decimal)
273{
274 unsigned int temp=0;
275 float decy=0.0;
276 float rounding = 0.5;
277 unsigned char f=0;
278 if(floatNumber<0.0)
279 {
280 putString("-");
281 floatNumber = -floatNumber;
282 f +=1;
283 }
284 for (unsigned char i=0; i<decimal; ++i)
285 {
286 rounding /= 10.0;
287 }
288 floatNumber += rounding;
289
290 temp = floatNumber;
291 f += putNumber(temp);
292 if(decimal>0)
293 {
294 putChar('.');
295 f +=1;
296 }
297 decy = floatNumber-temp;//decimal part,
298 for(unsigned char i=0;i<decimal;i++)//4
299 {
300 decy *=10;// for the next decimal
301 temp = decy;//get the decimal
302 putNumber(temp);
303 decy -= temp;
304 }
305 f +=decimal;
306 return f;
307}
308unsigned char SeeedOLED::putFloat(float floatNumber)
309{
310 unsigned char decimal=2;
311 unsigned int temp=0;
312 float decy=0.0;
313 float rounding = 0.5;
314 unsigned char f=0;
315 if(floatNumber<0.0)
316 {
317 putString("-");
318 floatNumber = -floatNumber;
319 f +=1;
320 }
321 for (unsigned char i=0; i<decimal; ++i)
322 {
323 rounding /= 10.0;
324 }
325 floatNumber += rounding;
326
327 temp = floatNumber;
328 f += putNumber(temp);
329 if(decimal>0)
330 {
331 putChar('.');
332 f +=1;
333 }
334 decy = floatNumber-temp;//decimal part,
335 for(unsigned char i=0;i<decimal;i++)//4
336 {
337 decy *=10;// for the next decimal
338 temp = decy;//get the decimal
339 putNumber(temp);
340 decy -= temp;
341 }
342 f +=decimal;
343 return f;
344}
345
346void SeeedOLED::drawBitmap(unsigned char *bitmaparray,int bytes)
347{
348 char localAddressMode = addressingMode;
349 if(addressingMode != HORIZONTAL_MODE)
350 {
351 //Bitmap is drawn in horizontal mode
352 setHorizontalMode();
353 }
354
355 for(int i=0;i<bytes;i++)
356 {
357 sendData(pgm_read_byte(&bitmaparray[i]));
358 }
359
360 if(localAddressMode == PAGE_MODE)
361 {
362 //If pageMode was used earlier, restore it.
363 setPageMode();
364 }
365
366}
367
368void SeeedOLED::setHorizontalScrollProperties(bool direction,unsigned char startPage, unsigned char endPage, unsigned char scrollSpeed)
369{
370/*
371Use the following defines for 'direction' :
372
373 Scroll_Left
374 Scroll_Right
375
376Use the following defines for 'scrollSpeed' :
377
378 Scroll_2Frames
379 Scroll_3Frames
380 Scroll_4Frames
381 Scroll_5Frames
382 Scroll_25Frames
383 Scroll_64Frames
384 Scroll_128Frames
385 Scroll_256Frames
386
387*/
388
389 if(Scroll_Right == direction)
390 {
391 //Scroll Right
392 sendCommand(0x26);
393 }
394 else
395 {
396 //Scroll Left
397 sendCommand(0x27);
398
399 }
400 sendCommand(0x00);
401 sendCommand(startPage);
402 sendCommand(scrollSpeed);
403 sendCommand(endPage);
404 sendCommand(0x00);
405 sendCommand(0xFF);
406}
407
408void SeeedOLED::activateScroll()
409{
410 sendCommand(SeeedOLED_Activate_Scroll_Cmd);
411}
412
413void SeeedOLED::deactivateScroll()
414{
415 sendCommand(SeeedOLED_Dectivate_Scroll_Cmd);
416}
417
418void SeeedOLED::setNormalDisplay()
419{
420 sendCommand(SeeedOLED_Normal_Display_Cmd);
421}
422
423void SeeedOLED::setInverseDisplay()
424{
425 sendCommand(SeeedOLED_Inverse_Display_Cmd);
426}
427
428
429SeeedOLED SeeedOled; // Preinstantiate Objects
430
Note: See TracBrowser for help on using the repository browser.