JapanDisplayInc / MIP8F_SPI_Ver40

Dependents:   MIP8f_FRDM_LineBuffer_sample

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MIP8F_SPI.cpp Source File

MIP8F_SPI.cpp

Go to the documentation of this file.
00001 /**
00002 * @file MIP8F_SPI.cpp
00003 * @brief ver4.0 Library source code file: Class for JDI MIP8 display
00004 * @details
00005 * Ver4.0 Addtional function is Line buffer version
00006 * Ver3.0 Addtional function is font display
00007 * ver2.0 Addtional function is Monochome display by 1bit mode of SPI transfer.
00008 *
00009 * <license>
00010 * Copyright 2018 Japan Display Inc.
00011 * Licensed under the Apache License, Version 2.0 (the "License");
00012 * you may not use this file except in compliance with the License.
00013 * You may obtain a copy of the License at
00014 *     http://www.apache.org/licenses/LICENSE-2.0
00015 * Unless required by applicable law or agreed to in writing, software
00016 * distributed under the License is distributed on an "AS IS" BASIS,
00017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018 * See the License for the specific language governing permissions and
00019 * limitations under the License.
00020 */
00021 
00022 #include "mbed.h"
00023 #include "MIP8F_SPI.h"
00024 
00025 // for debug
00026 //Serial pc2(USBTX, USBRX); // tx, rx
00027 
00028 memLCD8::memLCD8(PinName mosi,PinName miso,PinName sclk,PinName cs,PinName disp,PinName power)
00029     : _spi(mosi, miso, sclk),_cs(cs),_disp(disp),_power(power)
00030 {
00031 
00032     _power= 0;
00033     _disp=  0;
00034     _cs  =  0;
00035     wait_us(100);
00036 
00037     _power= 1;
00038     _spi.format(8,0);                // 8bit mode3
00039     _spi.frequency(2000000);         // 2 Mhz SPI clock
00040     _spi.write(0x00);                // mbed dummy
00041 
00042     /*
00043         _cs  = 1;
00044         command(0x02);   // All crear mode
00045         _cs  = 0;
00046     */
00047     _FixedFontWidth=0;//setting:actual font width display  if _FixedFontWidth !=1: font fixed witdh
00048 }
00049 
00050 /**
00051 * @brief  set font name
00052 */
00053 void memLCD8::set_font(unsigned char* f)
00054 {
00055     font = f;
00056 }
00057 
00058 /**
00059 * @brief  set allocation for font display
00060 */
00061 void memLCD8::locate(int x, int y)
00062 {
00063     char_x = x;
00064     char_y = y;
00065 }
00066 
00067 /**
00068 * @brief  setting fixed width between charactor and charctor  for font display
00069 */
00070 void memLCD8::set_FixedFontWidth( unsigned char width )
00071 {
00072     _FixedFontWidth = width;
00073 }
00074 /**
00075 * @brief  setting Actual width between charactor and charctor  for font display
00076 */
00077 void memLCD8::set_ActualFontWidth(void)
00078 {
00079     _FixedFontWidth = 0;
00080 }
00081 
00082 /**
00083 * @brief  set Configuration for transfer mode
00084 * @param[in] int transfermode : instruction the transfer data size ,4bit,3bit,1bit and some parameter
00085 */
00086 void memLCD8::SetTransfermode(int transfermode)
00087 {
00088     switch(transfermode)
00089     {
00090     case TrBIT4:
00091         TrModeCommand = 0x90;
00092         TrAdd = 1;
00093         TrValNum = 1;
00094         break;
00095     case TrBIT3:
00096         TrModeCommand = 0x80;
00097         TrAdd = 4;
00098         TrValNum = 3;
00099         break;
00100     case TrBIT1:
00101         TrModeCommand = 0x88;
00102         TrAdd = 4;
00103         TrValNum = 1;
00104         break;
00105     }
00106 }
00107 
00108 /**
00109 * @brief  set a display size ,width ,height
00110 */
00111 void memLCD8::setWH(int width, int height)
00112 {
00113     _width  = width;
00114     _height = height;
00115 }
00116 
00117 /**
00118 * @brief  set color data of foreground
00119 */
00120 void memLCD8::foreground(uint8_t colour)
00121 {
00122     _foreground = colour;
00123 }
00124 
00125 /**
00126 * @brief  set color data of background
00127 */
00128 void memLCD8::background(uint8_t colour)
00129 {
00130     _background = colour;
00131 }
00132 
00133 /**
00134 * @brief  set the Diaplay On/Off data
00135 */
00136 void memLCD8::SwDisp(bool ONorOFF)
00137 {
00138     _disp= ONorOFF;
00139 }
00140 
00141 /**
00142 * @brief  transfer a command code to the display by SPI
00143 */
00144 void memLCD8::command(char command)
00145 {
00146     wait_us(6);
00147     _cs  = 1;
00148     wait_us(6);
00149     _spi.write(command);//
00150     _spi.write(0x00);// dummy
00151     wait_us(6);
00152     _cs  = 0;
00153 }
00154 
00155 /**
00156 * @brief  putc
00157 */
00158 int memLCD8::_putc(int value)
00159 {
00160     if (value == '\n') {    // new line
00161         char_x = 0;
00162         char_y = char_y + font[3];
00163         if (char_y >= _height - font[3]) {
00164             char_y = 0;
00165         }
00166     }
00167 #if FRAMEBUFF_MODE
00168      else {
00169         character(char_x, char_y, value);
00170     }
00171 #endif
00172     return value;
00173 }
00174 
00175 /**
00176 * @brief  getc
00177 */
00178 int memLCD8::_getc()
00179 {
00180     return -1;
00181 }
00182 /**
00183 * @brief  get color data of background
00184 */
00185 unsigned char memLCD8::get_Background(void)
00186 {
00187     return _background;
00188 }
00189 /*
00190 void memLCD8::setmarge(bool ifMarge)
00191 {
00192     _ifMarge = ifMarge;
00193 }
00194 */
00195 //////////////////////////////////////////////////////////////////////////////
00196 // line buffer mode
00197 //////////////////////////////////////////////////////////////////////////////
00198 #if LINEBUFF_MODE
00199 /**
00200 * @brief  Transfer One Pixel Data with x,y allocation Line buffer mode
00201 * @param[in] int x : horizontal allocation left to right
00202 * @param[in] uint8_t color : the color data for Drawing  0x0X  x is color data(RGBC) C is not used
00203 */
00204 void memLCD8::pixel(int x, uint8_t color)
00205 {
00206     if(!(x % 2)) _dispLINEBUF[x/2] =  _dispLINEBUF[x/2]&0x0F  |  (color << 4)&0xF0 ;   //MASK 0000 1111
00207     else         _dispLINEBUF[x/2] =  _dispLINEBUF[x/2]&0xF0  |  (color     )&0x0F ;   //MASK 1111 0000
00208 }
00209 
00210 /**
00211 * @brief  Transfer Pixel Data from same Line buffer to Display
00212 */
00213 void memLCD8::writeDISPLinebuffer(void)    // refresh whole display
00214 {
00215 
00216     char pol = 0;
00217     char command = 0x90; // 8b 1*0xNNNN *=POL x=AutoW  A  1010
00218 
00219     // frame
00220     for (int i=0; i<_height; i++) {
00221         // line
00222         wait_us(6);
00223         _cs  = 1;
00224         wait_us(6);
00225         _spi.write(command | (pol << 6) | (i+1)>>8 ); // COMMAND
00226         _spi.write((i+1)& 0x00FF ); // V ADDR
00227         for(int j=0; j<_width/2; j++) _spi.write(_dispLINEBUF[j]);
00228         _spi.write(0x00); // DUMMY transfer
00229         _spi.write(0x00); // DUMMY transfer
00230         wait_us(6);
00231         _cs  = 0;
00232         if(pol) pol=0x00;
00233         else    pol=0x01;
00234     }
00235 }
00236 
00237 /**
00238 * @brief  Transfer Pixel Data from line buffer to Display
00239 * @param[in] int line : set line number for display.
00240 * @param[in] int transfermode : instruction the transfer data size ,4bit,3bit,1bit
00241 */
00242 void memLCD8::writeDISP(int line,int transfermode)    // refresh gate line display
00243 {
00244 
00245     char pol = 0;
00246     //char command = 0x90; // 8b 1*0xNNNN *=POL x=AutoW  A  1010
00247     SetTransfermode(transfermode);
00248     
00249     // frame
00250         // line
00251         wait_us(6);
00252         _cs  = 1;
00253         wait_us(6);
00254         _spi.write(TrModeCommand | (pol << 6) | (line+1)>>8 ); // COMMAND
00255         //pc2.printf("com 0x%x\n",TrModeCommand | (pol << 6) | (i+1)>>8);
00256         
00257         _spi.write((line+1)& 0x00FF ); // V ADDR
00258         //pc2.printf("v adr 0x%x\n",(i+1)& 0x00FF);
00259         
00260         for(int j=0; j<_width; j+=TrAdd)
00261         {
00262             GetPixelValueFromLineBuffer(j,_dispLINEBUF);
00263             //pc2.printf("data=[%d]{%d][%d]/[%d]\n",j*TrAdd,i,TrValue[0],TrAdd);
00264             for(int k=0;k<TrValNum;k++)
00265                 _spi.write(TrValue[k]);
00266         }
00267         _spi.write(0x00); // DUMMY transfer
00268         _spi.write(0x00); // DUMMY transfer
00269 //        wait_ms(1);      //1.8Hz simulation
00270 //        wait_ms(2);      //1.35Hz simulation
00271         wait_us(6);
00272         _cs  = 0;
00273         if(pol) pol=0x00;
00274         else    pol=0x01;
00275 
00276 }
00277 
00278 /**
00279 * @brief  Get Edited data for SPI transfer from line buffer
00280 * @param[in] int x : horizontal allocation left to right
00281 * @param[in] uint8_t* buff : buffer data for Display
00282 */
00283 int* memLCD8::GetPixelValueFromLineBuffer(int _x,uint8_t* buff)
00284 {
00285     //bitmap data = 4bit data => modify transfer data bit size;
00286     switch(TrModeCommand)
00287     {
00288     case 0x90: //TrBIT4:
00289         // buffer 2pixel/1byte => 2pixel/1byte  buffe 2byte毎進める。
00290         TrValue[0] = _dispLINEBUF[_x];
00291         break;
00292     case 0x80://TrBIT3:
00293         // buffer 2pixel/1byte => 3pixel-1subpixel/1bye (24 pixel/3byte) buffer 3byte毎進める。
00294         for(int j=0;j<3;j++) TrValue[j] = 0;
00295         //for( int i = 0 ; i<12 ; i--)
00296         {
00297             //4 bit RGBN(Nは予備) => 3bit RGB
00298             if( _width/2 > _x )
00299             {
00300                 TrValue[0] = TrValue[0] | ( ( (_dispLINEBUF[_x    ]&0xE0) ) );
00301                 TrValue[0] = TrValue[0] | ( ( (_dispLINEBUF[_x    ]&0x0E) ) << 1);
00302             }
00303             if( _width/2 > _x + 1 )
00304             {
00305                 TrValue[0] = TrValue[0] | ( ( (_dispLINEBUF[_x + 1]&0xC0) ) >> 6);
00306 
00307                 TrValue[1] = TrValue[1] | ( ( (_dispLINEBUF[_x + 1]&0x20) ) << 2);
00308                 TrValue[1] = TrValue[1] | ( ( (_dispLINEBUF[_x + 1]&0x0E) ) << 3);
00309             }
00310             if( _width/2 > _x + 2 )
00311             {
00312                 TrValue[1] = TrValue[1] | ( ( (_dispLINEBUF[ _x + 2]&0xE0) ) >> 4);
00313                 TrValue[1] = TrValue[1] | ( ( (_dispLINEBUF[ _x + 2]&0x08) ) >> 3);
00314 
00315                 TrValue[2] = TrValue[2] | ( ( (_dispLINEBUF[_x + 2]&0x06) ) << 5);
00316             }
00317             if( _width/2 > _x + 3 )
00318             {
00319                 TrValue[2] = TrValue[2] | ( ( (_dispLINEBUF[ _x + 3]&0xE0) ) >> 2);
00320                 TrValue[2] = TrValue[2] | ( ( (_dispLINEBUF[_x + 3]&0x0E) ) >> 1);
00321             }    
00322         }
00323         break;
00324     case 0x88://TrBIT1:
00325         // buffer 2pixel/1byte => 8 pixel/1byte  buffe 4byte毎進める。
00326         for(int j=0;j<3;j++) TrValue[j] = 0;
00327         for(int i = 0 ; i<4 ; i++)
00328         {
00329             //Green bit => monochrome bit
00330             if( _width/2 > _x + i )
00331             {
00332                 TrValue[0] = TrValue[0] | ( ( (_dispLINEBUF[_x + i]&0x40) == 0 ? 0 : 1 ) << (7-i*2)   );
00333                 TrValue[0] = TrValue[0] | ( ( (_dispLINEBUF[_x + i]&0x04) == 0 ? 0 : 1 ) << (7-i*2)-1 );
00334                 //pc2.printf("[%d+%d][%d]<0x%x>\n",_x,i,_y,_dispBUF[_y* _width/2 + _x + i]);
00335             }
00336         }
00337         break;
00338     }
00339     return TrValue;   
00340 }
00341 
00342 /**
00343 * @brief  clear line buffer data by background color data
00344 */
00345 void memLCD8::clsLINEBUF(void)
00346 {
00347 
00348     for (int j=0; j<_width; j++) {
00349         pixel(j,_background);
00350     }
00351 }
00352 #endif
00353 //////////////////////////////////////////////////////////////////////////////
00354 // Frame buffer mode
00355 //////////////////////////////////////////////////////////////////////////////
00356 #if FRAMEBUFF_MODE
00357 
00358 /**
00359 * @brief  display the text.
00360 * @param[in] int x : horizontal allocation , up-left of text
00361 * @param[in] int y : vertical allocation , bottom-right of text
00362 * @param[in] char* text : strings
00363 */
00364 int memLCD8::textout(int x,int y,char* text)
00365 {
00366     int i=0;
00367     char_x = x;
00368     char_y = y;
00369 
00370     while(text[i] != 0x00 )
00371     {
00372        character(char_x, char_y, text[i]);
00373        i++;
00374     }
00375     return text[i];
00376 }
00377 
00378 /**
00379 * @brief  display the oblique text.
00380 * @param[in] int x : horizontal allocation , up-left of text
00381 * @param[in] int y : vertical allocation , bottom-right of text
00382 * @param[in] char* text : strings
00383 */
00384 int memLCD8::obliqueout(int x,int y,char* text)
00385 {
00386     int i=0;
00387     char_x = x;
00388     char_y = y;
00389 
00390     while(text[i] != 0x00 )
00391     {
00392        oblique(char_x, char_y, text[i]);
00393        i++;
00394     }
00395     return text[i];
00396 }
00397 
00398 /**
00399 * @brief  dispay a character by bog font. big font is that "char data" byte size is over 0xff.
00400 * @param[in] int x : horizontal allocation , up-left of text
00401 * @param[in] int y : vertical allocation , bottom-right of text
00402 * @param[in] char c : a charactor.
00403 * 
00404 */
00405 void memLCD8::character(int x, int y, int c)
00406 {
00407     unsigned int hor,vert,offset0,offset1,bpl,j,i,b; // T.Okamoto modified, for big font
00408     unsigned int headroffset;// 2018-10-26 added by Y.Saruhashi
00409     unsigned char* zeichen;
00410     unsigned char z,w;
00411 
00412     if ((c < 32) || (c > 127)) return;   // test char range
00413 
00414     //for big font
00415     offset0 = font[0];  // bytes / char uppser adress
00416     offset1 = font[1];  // bytes / char lower adress
00417     hor = font[2];      // get hor size of font
00418     vert = font[3];     // get vert size of font
00419     bpl = font[4];      // bytes per line
00420     headroffset = 5;
00421 
00422     if (char_x + hor > _width) {
00423         char_x = 0;
00424         char_y = char_y + vert;
00425         if (char_y >= _height - vert) char_y = 0; // original =  font[2]  T.Okamoto modified, for big font
00426     }
00427 
00428     zeichen = &font[(c -32) * (offset0 *256 + offset1) + headroffset]; // start of char bitmap // original =  +4  T.Okamoto modified, for big font
00429     
00430     if( _FixedFontWidth == 0 ) w = zeichen[0];                          // width of actual char
00431     else                       w = _FixedFontWidth;                     // fixed width of char
00432 
00433     for (j=0; j<vert; j++) {  //  vert line
00434         for (i=0; i<hor; i++) {   //  horz line
00435             z =  zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
00436             b = 1 << (j & 0x07);
00437             if (( z & b ) != 0x00)  pixel(x+i,y+j,_foreground);
00438             else if (_ifMarge == 0) pixel(x+i,y+j,_background);//  _background  -> _LayerBUF[index];
00439 
00440         }
00441     }
00442     if ((w + 2) < hor) char_x += w + 2;                  // x offset to next char
00443     else               char_x += hor;
00444 }
00445 
00446 /**
00447 * @brief  dispay a oblique typte character by big font. 
00448 * @param[in] int x : horizontal allocation , up-left of text
00449 * @param[in] int y : vertical allocation , bottom-right of text
00450 * @param[in] char c : a charactor.
00451 */
00452 void memLCD8::oblique(int x, int y, int c)
00453 {
00454     unsigned int hor,vert,offset0,offset1,bpl,j,i,b; // T.Okamoto modified, for big font
00455     unsigned int headroffset;// 2018-10-26 added by Y.Saruhashi
00456     unsigned char* zeichen;
00457     unsigned char z,w;
00458     if ((c < 32) || (c > 127)) return;   // test char range
00459 
00460     //for big font
00461     offset0 = font[0];  // bytes / char uppser adress
00462     offset1 = font[1];  // bytes / char lower adress
00463     hor = font[2];      // get hor size of font
00464     vert = font[3];     // get vert size of font
00465     bpl = font[4];      // bytes per line
00466     headroffset = 5;
00467 
00468     int oblique_raio=3;    // 3 = 30(%percentage) /  10
00469     int shift_x = (hor*oblique_raio)/10; //oblique pixel x size, top of char;
00470     int shift_y = vert / shift_x;        //oblique ratio for vertical.  
00471 
00472     if (char_x + hor > _width) {
00473         char_x = 0;
00474         char_y = char_y + vert;
00475         if (char_y >= _height - vert) char_y = 0;
00476     }
00477     
00478     zeichen = &font[(c -32) * (offset0 *256 + offset1) + headroffset];
00479     if( _FixedFontWidth == 0 ) w = zeichen[0];                          // actual width of char
00480     else                       w = _FixedFontWidth;                     // fixed width of char
00481 
00482     for (j=0; j<vert; j++) {  //  vert line
00483         for (i=0; i<hor; i++) {   //  horz line
00484             z =  zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
00485             b = 1 << (j & 0x07);
00486             if (( z & b ) != 0x00)  pixel(x+i+shift_x-(j/shift_y),y+j,_foreground);
00487             else if (_ifMarge == 0) pixel(x+i+shift_x-(j/shift_y),y+j,_background);//  _background  -> _LayerBUF[index];
00488 
00489         }
00490     }
00491     if ((w + 2) < hor) char_x += w + 2;                  // x offset to next char
00492     else               char_x += hor;
00493 }
00494 
00495 /**
00496 * @brief  dispay the image from symbol data
00497 */
00498 void memLCD8::Symbol(unsigned int x, unsigned int y, unsigned char *symbol)
00499 {
00500     unsigned int hor,vert,bpl,j,i,b;
00501     unsigned char* zeichen;
00502     unsigned char z,w;
00503     hor = symbol[0];                       // get hor size of font
00504     vert = symbol[1];                      // get vert size of font
00505     bpl = symbol[2];                       // bytes per line
00506 
00507     if (char_x + hor > _width) {
00508         char_x = 0;
00509         char_y = char_y + vert;
00510         if (char_y >= _height - symbol[1]) char_y = 0;
00511     }
00512 
00513     zeichen = &symbol[3];
00514     w = zeichen[0];                          // actual width of char
00515     for (j=0; j<vert; j++) {  //  vert line
00516         for (i=0; i<hor; i++) {   //  horz line
00517             z =  zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
00518             b = 1 << (j & 0x07);
00519             if (( z & b ) != 0x00)  pixel(x+i,y+j,_foreground);
00520             else if (_ifMarge == 0) pixel(x+i,y+j,_background);//  _background  -> _LayerBUF[index];
00521         }
00522     }
00523     if ((w + 2) < hor) char_x += w + 2;                  // x offset to next char
00524     else               char_x += hor;
00525 }
00526 
00527 /**
00528 * @brief  dispay a circle line by color data
00529 */
00530 void memLCD8::circle(int x0, int y0, int r, uint8_t color)
00531 {
00532     int x = -r, y = 0, err = 2-2*r, e2;
00533     do {
00534         pixel(x0-x, y0+y,color);
00535         pixel(x0+x, y0+y,color);
00536         pixel(x0+x, y0-y,color);
00537         pixel(x0-x, y0-y,color);
00538         e2 = err;
00539         if (e2 <= y) {
00540             err += ++y*2+1;
00541             if (-x == y && e2 <= x) e2 = 0;
00542         }
00543         if (e2 > x) err += ++x*2+1;
00544     } while (x <= 0);
00545 
00546 }
00547 
00548 /**
00549 * @brief  dispay a filled circle by color data
00550 */
00551 void memLCD8::fillcircle(int x0, int y0, int r, uint8_t color)
00552 {
00553     int x = -r, y = 0, err = 2-2*r, e2;
00554     do {
00555         vline(x0-x, y0-y, y0+y, color);
00556         vline(x0+x, y0-y, y0+y, color);
00557         e2 = err;
00558         if (e2 <= y) {
00559             err += ++y*2+1;
00560             if (-x == y && e2 <= x) e2 = 0;
00561         }
00562         if (e2 > x) err += ++x*2+1;
00563     } while (x <= 0);
00564 }
00565 
00566 /**
00567 * @brief  dispay a horizontal line by color data
00568 */
00569 void memLCD8::hline(int x0, int x1, int y, uint8_t color)
00570 {
00571     int w;
00572     w = x1 - x0 + 1;
00573     for (int j=0; j<w; j++) pixel(x0+j, y,color);
00574 }
00575 /**
00576 * @brief  dispay a vertical line by color data
00577 */
00578 void memLCD8::vline(int x, int y0, int y1, uint8_t color)
00579 {
00580     int h;
00581     h = y1 - y0 + 1;
00582     for (int j=0; j<h; j++) pixel(x, y0+j,color);
00583 }
00584 
00585 /**
00586 * @brief  dispay a line by color data
00587 */
00588 void memLCD8::line(int x0, int y0, int x1, int y1, uint8_t color)
00589 {
00590     int   dx = 0, dy = 0;
00591     int   dx_sym = 0, dy_sym = 0;
00592     int   dx_x2 = 0, dy_x2 = 0;
00593     int   di = 0;
00594 
00595     dx = x1-x0;
00596     dy = y1-y0;
00597 
00598     if (dx == 0) {        // vertical line
00599         if (y1 > y0) vline(x0,y0,y1,color);
00600         else vline(x0,y1,y0,color);
00601         return;
00602     }
00603 
00604     if (dx > 0) {
00605         dx_sym = 1;
00606     } else {
00607         dx_sym = -1;
00608     }
00609     if (dy == 0) {        // horizontal line
00610         if (x1 > x0) hline(x0,x1,y0,color);
00611         else  hline(x1,x0,y0,color);
00612         return;
00613     }
00614 
00615     if (dy > 0) {
00616         dy_sym = 1;
00617     } else {
00618         dy_sym = -1;
00619     }
00620 
00621     dx = dx_sym*dx;
00622     dy = dy_sym*dy;
00623 
00624     dx_x2 = dx*2;
00625     dy_x2 = dy*2;
00626 
00627     if (dx >= dy) {
00628         di = dy_x2 - dx;
00629         while (x0 != x1) {
00630 
00631             pixel(x0, y0, color);
00632             x0 += dx_sym;
00633             if (di<0) {
00634                 di += dy_x2;
00635             } else {
00636                 di += dy_x2 - dx_x2;
00637                 y0 += dy_sym;
00638             }
00639         }
00640         pixel(x0, y0, color);
00641     } else {
00642         di = dx_x2 - dy;
00643         while (y0 != y1) {
00644             pixel(x0, y0, color);
00645             y0 += dy_sym;
00646             if (di < 0) {
00647                 di += dx_x2;
00648             } else {
00649                 di += dx_x2 - dy_x2;
00650                 x0 += dx_sym;
00651             }
00652         }
00653         pixel(x0, y0, color);
00654     }
00655     return;
00656 }
00657 /**
00658 * @brief  dispay a rectangle line by color data
00659 */
00660 void memLCD8::rect(int x0, int y0, int x1, int y1, uint8_t color)
00661 {
00662 
00663     if (x1 > x0) hline(x0,x1,y0,color);
00664     else  hline(x1,x0,y0,color);
00665 
00666     if (y1 > y0) vline(x0,y0,y1,color);
00667     else vline(x0,y1,y0,color);
00668 
00669     if (x1 > x0) hline(x0,x1,y1,color);
00670     else  hline(x1,x0,y1,color);
00671 
00672     if (y1 > y0) vline(x1,y0,y1,color);
00673     else vline(x1,y1,y0,color);
00674 
00675     return;
00676 }
00677 /**
00678 * @brief  dispay a filled rectangle by color data
00679 */
00680 void memLCD8::fillrect(int x0, int y0, int x1, int y1, uint8_t color)
00681 {
00682     int h = y1 - y0 + 1;
00683     for (int i=0; i<h; i++) hline(x0, x1, y0+i, color);
00684 }
00685 /**
00686 * @brief  Transfer One Pixel Data with x,y allocation
00687 * @param[in] int x : horizontal allocation left to right
00688 * @param[in] int y : vertival allocation top to bottom
00689 * @param[in] uint8_t color : the color data for Drawing  0x0X  x is color data(RGBC) C is not used
00690 */
00691 void memLCD8::pixel(int x, int y, uint8_t color)
00692 {
00693     if(!(x % 2)) _dispBUF[y*_width/2+x/2] =  _dispBUF[y*_width/2+x/2]&0x0F  |  (color << 4)&0xF0 ;   //MASK 0000 1111
00694     else         _dispBUF[y*_width/2+x/2] =  _dispBUF[y*_width/2+x/2]&0xF0  |  (color     )&0x0F ;   //MASK 1111 0000
00695 }
00696 
00697 /**
00698 * @brief  Get Edited data for SPI transfer
00699 * @param[in] int x : horizontal allocation left to right
00700 * @param[in] int y : vertival allocation top to bottom
00701 * @param[in] uint8_t* buff : buffer data for Display
00702 */
00703 int* memLCD8::GetPixelValue(int _x, int _y ,uint8_t* buff)
00704 {
00705     //bitmap data = 4bit data => modify transfer data bit size;
00706     switch(TrModeCommand)
00707     {
00708     case 0x90: //TrBIT4:
00709         // buffer 2pixel/1byte => 2pixel/1byte  buffe 2byte毎進める。
00710         TrValue[0] = _dispBUF[_y* _width/2 + _x];
00711         break;
00712     case 0x80://TrBIT3:
00713         // buffer 2pixel/1byte => 3pixel-1subpixel/1bye (24 pixel/3byte) buffer 3byte毎進める。
00714         for(int j=0;j<3;j++) TrValue[j] = 0;
00715         //for( int i = 0 ; i<12 ; i--)
00716         {
00717             //4 bit RGBN(Nは予備) => 3bit RGB
00718             if( _width/2 > _x )
00719             {
00720                 TrValue[0] = TrValue[0] | ( ( (_dispBUF[_y* _width/2 + _x    ]&0xE0) ) );
00721                 TrValue[0] = TrValue[0] | ( ( (_dispBUF[_y* _width/2 + _x    ]&0x0E) ) << 1);
00722             }
00723             if( _width/2 > _x + 1 )
00724             {
00725                 TrValue[0] = TrValue[0] | ( ( (_dispBUF[_y* _width/2 + _x + 1]&0xC0) ) >> 6);
00726 
00727                 TrValue[1] = TrValue[1] | ( ( (_dispBUF[_y* _width/2 + _x + 1]&0x20) ) << 2);
00728                 TrValue[1] = TrValue[1] | ( ( (_dispBUF[_y* _width/2 + _x + 1]&0x0E) ) << 3);
00729             }
00730             if( _width/2 > _x + 2 )
00731             {
00732                 TrValue[1] = TrValue[1] | ( ( (_dispBUF[_y* _width/2 + _x + 2]&0xE0) ) >> 4);
00733                 TrValue[1] = TrValue[1] | ( ( (_dispBUF[_y* _width/2 + _x + 2]&0x08) ) >> 3);
00734 
00735                 TrValue[2] = TrValue[2] | ( ( (_dispBUF[_y* _width/2 + _x + 2]&0x06) ) << 5);
00736             }
00737             if( _width/2 > _x + 3 )
00738             {
00739                 TrValue[2] = TrValue[2] | ( ( (_dispBUF[_y* _width/2 + _x + 3]&0xE0) ) >> 2);
00740                 TrValue[2] = TrValue[2] | ( ( (_dispBUF[_y* _width/2 + _x + 3]&0x0E) ) >> 1);
00741             }    
00742         }
00743         break;
00744     case 0x88://TrBIT1:
00745         // buffer 2pixel/1byte => 8 pixel/1byte  buffe 4byte毎進める。
00746         for(int j=0;j<3;j++) TrValue[j] = 0;
00747         for(int i = 0 ; i<4 ; i++)
00748         {
00749             //Green bit => monochrome bit
00750             if( _width/2 > _x + i )
00751             {
00752                 TrValue[0] = TrValue[0] | ( ( (_dispBUF[_y* _width/2 + _x + i]&0x40) == 0 ? 0 : 1 ) << (7-i*2)   );
00753                 TrValue[0] = TrValue[0] | ( ( (_dispBUF[_y* _width/2 + _x + i]&0x04) == 0 ? 0 : 1 ) << (7-i*2)-1 );
00754                 //pc2.printf("[%d+%d][%d]<0x%x>\n",_x,i,_y,_dispBUF[_y* _width/2 + _x + i]);
00755             }
00756         }
00757         break;
00758     }
00759     return TrValue;   
00760 }
00761 
00762 /**
00763 * @brief  Transfer Pixel Data from buffer to Display
00764 * @param[in] int transfermode : instruction the transfer data size ,4bit,3bit,1bit
00765 */
00766 void memLCD8::writeDISP(int transfermode)    // refresh whole display
00767 {
00768     char pol = 0;
00769     //char command = 0x90; // 8b 1*0xNNNN *=POL x=AutoW  A  1010
00770     SetTransfermode(transfermode);
00771     
00772     // frame
00773     for (int i=0; i<_height; i++) {
00774         // line
00775         wait_us(6);
00776         _cs  = 1;
00777         wait_us(6);
00778         _spi.write(TrModeCommand | (pol << 6) | (i+1)>>8 ); // COMMAND
00779         //pc2.printf("com 0x%x\n",TrModeCommand | (pol << 6) | (i+1)>>8);
00780         
00781         _spi.write((i+1)& 0x00FF ); // V ADDR
00782         //pc2.printf("v adr 0x%x\n",(i+1)& 0x00FF);
00783         
00784         for(int j=0; j<_width; j+=TrAdd)
00785         {
00786             GetPixelValue(j,i,_dispBUF);
00787             //pc2.printf("data=[%d]{%d][%d]/[%d]\n",j*TrAdd,i,TrValue[0],TrAdd);
00788             for(int k=0;k<TrValNum;k++)
00789                 _spi.write(TrValue[k]);
00790         }
00791         _spi.write(0x00); // DUMMY transfer
00792         _spi.write(0x00); // DUMMY transfer
00793 //        wait_ms(1);      //1.8Hz simulation
00794 //        wait_ms(2);      //1.35Hz simulation
00795         wait_us(6);
00796         _cs  = 0;
00797         if(pol) pol=0x00;
00798         else    pol=0x01;
00799     }
00800 }
00801 
00802 /**
00803 * @brief  Transfer Pixel Data  : from Start line number to Display of frame buffer
00804 * @param[in] int startline : Start line number to Display
00805 * @param[in] int endline : end line number to Display
00806 * @param[in] int transfermode : instruction the transfer data size ,4bit,3bit,1bit
00807 */
00808 void memLCD8::writeDISP(int startline , int endline , int transfermode)    // refresh display selected line
00809 {
00810     char pol = 0;
00811     //char command = 0x90; // 8b 1*0xNNNN *=POL x=AutoW  A  1010
00812     SetTransfermode(transfermode);
00813     
00814     // frame
00815     for (int i=startline; i<=endline; i++) {
00816         // line
00817         wait_us(6);
00818         _cs  = 1;
00819         wait_us(6);
00820         _spi.write(TrModeCommand | (pol << 6) | (i+1)>>8 ); // COMMAND
00821         //pc2.printf("com 0x%x\n",TrModeCommand | (pol << 6) | (i+1)>>8);
00822         
00823         _spi.write((i+1)& 0x00FF ); // V ADDR
00824         //pc2.printf("v adr 0x%x\n",(i+1)& 0x00FF);
00825         
00826         for(int j=0; j<_width; j+=TrAdd)
00827         //for(int j=0; j<100; j+=TrAdd)
00828         {
00829             GetPixelValue(j,i,_dispBUF);
00830             //pc2.printf("data=[%d]{%d][%d]/[%d]\n",j*TrAdd,i,TrValue[0],TrAdd);
00831             for(int k=0;k<TrValNum;k++)
00832                 _spi.write(TrValue[k]);
00833         }
00834         _spi.write(0x00); // DUMMY transfer
00835         _spi.write(0x00); // DUMMY transfer
00836 //        wait_ms(1);      //1.8Hz simulation
00837 //        wait_ms(2);      //1.35Hz simulation
00838         wait_us(6);
00839         _cs  = 0;
00840         if(pol) pol=0x00;
00841         else    pol=0x01;
00842     }
00843 }
00844 
00845 /**
00846 * @brief  Transfer Pixel Data from frame buffer to Display
00847 */
00848 void memLCD8::writeDISP(void)    // refresh whole display
00849 {
00850 
00851     char pol = 0;
00852     char command = 0x90; // 8b 1*0xNNNN *=POL x=AutoW  A  1010
00853 
00854     // frame
00855     for (int i=0; i<_height; i++) {
00856         // line
00857         wait_us(6);
00858         _cs  = 1;
00859         wait_us(6);
00860         _spi.write(command | (pol << 6) | (i+1)>>8 ); // COMMAND
00861         _spi.write((i+1)& 0x00FF ); // V ADDR
00862         for(int j=0; j<_width/2; j++) _spi.write(_dispBUF[i*_width/2 + j]);
00863         _spi.write(0x00); // DUMMY transfer
00864         _spi.write(0x00); // DUMMY transfer
00865         wait_us(6);
00866         _cs  = 0;
00867         if(pol) pol=0x00;
00868         else    pol=0x01;
00869     }
00870 }
00871 
00872 /**
00873 * @brief  clear buffer data by background color data
00874 */
00875 void memLCD8::clsBUF(void)
00876 {
00877 
00878     for (int i=0; i<_height; i++) {
00879         for (int j=0; j<_width; j++) {
00880             pixel(j,i,_background);
00881         }
00882     }
00883 }
00884 #endif
00885