Guillaume Fricker / KS0108_PCF8574

Dependencies:   BusEnums

Dependents:   Menu

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