Ivo Noorhoff / GDEP015OC1

Fork of GDEP015OC1 by aconno dev team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GDEP015OC1.cpp Source File

GDEP015OC1.cpp

00001 /**
00002  *  Created by Filip Hormot (f.hormot@gmail.com) on 14/09/16.
00003  */
00004 #include "mbed.h"
00005 #include "GDEP015OC1.h"
00006 #include "5x7.h"
00007 
00008 static const unsigned char _lutFull[] = { 
00009     0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22, 0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 
00010     0x88, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51, 0x35, 0x51, 0x51, 0x19, 0x01, 0x00
00011 };
00012 
00013 static const unsigned char _lutPart[] = {
00014     0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00015     0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
00016 };
00017 
00018 GDEP015OC1::GDEP015OC1(SPI& spi, PinName cs=p5, PinName dc=p6, PinName rst=p7, PinName busy=p8) : _spi(spi), _cs(cs), _dc(dc), _rst(rst), _busy(busy){
00019     _bold = true;
00020     _italic = false;
00021     _init();
00022 }
00023 
00024 void GDEP015OC1::_spiCommand(unsigned char command){
00025     _cs = _dc = 0;
00026     wait_us(1);
00027     
00028     _spi.write(command);
00029 }
00030 
00031 void GDEP015OC1::_spiData(unsigned char data){
00032     _cs = 0;
00033     _dc = 1;
00034     wait_us(1);
00035     
00036     _spi.write(data);
00037 }
00038 
00039 void GDEP015OC1::_init(void){
00040     _rst = _cs = 1;
00041     empty();    
00042 }
00043 
00044 void GDEP015OC1::_wakeUp(bool partial){
00045     _rst = 0;
00046     wait_ms(10);
00047     _rst = 1;
00048     wait_ms(10);
00049     
00050     //Stage 3
00051     //Driver Output control
00052     _spiCommand(0x01);
00053     _spiData(0xC7);
00054     _spiData(0x00);
00055     _spiData(0x00);
00056     //Softstart
00057     _spiCommand(0x0C);
00058     _spiData(0xD7);
00059     _spiData(0xD6);
00060     _spiData(0x9D);
00061 
00062     _spiCommand(0x22);
00063     _spiData(0xF0);
00064 
00065     //VCOM setting
00066     _spiCommand(0x2C);
00067     _spiData(0xA8);
00068     //Dummy line period
00069     _spiCommand(0x3A);
00070     _spiData(0x1A);
00071     //Gate line width
00072     _spiCommand(0x3B);
00073     _spiData(0x08);
00074     //Set data entry mode
00075     _spiCommand(0x11);
00076     _spiData(0x01); //DO NOT TOUCH THIS! (MUST BE 0x01)
00077 
00078     //Gate driver start position
00079     _spiCommand(0x0F);
00080     _spiData(0x2B);
00081     _spiData(0x01);
00082 
00083     //VGH VGL
00084     _spiCommand(0x03);
00085     _spiData(0x00);
00086     //VSH, VSL
00087     _spiCommand(0x04);
00088     _spiData(0x0A);
00089 
00090     //Define X display size
00091     _spiCommand(0x44);
00092     _spiData(0x00);
00093     _spiData(0x18);
00094     //Define Y display size
00095     _spiCommand(0x45);
00096     _spiData(0xC7);
00097     _spiData(0x00);
00098     _spiData(0x2B);
00099     _spiData(0x01);
00100 
00101     //Define X RAM address
00102     _spiCommand(0x4E);
00103     _spiData(0x00);
00104     //Define Y RAM address
00105     _spiCommand(0x4F);
00106     _spiData(0xC7);
00107     _spiData(0x00);
00108 
00109     //Border waveform control
00110     _spiCommand(0x3C);
00111     _spiData(0x33);
00112 
00113     //Write LUT
00114     _spiCommand(0x32);
00115     if(partial){
00116         for(uint8_t i = 0; i<30;i++){
00117             _spiData(_lutPart[i]);
00118         }
00119     } else{
00120         for(uint8_t i = 0; i<30;i++){
00121             _spiData(_lutFull[i]);
00122         }
00123     }
00124 
00125     //Power on
00126     _spiCommand(0x22);
00127     _spiData(0xC0);
00128 
00129     //Define X RAM address
00130     _spiCommand(0x4E);
00131     _spiData(0x00);
00132     //Define Y RAM address
00133     _spiCommand(0x4F);
00134     _spiData(0xC7);
00135     _spiData(0x00);
00136 }
00137 
00138 void GDEP015OC1::_sleep(void){
00139     while(_busy == BUSY_STATE);
00140 
00141     _spiCommand(0x10);
00142     _spiData(0x01);
00143 }
00144 
00145 /*  
00146  *  If you touch this function satan will feast on your soul for an eternity!
00147  *  IM NOT PLAYING AROUND DONT FUCKING TOUCH IT!
00148  *  You are thinking about it...
00149  *  .
00150  *  .
00151  *  .
00152  *  DONT!
00153  */
00154 unsigned char GDEP015OC1::_pixelConv(unsigned char *data, int i){
00155     uint8_t pix = 0x00;
00156     for(uint8_t x = 0; x < 8; x++){
00157         pix |= ((*(data + (i*200)%5000 + (24-i/200) + x*25)>>((i/25)%8))&(0x01))<<(7-x);
00158     }
00159     return pix^0xFF;    
00160 }
00161 
00162 uint8_t GDEP015OC1::_mirrorData(uint8_t data){
00163     uint8_t mirror = 0x00;
00164     for(uint8_t i=0; i<8; i++)
00165         mirror |= ((data>>i) & 0x01) << (7 - i);
00166 
00167     return mirror;
00168 }
00169 
00170 void GDEP015OC1::fill(unsigned char data, int x){
00171     _buffer[x] = data;   
00172 }
00173 
00174 void GDEP015OC1::empty(void){
00175     for(uint16_t x=0; x<5000; x++)
00176         _buffer[x] = 0x00;
00177 }
00178 
00179 void GDEP015OC1::write(void){
00180     _wakeUp(true);
00181 
00182     _spiCommand(0x24);
00183     for(int16_t x=0; x>=0 && x<200; x++){
00184         for(int16_t y=24; y>=0 && y<25; y--){
00185             _spiData(_mirrorData(_pixelConv(_buffer, x*25+y)));
00186             wait_us(10);
00187         }
00188     }
00189 
00190     _spiCommand(0x22);
00191     _spiData(0x04);
00192     _spiCommand(0x22);
00193     _spiData(0x08);
00194 
00195     //Update
00196     _spiCommand(0x22);
00197     _spiData(0xC7);
00198     _spiCommand(0x20);
00199 
00200     _sleep();
00201 }
00202 
00203 void GDEP015OC1::writeFull(void){
00204     _wakeUp(false);
00205 
00206     _spiCommand(0x24);
00207     for(int16_t x=0; x>=0 && x<200; x++){
00208         for(int16_t y=24; y>=0 && y<25; y--){
00209             _spiData(_mirrorData(_pixelConv(_buffer, x*25+y)));
00210             wait_us(10);
00211         }
00212     }
00213 
00214     _spiCommand(0x22);
00215     _spiData(0x04);
00216     _spiCommand(0x22);
00217     _spiData(0x08);
00218 
00219     //Update
00220     _spiCommand(0x22);
00221     _spiData(0xC7);
00222     _spiCommand(0x20);
00223 
00224     _sleep();
00225 }
00226 
00227 void GDEP015OC1::drawPixel(uint16_t startX, uint16_t startY, bool color)
00228 {
00229     drawPixel( startX, startY, color?eBlack:eWhite);
00230 }
00231 
00232 void GDEP015OC1::drawLine(uint16_t startX,  uint16_t startY, uint16_t stopX, uint16_t stopY, bool color)
00233 {
00234     drawLine( startX, startY, stopX, stopY, color?eBlack:eWhite);
00235 }
00236  
00237 void GDEP015OC1::drawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, bool color)
00238 {
00239     drawTriangle( x1, y1, x2, y2, x3, y3, color?eBlack:eWhite);
00240 }
00241 
00242 void GDEP015OC1::drawRectangle(uint16_t startX, uint16_t startY, uint16_t stopX, uint16_t stopY, bool color)
00243 {
00244     drawRectangle( startX, startY, stopX, stopY, color?eBlack:eWhite);
00245 }
00246 
00247 void GDEP015OC1::drawCircle(uint16_t startX, uint16_t startY, uint16_t radius, bool color)
00248 {
00249     drawCircle(startX, startY, radius, color?eBlack:eWhite);
00250 }
00251 
00252 void GDEP015OC1::fillCircle(uint16_t startX, uint16_t startY, uint16_t radius, bool color)
00253 {
00254     fillCircle( startX, startY, radius, color?eBlack:eWhite);
00255 }
00256 
00257 void GDEP015OC1::drawEllipse(uint16_t startX, uint16_t startY, uint16_t width, uint16_t height, bool color)
00258 {
00259     drawEllipse(startX, startY, width, height, color?eBlack:eWhite);
00260 }
00261 
00262 void GDEP015OC1::fillEllipse(uint16_t startX, uint16_t startY, uint16_t width, uint16_t height, bool color)
00263 {
00264     fillEllipse(startX, startY, width, height, color?eBlack:eWhite);
00265 }
00266 
00267 void GDEP015OC1::writeChar(char character, uint16_t startX, uint16_t startY, bool color)
00268 {
00269     writeChar(character, startX, startY, color?eBlack:eWhite);
00270 }
00271 
00272 void GDEP015OC1::writeString(char *string, uint16_t startX, uint16_t startY, bool color)
00273 {
00274     writeString(string, startX, startY, color?eBlack:eWhite);
00275 }
00276 
00277 void GDEP015OC1::drawPixel(uint16_t startX, uint16_t startY, Color color=eBlack){
00278     if(startX>199 || startY>199) return;
00279     
00280     uint16_t i = startX/8 + startY*25;
00281 
00282     switch(color) {
00283         case eWhite :
00284            _buffer[i] = (_buffer[i] & (0xFF^(1<<(7-startX%8))));
00285            break;
00286         case eBlack :
00287            _buffer[i] = (_buffer[i] | (1<<(7-startX%8)));
00288            break;   
00289         case eInvert :
00290             _buffer[i] = (_buffer[i] ^ (1<<(7-startX%8)));
00291            break;          
00292     }
00293 }
00294 
00295 
00296 void GDEP015OC1::drawLine(uint16_t startX,  uint16_t startY, uint16_t stopX, uint16_t stopY, Color color=eBlack){
00297     int dx = abs(stopX-startX), sx = startX<stopX ? 1 : -1;
00298     int dy = abs(stopY-startY), sy = startY<stopY ? 1 : -1;
00299     int err = (dx>dy ? dx : -dy)/2, e2;
00300     
00301     for(;;){
00302         drawPixel(startX,startY,color);
00303         if (startX==stopX && startY==stopY) break;
00304         e2 = err;
00305         if (e2 >-dx) { err -= dy; startX += sx; }
00306         if (e2 < dy) { err += dx; startY += sy; }
00307     }
00308 }
00309 
00310 void GDEP015OC1::drawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, Color color=eBlack){
00311     drawLine(x1, y1, x2, y2, color);
00312     drawLine(x2, y2, x3, y3, color);
00313     drawLine(x3, y3, x1, y1, color);      
00314 }
00315 
00316 void GDEP015OC1::drawRectangle(uint16_t startX, uint16_t startY, uint16_t stopX, uint16_t stopY, Color color=eBlack){
00317     drawLine(startX, startY, stopX,  startY, color);
00318     drawLine(stopX,  startY, stopX,  stopY,  color);
00319     drawLine(stopX,  stopY,  startX, stopY,  color);
00320     drawLine(startX, stopY,  startX, startY, color);    
00321 }
00322 
00323 void GDEP015OC1::fillRectangle(uint16_t startX, uint16_t startY, uint16_t stopX, uint16_t stopY, Color color=eBlack){
00324     for(int x = startX; x<stopX; x++)
00325     {
00326         drawLine(x, startY, x,  stopY, color);
00327     }
00328     drawLine(startX, startY, stopX,  startY, color);
00329     drawLine(stopX,  startY, stopX,  stopY,  color);
00330     drawLine(stopX,  stopY,  startX, stopY,  color);
00331     drawLine(startX, stopY,  startX, startY, color);    
00332 }
00333 
00334 
00335 void GDEP015OC1::drawCircle(uint16_t startX, uint16_t startY, uint16_t radius, Color color=eBlack){
00336     int d,x,y;
00337 
00338     d=3-2*radius;
00339     x=0;
00340     y=radius;
00341     while(x<=y){
00342         drawPixel(startX+x,startY+y,color);
00343         drawPixel(startX-y,startY-x,color);
00344         drawPixel(startX+y,startY-x,color);
00345         drawPixel(startX-y,startY+x,color);
00346         drawPixel(startX+y,startY+x,color);
00347         drawPixel(startX-x,startY-y,color);
00348         drawPixel(startX+x,startY-y,color);
00349         drawPixel(startX-x,startY+y,color);
00350 
00351         if(d<=0)
00352             d=d+4*x+6;
00353         else{
00354             d=d+4*x-4*y+10;
00355             y--;
00356         }
00357         x++;
00358     }
00359 }
00360 
00361 void GDEP015OC1::fillCircle(uint16_t startX, uint16_t startY, uint16_t radius, Color color=eBlack){
00362     for(uint16_t r = 1;r<=radius; r++){
00363         drawCircle(startX,   startY, r,   color);
00364         drawCircle(startX+1, startY, r-1, color);
00365         drawCircle(startX-1, startY, r-1, color);
00366     }
00367 }
00368 
00369 void GDEP015OC1::drawEllipse(uint16_t startX, uint16_t startY, uint16_t width, uint16_t height, Color color=eBlack){
00370     int a2 = width*width;
00371     int b2 = height*height;
00372     int fa2 = 4*a2, fb2 = 4*b2;
00373     int x, y, sigma;
00374                                                         
00375   //First half                                                      
00376     for(int x = 0, y = height, sigma = 2*b2+a2*(1-2*height); b2*x <= a2*y; x++){
00377         drawPixel(startX+x,startY+y,color);
00378         drawPixel(startX-x,startY+y,color);
00379         drawPixel(startX+x,startY-y,color);
00380         drawPixel(startX-x,startY-y,color);
00381         if(sigma >= 0){
00382             sigma += fa2 * (1-y);
00383             y--;
00384         }
00385         sigma += b2 * ((4 * x) + 6);
00386     }
00387     //Second half
00388     for (x = width, y = 0, sigma = 2*a2+b2*(1-2*width); a2*y <= b2*x; y++){
00389         drawPixel(startX+x,startY+y,color);
00390         drawPixel(startX-x,startY+y,color);
00391         drawPixel(startX+x,startY-y,color);
00392         drawPixel(startX-x,startY-y,color);
00393         if (sigma >= 0){
00394                 sigma += fb2 * (1 - x);
00395                 x--;
00396         }
00397         sigma += a2 * ((4 * y) + 6);
00398     }       
00399 }
00400 
00401 void GDEP015OC1::fillEllipse(uint16_t startX, uint16_t startY, uint16_t width, uint16_t height, Color color=eBlack){
00402     for(uint16_t w = width; w > 0; w--){
00403         drawEllipse(startX, startX, w, height, color);
00404     }
00405     drawLine(startX, startY-height, startX, startY+height, color);
00406 }
00407 
00408 void GDEP015OC1::writeChar(char character, uint16_t startX, uint16_t startY, Color color=eBlack){
00409     unsigned char letter[FONT_WIDTH];
00410 
00411     //Grab data for the corresponding font
00412     for(uint8_t i = 0; i<FONT_WIDTH; i++)
00413         letter[i] = Font5x7[(character - ' ') * FONT_WIDTH + i];
00414 
00415     for(uint8_t i = 0; i<FONT_WIDTH; i++){
00416         for(uint8_t j = 0; j<FONT_HEIGHT; j++){
00417             if((letter[i]>>j)&0x01){
00418                 if(_italic){
00419                     drawPixel(startX+i+(FONT_HEIGHT/3 - j/3), startY+j, color);
00420                     if(_bold){
00421                         for(uint8_t z=0; z<2; z++)
00422                             drawPixel(startX+i-z+(FONT_HEIGHT/3- j/3), startY+j, color);
00423                     }
00424                 }
00425                 else{
00426                     drawPixel(startX+i, startY+j, color);
00427                     if(_bold){
00428                         for(uint8_t z=0; z<2; z++)
00429                             drawPixel(startX+i-z, startY+j, color);
00430                     }
00431                 }
00432             }
00433         }
00434     }   
00435 }
00436 
00437 void GDEP015OC1::writeString(char *string, uint16_t startX, uint16_t startY, Color color=eBlack){
00438     uint8_t length = 0;
00439     while(*(string+length) != '\0') length++;
00440     
00441     for(uint8_t x=0; x<length; x++)
00442         writeChar(*(string+x), startX+(FONT_WIDTH+1)*x, startY, color); //FONT_WIDTH+1 gives a 1px space between the characters
00443 }