UniGraphic-Fork for ST7920-LCD-controller and SH1106. Tested with 128x64 LCD with SPI and 128x64-OLED with IIC

Dependents:   UniGraphic-St7920-Test AfficheurUTILECO

Fork of UniGraphic by GraphicsDisplay

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SH1106.cpp Source File

SH1106.cpp

00001 /* mbed UniGraphic library - Device specific class
00002  * SH1106 by Karl Zweimüller, based on
00003  * SSD1306 by Copyright (c) 2015 Peter Drescher
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 #include "Protocols.h "
00008 #include "SH1106.h"
00009 
00010 //////////////////////////////////////////////////////////////////////////////////
00011 // display settings ///////////////////////////////////////////////////////
00012 /////////////////////////////////////////////////////////////////////////
00013 
00014 
00015 #define IC_X_SEGS    132 // 132 SEG
00016 #define IC_Y_COMS    64  // 64  COM
00017 
00018 #define SH1106_SETCONTRAST         0x81
00019 #define SH1106_DISPLAYALLON_RESUME 0xA4
00020 #define SH1106_DISPLAYALLON        0xA5
00021 #define SH1106_NORMALDISPLAY       0xA6
00022 #define SH1106_INVERTDISPLAY       0xA7
00023 #define SH1106_DISPLAYOFF          0xAE
00024 #define SH1106_DISPLAYON           0xAF
00025 #define SH1106_SETDISPLAYOFFSET    0xD3
00026 #define SH1106_SETCOMPINS          0xDA
00027 #define SH1106_SETVCOMDETECT       0xDB
00028 #define SH1106_SETDISPLAYCLOCKDIV  0xD5
00029 #define SH1106_SETPRECHARGE        0xD9
00030 #define SH1106_SETMULTIPLEX        0xA8
00031 #define SH1106_SETLOWCOLUMN        0x00
00032 #define SH1106_SETHIGHCOLUMN       0x10
00033 #define SH1106_SETSTARTLINE        0x40
00034 #define SH1106_MEMORYMODE          0x20
00035 #define SH1106_COMSCANINC          0xC0
00036 #define SH1106_COMSCANDEC          0xC8
00037 #define SH1106_SEGREMAP            0xA0
00038 #define SH1106_CHARGEPUMP          0x8D
00039 
00040 SH1106::SH1106(proto_t displayproto, PortName port, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD, const char *name , unsigned int LCDSIZE_X, unsigned  int LCDSIZE_Y)
00041     : LCD(displayproto, port, CS, reset, DC, WR, RD, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name)
00042 {
00043     hw_reset();
00044     BusEnable(true);
00045     init();
00046     cls();
00047     set_orientation(1);
00048     locate(0,0);
00049     copy_to_lcd();
00050 }
00051 SH1106::SH1106(proto_t displayproto, int Hz, PinName mosi, PinName miso, PinName sclk, PinName CS, PinName reset, PinName DC, const char *name, unsigned int LCDSIZE_X, unsigned  int LCDSIZE_Y)
00052     : LCD(displayproto, Hz, mosi, miso, sclk, CS, reset, DC, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name)
00053 {
00054     hw_reset();
00055     BusEnable(true);
00056     init();
00057     cls();
00058     set_orientation(1);
00059     locate(0,0);
00060     copy_to_lcd();
00061 }
00062 
00063 SH1106::SH1106(proto_t displayproto, int Hz, int address, PinName sda, PinName scl, PinName reset, const char* name , unsigned int LCDSIZE_X, unsigned  int LCDSIZE_Y)
00064     : LCD(displayproto, Hz, address, sda, scl,reset, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name)
00065 {
00066     hw_reset();
00067     init();
00068     cls();
00069     set_orientation(1);
00070     locate(0,0);
00071     copy_to_lcd();
00072 }
00073 
00074 
00075 // reset and init the lcd controller
00076 void SH1106::init()
00077 {
00078     /* Start Initial Sequence ----------------------------------------------------*/
00079     /* SH1106
00080      //  wr_cmd8(0xE2);   //  sw reset
00081        wait_ms(15);
00082 
00083        wr_cmd8(SH1106_DISPLAYOFF);    // no problem in SPI_16 for single byte cmds
00084        wr_cmd16((SH1106_SETDISPLAYCLOCKDIV<<8)|0x80);   // wr_cmd16 for multibyte cmds issue in SPI16 mode
00085      //  wr_cmd8(0x80); // in SPI_16 it would become 0xE380 and will break things up
00086        wr_cmd16((SH1106_SETMULTIPLEX<<8)|63);
00087     //   wr_cmd8(63);
00088 
00089        wr_cmd16((SH1106_SETDISPLAYOFFSET<<8)|0x00);
00090     //   wr_cmd8(0x0);
00091 
00092        wr_cmd8(SH1106_SETSTARTLINE | 0x0);            // line #0
00093 
00094        wr_cmd16((SH1106_CHARGEPUMP<<8)|0x14);
00095      //  wr_cmd8(0x14);                         // 0x10
00096 
00097        wr_cmd16((SH1106_MEMORYMODE<<8)|0x00);
00098     //   wr_cmd8(0x00);                                  // 0x0 act like ks0108
00099 
00100        wr_cmd8(SH1106_SEGREMAP ); //| 0x1);
00101 
00102        wr_cmd8(SH1106_COMSCANDEC);
00103 
00104        wr_cmd16((SH1106_SETCOMPINS<<8)|0x12);
00105      //  wr_cmd8(0x12);                           //        LCDSIZE_Y == 32 ? 0x02 : 0x12);
00106 
00107        wr_cmd16((SH1106_SETCONTRAST<<8)|0xCF);
00108     //   wr_cmd8(0xCF);                              //  _rawHeight == 32 ? 0x8F : ((vccstate == SH1106_EXTERNALVCC) ? 0x9F : 0xCF) );
00109 
00110        wr_cmd16((SH1106_SETPRECHARGE<<8)|0xF1);
00111     //   wr_cmd8(0xF1);                               // : 0x22);
00112 
00113        wr_cmd16((SH1106_SETVCOMDETECT<<8)|0x40);
00114     //   wr_cmd8(0x40);
00115 
00116        wr_cmd8(SH1106_DISPLAYALLON_RESUME);
00117 
00118        //wr_cmd8(SH1106_NORMALDISPLAY);
00119        wr_cmd8(SH1106_INVERTDISPLAY);
00120 
00121        wr_cmd8(SH1106_DISPLAYON);
00122     */
00123 
00124 //SH1106
00125     wr_cmd8(0xAE);//--turn off oled panel
00126     wr_cmd8(0x02);//---set low column address
00127     wr_cmd8(0x10);//---set high column address
00128     wr_cmd8(0x40);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
00129     wr_cmd8(0x81);//--set contrast control register
00130     wr_cmd8(0xA0);//--Set contrast
00131     wr_cmd8(0xA8);//--set multiplex ratio(1 to 64)
00132     wr_cmd8(0x3F);//--1/64 duty
00133     wr_cmd8(0xD3);//-set display offset    Shift Mapping RAM Counter (0x00~0x3F)
00134     wr_cmd8(0x00);//-not offset
00135     wr_cmd8(0xd5);//--set display clock divide ratio/oscillator frequency
00136     wr_cmd8(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
00137     wr_cmd8(0xD9);//--set pre-charge period
00138     wr_cmd8(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
00139     wr_cmd8(0xDA);//--set com pins hardware configuration
00140     wr_cmd8(0x12); // or 0x02
00141     wr_cmd8(0xDB);//--set vcomh
00142     wr_cmd8(0x40);//Set VCOM Deselect Level
00143     //wr_cmd8(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02) // not on SH1106
00144     wr_cmd8(0xA4);// Disable Entire Display On (0xa4/0xa5)
00145     wr_cmd8(0xA6);// Disable Inverse Display On (0xa6/a7)
00146 
00147     wr_cmd8(0xA0); // normal order
00148     //wr_cmd8(0xA1); // reverse order
00149     wr_cmd8(0xC0);//Set COM/Row Scan Direction normal
00150     //wr_cmd8(0xC8); //reverse direction
00151     wr_cmd8(0xAF);//--turn on oled panel
00152 }
00153 
00154 ////////////////////////////////////////////////////////////////////
00155 // functions that overrides the standard ones implemented in LCD.cpp
00156 ////////////////////////////////////////////////////////////////////
00157 
00158 void SH1106::copy_to_lcd(void)
00159 {
00160 
00161     for(uint8_t page=0; page<8 /*_LCDPAGES */; page++) {
00162         wr_cmd8(0xB0+page);        // set page
00163         wr_cmd8(0x02);             // set column low nibble My Display starts at column 2 (up to column 130 of 132)
00164         wr_cmd8(0x10);             // set column hi  nibble
00165         wr_grambuf(buffer16+(page*screensize_X>>1), screensize_X>>1);   // send whole page pixels as shorts(16bit) not bytes!
00166     }
00167 }
00168 
00169 void SH1106::cls(void)
00170 {
00171     unsigned short tmp = _background^0xFFFF;
00172     //memset(buffer,tmp,/*screensize_X*_LCDPAGES*/ 128*64/8);  // clear display buffer
00173     memset(buffer,0x00,screensize_X*(screensize_Y>>3));  // clear display buffer
00174     if (get_auto_up()) {
00175         copy_to_lcd();
00176     }
00177 }
00178 
00179 void SH1106::mirrorXY(mirror_t mode)
00180 {
00181     switch (mode) {
00182         case(NONE):
00183             wr_cmd16(0xA0C0);
00184             break;
00185         case(X):
00186             wr_cmd16(0xA1C0);
00187             break;
00188         case(Y):
00189             wr_cmd16(0xA0C8);
00190             break;
00191         case(XY):
00192             wr_cmd16(0xA1C8);
00193             break;
00194     }
00195 }
00196 
00197 void SH1106::set_contrast(int o)
00198 {
00199     contrast = o;
00200 
00201     wr_cmd16(0x8100|(o&0xFF));
00202 }
00203 
00204 void SH1106::pixel(int x, int y, unsigned short color)
00205 {
00206     // first check parameter
00207     if((x >= screensize_X) || (y >= screensize_Y) || (x<0) || (y<0)) return;
00208 
00209     //Buffer[(Xpoint + (Ypoint / 8) * sOLED_DIS.OLED_Dis_Column)] |= 1 << (Ypoint % 8);
00210     //Buffer[(Xpoint + (Ypoint / 8) * sOLED_DIS.OLED_Dis_Column)] |= 0 << (Ypoint % 8);
00211     if (color) {
00212         buffer[x+((y>>3)*screensize_X)] |=  1 << (y % 8);
00213     } else       {
00214         buffer[x+((y>>3)*screensize_X)] |=  0 << (y % 8);
00215     }
00216 }
00217 
00218 
00219 unsigned short SH1106::pixelread(int x, int y)
00220 {
00221     // first check parameter
00222     if((x >= screensize_X) || (y >= screensize_Y) || (x<0) || (y<0)) return 0;
00223 
00224     //Buffer[(Xpoint + (Ypoint / 8) * sOLED_DIS.OLED_Dis_Column)] |= 1 << (Ypoint % 8);
00225     //Buffer[(Xpoint + (Ypoint / 8) * sOLED_DIS.OLED_Dis_Column)] |= 0 << (Ypoint % 8);
00226     if (buffer[x+((y>>3)*screensize_X)] &  1 << (y % 8) == 0)
00227         return Black ;  // pixel not set, Black
00228     else return White; // pixel set, White
00229 }