Guillaume Fricker / KS0108_PCF8574

Dependencies:   BusEnums

Dependents:   Menu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers KS0108.cpp Source File

KS0108.cpp

00001 #include "KS0108.h"
00002 #include "PCF8574_DataBus.h"
00003 
00004 /*KS0108::KS0108 (PinName _RST,PinName _DI, PinName _RW, PinName _E, PinName _CS2, PinName _CS1, PinName DB0, PinName DB1, PinName DB2, PinName DB3, PinName DB4, PinName DB5, PinName DB6, PinName DB7)
00005 : DB(DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7),RST (_RST),DI(_DI), RW(_RW), E(_E), CS2(_CS2), CS1(_CS1) {*/
00006 
00007 
00008 
00009 KS0108::KS0108 (PinName _RST,PinName _DI, PinName _RW, PinName _E, PinName _CS2, PinName _CS1,PCF8574_DataBus &_DB)
00010     :
00011     RST(_RST),
00012     DI(_DI),
00013     RW(_RW),
00014     E(_E),
00015     CS2(_CS2),
00016     CS1(_CS1),
00017     DB(_DB)
00018 {
00019     RST.mode(OpenDrain);
00020     DI.mode(OpenDrain);
00021     RW.mode(OpenDrain);
00022     E.mode(OpenDrain);
00023     CS2.mode(OpenDrain);
00024     CS1.mode(OpenDrain);
00025 
00026     //RST.output();
00027     DI.output();
00028     RW.output();
00029     E.output();
00030     CS2.output();
00031     CS1.output();
00032     DB.output();
00033 
00034 
00035     CS1.output();
00036     CS2.output();
00037     RST.write(0);
00038     wait_us(10);
00039     RST.write(1);                         //reset screen
00040     E.write(0);
00041     ClearScreen();                      //clear display
00042     WriteInstruction(LCD_ON, BOTH);     //turn on lcd
00043     Inverted = 0;
00044 }
00045 
00046 
00047 
00048 void  KS0108::WriteInstruction(unsigned int Command,unsigned int side)
00049 {
00050     E.write(0);
00051     DI.write(0);
00052     RW.write(0);
00053 
00054     SelectSide(side);   //select controller
00055 
00056     wait(0.0000003);     //wait 300ns
00057     E.write(1);
00058     DB.output();
00059     DB.write(Command);
00060     wait(0.0000001);
00061     E.write(0);
00062 }
00063 
00064 
00065 void  KS0108::WriteData(unsigned int data,unsigned char side)
00066 {
00067     E.write(0);
00068     DI.write(1);
00069     RW.write(0);
00070 
00071     SelectSide(side);
00072 
00073     wait(0.0000003); // 300ns
00074     E.write(1);
00075     DB.output();
00076     DB.write(data);
00077     wait(0.0000001);
00078     E.write(0);
00079 }
00080 
00081 void KS0108::WriteData(unsigned int data)
00082 {
00083     unsigned int displayData, yOffset, chip;
00084 
00085     if(Coord.x >= SCREEN_WIDTH)
00086         return;
00087     chip = Coord.x/CHIP_WIDTH;
00088     wait(0.000000450); // 300ns
00089 
00090     if(Coord.x % CHIP_WIDTH == 0 && chip > 0) {
00091         GotoXY(Coord.x, Coord.y);
00092     }
00093 
00094     DI.write(1);                    // D/I = 1
00095     RW.write(0);                      // R/W = 0
00096     DB.output();                    // data port is output
00097 
00098     yOffset = Coord.y%8;
00099 
00100     if(yOffset != 0) {                 // first page
00101 
00102         displayData = ReadData();
00103 
00104         DI.write(1);                            // D/I = 1
00105         RW.write(0);                              // R/W = 0
00106         SelectSide(chip);
00107         DB.output();
00108         // data port is output
00109         displayData |= data << yOffset;
00110         if(Inverted)    displayData = ~displayData;
00111         DB.write(displayData);                     // write data
00112         wait(0.0000003);                         // 300ns
00113         E.write(1);
00114         wait(0.0000001);
00115         E.write(0);
00116 
00117         // second page
00118         GotoXY(Coord.x, Coord.y+8);
00119 
00120         displayData = ReadData();
00121 
00122         DI.write(1);                            // D/I = 1
00123         RW.write(0);                              // R/W = 0
00124         SelectSide(chip);
00125 
00126         DB.output();                // data port is output
00127 
00128         displayData |= data >> (8-yOffset);
00129 
00130         if(Inverted)
00131             displayData = ~displayData;
00132         DB.write(displayData);        // write data
00133 
00134         wait(0.0000003);             // 300ns
00135         E.write(1);
00136         wait(0.0000001);
00137         E.write(0);
00138 
00139         GotoXY(Coord.x+1, Coord.y-8);
00140     } else    {
00141 
00142         // just this code gets executed if the write is on a single page
00143         if(Inverted)
00144             data = ~data;
00145         wait(0.0000003);                 // 300nsEN_DELAY();
00146         DB.write(data);                    // write data
00147         wait(0.0000003);                 // 300ns
00148         E = 1;
00149         wait(0.0000001);
00150         E = 0;
00151         Coord.x++;
00152     }
00153 }
00154 
00155 
00156 void KS0108::WriteDataColPag(unsigned int page, unsigned int col,  unsigned int data)
00157 {
00158 
00159     SelectSide(NONE);
00160     col     = col%SCREEN_WIDTH;
00161     page    = page%8;
00162 
00163     if(col<(SCREEN_WIDTH/2)) {
00164         SelectSide(LEFT);
00165         WriteInstruction(LCD_SET_PAGE|page,LEFT);
00166         WriteInstruction(LCD_SET_ADD|col,LEFT);          //set page and column position
00167         WriteData(data,LEFT);                            //output data to D0-D7
00168     } else {
00169 
00170         SelectSide(RIGHT);
00171         col -= (SCREEN_WIDTH/2);
00172         WriteInstruction(LCD_SET_PAGE|page,RIGHT);
00173         WriteInstruction(LCD_SET_ADD|col,RIGHT);        //set page and column position
00174         WriteData(data,RIGHT);                          //output data to D0-D7
00175     }
00176 
00177     SelectSide(NONE);
00178 }
00179 
00180 
00181 
00182 unsigned int KS0108::ReadData()
00183 {
00184     unsigned int data;
00185     DB.input();
00186 
00187 
00188     DI.write(1);
00189     RW.write(1);
00190 
00191     E.write(1);
00192     wait(0.00000045);
00193 
00194     data = DB.read();
00195     wait(0.0000001);
00196     E.write(0);
00197     DB.output();
00198 
00199     return data;
00200 }
00201 
00202 unsigned int KS0108::ReadStatus()
00203 {
00204     unsigned int status;
00205     DB.input();
00206 
00207     DI.write(0);
00208 
00209     RW.write(1);
00210     E.write(1);
00211     wait(0.00000045);
00212 
00213     status = DB.read();
00214     E.write(0);
00215     wait(0.0000001);
00216     DB.output();
00217 
00218     return status;
00219 }
00220 
00221 
00222 
00223 void KS0108::SelectSide(unsigned char side)
00224 {
00225     if(side==LEFT) {
00226         CS1.write(1);
00227         CS2.write(0);
00228     } else if(side==RIGHT) {
00229         CS1.write(0);
00230         CS2.write(1);
00231     } else if (side==BOTH) {
00232         CS1.write(1);
00233         CS2.write(1);
00234     } else if (side==NONE) {
00235         CS1.write(0);
00236         CS2.write(0);
00237     }
00238 }
00239 
00240 
00241 void KS0108::ClearScreen()
00242 {
00243     for (int col=0; col<128; col++) {
00244         for (int page=0; page<8; page++) {
00245             WriteDataColPag(page,col,WHITE);
00246         }
00247     }
00248 }
00249 
00250 
00251 void KS0108::TurnOn()
00252 {
00253     WriteInstruction(LCD_ON,BOTH);
00254 }
00255 
00256 
00257 void KS0108::TurnOff()
00258 {
00259     WriteInstruction(LCD_OFF,BOTH);
00260 }
00261 
00262 
00263 /*******************************************************************************************/
00264 
00265 
00266 void KS0108::SetPixel(unsigned int x,  unsigned int y,  unsigned int color)
00267 {
00268 
00269     unsigned int position;
00270 
00271     SelectSide(NONE);
00272     WriteInstruction(LCD_SET_ADD,NONE);
00273 
00274     if(x>=64) {
00275         WriteInstruction(LCD_SET_PAGE|(y/8),RIGHT);
00276         WriteInstruction(LCD_SET_ADD|x,RIGHT);
00277         position = ReadData();                            //dummy read
00278         position = ReadData();                            //actual read
00279         WriteInstruction(LCD_SET_ADD|x,RIGHT);
00280         if(color==WHITE)
00281             WriteData(position&(~(1<<(y%8))),RIGHT);         // draw a white pixel
00282         else
00283             WriteData(position|(1<<(y%8)),RIGHT);
00284         wait_us(450);
00285     } else {
00286         WriteInstruction(LCD_SET_PAGE|(y/8),LEFT);
00287         WriteInstruction(LCD_SET_ADD|x,LEFT);
00288         position = ReadData();                            //dummy read
00289         position = ReadData();                            //actual read
00290         WriteInstruction(LCD_SET_ADD|x,LEFT);
00291         if(color==WHITE)
00292             WriteData(position&(~(1<<(y%8))),LEFT);
00293         else
00294             WriteData(position|(1<<(y%8)),LEFT);
00295 
00296         wait_us(450);
00297 
00298     }
00299 
00300 }
00301 
00302 
00303 
00304 void KS0108::FullRectangle(unsigned int Xaxis1, unsigned int Yaxis1, unsigned int Xaxis2 ,unsigned int Yaxis2,unsigned int color)
00305 {
00306 
00307     for(unsigned int i=Xaxis1; i<=Xaxis2; i++) {
00308         for(unsigned int j=Yaxis1; j<=Yaxis2; j++) {
00309             SetPixel(i,j,color);
00310 
00311         }
00312     }
00313 }
00314 
00315 
00316 void KS0108::EmptyRectangle(unsigned int Xaxis1,unsigned int Yaxis1, unsigned int Xaxis2,unsigned int Yaxis2,unsigned int color)
00317 {
00318     unsigned int CurrentValue;
00319 
00320     /* Draw the two horizontal lines */
00321     for (CurrentValue = 0; CurrentValue < Xaxis2 - Xaxis1+ 1; CurrentValue++) {
00322         SetPixel(Xaxis1 + CurrentValue, Yaxis1,color);
00323         SetPixel(Xaxis1 + CurrentValue, Yaxis2,color);
00324     }
00325 
00326     /* draw the two vertical lines */
00327     for (CurrentValue = 0; CurrentValue < Yaxis2 - Yaxis1 + 1; CurrentValue++) {
00328         SetPixel(Xaxis1, Yaxis1 + CurrentValue,color);
00329         SetPixel(Xaxis2, Yaxis1 + CurrentValue,color);
00330     }
00331 }
00332 
00333 
00334 void KS0108::RoundRectangle(unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int radius, unsigned int color)
00335 {
00336     int tSwitch, x1 = 0, y1 = radius;
00337     tSwitch = 3 - 2 * radius;
00338 
00339     while (x1 <= y1) {
00340         SetPixel(x+radius - x1, y+radius - y1, color);
00341         SetPixel(x+radius - y1, y+radius - x1, color);
00342 
00343         SetPixel(x+width-radius + x1, y+radius - y1, color);
00344         SetPixel(x+width-radius + y1, y+radius - x1, color);
00345 
00346         SetPixel(x+width-radius + x1, y+height-radius + y1, color);
00347         SetPixel(x+width-radius + y1, y+height-radius + x1, color);
00348 
00349         SetPixel(x+radius - x1, y+height-radius + y1, color);
00350         SetPixel(x+radius - y1, y+height-radius + x1, color);
00351 
00352         if (tSwitch < 0) {
00353             tSwitch += (4 * x1 + 6);
00354         } else {
00355             tSwitch += (4 * (x1 - y1) + 10);
00356             y1--;
00357         }
00358         x1++;
00359     }
00360 
00361     HLineShort(x+radius,y, width-(2*radius), color);                // top
00362     HLineShort(x+radius,y+height, width-(2*radius),  color);        // bottom
00363     VLineShort(x,y+radius,height-(2*radius), color);                // left
00364     VLineShort(x+width, y+radius,height-(2*radius),  color);        // right
00365 }
00366 
00367 
00368 void KS0108::HLine(unsigned int Xaxis1, unsigned int Xaxis2 ,unsigned int Yaxis,unsigned int color)
00369 {
00370     FullRectangle(Xaxis1,Yaxis,Xaxis2,Yaxis,color);
00371 
00372 }
00373 
00374 
00375 void KS0108::HLineShort(unsigned int Xaxis, unsigned int Yaxis,unsigned int width ,unsigned int color)
00376 {
00377     FullRectangle(Xaxis,Yaxis,Xaxis+width,Yaxis,color);
00378 
00379 }
00380 
00381 
00382 void KS0108::VLine(unsigned int Xaxis, unsigned int Yaxis1 ,unsigned int Yaxis2,unsigned int color)
00383 {
00384     FullRectangle(Xaxis,Yaxis1,Xaxis,Yaxis2,color);
00385 }
00386 
00387 
00388 void KS0108::VLineShort(unsigned int Xaxis,unsigned int Yaxis, unsigned int height ,unsigned int color)
00389 {
00390     FullRectangle(Xaxis,Yaxis,Xaxis,Yaxis+height,color);
00391 
00392 }
00393 
00394 
00395 void KS0108::DegreeLine(unsigned int x, int y,unsigned int degree,unsigned int inner_radius,unsigned int outer_radius, unsigned int color)
00396 {
00397     int fx,fy,tx,ty;
00398     fx = x + dfloor(inner_radius * sin(degree * 3.14 / 180));
00399     fy = y - dfloor(inner_radius * cos(degree * 3.14 / 180));
00400     tx = x + dfloor(outer_radius * sin(degree * 3.14 / 180));
00401     ty = y - dfloor(outer_radius * cos(degree * 3.14 / 180));
00402     SlantyLine(fx,fy,tx,ty,color);
00403 }
00404 
00405 
00406 double KS0108::dfloor( double value )
00407 {
00408 
00409     if (value < 0.0)
00410         return ceil( value );
00411     else
00412         return floor( value );
00413 
00414 }
00415 
00416 
00417 void KS0108::SlantyLine(unsigned int lX1, unsigned int lY1, unsigned int lX2,unsigned int lY2,unsigned int color)
00418 {
00419     long lError, lDeltaX, lDeltaY, lYStep, bSteep;
00420 
00421     // A steep line has a bigger ordinate.
00422 
00423     if(((lY2 > lY1) ? (lY2 - lY1) : (lY1 - lY2)) > ((lX2 > lX1) ? (lX2 - lX1) : (lX1 - lX2))) {
00424         bSteep = 1;
00425     } else {
00426         bSteep = 0;
00427     }
00428 
00429     // If line is steep, swap the X and Y coordinates.
00430     if(bSteep) {
00431         lError = lX1;
00432         lX1 = lY1;
00433         lY1 = lError;
00434         lError = lX2;
00435         lX2 = lY2;
00436         lY2 = lError;
00437     }
00438 
00439 
00440     // If the starting X coordinate is larger than the ending X coordinate,
00441     // swap coordinates.
00442     if(lX1 > lX2) {
00443         lError = lX1;
00444         lX1 = lX2;
00445         lX2 = lError;
00446         lError = lY1;
00447         lY1 = lY2;
00448         lY2 = lError;
00449     }
00450 
00451     // Compute the difference between the start and end coordinates.
00452     lDeltaX = lX2 - lX1;
00453     lDeltaY = (lY2 > lY1) ? (lY2 - lY1) : (lY1 - lY2);
00454 
00455     lError = -lDeltaX / 2;          // Initialize the error term to negative half the X delta.
00456 
00457     if(lY1 < lY2) {                  // Determine the direction to step in the Y axis when required.
00458         lYStep = 1;
00459     } else {
00460         lYStep = -1;
00461     }
00462 
00463     for(; lX1 <= lX2; lX1++) {   // Loop through all the points along the X axis of the line.
00464 
00465         // See if this is a steep line.
00466 
00467         if(bSteep) {
00468 
00469             // Plot this point of the line, swapping the X and Y coordinates.
00470 
00471             SetPixel(lY1, lX1,color);
00472         } else {         // Plot this point of the line, using the coordinates as is.
00473             SetPixel(lX1, lY1,color);
00474         }
00475 
00476         // Increment the error term by the Y delta.
00477 
00478         lError += lDeltaY;
00479 
00480         if(lError > 0) {               // See if the error term is now greater than zero.
00481 
00482             lY1 += lYStep;            // Take a step in the Y axis.
00483 
00484             lError -= lDeltaX;         // Decrement the error term by the X delta.
00485         }
00486     }
00487 }
00488 
00489 
00490 
00491 void KS0108::Line(unsigned int x1, unsigned int  y1, unsigned int  x2, unsigned int  y2, unsigned int color)
00492 {
00493     unsigned int  deltax, deltay, x,y, steep;
00494     int lerror, ystep;
00495 
00496     steep = absDiff(y1,y2) > absDiff(x1,x2);   //check slope
00497 
00498     if ( steep ) {
00499         swap(x1, y1);
00500         swap(x2, y2);
00501     }
00502 
00503     if (x1 > x2) {
00504         swap(x1, x2);
00505         swap(y1, y2);
00506     }
00507 
00508     deltax = x2 - x1;
00509     deltay = absDiff(y2,y1);
00510     lerror = deltax / 2;
00511     y = y1;
00512     if(y1 < y2) ystep = 1;
00513     else ystep = -1;
00514 
00515     for(x = x1; x <= x2; x++) {
00516         if (steep) SetPixel(y,x, color);
00517         else SetPixel(x,y, color);
00518         lerror -= deltay;
00519         if (lerror < 0) {
00520             y = y + ystep;
00521             lerror += deltax;
00522         }
00523     }
00524 }
00525 
00526 
00527 void KS0108::EmptyCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color)
00528 {
00529     unsigned int y=0, x=0, d = 0;
00530     int part;
00531     d = CenterY - CenterX;
00532     y = Radius;
00533     part = 3 - 2 * Radius;
00534 
00535     while (x <= y) {
00536         SetPixel(CenterX + x, CenterY + y,color);
00537         SetPixel(CenterX + x, CenterY - y,color);
00538         SetPixel(CenterX - x, CenterY + y,color);
00539         SetPixel(CenterX - x, CenterY - y,color);
00540         SetPixel(CenterY + y - d, CenterY + x,color);
00541         SetPixel(CenterY + y - d, CenterY - x,color);
00542         SetPixel(CenterY - y - d, CenterY + x,color);
00543         SetPixel(CenterY - y - d, CenterY - x,color);
00544 
00545         if (part < 0) part += (4 * x + 6);
00546         else {
00547             part += (4 * (x - y) + 10);
00548             y--;
00549         }
00550         x++;
00551     }
00552 
00553 }
00554 
00555 
00556 void KS0108::FullCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color)
00557 {
00558 
00559     int f = 1 - Radius;
00560     int ddF_x = 1;
00561     int ddF_y = 2 * Radius;             //changed sign of -2
00562     unsigned int x = 0;
00563     unsigned int y = Radius;
00564 
00565     //Fill in the center between the two halves
00566 
00567     Line(CenterX, CenterY-Radius, CenterX, CenterY+Radius, color);
00568 
00569     while(x < y) {
00570         if(f >= 0) {
00571             y--;
00572             ddF_y += 2;
00573             f += ddF_y;
00574         }
00575         x++;
00576         ddF_x += 2;
00577         f += ddF_x;
00578 
00579         /*
00580          * Now draw vertical lines between the points on the circle rather than
00581          * draw the points of the circle. This draws lines between the
00582          * perimeter points on the upper and lower quadrants of the 2 halves of the circle.
00583          */
00584 
00585         Line(CenterX+x, CenterY+y, CenterX+x, CenterY-y, color);
00586         Line(CenterX-x, CenterY+y, CenterX-x, CenterY-y, color);
00587         Line(CenterX+y, CenterY+x, y+CenterX, CenterY-x, color);
00588         Line(CenterX-y, CenterY+x, CenterX-y, CenterY-x, color);
00589     }
00590 }
00591 
00592 
00593 
00594 void KS0108::PlotEllipse(long CX, long  CY, long XRadius,long YRadius, int color)
00595 {
00596 
00597 
00598     long X, Y;
00599     long XChange, YChange;
00600     long EllipseError;
00601     long TwoASquare,TwoBSquare;
00602     long StoppingX, StoppingY;
00603     TwoASquare = 2*XRadius*XRadius;
00604     TwoBSquare = 2*YRadius*YRadius;
00605     X = XRadius;
00606     Y = 0;
00607     XChange = YRadius*YRadius*(1-2*XRadius);
00608     YChange = XRadius*XRadius;
00609     EllipseError = 0;
00610     StoppingX = TwoBSquare*XRadius;
00611     StoppingY = 0;
00612 
00613     while ( StoppingX >=StoppingY ) {               //first set of points,y'>-1
00614         Plot4EllipsePoints(CX,CY,X,Y,color);
00615         Y++;
00616         StoppingY=StoppingY+ TwoASquare;
00617         EllipseError = EllipseError+ YChange;
00618         YChange=YChange+TwoASquare;
00619         if ((2*EllipseError + XChange) > 0 ) {
00620             X--;
00621             StoppingX=StoppingX- TwoBSquare;
00622             EllipseError=EllipseError+ XChange;
00623             XChange=XChange+TwoBSquare;
00624         }
00625     }
00626 
00627     Y = YRadius;
00628     X = 0;
00629     YChange = XRadius*XRadius*(1-2*YRadius);
00630     XChange = YRadius*YRadius;
00631     EllipseError = 0;
00632     StoppingY = TwoASquare*YRadius;
00633     StoppingX = 0;
00634 
00635     while ( StoppingY >=StoppingX ) {               //{2nd set of points, y'< -1}
00636         Plot4EllipsePoints(CX,CY,X,Y,color);
00637         X++;
00638         StoppingX=StoppingX + TwoBSquare;
00639         EllipseError=EllipseError+ XChange;
00640         XChange=XChange+TwoBSquare;
00641         if ((2*EllipseError + YChange) > 0 ) {
00642             Y--;
00643             StoppingY=StoppingY- TwoASquare;
00644             EllipseError=EllipseError+ YChange;
00645             YChange=YChange+TwoASquare;
00646         }
00647     }
00648 }
00649 
00650 
00651 
00652 void KS0108::Plot4EllipsePoints(long CX,long  CY, long X, long Y, int color)
00653 {
00654     SetPixel(CX+X, CY+Y, color); //{point in quadrant 1}
00655     SetPixel(CX-X, CY+Y, color); //{point in quadrant 2}
00656     SetPixel(CX-X, CY-Y, color); //{point in quadrant 3}
00657     SetPixel(CX+X, CY-Y, color); //{point in quadrant 4}
00658 }
00659 
00660 
00661 void KS0108::RightTriangle ( int topx, int topy, int rightx, int righty)
00662 {
00663 
00664     //draw rectangle one line at a time
00665     Line( topx,topy, rightx,righty,BLACK );        //draw hypotenuse
00666     Line ( topx,righty,topx,topy,BLACK);         //draw perpendicular
00667     Line (topx,righty, rightx,righty,BLACK);      // draw base
00668 
00669 }
00670 
00671 void KS0108::Triangle ( int topx, int topy, int rightx, int righty)
00672 {
00673     int base =0;
00674     base = 2* rightx-topx;
00675     //draw rectangle one line at a time
00676     Line( topx,topy, rightx,righty,BLACK );                //draw hypotenuse
00677     Line ( topx,righty,topx,topy,BLACK);                     //draw perpendicular
00678     Line(topx-base/2,righty, rightx,righty,BLACK);         // draw base
00679     Line(topx-base/2, righty, topx,topy,BLACK);            // draw hypotenuse
00680 
00681 }
00682 
00683 
00684 
00685 
00686 /***********************************************************************************/
00687 
00688 
00689 void KS0108::FullScreenBMP (unsigned char *PictureData)
00690 {
00691     unsigned int Page=0;
00692     unsigned int Column=0;
00693 
00694     // Draw left side of the picture
00695     SelectSide(LEFT);
00696     for (Page = 0; Page < 8; Page++) {                    /* loop on the 8 pages */
00697         WriteInstruction(LCD_SET_PAGE | Page,LEFT); /* Set the page */
00698         for (Column = 0; Column < 64; Column++)
00699             WriteData(PictureData[(128*Page)+Column],LEFT);
00700     }
00701 
00702     // Draw right side of the picture
00703     SelectSide(RIGHT);
00704     for (Page = 0; Page < 8; Page++) {                    /* loop on the 8 pages */
00705 
00706         WriteInstruction(LCD_SET_PAGE| Page,RIGHT); /* Set the page */
00707         for (Column = 64; Column < 128; Column++)
00708             WriteData(PictureData[(128*Page)+Column],RIGHT);
00709     }
00710 }
00711 
00712 unsigned int KS0108::ReadArrayData(const unsigned int* ptr)
00713 {
00714     return (*ptr);
00715 }
00716 
00717 void KS0108::DrawBitmap(const unsigned int * bitmap, unsigned int x, unsigned int y, unsigned int color)
00718 {
00719     unsigned int width, height;
00720     unsigned int i, j;
00721 
00722     width = ReadArrayData(bitmap++);
00723     height = ReadArrayData(bitmap++);
00724     for(j = 0; j < height / 8; j++) {
00725         GotoXY(x, y + (j*8) );
00726         for(i = 0; i < width; i++) {
00727             unsigned int displayData = ReadArrayData(bitmap++);
00728             if(color == BLACK)
00729                 WriteData(displayData);
00730             else
00731                 WriteData(~displayData);
00732         }
00733     }
00734 }
00735 /******************************************************************************************/
00736 
00737 
00738 void KS0108::GotoXY(unsigned int x, unsigned int y)
00739 {
00740     unsigned int chip, cmd;
00741 
00742     if( (x > SCREEN_WIDTH-1) || (y > SCREEN_HEIGHT-1) )    // exit if coordinates are not legal
00743         return;
00744     Coord.x = x;                                    // save new coordinates
00745     Coord.y = y;
00746 
00747     if(y/8 != Coord.page) {
00748         Coord.page = y/8;
00749         cmd = LCD_SET_PAGE | Coord.page;            // set y address on all chips
00750         for(chip=0; chip < 2; chip++) {
00751             WriteInstruction(cmd, chip);
00752         }
00753     }
00754     chip = Coord.x/64; //select chip
00755     x = x % 64; // valeur de X quelque soit le CS affich&#65533;
00756     cmd = LCD_SET_ADD | x;
00757     WriteInstruction(cmd, chip);                    // set x address on active chip
00758 
00759 }
00760 
00761 
00762 /*****************************************************************************************/
00763 
00764 
00765 
00766 void KS0108::Putchar (int page, int col,unsigned char c)
00767 {
00768     if (c>31 && c<127) {
00769         for(int i=0; i<5; i++) {
00770             WriteDataColPag(page,col+i,System5x7[((c-32)*5+i)+6]);
00771         }
00772     }
00773 }
00774 
00775 
00776 
00777 void KS0108::PutString(unsigned int x, unsigned int y,char* str)
00778 {
00779 
00780     while(*str != 0) {
00781         Putchar(x,y,*str);
00782         str++;
00783         y+=System5x7[2];
00784     }
00785 
00786 }
00787 
00788 void KS0108::PrintFloat(float val, unsigned int x,unsigned int y)
00789 {
00790     char buf[20] = {};  // prints up to 20 digits
00791     sprintf(buf,"%f",val);
00792     PutString(x,y,buf);
00793 
00794 }
00795 
00796 
00797 void KS0108::PrintInteger(int val,unsigned int x,unsigned int y)
00798 {
00799     char buf[20] = {};  // prints up to 20 digits
00800     sprintf(buf,"%d",val);
00801     PutString(x,y,buf);
00802 }
00803 
00804 void KS0108::SelectFont(unsigned int* font,unsigned int color, FontCallback callback)
00805 {
00806     Font = font;
00807     FontRead = callback;
00808     FontColor = color;
00809 }
00810 
00811 
00812 int KS0108::PrintChar(char c)
00813 {
00814     unsigned int width = 0;
00815     unsigned int height = FontRead(Font+FONT_HEIGHT);
00816     unsigned int bytes = (height+7)/8;
00817 
00818     unsigned int firstChar = FontRead(Font+FONT_FIRST_CHAR);
00819     unsigned int charCount = FontRead(Font+FONT_CHAR_COUNT);
00820 
00821     unsigned int index = 0;
00822     unsigned int x=Coord.x , y=Coord.y;
00823 
00824     if(c < firstChar || c >= (firstChar+charCount)) {
00825         return 1;
00826     }
00827     c-= firstChar;
00828 
00829     if( FontRead(Font+FONT_LENGTH) == 0 && FontRead(Font+FONT_LENGTH+1) == 0) {
00830         // zero length is flag indicating fixed width font (array does not contain width data entries)
00831         width = FontRead(Font+FONT_FIXED_WIDTH);
00832         index = c*bytes*width+FONT_WIDTH_TABLE;
00833     } else {
00834         // variable width font, read width data, to get the index
00835         for(unsigned int i=0; i<c; i++) {
00836             index += FontRead(Font+FONT_WIDTH_TABLE+i);
00837         }
00838         index = index*bytes+charCount+FONT_WIDTH_TABLE;
00839         width = FontRead(Font+FONT_WIDTH_TABLE+c);
00840     }
00841 
00842     // last but not least, draw the character
00843     for(unsigned int i=0; i<bytes; i++) {
00844         unsigned int page = i*width;
00845         for(unsigned int j=0; j<width; j++) {
00846             unsigned int data = FontRead(Font+index+page+j);
00847 
00848             if(height > 8 && height < (i+1)*8) {
00849                 data >>= (i+1)*8-height;
00850             }
00851 
00852             WriteData(data);
00853 
00854         }
00855         // 1px gap between chars
00856         WriteData(0x00);
00857         GotoXY(x,Coord.y+8);
00858 
00859     }
00860     GotoXY(x+width+1, y);
00861 
00862 
00863     return 0;
00864 }
00865 
00866 void KS0108::PrintString(char* str)
00867 {
00868     int x = Coord.x;
00869     while(*str != 0) {
00870         if(*str == '\n') {
00871             GotoXY(x, Coord.y+ FontRead(Font+FONT_HEIGHT));
00872         } else {
00873             PrintChar(*str);
00874         }
00875         str++;
00876     }
00877 }
00878 
00879 void KS0108::PrintNumber(long n)
00880 {
00881     char buf[10];  // prints up to 10 digits
00882     char i=0;
00883     if(n==0)
00884         PrintChar('0');
00885     else {
00886         if(n < 0) {
00887             PrintChar('-');
00888             n = -n;
00889         }
00890         while(n>0 && i <= 10) {
00891             buf[i++] = n % 10;  // n % base
00892             n /= 10;   // n/= base
00893         }
00894         for(; i >0; i--)
00895             PrintChar((char) (buf[i-1] < 10 ? '0' + buf[i-1] : 'A' + buf[i-1] - 10));
00896     }
00897 }
00898 
00899 bool KS0108::writeLine (const char *line, uint8_t row)
00900 {
00901 //"SystemFont5x7.h"
00902 // |y   _x
00903     int x = Coord.x;
00904 
00905     Font = System5x7;
00906 
00907     FontColor = BLACK;
00908 
00909     // LCD.SelectFont(System5x7,BLACK,ReadData);
00910     GotoXY(x+10, Coord.y+7);
00911     PrintString(const_cast<char *> (line) );
00912     return 1;
00913 }
00914 void KS0108::showUpArrow (bool show)
00915 {
00916 
00917 }
00918 void KS0108::showDownArrow (bool show)
00919 {
00920 
00921 }
00922 uint8_t KS0108::getLines (void)
00923 {
00924 return 0;
00925 }
00926 
00927 uint8_t KS0108::getLineLength (void) 
00928 {
00929 return 0;
00930 }
00931 
00932 
00933