Char font '~' changed to block for making Big number font

Dependents:   Real-time_Bus_Arrival_Alarm WaterLogger

Fork of SeeedGrayOLED by Dan Cohen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SeeedGrayOLED.cpp Source File

SeeedGrayOLED.cpp

00001 /*
00002  * SeeedGrayOLED.cpp
00003  * SSD1327 Gray OLED Driver Library for SSD1327
00004  *
00005  * Copyright (c) 2011 seeed technology inc.
00006  * Author        :   Visweswara R
00007  * Create Time   :   Dec 2011
00008  * Change Log    :
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00023  */
00024 
00025 #include "mbed.h"
00026 #include "SeeedGrayOLED.h"
00027 #include <stdio.h>
00028 #include <string.h>
00029 
00030 // 8x8 Font ASCII 32 - 127 Implemented
00031 // Users can modify this to support more characters(glyphs)
00032 // BasicFont is placed in code memory.
00033 
00034 // This font can be freely used without any restriction(It is placed in public domain)
00035 // const unsigned char BasicFont[][8] PROGMEM=
00036 
00037 
00038 const uint8_t BasicFont[][8] = {
00039   {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
00040   {0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00},
00041   {0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00},
00042   {0x00,0x14,0x7F,0x14,0x7F,0x14,0x00,0x00},
00043   {0x00,0x24,0x2A,0x7F,0x2A,0x12,0x00,0x00},
00044   {0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00},
00045   {0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00},
00046   {0x00,0x00,0x05,0x03,0x00,0x00,0x00,0x00},
00047   {0x00,0x1C,0x22,0x41,0x00,0x00,0x00,0x00},
00048   {0x00,0x41,0x22,0x1C,0x00,0x00,0x00,0x00},
00049   {0x00,0x08,0x2A,0x1C,0x2A,0x08,0x00,0x00},
00050   {0x00,0x08,0x08,0x3E,0x08,0x08,0x00,0x00},
00051   {0x00,0xA0,0x60,0x00,0x00,0x00,0x00,0x00},
00052   {0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00},
00053   {0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00},
00054   {0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00},
00055   {0x00,0x3E,0x51,0x49,0x45,0x3E,0x00,0x00},
00056   {0x00,0x00,0x42,0x7F,0x40,0x00,0x00,0x00},
00057   {0x00,0x62,0x51,0x49,0x49,0x46,0x00,0x00},
00058   {0x00,0x22,0x41,0x49,0x49,0x36,0x00,0x00},
00059   {0x00,0x18,0x14,0x12,0x7F,0x10,0x00,0x00},
00060   {0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00},
00061   {0x00,0x3C,0x4A,0x49,0x49,0x30,0x00,0x00},
00062   {0x00,0x01,0x71,0x09,0x05,0x03,0x00,0x00},
00063   {0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00},
00064   {0x00,0x06,0x49,0x49,0x29,0x1E,0x00,0x00},
00065   {0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00},
00066   {0x00,0x00,0xAC,0x6C,0x00,0x00,0x00,0x00},
00067   {0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00},
00068   {0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00},
00069   {0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00},
00070   {0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00},
00071   {0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00},
00072   {0x00,0x7E,0x09,0x09,0x09,0x7E,0x00,0x00},
00073   {0x00,0x7F,0x49,0x49,0x49,0x36,0x00,0x00},
00074   {0x00,0x3E,0x41,0x41,0x41,0x22,0x00,0x00},
00075   {0x00,0x7F,0x41,0x41,0x22,0x1C,0x00,0x00},
00076   {0x00,0x7F,0x49,0x49,0x49,0x41,0x00,0x00},
00077   {0x00,0x7F,0x09,0x09,0x09,0x01,0x00,0x00},
00078   {0x00,0x3E,0x41,0x41,0x51,0x72,0x00,0x00},
00079   {0x00,0x7F,0x08,0x08,0x08,0x7F,0x00,0x00},
00080   {0x00,0x41,0x7F,0x41,0x00,0x00,0x00,0x00},
00081   {0x00,0x20,0x40,0x41,0x3F,0x01,0x00,0x00},
00082   {0x00,0x7F,0x08,0x14,0x22,0x41,0x00,0x00},
00083   {0x00,0x7F,0x40,0x40,0x40,0x40,0x00,0x00},
00084   {0x00,0x7F,0x02,0x0C,0x02,0x7F,0x00,0x00},
00085   {0x00,0x7F,0x04,0x08,0x10,0x7F,0x00,0x00},
00086   {0x00,0x3E,0x41,0x41,0x41,0x3E,0x00,0x00},
00087   {0x00,0x7F,0x09,0x09,0x09,0x06,0x00,0x00},
00088   {0x00,0x3E,0x41,0x51,0x21,0x5E,0x00,0x00},
00089   {0x00,0x7F,0x09,0x19,0x29,0x46,0x00,0x00},
00090   {0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00},
00091   {0x00,0x01,0x01,0x7F,0x01,0x01,0x00,0x00},
00092   {0x00,0x3F,0x40,0x40,0x40,0x3F,0x00,0x00},
00093   {0x00,0x1F,0x20,0x40,0x20,0x1F,0x00,0x00},
00094   {0x00,0x3F,0x40,0x38,0x40,0x3F,0x00,0x00},
00095   {0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00},
00096   {0x00,0x03,0x04,0x78,0x04,0x03,0x00,0x00},
00097   {0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00},
00098   {0x00,0x7F,0x41,0x41,0x00,0x00,0x00,0x00},
00099   {0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00},
00100   {0x00,0x41,0x41,0x7F,0x00,0x00,0x00,0x00},
00101   {0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x00},
00102   {0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00},
00103   {0x00,0x01,0x02,0x04,0x00,0x00,0x00,0x00},
00104   {0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00},
00105   {0x00,0x7F,0x48,0x44,0x44,0x38,0x00,0x00},
00106   {0x00,0x38,0x44,0x44,0x28,0x00,0x00,0x00},
00107   {0x00,0x38,0x44,0x44,0x48,0x7F,0x00,0x00},
00108   {0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00},
00109   {0x00,0x08,0x7E,0x09,0x02,0x00,0x00,0x00},
00110   {0x00,0x18,0xA4,0xA4,0xA4,0x7C,0x00,0x00},
00111   {0x00,0x7F,0x08,0x04,0x04,0x78,0x00,0x00},
00112   {0x00,0x00,0x7D,0x00,0x00,0x00,0x00,0x00},
00113   {0x00,0x80,0x84,0x7D,0x00,0x00,0x00,0x00},
00114   {0x00,0x7F,0x10,0x28,0x44,0x00,0x00,0x00},
00115   {0x00,0x41,0x7F,0x40,0x00,0x00,0x00,0x00},
00116   {0x00,0x7C,0x04,0x18,0x04,0x78,0x00,0x00},
00117   {0x00,0x7C,0x08,0x04,0x7C,0x00,0x00,0x00},
00118   {0x00,0x38,0x44,0x44,0x38,0x00,0x00,0x00},
00119   {0x00,0xFC,0x24,0x24,0x18,0x00,0x00,0x00},
00120   {0x00,0x18,0x24,0x24,0xFC,0x00,0x00,0x00},
00121   {0x00,0x00,0x7C,0x08,0x04,0x00,0x00,0x00},
00122   {0x00,0x48,0x54,0x54,0x24,0x00,0x00,0x00},
00123   {0x00,0x04,0x7F,0x44,0x00,0x00,0x00,0x00},
00124   {0x00,0x3C,0x40,0x40,0x7C,0x00,0x00,0x00},
00125   {0x00,0x1C,0x20,0x40,0x20,0x1C,0x00,0x00},
00126   {0x00,0x3C,0x40,0x30,0x40,0x3C,0x00,0x00},
00127   {0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00},
00128   {0x00,0x1C,0xA0,0xA0,0x7C,0x00,0x00,0x00},
00129   {0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00},
00130   {0x00,0x08,0x36,0x41,0x00,0x00,0x00,0x00},
00131   {0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00},
00132   {0x00,0x41,0x36,0x08,0x00,0x00,0x00,0x00},
00133   {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},//{0x00,0x02,0x01,0x01,0x02,0x01,0x00,0x00}, // '~' => block
00134   {0x00,0x02,0x05,0x05,0x02,0x00,0x00,0x00} 
00135 };
00136 
00137 
00138 SeeedGrayOLED::SeeedGrayOLED(PinName sda, PinName scl):
00139     _device(sda, scl)
00140 {    
00141 }
00142 
00143 
00144 void SeeedGrayOLED::init(void)
00145 {
00146     sendCommand(0xFD); // Unlock OLED driver IC MCU interface from entering command. i.e: Accept commands
00147     sendCommand(0x12);
00148     sendCommand(0xAE); // Set display off
00149     sendCommand(0xA8); // set multiplex ratio
00150     sendCommand(0x5F); // 96
00151     sendCommand(0xA1); // set display start line
00152     sendCommand(0x00);
00153     sendCommand(0xA2); // set display offset
00154     sendCommand(0x60);
00155     sendCommand(0xA0); // set remap
00156     sendCommand(0x46);
00157     sendCommand(0xAB); // set vdd internal
00158     sendCommand(0x01); //
00159     sendCommand(0x81); // set contrasr
00160     sendCommand(0x53); // 100 nit
00161     sendCommand(0xB1); // Set Phase Length
00162     sendCommand(0X51); //
00163     sendCommand(0xB3); // Set Display Clock Divide Ratio/Oscillator Frequency
00164     sendCommand(0x01);
00165     sendCommand(0xB9); //
00166     sendCommand(0xBC); // set pre_charge voltage/VCOMH
00167     sendCommand(0x08); // (0x08);
00168     sendCommand(0xBE); // set VCOMH
00169     sendCommand(0X07); // (0x07);
00170     sendCommand(0xB6); // Set second pre-charge period
00171     sendCommand(0x01); //
00172     sendCommand(0xD5); // enable second precharge and enternal vsl
00173     sendCommand(0X62); // (0x62);
00174     sendCommand(0xA4); // Set Normal Display Mode
00175     sendCommand(0x2E); // Deactivate Scroll
00176     sendCommand(0xAF); // Switch on display
00177     wait_ms(100);
00178 
00179     // Row Address
00180     sendCommand(0x75);    // Set Row Address 
00181     sendCommand(0x00);    // Start 0
00182     sendCommand(0x5f);    // End 95 
00183 
00184 
00185     // Column Address
00186     sendCommand(0x15);    // Set Column Address 
00187     sendCommand(0x08);    // Start from 8th Column of driver IC. This is 0th Column for OLED 
00188     sendCommand(0x37);    // End at  (8 + 47)th column. Each Column has 2 pixels(segments)
00189 
00190     // Init gray level for text. Default:Brightest White
00191     grayH= 0xF0;
00192     grayL= 0x0F;
00193 }
00194 
00195 void SeeedGrayOLED::sendCommand(unsigned char command)
00196 {
00197     int transmissionSuccessful;
00198     _device.start();
00199     transmissionSuccessful  = _device.write(SeeedGrayOLED_Address << 1);
00200     transmissionSuccessful |= _device.write(SeeedGrayOLED_Command_Mode);
00201     transmissionSuccessful |= _device.write(command);
00202     _device.stop();
00203     // return (transmissionSuccessful);    
00204 }
00205 
00206 void SeeedGrayOLED::setContrastLevel(unsigned char ContrastLevel)
00207 {
00208     sendCommand(SeeedGrayOLED_Set_ContrastLevel_Cmd);
00209     sendCommand(ContrastLevel);
00210 }
00211 
00212 void SeeedGrayOLED::setHorizontalMode()
00213 {
00214     sendCommand(0xA0); // remap to
00215     sendCommand(0x42); // horizontal mode
00216 
00217     // Row Address
00218     sendCommand(0x75);    // Set Row Address 
00219     sendCommand(0x00);    // Start 0
00220     sendCommand(0x5f);    // End 95 
00221 
00222     // Column Address
00223     sendCommand(0x15);    // Set Column Address 
00224     sendCommand(0x08);    // Start from 8th Column of driver IC. This is 0th Column for OLED 
00225     sendCommand(0x37);    // End at  (8 + 47)th column. Each Column has 2 pixels(or segments)
00226 }
00227 
00228 void SeeedGrayOLED::setVerticalMode()
00229 {
00230     sendCommand(0xA0); // remap to
00231     sendCommand(0x46); // Vertical mode
00232 }
00233 
00234 void SeeedGrayOLED::setTextXY(unsigned char Row, unsigned char Column)
00235 {
00236     //Column Address
00237     sendCommand(0x15);             /* Set Column Address */
00238     sendCommand(0x08+(Column*4));  /* Start Column: Start from 8 */
00239     sendCommand(0x37);             /* End Column */
00240     // Row Address
00241     sendCommand(0x75);             /* Set Row Address */
00242     sendCommand(0x00+(Row*8));     /* Start Row*/
00243     sendCommand(0x07+(Row*8));     /* End Row*/
00244 }
00245 
00246 void SeeedGrayOLED::clearDisplay()
00247 {
00248     unsigned char i,j;
00249     for(j=0;j<48;j++)
00250     {
00251         for(i=0;i<96;i++)  //clear all columns
00252         {
00253             sendData(0x00);
00254         }
00255     }
00256 
00257 }
00258 
00259 void SeeedGrayOLED::sendData(unsigned char Data)
00260 {
00261     int transmissionSuccessful;
00262     _device.start();
00263     transmissionSuccessful  = _device.write(SeeedGrayOLED_Address << 1);
00264     transmissionSuccessful |= _device.write(SeeedGrayOLED_Data_Mode);
00265     transmissionSuccessful |= _device.write(Data);
00266     _device.stop();
00267     // return (transmissionSuccessful);       
00268 }
00269 
00270 void SeeedGrayOLED::setGrayLevel(unsigned char grayLevel)
00271 {
00272     grayH = (grayLevel << 4) & 0xF0;
00273     grayL =  grayLevel & 0x0F;
00274 }
00275 
00276 void SeeedGrayOLED::putChar(unsigned char C)
00277 {
00278     if(C < 32 || C > 127) //Ignore non-printable ASCII characters. This can be modified for multilingual font.
00279     {
00280         C=' '; //Space
00281     }   
00282 
00283     
00284     for(char i=0;i<8;i=i+2)
00285     {
00286         for(char j=0;j<8;j++)
00287         {
00288             // Character is constructed two pixel at a time using vertical mode from the default 8x8 font
00289             char c=0x00;
00290             char bit1=(BasicFont[C-32][i]   >> j)  & 0x01;  
00291             char bit2=(BasicFont[C-32][i+1] >> j)  & 0x01;
00292             // Each bit is changed to a nibble
00293             c|=(bit1)?grayH:0x00;
00294             c|=(bit2)?grayL:0x00;
00295             sendData(c);
00296         }
00297     }
00298 }
00299 
00300 void SeeedGrayOLED::putString(const char *String)
00301 {
00302     unsigned char i=0;
00303     while(String[i])
00304     {
00305         putChar(String[i]);     
00306         i++;
00307     }
00308 }
00309 
00310 unsigned char SeeedGrayOLED::putNumber(long long_num)
00311 {
00312     unsigned char char_buffer[10]="";
00313     unsigned char i = 0;
00314     unsigned char f = 0;
00315 
00316     if (long_num < 0)
00317     {
00318         f=1;
00319         putChar('-');
00320         long_num = -long_num;
00321     }
00322     else if (long_num == 0)
00323     {
00324         f=1;
00325         putChar('0');
00326         return f;
00327     }
00328 
00329     while (long_num > 0)
00330     {
00331         char_buffer[i++] = long_num % 10;
00332         long_num /= 10;
00333     }
00334 
00335     f=f+i;
00336     for(; i > 0; i--)
00337     {
00338         putChar('0'+ char_buffer[i - 1]);
00339     }
00340     return f;
00341 
00342 }
00343 
00344 
00345 
00346 void SeeedGrayOLED::drawBitmap(unsigned char *bitmaparray,int bytes)
00347 {
00348     char localAddressMode = addressingMode;
00349     if(addressingMode != HORIZONTAL_MODE)
00350     {
00351         //Bitmap is drawn in horizontal mode
00352         setHorizontalMode();
00353     }
00354 
00355     for(int i=0;i<bytes;i++)    
00356     {
00357 
00358     for(int j=0;j<8;j=j+2)     
00359     {
00360         char c=0x00;
00361         char bit1=bitmaparray[i] <<  j    & 0x80;  
00362         char bit2=bitmaparray[i] << (j+1) & 0x80;
00363 
00364         // Each bit is changed to a nibble
00365         c|=(bit1)?grayH:0x00;
00366         // Each bit is changed to a nibble
00367         c|=(bit2)?grayL:0x00;
00368         sendData(c);
00369      }
00370     }
00371     if(localAddressMode == VERTICAL_MODE)
00372     {
00373         //If Vertical Mode was used earlier, restore it.
00374         setVerticalMode();
00375     }
00376 
00377 }
00378 
00379 void SeeedGrayOLED::setHorizontalScrollProperties(bool direction,unsigned char startRow, unsigned char endRow,unsigned char startColumn, unsigned char endColumn, unsigned char scrollSpeed)
00380 {
00381     /*
00382 Use the following defines for 'direction' :
00383 
00384  Scroll_Left            
00385  Scroll_Right           
00386 
00387 Use the following defines for 'scrollSpeed' :
00388 
00389  Scroll_2Frames     
00390  Scroll_3Frames
00391  Scroll_4Frames
00392  Scroll_5Frames 
00393  Scroll_25Frames
00394  Scroll_64Frames
00395  Scroll_128Frames
00396  Scroll_256Frames
00397 
00398 */
00399 
00400     if(Scroll_Right == direction)
00401     {
00402         //Scroll Right
00403         sendCommand(0x27);
00404     }
00405     else
00406     {
00407         //Scroll Left  
00408         sendCommand(0x26);
00409     }
00410     sendCommand(0x00);       //Dummmy byte
00411     sendCommand(startRow);
00412     sendCommand(scrollSpeed);
00413     sendCommand(endRow);
00414     sendCommand(startColumn+8);
00415     sendCommand(endColumn+8);
00416     sendCommand(0x00);      //Dummmy byte
00417 
00418 }
00419 
00420 void SeeedGrayOLED::activateScroll()
00421 {
00422     sendCommand(SeeedGrayOLED_Activate_Scroll_Cmd);
00423 }
00424 
00425 void SeeedGrayOLED::deactivateScroll()
00426 {
00427     sendCommand(SeeedGrayOLED_Dectivate_Scroll_Cmd);
00428 }
00429 
00430 void SeeedGrayOLED::setNormalDisplay()
00431 {
00432     sendCommand(SeeedGrayOLED_Normal_Display_Cmd);
00433 }
00434 
00435 void SeeedGrayOLED::setInverseDisplay()
00436 {
00437     sendCommand(SeeedGrayOLED_Inverse_Display_Cmd);
00438 }
00439 
00440