takashi kadono / ssd1331

Dependents:   Nucleo_446 Nucleo446_SSD1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ssd1331.cpp Source File

ssd1331.cpp

00001 
00002 #include "ssd1331.h"
00003 #include "mbed.h"
00004 
00005 
00006 
00007 /*
00008     private function
00009 */
00010 
00011 void ssd1331::DataWr(uint8_t data) {
00012     _dc=1;
00013     _cs=0;
00014     _spi.write(data);
00015     _cs=1;
00016 }
00017 
00018 void ssd1331::CmdWr(uint8_t Cmd) {
00019     _dc=0;
00020     _cs=0;
00021     _spi.write(Cmd);
00022     _cs=1;
00023 }
00024 
00025 ssd1331::ssd1331(PinName mosi,PinName miso,PinName sclk,PinName cs,PinName dc,PinName reset) 
00026     : _spi(mosi,miso,sclk),_cs(cs),_dc(dc),_rst(reset) 
00027 {
00028     _spi.format(8,3);
00029     _spi.frequency(500000);
00030     _cs =1;
00031     _dc =1;
00032     _rst =0;
00033     
00034     SetFontInf();
00035 }
00036 
00037 
00038 
00039 void ssd1331::Initssd1331()
00040 {
00041     _cs =1;
00042     _dc =1;
00043     _rst =0;
00044     
00045     wait_ms(100);
00046     _rst =1;
00047     wait_ms(1);
00048     
00049     CmdWr(SSD1331_CMD_SetDspOff);
00050     CmdWr(SSD1331_CMD_SetRemap_DataFormat); CmdWr(0x72);    //A[7:6] = 00; 256 color. A[7:6] = 01; 65k color format
00051     CmdWr(SSD1331_CMD_SetDspStrtLine);      CmdWr(0x0);     //
00052     CmdWr(SSD1331_CMD_SetDspOffset);        CmdWr(0x0);     //
00053     CmdWr(SSD1331_CMD_SetDspModeNorm);
00054     CmdWr(SSD1331_CMD_SetMltplRatio);       CmdWr(63);      //
00055     CmdWr(SSD1331_CMD_SetMstrCnfg);         CmdWr(0x8e);    //
00056     CmdWr(SSD1331_CMD_PowerSave);           CmdWr(0x1A);    //
00057     CmdWr(SSD1331_CMD_P1_P2_ADJ);           CmdWr(0x74);    //
00058     CmdWr(SSD1331_CMD_SetDispCLK);          CmdWr(0xf0);    //
00059     CmdWr(SSD1331_CMD_SetChrgA);            CmdWr(0x81);    //
00060     CmdWr(SSD1331_CMD_SetChrgB);            CmdWr(0x82);    //
00061     CmdWr(SSD1331_CMD_SetChrgC);            CmdWr(0x83);    //
00062     CmdWr(SSD1331_CMD_SetPrChrgV);          CmdWr(0x3A);    //
00063     CmdWr(SSD1331_CMD_SetVcomhV);           CmdWr(0x3E);    //
00064     CmdWr(SSD1331_CMD_SetMstrCrrnt);        CmdWr(0x06);    //
00065     CmdWr(SSD1331_CMD_SetClmAddr);          CmdWr(0);   CmdWr(95);  //
00066     CmdWr(SSD1331_CMD_SetRawAddr);          CmdWr(0);   CmdWr(63);  //
00067     CmdWr(SSD1331_CMD_SetCntrstA);          CmdWr(0xff);    //
00068     CmdWr(SSD1331_CMD_SetCntrstB);          CmdWr(0xff);    //
00069     CmdWr(SSD1331_CMD_SetCntrstC);          CmdWr(0xfF);    //
00070 
00071     CmdWr(SSD1331_CMD_SetDspOn);            //
00072     wait_ms(100);    
00073 }
00074 
00075 void ssd1331::ClearScreen()
00076 {
00077 
00078     wait_ms(1); //クリアーコマンドは400μs 以上の休止期間が必要かも
00079 
00080     CmdWr(SSD1331_CMD_ClearWindow);
00081         CmdWr(0);   //X始点
00082         CmdWr(0);   //Y始点
00083         CmdWr(WIDTH_1331-1);    //X終点
00084         CmdWr(HEIGHT_1331-1);   //Y終点
00085 
00086     wait_ms(1); //クリアーコマンドは400μs 以上の休止期間が必要かも
00087     }
00088 
00089 
00090 void ssd1331::DrawPix(    uint16_t x, 
00091                             uint16_t y, 
00092                             uint16_t color   )
00093 {
00094     if(x>=WIDTH_1331-1){        return ;    }
00095     if(y>=HEIGHT_1331-1){   return ;    }
00096 
00097     CmdWr(SSD1331_CMD_SetClmAddr);      CmdWr(x);   CmdWr(x);
00098     CmdWr(SSD1331_CMD_SetRawAddr);      CmdWr(y);   CmdWr(y);
00099     DataWr( 0xff & (color>>8) );        DataWr( 0xff & (color) );
00100     return;
00101 
00102 }
00103 
00104 void ssd1331::DrawLine( uint8_t xs, 
00105                         uint8_t ys, 
00106                         uint8_t xe, 
00107                         uint8_t ye, 
00108                         uint16_t color  )
00109 {
00110     if(xe>=WIDTH_1331-1){   return ;    }
00111     if(ye>=HEIGHT_1331-1){  return ;    }
00112     if(xe>=WIDTH_1331-1){   return ;    }
00113     if(ye>=HEIGHT_1331-1){  return ;    }
00114 
00115     CmdWr(SSD1331_CMD_DrawLine);
00116         CmdWr(xs);  CmdWr(ys);  CmdWr(xe);  CmdWr(ye);
00117 
00118         //color     R 5bit      bit 15-11
00119         //          G 6bit      bit 10- 5
00120         //          B 5bit      bit  4- 0
00121         CmdWr((color>>10) & 0x3e);  //R 5bit
00122         CmdWr((color>>5) & 0x3f);   //G 6bit
00123         CmdWr((color<<1)  & 0x3e);  //B 5bit
00124 }
00125 
00126 void ssd1331::DrawRectangle(  uint8_t xs,   
00127                                 uint8_t ys, 
00128                                 uint8_t xe, 
00129                                 uint8_t ye, 
00130                                 uint16_t color  )
00131 {
00132     CmdWr(SSD1331_CMD_FillEnable);  CmdWr(0x00);    //塗りつぶし禁止
00133 
00134     CmdWr(SSD1331_CMD_DrawRectangle);
00135         CmdWr(xs);  CmdWr(ys);  CmdWr(xe);  CmdWr(ye);
00136 
00137         //color     R 5bit      bit 15-11
00138         //          G 6bit      bit 10- 5
00139         //          B 5bit      bit  4- 0
00140         CmdWr((color>>10) & 0x3e);  //R 5bit
00141         CmdWr((color>>5) & 0x3f);   //G 6bit
00142         CmdWr((color<<1)  & 0x3e);  //B 5bit
00143         CmdWr(0);   //R 5bit
00144         CmdWr(0);   //G 6bit
00145         CmdWr(0);   //B 5bit
00146     wait_us(100);
00147 
00148 }
00149 
00150 void ssd1331::DrawRectangleFill(  uint8_t xs,   
00151                                     uint8_t ys, 
00152                                     uint8_t xe, 
00153                                     uint8_t ye, 
00154                                     uint16_t line_color,
00155                                     uint16_t fill_color )
00156 
00157 {
00158     CmdWr(SSD1331_CMD_FillEnable);  CmdWr(0x01);    //塗りつぶし禁止
00159 
00160     CmdWr(SSD1331_CMD_DrawRectangle);
00161         CmdWr(xs);  CmdWr(ys);  CmdWr(xe);  CmdWr(ye);
00162 
00163         //color     R 5bit      bit 15-11
00164         //          G 6bit      bit 10- 5
00165         //          B 5bit      bit  4- 0
00166         CmdWr((line_color>>10) & 0x3e); //R 5bit
00167         CmdWr((line_color>>5) & 0x3f);  //G 6bit
00168         CmdWr((line_color<<1)  & 0x3e); //B 5bit
00169         CmdWr((fill_color>>10) & 0x3e); //R 5bit
00170         CmdWr((fill_color>>5) & 0x3f);  //G 6bit
00171         CmdWr((fill_color<<1)  & 0x3e); //B 5bit
00172     wait_us(400);
00173 }
00174 
00175 
00176 
00177 /*
00178  * Adafruit Font Library
00179  * */
00180 int ssd1331::put_charPattern(char CharCode,uint16_t CHAR_COLOR,uint16_t xs,uint16_t ys,int FontSel)
00181 {
00182     uint8_t FirstCode,char_offset;
00183     uint8_t *pFNT;
00184 
00185     int x,y;
00186     int FontWidth,FontHeight,FontDotCap,bitCnt;
00187     int xOffset,yOffset;
00188     uint8_t LinePattern;
00189     int BitCount;
00190 
00191     FirstCode = FntList[FontSel].pFnt->first;
00192     if ((CharCode > FntList[FontSel].pFnt->last)||(CharCode < FirstCode)){
00193         return -1;
00194     }
00195 
00196     char_offset=(CharCode - FirstCode);
00197     pFNT = FntList[FontSel].pFnt->bitmap;
00198     pFNT += FntList[FontSel].pFnt->glyph[char_offset].bitmapOffset;
00199 
00200     FontWidth = FntList[FontSel].pFnt->glyph[char_offset].width;
00201     FontHeight = FntList[FontSel].pFnt->glyph[char_offset].height;
00202     FontDotCap = FontWidth * FontHeight;
00203 
00204     xOffset = FntList[FontSel].pFnt->glyph[char_offset].xOffset;
00205     yOffset = FntList[FontSel].pFnt->glyph[char_offset].yOffset;
00206 
00207     x=0;
00208     y=0;
00209     BitCount=8;
00210     
00211     for(bitCnt=0;bitCnt<FontDotCap;bitCnt++){
00212         if (8==BitCount){
00213             LinePattern = *pFNT++;
00214             BitCount=0;
00215         }
00216         if((LinePattern & 0x80)!=0){
00217             DrawPix(x+xs+xOffset,y+ys+yOffset,CHAR_COLOR);
00218 //          Serial.print("@");  
00219 
00220 //          Serial.print(" x ");    
00221 //          Serial.print(x+xs+xOffset); 
00222 //          Serial.print(" y ");    
00223 //          Serial.print(y+ys+yOffset); 
00224 
00225         }else{
00226 //          Serial.print(".");  
00227         }
00228 
00229         LinePattern = LinePattern<<1;
00230 
00231         if(x>=FontWidth-1){
00232             x=0;    y++;
00233 //          Serial.println();   
00234         }else{
00235             x++;
00236         }
00237         BitCount++;
00238     }
00239     return FntList[FontSel].pFnt->glyph[char_offset].xAdvance;
00240 }
00241 
00242 int ssd1331::PutString(int x,int y,char *string,uint16_t CharColor,int FontSel)
00243 {
00244     int i,j,k;
00245     char *STR;
00246 
00247     STR = (char *)string;
00248 
00249     for(i=0,j=x;;i++,STR++){
00250 
00251         if (*STR==0){
00252             return 0;
00253         }
00254 
00255         k=put_charPattern(*STR,CharColor,j,y,FontSel);
00256         if(k<0){
00257             return -2;
00258         }else{
00259             j=j+k;
00260         }
00261     }
00262 //    return 0;
00263 }
00264 
00265 void ssd1331::GetFontInfo(int FontNo, FontList  *FntLst){
00266     FntLst->pFnt =FntList[FontNo].pFnt; 
00267 
00268     for(int i=0;i<16;i++){
00269         FntLst->FontName[i] = FntList[FontNo].FontName[i];
00270     }
00271 
00272 //    strcpy(FntList->FontName,FntList[0].FontName);
00273 
00274 }
00275 int ssd1331::GetFontNum(void){
00276     return FontNum;
00277 }
00278 
00279 int ssd1331::SetFontInf()
00280 {
00281     FntList[7].pFnt=(GFXfont *)0;
00282     FntList[6].pFnt=(GFXfont *)0;
00283     FntList[5].pFnt=(GFXfont *)0;
00284     FntList[4].pFnt=(GFXfont *)0;
00285     FntList[3].pFnt=(GFXfont *)0;
00286     FntList[2].pFnt=(GFXfont *)0;
00287     FntList[1].pFnt=(GFXfont *)0;
00288     FntList[0].pFnt=(GFXfont *)0;
00289 
00290     FntList[3].pFnt=(GFXfont *)&TomThumb;
00291     strcpy(FntList[3].FontName,"TomThumb\0");
00292 
00293     FntList[2].pFnt=(GFXfont *)&FreeSerif9pt7b;
00294     strcpy(FntList[2].FontName,"Serif9pt7b\0");
00295 
00296     FntList[1].pFnt=(GFXfont *)&FreeSans9pt7b;
00297     strcpy(FntList[1].FontName,"Sans9pt7b\0");
00298 
00299     FntList[0].pFnt=(GFXfont *)&FreeMonoBold9pt7b;
00300     strcpy(FntList[0].FontName,"MonoBold9pt7b\0");
00301 
00302     return 5;
00303 }