Added custom fonts. Added triangle drawing function

Dependents:   sc100016x4lcd REVO_Updated_Steering Driving_game Arkanoid_v1 ... more

KS0108.h

Committer:
DimiterK
Date:
2011-01-05
Revision:
1:a368f2688222
Parent:
0:135b9a0a816e
Child:
2:03d27b3fce6e

File content as of revision 1:a368f2688222:

/*************************************************************************
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

#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


typedef struct {
    unsigned int x;
    unsigned int y;
    unsigned int page;
} LCDCoord;


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


class KS0108  {
public:
    /*Constructor, initializes the lcd on the respective pins.
     *
     *@param pins
     *@param databus
     */

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

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

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

    /*Write data to the screen on specific page and column
    *
    *@param page page varies from0-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);


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

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

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

    /*Clears display
     *
     *@param none
     *@return none
     *
     */
    void ClearScreen();

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

    /*Set pixel to specific location on the screen.
     *@param x page 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);


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

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

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

    /*Draw a vertical line
     *@param Xaxis1
     *@param Xaxis2
     *@param Yaxis
     *@param color can be BLACK or WHITE
     *@return none
     *
     */
    void VLineShort(unsigned int Xaxis,unsigned int Yaxis, unsigned int height ,unsigned int color);


    /*
     *Draws a 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 Line(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2, unsigned int color);


    /*
     *Draws a slanty line  .
     *@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);

    /*
     *Draws a line from x,y at given degree from inner_radius to outer_radius.
     *@param Xaxis1
     *@param Xaxis2
     *@param Yaxis
     *@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);

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

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


    /*Draw a rectangle with round corners
     *@param Xaxis1
     *@param Yaxis1
     *@param Xaxis2
     *@param Yaxis2
     *@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 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 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
     *@param CenterY
     *@param Radius
     *@param color
     */
    void FullCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color);

    /*
     *Draws an ellipse.
     *@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
     *
     * 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);

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

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

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


    /*
     *Print a char on the specified coordinates
     *@param page   row coordinate
     *@param col   y coordinate
     *@param c   character from systemfont
     *@return none
     *
     *
     */
    void Putc (int page, int col,unsigned char c);

    /*
     *Print a string on the specified coordinates.
     *@param x   row coordinate
     *@param y   y coordinate
     *@param str   character string
     *@return none
     *
     *
     */
    void PutString(unsigned int x, unsigned int y,char* str);

    /*
     *Print a float on the specified coordiantes.
     *@param val float number
     *@param x  row coordinate
     *@param y  y coordinate
     *@return none
     *
     *
     */
    void PrintFloat(float val, unsigned int x,unsigned int y);

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

    /*
     *Moves cursor to x,y.
     *@param x   x coordinate
     *@param y   y coordinate
     *@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;
    LCDCoord                Coord;
};


#endif