Added custom fonts. Added triangle drawing function

Dependents:   sc100016x4lcd REVO_Updated_Steering Driving_game Arkanoid_v1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers KS0108.h Source File

KS0108.h

00001 #ifndef KS0108_H
00002 #define KS0108_H
00003 
00004 #define VERSION 2.0
00005 
00006 #include <mbed.h>
00007 #include "SystemFont5x7.h"
00008 
00009 /************************************************************************************/
00010 // Commands
00011 #define LCD_ON              0x3F
00012 #define LCD_OFF             0x3E
00013 #define LCD_SET_ADD         0x40
00014 #define LCD_SET_PAGE        0xB8
00015 #define LCD_DISP_START      0xC0
00016 
00017 //Controller directives
00018 #define LEFT                0
00019 #define RIGHT               1
00020 #define BOTH                3
00021 #define NONE                4
00022 
00023 // Colors
00024 #define BLACK               0xFF
00025 #define WHITE               0x00
00026 
00027 //Screen dimensions
00028 #define SCREEN_HEIGHT    64
00029 #define SCREEN_WIDTH    128
00030 #define CHIP_WIDTH         64
00031 
00032 /***********************************************************************************/
00033 //helper functions 
00034 
00035 #define absDiff(x,y) ((x>y) ?  (x-y) : (y-x))
00036 #define swap(a,b) \
00037 do\
00038 {\
00039 uint8_t t;\
00040     t=a;\
00041     a=b;\
00042     b=t;\
00043 } while(0)    
00044 
00045 /**************************************************************************************/
00046 
00047 // Font Indices
00048 #define FONT_LENGTH         0
00049 #define FONT_FIXED_WIDTH    2
00050 #define FONT_HEIGHT         3
00051 #define FONT_FIRST_CHAR     4
00052 #define FONT_CHAR_COUNT     5
00053 #define FONT_WIDTH_TABLE    6
00054 
00055 
00056 /*************************Callback function definietion for fonts *********************/
00057 typedef unsigned int (*FontCallback)(unsigned int*);
00058 
00059 /*************************Callback function    for reading font array*********************/
00060 static unsigned int ReadData(unsigned int* ptr) {  
00061     return *ptr;
00062 }
00063 
00064 /*************************************************************************************/
00065 #define MAX_IMG_SIZE 128*64
00066 
00067 typedef struct {      
00068      unsigned int imgWidth;
00069     unsigned int imgHeight;         
00070     unsigned char imgarray[MAX_IMG_SIZE];
00071 }Image;
00072 
00073 
00074 typedef struct {
00075     unsigned int x;
00076     unsigned int y;
00077     unsigned int page;
00078 } LCDCoord;
00079 
00080 
00081 /****************************************************************************************/
00082 
00083 
00084 
00085 class KS0108  {
00086 public:
00087 
00088    /**
00089     *@brief Constructor, initializes the lcd on the respective pins.
00090     *@param control pins     RST,DI,RW,E,CS2,CS1
00091     *@param databus        DB0-DB7    data pins
00092     *@return none
00093     */
00094 
00095     KS0108 (PinName _RST,PinName _DI, PinName _RW, PinName _E, PinName _CS1, PinName _CS2, PinName DB0, PinName DB1, PinName DB2, PinName DB3, PinName DB4, PinName DB5, PinName DB6, PinName DB7);
00096 
00097    /**
00098     *@brief Write instruction to the specific controller.
00099     *@param Command     command to send to the controller
00100     *@param side         controller side can be LEFT or RIGHT
00101     *@return none
00102     *
00103     */
00104     void  WriteInstruction(unsigned int Command,unsigned int side);
00105 
00106     /**
00107      *@brief Write data byte to the controller.
00108      *@param data     data send to the controller chip
00109      *@param side     selected controller can be LEFT or RIGHT
00110      *@return none
00111      *
00112      */
00113     void  WriteData(unsigned int data ,unsigned char side);
00114 
00115 
00116     /**
00117      *@brief Write data byte to the controller (overloaded function).
00118      *
00119        *@param data     data send to the controller chip
00120      *@return none
00121      */
00122     void  WriteData(unsigned int data);
00123 
00124     /**
00125      *@brief Write data byte to the screen on specific page and column
00126      *@param page     page varies from 0-7 for each side
00127      *@param col     col varies from 0-64 for each side
00128      *@param data     info to be written on given coordinates
00129      *@return none
00130      *
00131      */
00132     void  WriteDataColPag(unsigned int page, unsigned int col,  unsigned int data);
00133 
00134     /**
00135      *@brief Read data from display
00136      *@param none
00137      *@return none
00138      *
00139      */
00140     unsigned int ReadData();
00141 
00142     /**
00143      *@brief Read status of display , and check if it's busy
00144      *@param none
00145      *@return status     status of display
00146      *
00147      */
00148     unsigned int ReadStatus();
00149 
00150     /**
00151      *@brief Select controller chip
00152      *
00153      *@param side     controller side can be LEFT or RIGHT
00154      *@return none
00155      *
00156      */
00157     void SelectSide(unsigned char side);
00158 
00159     
00160        /**
00161      *@brief Set cursor to specified coordinates
00162      *
00163      *@param  x     row
00164      *@param  y     column
00165      *@return none
00166      */
00167     void GotoXY(unsigned int x, unsigned int y); 
00168 
00169 
00170     /**
00171      *@brief Clears display
00172      *
00173      *@param none
00174      *@return none
00175      *
00176      */
00177     void ClearScreen();
00178     
00179     
00180     /**
00181      *@brief Turn on display
00182      *
00183      *@param none
00184      *@return none
00185      *
00186      */
00187     void TurnOn();
00188     
00189     
00190     /**
00191      *@brief Turn Off display
00192      *
00193      *@param none
00194      *@return none
00195      *
00196      */
00197     void TurnOff();
00198 
00199     /*******************************Graphic functions************************************************/
00200 
00201     /**
00202     *@brief Set pixel to specific location on the screen.
00203     *@param x coordinate varies from 0-128
00204     *@param y col varies from 0-64
00205     *@param color color of pixel, can be BLACK or WHITE
00206     *@return none
00207     *
00208     */
00209     void SetPixel( unsigned int x,  unsigned int y,  unsigned int color);
00210 
00211 
00212     /**
00213      *@brief Draws a line from x1,y1 to x2,y1
00214      *@param Xaxis1   x coordinate of one side
00215      *@param Xaxis2   x coordinate of one side
00216      *@param Yaxis   y coordinate both points
00217      *@param color can be BLACK or WHITE
00218      *@return none
00219      *
00220      */
00221      void HLine(unsigned int Xaxis1, unsigned int Xaxis2 ,unsigned int Yaxis,unsigned int color);
00222 
00223     /**
00224      *@brief Draw a horizontal line
00225      *@param Xaxis1
00226      *@param Xaxis2
00227      *@param width
00228      *@param color can be BLACK or WHITE
00229      *@return none
00230      *
00231      */
00232     void HLineShort(unsigned int Xaxis, unsigned int Yaxis,unsigned int width ,unsigned int color);
00233 
00234     /**
00235      *@brief Draws a vertical line
00236      *@param Xaxis
00237      *@param Yaxis1
00238      *@param Yaxis2
00239      *@param color can be BLACK or WHITE
00240      *@return none
00241      *
00242      */
00243     void VLine(unsigned int Xaxis, unsigned int Yaxis1 ,unsigned int Yaxis2,unsigned int color);
00244 
00245     /**
00246      *@brief Draw a vertical line of a given width starting from X, Y 
00247      *@param Xaxis
00248      *@param Yaxis
00249      *@param height    Height of line
00250      *@param color can be BLACK or WHITE
00251      *@return none
00252      *
00253      */
00254     void VLineShort(unsigned int Xaxis,unsigned int Yaxis, unsigned int height ,unsigned int color);
00255 
00256 
00257     /**
00258      *@brief Draws a line from x1,y1 to x2,y2.
00259      *@param x1   x coordinate of one side
00260      *@param y1   y coordinate of one side
00261      *@param x2   x coordinate of other side
00262      *@param y2   y coordinate of other side
00263      *@param color can be BLACK or WHITE
00264      *@return none
00265      *
00266      */
00267     void Line(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2, unsigned int color);
00268 
00269 
00270     /**
00271      *@brief Draws a slanty line from x1,y1  to x2,y2
00272      *@param lX1   x coordinate of one side
00273      *@param lY1   y coordinate of one side
00274      *@param lX2   x coordinate of other side
00275      *@param lY2   y coordinate of other side
00276      *@param color can be BLACK or WHITE
00277      *@return none
00278      *
00279      */
00280     void SlantyLine(unsigned int lX1, unsigned int lY1, unsigned int lX2,unsigned int lY2,unsigned int color);
00281 
00282     /**
00283      *@brief Draws a line from x,y at given degree from inner_radius to outer_radius.
00284      *@param x
00285      *@param y
00286      *@param inner_radius
00287      *@param outer_radius
00288      *@param color can be BLACK or WHITE
00289      *@return none
00290      *
00291      */
00292     void DegreeLine(unsigned int x, int y,unsigned int degree,unsigned int inner_radius,unsigned int outer_radius, unsigned int color);
00293 
00294     /**
00295      *@brief Draw a filled reactangle
00296      *
00297      *@param Xaxis1
00298      *@param Yaxis1
00299      *@param Xaxis2
00300      *@param Yaxis2
00301      *@param color can be BLACK or WHITE
00302      *@return none
00303      *
00304      */
00305     void FullRectangle(unsigned int Xaxis1, unsigned int Yaxis1, unsigned int Xaxis2 ,unsigned int Yaxis2,unsigned int color);
00306 
00307     /**
00308      *@brief Draw an empty rectangle
00309      *@param Xaxis1
00310      *@param Yaxis1
00311      *@param Xaxis2
00312      *@param Yaxis2
00313      *@param color can be BLACK or WHITE
00314      *@return none
00315      *
00316      */
00317     void EmptyRectangle(unsigned int Xaxis1,unsigned int Yaxis1, unsigned int Xaxis2,unsigned int Yaxis2,unsigned int color);
00318 
00319 
00320     /**
00321      *@brief Draw a rectangle with round corners
00322      *@param Xaxis1 x-coordinate of the top left point
00323      *@param Yaxis1 y-coordinate of the top left point
00324      *@param width  rectangle width
00325      *@param height rectangle height
00326      *@param radius radius of the edges
00327      *@param color can be BLACK or WHITE
00328      *@return none
00329      *
00330      */
00331     void RoundRectangle(unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int radius, unsigned int color);
00332 
00333       
00334     /**
00335      *Draws a triangle .
00336      *@param 
00337      *@param 
00338      *@param 
00339      *@param 
00340      *@return none
00341      *
00342      */
00343     
00344     void Triangle ( int topx, int topy, int rightx, int righty);
00345     
00346 
00347     /**
00348      *Draws a right angle triangle .
00349      *@param 
00350      *@param 
00351      *@param 
00352      *@param 
00353      *@return none
00354      *
00355      */
00356     void RightTriangle ( int topx, int topy, int rightx, int righty);
00357 
00358 
00359     /**
00360      *Draws an empty circle centered a x,y with radius R and specific color.
00361      *@param CenterX   center x coordinate 
00362      *@param CenterY   center y coordinate
00363      *@param Radius    circle radius
00364      *@param color     Color can be BLACK or WHITE
00365      *@return none
00366      *
00367      */
00368     void EmptyCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color);
00369 
00370     /**
00371      * Circle fill Code is merely a modification of the midpoint
00372      * circle algorithem which is an adaption of Bresenham's line algorithm
00373      * http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
00374      * http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
00375      * Adapted from arduino lib
00376      *
00377      *@param  CenterX center x coordinate
00378      *@param CenterY  center y coordinate
00379      *@param Radius   circle radius
00380      *@param color    Color can be BLACK or WHITE
00381      */
00382     void FullCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color);
00383 
00384     /**
00385      *Draws an ellipse.
00386      *@param CX   x coordinate of one side
00387      *@param CY   y coordinate of one side
00388      *@param XRadius   x coordinate of other side
00389      *@param YRadius   y coordinate of other side
00390      *@param color can be BLACK or WHITE
00391      *@return none
00392      *
00393      * Ported the algorithm found at http://homepage.smc.edu/kennedy_john/belipse.pdf
00394      *
00395      */
00396     void PlotEllipse(long CX, long  CY, long XRadius,long YRadius, int color);
00397     void Plot4EllipsePoints(long CX,long  CY, long X, long Y, int color);
00398 
00399     
00400     /**
00401      *@brief Round a double
00402      *@param double
00403      *@return value
00404      *
00405      */
00406     double dfloor( double value );
00407 
00408 
00409     /*****************************Bitmaps *****************************************************************/
00410     
00411     /**
00412      *@brief Draws an image on screen.
00413      *@param PictureData  128x64 image array
00414      *@return none
00415      *
00416      *
00417      */
00418     void FullScreenBMP (unsigned char *ImageData);
00419 
00420     /**
00421      *@brief Draw a 1-bit bitmap
00422       *
00423      *@param  image struct containing img size and array
00424      *@param x x-coordinate
00425      *@param y y-coordinate
00426      *@param color can be BLACK or WHITE
00427      *@return none
00428      */
00429     void DrawBitmap(const unsigned int * bitmap, unsigned int x, unsigned int y, unsigned int color);
00430 
00431     /**
00432      *@brief Static function , mplemented to read an array
00433      *@param  ptr     data array
00434      *@return none
00435      */
00436     unsigned int ReadArrayData(const unsigned int* ptr); 
00437 
00438    
00439     /*************************************Font functions **************************************/
00440 
00441    /**
00442     *@brief Print a character on specified coordinates
00443     *
00444     *@param  page     row
00445     *@param  col     column
00446     *@param  c     integer value
00447     *@return none 
00448     */
00449     void Putchar (int page, int col,unsigned char c);
00450 
00451    /**
00452     *@brief Print a string on specified coordinates
00453     *
00454     *@param  str     char array
00455     *@param  x     row
00456     *@param  y     column 
00457     *@return none 
00458     */
00459     void PutString(unsigned int x, unsigned int y,char* str);
00460 
00461    /**
00462     *@brief Print a float on specified coordinates
00463     *
00464     *@param  val     float value
00465     *@param  x     row
00466     *@param  y     column
00467     *@return none 
00468     */
00469     void PrintFloat(float val, unsigned int x,unsigned int y);
00470 
00471    /**
00472     *@brief Print an integer on specified coordinates
00473     *
00474     *@param  val     integer value
00475     *@param  x     row
00476     *@param  y     column
00477     *@return none 
00478     */ 
00479     void PrintInteger(int val,unsigned int x,unsigned int y);
00480 
00481 
00482    /**
00483     *@brief Select a specific font
00484     *
00485     *@param  font          font array
00486     *@param  color         font color , can be BLACK or WHITE
00487     *@param  callback    function pointer to load font
00488     *@return none 
00489     */
00490     void SelectFont(unsigned int* font,unsigned int color, FontCallback callback);
00491 
00492     
00493     /**
00494      *@brief Print a character
00495      *
00496      *@param  c     char
00497      *@return none
00498      */
00499     int PrintChar(char c);
00500 
00501     
00502     /**
00503      *@brief Print a character string 
00504      *
00505      *@param  str     char string
00506      *@return none
00507      */
00508     void PrintString(char* str);
00509     
00510     
00511     /**
00512      *@brief Print a number 
00513      *
00514      *@param  n     number
00515      *@return none
00516      */
00517     void PrintNumber(long n);      
00518                                    
00519     
00520 private:
00521     BusInOut DB;
00522     DigitalOut RST;
00523     DigitalOut DI;
00524     DigitalOut RW;
00525     DigitalOut E;
00526     DigitalInOut CS2;
00527     DigitalInOut CS1;
00528     bool    Inverted;                  
00529     
00530     LCDCoord                Coord;
00531     FontCallback            FontRead ;
00532     unsigned int            FontColor;
00533     unsigned int*            Font;
00534     unsigned int color;
00535 
00536    
00537 };
00538 
00539 
00540 #endif