Added custom fonts. Added triangle drawing function

Dependents:   sc100016x4lcd REVO_Updated_Steering Driving_game Arkanoid_v1 ... more

KS0108.h

Committer:
DimiterK
Date:
2011-02-10
Revision:
4:bdc04bb2ffc1
Parent:
2:03d27b3fce6e
Child:
5:e4b50f4c13a8

File content as of revision 4:bdc04bb2ffc1:

/*************************************************************************
Copyright (c) 2010 Dimiter Kentri

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/

#ifndef KS0108_H
#define KS0108_H

#define VERSION 1.11

#include "mbed.h"
#include "SystemFont5x7.h"

/************************************************************************************/
// Commands
#define LCD_ON              0x3F
#define LCD_OFF             0x3E
#define LCD_SET_ADD         0x40
#define LCD_SET_PAGE        0xB8
#define LCD_DISP_START      0xC0

//Controller directives
#define LEFT                1
#define RIGHT               2
#define BOTH                3
#define NONE                4

// Colors
#define BLACK               0xFF
#define WHITE               0x00

//Screen dimensions
#define SCREEN_HEIGHT    64
#define SCREEN_WIDTH    128

/***********************************************************************************/

#define absDiff(x,y) ((x>y) ?  (x-y) : (y-x))
#define swap(a,b) \
do\
{\
uint8_t t;\
    t=a;\
    a=b;\
    b=t;\
} while(0)


/**************************************************************************************/

// Font Indices
#define FONT_LENGTH         0
#define FONT_FIXED_WIDTH    2
#define FONT_HEIGHT         3
#define FONT_FIRST_CHAR     4
#define FONT_CHAR_COUNT     5
#define FONT_WIDTH_TABLE    6

/*************************************************************************************/
#define MAX_IMG_SIZE 128*64

typedef struct {      
     unsigned int imgWidth;
    unsigned int imgHeight;         
    unsigned char imgarray[MAX_IMG_SIZE];
}Image;

/**************************************************************************************/

class KS0108  {
public:

