基于NIOS II的液晶显示设计——自定义图形库

下面是我写的简单图形库

//////////////////////////////////////////////////////////////////////////////////////////////////

graphics.h

/////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef GRAPHICS_H_
#define GRAPHICS_H_

#include"IO.h"
#include"system.h"
#include"alt_types.h"
//定义SRAM缓存的基地址
    #define SRAM_BASE       LCD_BASE + 320*2
    #define lcd_frame       320*240
//数据类型定义
    #define WORD            alt_u16
    #define SHORT           alt_u16
    #define pi              3.1415926
//色彩编码
    #define BLACK           (WORD) 0x0000
    #define BRIGHTBLUE      (WORD) 0x001f
    #define BRIGHTGREEN     (WORD) 0x07e0
    #define BRIGHTCYAN      (WORD) 0x07ff
    #define BRIGHTRED       (WORD) 0xf800
    #define BRIGHTMAGENTA   (WORD) 0xf81f
    #define BRIGHTYELLOW    (WORD) 0xffe0
    #define BLUE            (WORD) 0x0010
    #define GREEN           (WORD) 0x0400
    #define CYAN            (WORD) 0x0410
    #define RED             (WORD) 0x8000
    #define MAGENTA         (WORD) 0x8010
    #define BROWN           (WORD) 0xfc00
    #define LIGHTGRAY       (WORD) 0x8410
    #define DARKGRAY        (WORD) 0x4208
    #define LIGHTBLUE       (WORD) 0x841f
    #define LIGHTGREEN      (WORD) 0x87f0
    #define LIGHTCYAN       (WORD) 0x87ff
    #define LIGHTRED        (WORD) 0xfc10
    #define LIGHTMAGENTA    (WORD) 0xfc1f
    #define YELLOW          (WORD) 0xfff0
    #define WHITE           (WORD) 0xffff

    #define GRAY0           (WORD) 0xe71c
    #define GRAY1           (WORD) 0xc618
    #define GRAY2           (WORD) 0xa514
    #define GRAY3           (WORD) 0x8410
    #define GRAY4           (WORD) 0x630c
    #define GRAY5           (WORD) 0x4208
    #define GRAY6           (WORD) 0x2104
  
 ////////////////////////////////////////////////////////////////////
 //TFT LCD 初始化
void lcd_init(void);
/////////////////////////////////////////////////////////////////////
//set TFT LCD display color
void set_color(alt_u16 color);
///////////////////////////////////////////////////////////////////
//display a box
void box(alt_u16 x1, alt_u16 y1, alt_u16 x2, alt_u16 y2);
//////////////////////////////////////////////////////////////////////
//display frame
void frame(alt_u16 x1, alt_u16 y1, alt_u16 x2, alt_u16 y2, alt_u16 width);
///////////////////////////////////////////////////////////////////////
//move to pixel
void move_to(alt_u16 x, alt_u16 y);
////////////////////////////////////////////////////////////////////////
//display one pixel
void pixel(alt_u16 x, alt_u16 y);
////////////////////////////////////////////////////////////////////////
//display a line
void line(alt_u16 x1, alt_u16 y1, alt_u16 x2, alt_u16 y2, alt_u16 width);
///////////////////////////////////////////////////////////////////////////
//display a line ,start dot is x_pos,y_pos
void line_to(alt_u16 x, alt_u16 y, alt_u16 width);
////////////////////////////////////////////////////////////////////////
//display circle
void circle(alt_u16 x0, alt_u16 y0, alt_u16 radius,alt_u8 width);


#endif /*GRAPHICS_H_*/

/////////////////////////////////////////////////////////////////////////////////////////////////

graphics.c

/////////////////////////////////////////////////////////////////////////////////////////////////

#include"IO.h"
#include"math.h"
#include"graphics.h"

//define TFT LCD color
alt_u16         pixel_color;
alt_u16         x_pos,y_pos;
alt_u32         address;
////////////////////////////////////////////////////////////////////
//TFT LCD 初始化
void lcd_init(void)
{
   int i;
   for(i=0;i<lcd_frame/2;i++)  IOWR_32DIRECT(SRAM_BASE,4*i,BLACK);
   x_pos = 0;
   y_pos = 0;
}
/////////////////////////////////////////////////////////////////////
//set TFT LCD display color
void set_color(alt_u16 color)
{
    pixel_color = color;

///////////////////////////////////////////////////////////////////
//display a box
void box(alt_u16 x1, alt_u16 y1, alt_u16 x2, alt_u16 y2)
{
    int offset, row, col;
    for (row = y1; row <= y2; row++)
    {
        col = x1;
        while (col <= x2)
        {
            offset = row*320 + col;
            IOWR_16DIRECT(SRAM_BASE,2*offset,pixel_color);
            ++col;
        }
    }
   x_pos = x2;
   y_pos = y2;
}
//////////////////////////////////////////////////////////////////////
//display frame
void frame(alt_u16 x1, alt_u16 y1, alt_u16 x2, alt_u16 y2, alt_u16 width)
{
   box(x1,y1,x2,y1+width);
   box(x1,y2-width,x2,y2);
   box(x1,y1,x1+width,y2);
   box(x2-width,y1,x2,y2);
   x_pos = x2;
   y_pos = y2;
}
///////////////////////////////////////////////////////////////////////
//move to pixel
void move_to(alt_u16 x, alt_u16 y)
{
    x_pos = x;
    y_pos = y;
    address = y_pos*320 + x_pos;
}
////////////////////////////////////////////////////////////////////////
//display one pixel
void pixel(alt_u16 x, alt_u16 y)
{
   x_pos = x;
   y_pos = y;
   address = y_pos*320 + x_pos;
   IOWR_16DIRECT(SRAM_BASE,2*address,pixel_color);
}
////////////////////////////////////////////////////////////////////////
//display a line
void line(alt_u16 x1, alt_u16 y1, alt_u16 x2, alt_u16 y2, alt_u16 width)
{
    int i,j,k;
    float slope;
    for(i=0;i<width;i++)
    {
        if(x1==x2)
        {
            for(k=y1;k<=y2;k++)  pixel(x1+i,k); 
        }
        else if(y1==y2)
        {
            for(j=x1;j<=x2;j++)  pixel(j,y1+i);
        }
        else
        {
            slope =(float)(y1-y2)/(x1-x2);
            for(j=x1;j<=x2;j++)
            {
               k=floor( slope*(j-x1)+y1 + 0.5);
               pixel(j+i,k);               
            }
           
        }
    }
    x_pos = x2;
    y_pos = y2;
}
///////////////////////////////////////////////////////////////////////////
//display a line ,start dot is x_pos,y_pos
void line_to(alt_u16 x, alt_u16 y, alt_u16 width)
{
     line(x_pos,y_pos,x,y,width);
     x_pos = x;
     y_pos = y;
}
////////////////////////////////////////////////////////////////////////
//display circle
void circle(alt_u16 x0, alt_u16 y0, alt_u16 radius,alt_u8 width )
{
    alt_u16 j,k;
    alt_u32 RR;
    for(j=x0-radius;j<=x0+radius;j++)
    {
      for(k=y0-radius;k<=y0+radius;k++)
      {
        RR = (j-x0)*(j-x0) + (k-y0)*(k-y0);
        if( RR>= (radius-width)*(radius-width) && RR <= radius*radius )  pixel(j,k);        
     }
   }
   x_pos = x0;
   y_pos = y0;
}

////////////////////////////////////////////////////////////////////////////////////////////////

原文地址:https://www.cnblogs.com/Neddy/p/2026884.html