source: rtos_arduino/trunk/examples/NCESIoT/LCD_Display9696/SeeedGrayOLED.cpp@ 226

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

Groveモジュールを使用したサンプルの追加

File size: 12.8 KB
Line 
1/*
2 SeeedGrayOLED.cpp - SSD1327 Gray 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 "Arduino.h"
23
24//#include "WProgram.h"
25#include "Wire.h"
26
27#include "SeeedGrayOLED.h"
28
29#include <avr/pgmspace.h>
30
31// 8x8 Font ASCII 32 - 127 Implemented
32// Users can modify this to support more characters(glyphs)
33// BasicFont is placed in code memory.
34
35// This font can be freely used without any restriction(It is placed in public domain)
36const unsigned char BasicFont[][8] PROGMEM=
37{
38 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
39 {0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00},
40 {0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00},
41 {0x00,0x14,0x7F,0x14,0x7F,0x14,0x00,0x00},
42 {0x00,0x24,0x2A,0x7F,0x2A,0x12,0x00,0x00},
43 {0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00},
44 {0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00},
45 {0x00,0x00,0x05,0x03,0x00,0x00,0x00,0x00},
46 {0x00,0x1C,0x22,0x41,0x00,0x00,0x00,0x00},
47 {0x00,0x41,0x22,0x1C,0x00,0x00,0x00,0x00},
48 {0x00,0x08,0x2A,0x1C,0x2A,0x08,0x00,0x00},
49 {0x00,0x08,0x08,0x3E,0x08,0x08,0x00,0x00},
50 {0x00,0xA0,0x60,0x00,0x00,0x00,0x00,0x00},
51 {0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00},
52 {0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00},
53 {0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00},
54 {0x00,0x3E,0x51,0x49,0x45,0x3E,0x00,0x00},
55 {0x00,0x00,0x42,0x7F,0x40,0x00,0x00,0x00},
56 {0x00,0x62,0x51,0x49,0x49,0x46,0x00,0x00},
57 {0x00,0x22,0x41,0x49,0x49,0x36,0x00,0x00},
58 {0x00,0x18,0x14,0x12,0x7F,0x10,0x00,0x00},
59 {0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00},
60 {0x00,0x3C,0x4A,0x49,0x49,0x30,0x00,0x00},
61 {0x00,0x01,0x71,0x09,0x05,0x03,0x00,0x00},
62 {0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00},
63 {0x00,0x06,0x49,0x49,0x29,0x1E,0x00,0x00},
64 {0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00},
65 {0x00,0x00,0xAC,0x6C,0x00,0x00,0x00,0x00},
66 {0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00},
67 {0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00},
68 {0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00},
69 {0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00},
70 {0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00},
71 {0x00,0x7E,0x09,0x09,0x09,0x7E,0x00,0x00},
72 {0x00,0x7F,0x49,0x49,0x49,0x36,0x00,0x00},
73 {0x00,0x3E,0x41,0x41,0x41,0x22,0x00,0x00},
74 {0x00,0x7F,0x41,0x41,0x22,0x1C,0x00,0x00},
75 {0x00,0x7F,0x49,0x49,0x49,0x41,0x00,0x00},
76 {0x00,0x7F,0x09,0x09,0x09,0x01,0x00,0x00},
77 {0x00,0x3E,0x41,0x41,0x51,0x72,0x00,0x00},
78 {0x00,0x7F,0x08,0x08,0x08,0x7F,0x00,0x00},
79 {0x00,0x41,0x7F,0x41,0x00,0x00,0x00,0x00},
80 {0x00,0x20,0x40,0x41,0x3F,0x01,0x00,0x00},
81 {0x00,0x7F,0x08,0x14,0x22,0x41,0x00,0x00},
82 {0x00,0x7F,0x40,0x40,0x40,0x40,0x00,0x00},
83 {0x00,0x7F,0x02,0x0C,0x02,0x7F,0x00,0x00},
84 {0x00,0x7F,0x04,0x08,0x10,0x7F,0x00,0x00},
85 {0x00,0x3E,0x41,0x41,0x41,0x3E,0x00,0x00},
86 {0x00,0x7F,0x09,0x09,0x09,0x06,0x00,0x00},
87 {0x00,0x3E,0x41,0x51,0x21,0x5E,0x00,0x00},
88 {0x00,0x7F,0x09,0x19,0x29,0x46,0x00,0x00},
89 {0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00},
90 {0x00,0x01,0x01,0x7F,0x01,0x01,0x00,0x00},
91 {0x00,0x3F,0x40,0x40,0x40,0x3F,0x00,0x00},
92 {0x00,0x1F,0x20,0x40,0x20,0x1F,0x00,0x00},
93 {0x00,0x3F,0x40,0x38,0x40,0x3F,0x00,0x00},
94 {0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00},
95 {0x00,0x03,0x04,0x78,0x04,0x03,0x00,0x00},
96 {0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00},
97 {0x00,0x7F,0x41,0x41,0x00,0x00,0x00,0x00},
98 {0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00},
99 {0x00,0x41,0x41,0x7F,0x00,0x00,0x00,0x00},
100 {0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x00},
101 {0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00},
102 {0x00,0x01,0x02,0x04,0x00,0x00,0x00,0x00},
103 {0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00},
104 {0x00,0x7F,0x48,0x44,0x44,0x38,0x00,0x00},
105 {0x00,0x38,0x44,0x44,0x28,0x00,0x00,0x00},
106 {0x00,0x38,0x44,0x44,0x48,0x7F,0x00,0x00},
107 {0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00},
108 {0x00,0x08,0x7E,0x09,0x02,0x00,0x00,0x00},
109 {0x00,0x18,0xA4,0xA4,0xA4,0x7C,0x00,0x00},
110 {0x00,0x7F,0x08,0x04,0x04,0x78,0x00,0x00},
111 {0x00,0x00,0x7D,0x00,0x00,0x00,0x00,0x00},
112 {0x00,0x80,0x84,0x7D,0x00,0x00,0x00,0x00},
113 {0x00,0x7F,0x10,0x28,0x44,0x00,0x00,0x00},
114 {0x00,0x41,0x7F,0x40,0x00,0x00,0x00,0x00},
115 {0x00,0x7C,0x04,0x18,0x04,0x78,0x00,0x00},
116 {0x00,0x7C,0x08,0x04,0x7C,0x00,0x00,0x00},
117 {0x00,0x38,0x44,0x44,0x38,0x00,0x00,0x00},
118 {0x00,0xFC,0x24,0x24,0x18,0x00,0x00,0x00},
119 {0x00,0x18,0x24,0x24,0xFC,0x00,0x00,0x00},
120 {0x00,0x00,0x7C,0x08,0x04,0x00,0x00,0x00},
121 {0x00,0x48,0x54,0x54,0x24,0x00,0x00,0x00},
122 {0x00,0x04,0x7F,0x44,0x00,0x00,0x00,0x00},
123 {0x00,0x3C,0x40,0x40,0x7C,0x00,0x00,0x00},
124 {0x00,0x1C,0x20,0x40,0x20,0x1C,0x00,0x00},
125 {0x00,0x3C,0x40,0x30,0x40,0x3C,0x00,0x00},
126 {0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00},
127 {0x00,0x1C,0xA0,0xA0,0x7C,0x00,0x00,0x00},
128 {0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00},
129 {0x00,0x08,0x36,0x41,0x00,0x00,0x00,0x00},
130 {0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00},
131 {0x00,0x41,0x36,0x08,0x00,0x00,0x00,0x00},
132 {0x00,0x02,0x01,0x01,0x02,0x01,0x00,0x00},
133 {0x00,0x02,0x05,0x05,0x02,0x00,0x00,0x00}
134};
135
136void SeeedGrayOLED::init(void)
137{
138 sendCommand(0xFD); // Unlock OLED driver IC MCU interface from entering command. i.e: Accept commands
139 sendCommand(0x12);
140 sendCommand(0xAE); // Set display off
141 sendCommand(0xA8); // set multiplex ratio
142 sendCommand(0x5F); // 96
143 sendCommand(0xA1); // set display start line
144 sendCommand(0x00);
145 sendCommand(0xA2); // set display offset
146 sendCommand(0x60);
147 sendCommand(0xA0); // set remap
148 sendCommand(0x46);
149 sendCommand(0xAB); // set vdd internal
150 sendCommand(0x01); //
151 sendCommand(0x81); // set contrasr
152 sendCommand(0x53); // 100 nit
153 sendCommand(0xB1); // Set Phase Length
154 sendCommand(0X51); //
155 sendCommand(0xB3); // Set Display Clock Divide Ratio/Oscillator Frequency
156 sendCommand(0x01);
157 sendCommand(0xB9); //
158 sendCommand(0xBC); // set pre_charge voltage/VCOMH
159 sendCommand(0x08); // (0x08);
160 sendCommand(0xBE); // set VCOMH
161 sendCommand(0X07); // (0x07);
162 sendCommand(0xB6); // Set second pre-charge period
163 sendCommand(0x01); //
164 sendCommand(0xD5); // enable second precharge and enternal vsl
165 sendCommand(0X62); // (0x62);
166 sendCommand(0xA4); // Set Normal Display Mode
167 sendCommand(0x2E); // Deactivate Scroll
168 sendCommand(0xAF); // Switch on display
169 delay(100);
170
171 // Row Address
172 sendCommand(0x75); // Set Row Address
173 sendCommand(0x00); // Start 0
174 sendCommand(0x5f); // End 95
175
176
177 // Column Address
178 sendCommand(0x15); // Set Column Address
179 sendCommand(0x08); // Start from 8th Column of driver IC. This is 0th Column for OLED
180 sendCommand(0x37); // End at (8 + 47)th column. Each Column has 2 pixels(segments)
181
182 // Init gray level for text. Default:Brightest White
183 grayH= 0xF0;
184 grayL= 0x0F;
185}
186
187void SeeedGrayOLED::sendCommand(unsigned char command)
188{
189 Wire.beginTransmission(SeeedGrayOLED_Address); // begin I2C communication
190 Wire.write(SeeedGrayOLED_Command_Mode); // Set OLED Command mode
191 Wire.write(command);
192 Wire.endTransmission(); // End I2C communication
193}
194
195void SeeedGrayOLED::setContrastLevel(unsigned char ContrastLevel)
196{
197 sendCommand(SeeedGrayOLED_Set_ContrastLevel_Cmd);
198 sendCommand(ContrastLevel);
199}
200
201void SeeedGrayOLED::setHorizontalMode()
202{
203 sendCommand(0xA0); // remap to
204 sendCommand(0x42); // horizontal mode
205
206 // Row Address
207 sendCommand(0x75); // Set Row Address
208 sendCommand(0x00); // Start 0
209 sendCommand(0x5f); // End 95
210
211 // Column Address
212 sendCommand(0x15); // Set Column Address
213 sendCommand(0x08); // Start from 8th Column of driver IC. This is 0th Column for OLED
214 sendCommand(0x37); // End at (8 + 47)th column. Each Column has 2 pixels(or segments)
215}
216
217void SeeedGrayOLED::setVerticalMode()
218{
219 sendCommand(0xA0); // remap to
220 sendCommand(0x46); // Vertical mode
221}
222
223void SeeedGrayOLED::setTextXY(unsigned char Row, unsigned char Column)
224{
225 //Column Address
226 sendCommand(0x15); /* Set Column Address */
227 sendCommand(0x08+(Column*4)); /* Start Column: Start from 8 */
228 sendCommand(0x37); /* End Column */
229 // Row Address
230 sendCommand(0x75); /* Set Row Address */
231 sendCommand(0x00+(Row*8)); /* Start Row*/
232 sendCommand(0x07+(Row*8)); /* End Row*/
233}
234
235void SeeedGrayOLED::clearDisplay()
236{
237 unsigned char i,j;
238 for(j=0;j<48;j++)
239 {
240 for(i=0;i<96;i++) //clear all columns
241 {
242 sendData(0x00);
243 }
244 }
245
246}
247
248void SeeedGrayOLED::sendData(unsigned char Data)
249{
250 Wire.beginTransmission(SeeedGrayOLED_Address); // begin I2C transmission
251 Wire.write(SeeedGrayOLED_Data_Mode); // data mode
252 Wire.write(Data);
253 Wire.endTransmission(); // stop I2C transmission
254}
255
256void SeeedGrayOLED::setGrayLevel(unsigned char grayLevel)
257{
258 grayH = (grayLevel << 4) & 0xF0;
259 grayL = grayLevel & 0x0F;
260}
261
262void SeeedGrayOLED::putChar(unsigned char C)
263{
264 if(C < 32 || C > 127) //Ignore non-printable ASCII characters. This can be modified for multilingual font.
265 {
266 C=' '; //Space
267 }
268
269
270 for(char i=0;i<8;i=i+2)
271 {
272 for(char j=0;j<8;j++)
273 {
274 // Character is constructed two pixel at a time using vertical mode from the default 8x8 font
275 char c=0x00;
276 char bit1=(pgm_read_byte(&BasicFont[C-32][i]) >> j) & 0x01;
277 char bit2=(pgm_read_byte(&BasicFont[C-32][i+1]) >> j) & 0x01;
278 // Each bit is changed to a nibble
279 c|=(bit1)?grayH:0x00;
280 c|=(bit2)?grayL:0x00;
281 sendData(c);
282 }
283 }
284}
285
286void SeeedGrayOLED::putString(const char *String)
287{
288 unsigned char i=0;
289 while(String[i])
290 {
291 putChar(String[i]);
292 i++;
293 }
294}
295
296unsigned char SeeedGrayOLED::putNumber(long long_num)
297{
298 unsigned char char_buffer[10]="";
299 unsigned char i = 0;
300 unsigned char f = 0;
301
302 if (long_num < 0)
303 {
304 f=1;
305 putChar('-');
306 long_num = -long_num;
307 }
308 else if (long_num == 0)
309 {
310 f=1;
311 putChar('0');
312 return f;
313 }
314
315 while (long_num > 0)
316 {
317 char_buffer[i++] = long_num % 10;
318 long_num /= 10;
319 }
320
321 f=f+i;
322 for(; i > 0; i--)
323 {
324 putChar('0'+ char_buffer[i - 1]);
325 }
326 return f;
327
328}
329
330
331
332void SeeedGrayOLED::drawBitmap(unsigned char *bitmaparray,int bytes)
333{
334 char localAddressMode = addressingMode;
335 if(addressingMode != HORIZONTAL_MODE)
336 {
337 //Bitmap is drawn in horizontal mode
338 setHorizontalMode();
339 }
340
341 for(int i=0;i<bytes;i++)
342 {
343
344 for(int j=0;j<8;j=j+2)
345 {
346 char c=0x00;
347 char bit1=pgm_read_byte(&bitmaparray[i]) << j & 0x80;
348 char bit2=pgm_read_byte(&bitmaparray[i]) << (j+1) & 0x80;
349
350 // Each bit is changed to a nibble
351 c|=(bit1)?grayH:0x00;
352 // Each bit is changed to a nibble
353 c|=(bit2)?grayL:0x00;
354 sendData(c);
355 }
356 }
357 if(localAddressMode == VERTICAL_MODE)
358 {
359 //If Vertical Mode was used earlier, restore it.
360 setVerticalMode();
361 }
362
363}
364
365void SeeedGrayOLED::setHorizontalScrollProperties(bool direction,unsigned char startRow, unsigned char endRow,unsigned char startColumn, unsigned char endColumn, unsigned char scrollSpeed)
366{
367 /*
368Use the following defines for 'direction' :
369
370 Scroll_Left
371 Scroll_Right
372
373Use the following defines for 'scrollSpeed' :
374
375 Scroll_2Frames
376 Scroll_3Frames
377 Scroll_4Frames
378 Scroll_5Frames
379 Scroll_25Frames
380 Scroll_64Frames
381 Scroll_128Frames
382 Scroll_256Frames
383
384*/
385
386 if(Scroll_Right == direction)
387 {
388 //Scroll Right
389 sendCommand(0x27);
390 }
391 else
392 {
393 //Scroll Left
394 sendCommand(0x26);
395 }
396 sendCommand(0x00); //Dummmy byte
397 sendCommand(startRow);
398 sendCommand(scrollSpeed);
399 sendCommand(endRow);
400 sendCommand(startColumn+8);
401 sendCommand(endColumn+8);
402 sendCommand(0x00); //Dummmy byte
403
404}
405
406void SeeedGrayOLED::activateScroll()
407{
408 sendCommand(SeeedGrayOLED_Activate_Scroll_Cmd);
409}
410
411void SeeedGrayOLED::deactivateScroll()
412{
413 sendCommand(SeeedGrayOLED_Dectivate_Scroll_Cmd);
414}
415
416void SeeedGrayOLED::setNormalDisplay()
417{
418 sendCommand(SeeedGrayOLED_Normal_Display_Cmd);
419}
420
421void SeeedGrayOLED::setInverseDisplay()
422{
423 sendCommand(SeeedGrayOLED_Inverse_Display_Cmd);
424}
425
426SeeedGrayOLED SeeedGrayOled; // Preinstantiate Objects
427
Note: See TracBrowser for help on using the repository browser.