   /**
    *@brief Constructor, initializes the lcd on the respective pins.
    *@param control pins     RST,DI,RW,E,CS2,CS1
    *@param databus        DB0-DB7    data pins
    *@return none
    */

    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);

   /**
    *@brief Write instruction to the specific controller.
    *@param Command     command to send to the controller
    *@param side         controller side can be LEFT or RIGHT
    *@return none
    *
    */
    void  WriteInstruction(unsigned int Command,unsigned int side);

    /**
     *@brief Write data byte to the controller.
     *@param data     data send to the controller chip
     *@param side     selected controller can be LEFT or RIGHT
     *@return none
     *
     */
    void  WriteData(unsigned int data ,unsigned char side);

    /**
     *@brief Write data byte to the screen on specific page and column
     *@param page     page varies from 0-7 for each side
     *@param col     col varies from 0-64 for each side
     *@param data     info to be written on given coordinates
     *@return none
     *
     */
    void  WriteDataColPag(unsigned int page, unsigned int col,  unsigned int data);

    /**
     *@brief Read data from display
     *@param none
     *@return none
     *
     */
    unsigned int ReadData();

    /**
     *@brief Read status of display , and check if it's busy
     *@param none
     *@return status     status of display
     *
     */
    unsigned int ReadStatus();

    /**
     *@brief Select controller chip
     *
     *@param side     controller side can be LEFT or RIGHT
     *@return none
     *
     */
    void SelectSide(unsigned char side);

    /**
     *@brief Clears display
     *
     *@param none
     *@return none
     *
     */
    void ClearScreen();
    
        
    /**
     *@brief Turn on display
     *
     *@param none
     *@return none
     *
     */
    void TurnOn();
    
    
    /**
     *@brief Turn Off display
     *
     *@param none
     *@return none
     *
     */
    void TurnOff();

    /*******************************Graphic functions************************************************/

    /**
    *@brief Set pixel to specific location on the screen.
    *@param x coordinate varies from 0-128
    *@param y col varies from 0-64
    *@param color color of pixel, can be BLACK or WHITE
    *@return none
    *
    */
    void SetPixel( unsigned int x,  unsigned int y,  unsigned int color);


    /**
     *@brief Draws a line from x1,y1 to x2,y1
     *@param Xaxis1   x coordinate of one side
     *@param Xaxis2   x coordinate of one side
     *@param Yaxis   y coordinate both points
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
     void HLine(unsigned int Xaxis1, unsigned int Xaxis2 ,unsigned int Yaxis,unsigned int color);

    /**
     *@brief Draw a horizontal line
     *@param Xaxis1
     *@param Xaxis2
     *@param width
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
    void HLineShort(unsigned int Xaxis, unsigned int Yaxis,unsigned int width ,unsigned int color);

    /**
     *@brief Draws a vertical line
     *@param Xaxis
     *@param Yaxis1
     *@param Yaxis2
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
    void VLine(unsigned int Xaxis, unsigned int Yaxis1 ,unsigned int Yaxis2,unsigned int color);

    /**
     *@brief Draw a vertical line of a given width starting from X, Y 
     *@param Xaxis
     *@param Yaxis
     *@param height    Height of line
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
    void VLineShort(unsigned int Xaxis,unsigned int Yaxis, unsigned int height ,unsigned int color);


    /**
     *@brief Draws a line from x1,y1 to x2,y2.
     *@param x1   x coordinate of one side
     *@param y1   y coordinate of one side
     *@param x2   x coordinate of other side
     *@param y2   y coordinate of other side
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
    void Line(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2, unsigned int color);


    /**
     *@brief Draws a slanty line from x1,y1  to x2,y2
     *@param lX1   x coordinate of one side
     *@param lY1   y coordinate of one side
     *@param lX2   x coordinate of other side
     *@param lY2   y coordinate of other side
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
    void SlantyLine(unsigned int lX1, unsigned int lY1, unsigned int lX2,unsigned int lY2,unsigned int color);

    /**
     *@brief Draws a line from x,y at given degree from inner_radius to outer_radius.
     *@param x
     *@param y
     *@param inner_radius
     *@param outer_radius
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
    void DegreeLine(unsigned int x, int y,unsigned int degree,unsigned int inner_radius,unsigned int outer_radius, unsigned int color);

    /**
     *@brief Draw a filled reactangle
     *
     *@param Xaxis1
     *@param Yaxis1
     *@param Xaxis2
     *@param Yaxis2
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
    void FullRectangle(unsigned int Xaxis1, unsigned int Yaxis1, unsigned int Xaxis2 ,unsigned int Yaxis2,unsigned int color);

    /**
     *@brief Draw an empty rectangle
     *@param Xaxis1
     *@param Yaxis1
     *@param Xaxis2
     *@param Yaxis2
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
    void EmptyRectangle(unsigned int Xaxis1,unsigned int Yaxis1, unsigned int Xaxis2,unsigned int Yaxis2,unsigned int color);


    /**
     *@brief Draw a rectangle with round corners
     *@param Xaxis1 x-coordinate of the top left point
     *@param Yaxis1 y-coordinate of the top left point
     *@param width  rectangle width
     *@param height rectangle height
     *@param radius radius of the edges
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
    void RoundRectangle(unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int radius, unsigned int color);


    /*
     *Draws an empty circle centered a x,y with radius R and specific color.
     *@param CenterX   center x coordinate 
     *@param CenterY   center y coordinate
     *@param Radius    circle radius
     *@param color     Color can be BLACK or WHITE
     *@return none
     *
     */
    void EmptyCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color);

    /*
     * Circle fill Code is merely a modification of the midpoint
     * circle algorithem which is an adaption of Bresenham's line algorithm
     * http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
     * http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
     * Adapted from arduino lib
     *
     *@param  CenterX center x coordinate
     *@param CenterY  center y coordinate
     *@param Radius   circle radius
     *@param color    Color can be BLACK or WHITE
     */
    void FullCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color);

    /*
     *Draws an ellipse.
     *@param CX   x coordinate of one side
     *@param CY   y coordinate of one side
     *@param XRadius   x coordinate of other side
     *@param YRadius   y coordinate of other side
     *@param color can be BLACK or WHITE
     *@return none
     *
     * Ported the algorithm found at http://homepage.smc.edu/kennedy_john/belipse.pdf
     *
     */
    void PlotEllipse(long CX, long  CY, long XRadius,long YRadius, int color);
    void Plot4EllipsePoints(long CX,long  CY, long X, long Y, int color);

    /**
     *@brief Draws an image on screen.
     *@param IamgeData  128x64 image array
     *@return none
     *
     *
     */
    void FullScreenBMP (unsigned char *ImageData);


    /**
     *@brief Draw a 1 bit bmp image at specified coordinates
     *
     *@param  image struct containing img size and array
     *@param x x-coordinate
     *@param y y-coordinate
     *@param color can be BLACK or WHITE
     *@return none
     */
    void CustomImage(Image* image,unsigned int x, unsigned int y, unsigned int color);

    /**
     *@brief Round a double
     *@param double
     *@return value
     *
     */
    double dfloor( double value );


   /**
    *@brief Print a character on specified coordinates
    *
    *@param  page     row
    *@param  col     column
    *@param  c     integer value
    *@return none 
    */
    void Putc (int page, int col,unsigned char c);

   /**
    *@brief Print a string on specified coordinates
    *
    *@param  str     char array
    *@param  x     row
    *@param  y     column 
    *@return none 
    */
    void PutString(unsigned int x, unsigned int y,char* str);

   /**
    *@brief Print a float on specified coordinates
    *
    *@param  val     float value
    *@param  x     row
    *@param  y     column
    *@return none 
    */
    void PrintFloat(float val, unsigned int x,unsigned int y);

   /**
    *@brief Print an integer on specified coordinates
    *
    *@param  val     integer value
    *@param  x     row
    *@param  y     column
    *@return none 
    */ 
    void PrintInteger(int val,unsigned int x,unsigned int y);

    /**
     *@brief Set cursor to specified coordinates
     *
     *@param  x     row
     *@param  y     column
     *@return none
     */
    void CursorXY( unsigned int x, unsigned int y);



private:
    BusInOut DB;
    DigitalOut RST;
    DigitalOut DI;
    DigitalOut RW;
    DigitalOut E;
    DigitalInOut CS2;
    DigitalInOut CS1;

    unsigned int color;
    unsigned int*            Font;  
};


#endif