⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 draw.c

📁 NIOS II 的几个源代码
💻 C
字号:
#include <stdio.h>
#include <sys/unistd.h>
#include <math.h>
#include <string.h>

#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#include "mydata.h"
#include "draw.h"

/*************************************************************/
void Draw_Pixel(U16 x, U16 y, alt_u16 color)    //在(x,y)位置画点
{
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_ADDR_BASE, y*LCD_W+x);  //output addr
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_WDATA_BASE, color);     //output data
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_CS_BASE, 0);  //cs = 0
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_WR_BASE, 0);  //wr = 0
  while((IORD_ALTERA_AVALON_PIO_DATA(LCD_BUSY_BASE)&1) == 0);
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_WR_BASE, 1);  //wr = 1
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_CS_BASE, 1);  //cs = 1
}

U16 Get_Pixel(U16 x, U16 y)      //读(x,y)位置像素值
{
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_ADDR_BASE, y*LCD_W+x);  //output addr
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_CS_BASE, 0);  //cs = 0
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_RD_BASE, 0);  //rd = 0
  while((IORD_ALTERA_AVALON_PIO_DATA(LCD_BUSY_BASE)&1) == 0);
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_RD_BASE, 1);  //rd = 1
  IOWR_ALTERA_AVALON_PIO_DATA(LCD_CS_BASE, 1);  //cs = 1
  return IORD_ALTERA_AVALON_PIO_DATA(LCD_RDATA_BASE);     //return data
}

void DrawLine(U16 x1, U16 y1, U16 x2, U16 y2, U16 color) //从(x1,y1)到(x2,y2)的线段
{
  int Lx, Ly, Cx, Cy, XER = 0,YER = 0, DTC;
  U16 i = 0;
  
  Lx = x2 - x1;
  Ly = y2 - y1;

  if(Lx > 0)
    Cx = 1;
  else if(Lx < 0)
    Cx = -1;
  
  if(Ly > 0)
    Cy = 1;
  else if(Ly < 0)
    Cy = -1;
  
  if(Lx == 0) //Vertical Line
  {
    if(Ly == 0)
      Draw_Pixel(x1, y1, color);
    else
    {
      do
      {
        Draw_Pixel(x1, y1, color);
        y1 += Cy;     
      }
      while(y1 != y2);
      Draw_Pixel(x1, y1, color);
    }
    return;
  }
  else if(Ly == 0)  //Horizontal Line
  {
    do
    {
      Draw_Pixel(x1, y1, color);
      x1 += Cx;     
    }
    while(x1 != x2);
    Draw_Pixel(x1, y1, color);
    return;
  }
  
  Lx = fabs(Lx);
  Ly = fabs(Ly);
  
  if(Ly>Lx)
    DTC = Ly;
  else  
    DTC = Lx;
  
  for(i=0; i<=DTC+1; i++)
  {
    Draw_Pixel(x1, y1, color);
    XER += Lx;
    YER += Ly;
    if(XER>DTC)
    {
       XER -= DTC;
       x1 += Cx;
    }
    if(YER>DTC)
    {
       YER -= DTC;
       y1 += Cy;
    }
  }
}

void DrawRect(U16 xp, U16 yp, U16 xl, U16 yl, U16 color) //画矩形
{
  DrawLine(xp,  yp,   xp+xl,  yp,   color);
  DrawLine(xp,  yp,   xp,   yp+yl,  color);
  DrawLine(xp+xl, yp,   xp+xl,  yp+yl,  color);
  DrawLine(xp+1,  yp+yl,  xp+xl,  yp+yl,  color);
}

void DrawFillRect(U16 xp, U16 yp, U16 xl, U16 yl, U16 color) //画填充的矩形
{
  U16 i;
  if(xl && yl)
    for(i=0; i<=yl; i++)
      DrawLine(xp, yp+i, xp+xl, yp+i, color);
}

void DrawEllipse(U16 xp, U16 yp, U16 a, U16 b, U16 color) //画椭圆
{
  U16 x, y;
  float tmp;
  
  for(x=0; x<=a; x++)
  {
    tmp = x;
    tmp /= a;
    y = b * sqrt(1 - tmp * tmp) + 0.5;
    
    Draw_Pixel(xp + x, yp + y, color);
    Draw_Pixel(xp + x, yp - y, color);
    Draw_Pixel(xp - x, yp + y, color);
    Draw_Pixel(xp - x, yp - y, color);
  } 
  for(y=0; y<=b; y++)
  {
    tmp = y;
    tmp /= b;
    x = a * sqrt(1 - tmp * tmp) + 0.5;
    
    Draw_Pixel(xp + x, yp + y, color);
    Draw_Pixel(xp + x, yp - y, color);
    Draw_Pixel(xp - x, yp + y, color);
    Draw_Pixel(xp - x, yp - y, color);
  } 
}

/****************【基本显示字符、汉字函数】**********/
void DisplayChar(char Char, U16 xp, U16 yp, U16 BK_Color, U16 FT_Color)  //显示任意字符
{
  U8 i,j;
  U8 *Font_Addr = (U8 *)(0x7c0000 + ((Char)<<4));
  U8 ft = 0;

  for(j=0; j<16; j++)
  {
    for(i=0; i<8; i++)
    {
      if(ft & 0x80)     //get the word dot,if true,display 
        Draw_Pixel(xp+i, yp+j, FT_Color);
      else
        Draw_Pixel(xp+i, yp+j, BK_Color);
      ft <<= 1;
    }
    
    if(j)
      ft= *Font_Addr++;
  }
}

void DisplayHanzi(U16 Unicode, U16 xp, U16 yp, U16 BK_Color, U16 FT_Color) //显示任意汉字
{
  U8 i,j;
  U16 ft;
  U16 *Font_Addr = (U16 *)(0x7c0000 + ((Unicode-0x3bbf-(Unicode>>8)*0xa2)<<5) + 0x01000);
  for(j=0; j<16; j++)
  {
    ft = *Font_Addr++;
    ft = (ft << 8) | (ft >> 8);
    for(i=0; i<16; i++)
    {
      if(ft & 0x8000)     //get the word dot,if true,display 
        Draw_Pixel(xp+i, yp+j, FT_Color);
      else
        Draw_Pixel(xp+i, yp+j, BK_Color);
      ft <<= 1;
    }
  }
}

U16 SizeofString(U8 *str) //求取字符串长度
{
  U16 len = 0;
  while(str[len++]);
  return len-1;
}

//显示任意字符串(中文为宋体,英文为8×16点阵)
void DisplayAnyString(U8 *Str, U16 xp, U16 yp, U16 BK_Color, U16 FT_Color)
{
  U16 i;
  U16 str_len = SizeofString(Str);
  
  for(i=0; i<str_len; i++)
  {
    if(Str[i] < 0x80)
    {
      if(xp > LCD_W - 8)
      {
        xp = 0;
        yp += 16;
      }
      if(Str[i] == 0x0d)      //回车
        xp = 0;
      else if(Str[i] == 0x0a)   //换行
        yp += 16;
      else
      {
        DisplayChar(Str[i], xp, yp, BK_Color, FT_Color);
        xp += 8;
      }
    }
    else
    {
      if(xp > LCD_W - 16)
      {
        xp = 0;
        yp += 16;
      }
      DisplayHanzi((Str[i]<<8)|Str[i+1], xp, yp, BK_Color, FT_Color);
      xp += 16;
      i++;
    }
  } 
}

void Display_Pic(U16 x, U16 y, U16 w, U16 h, U8 *bmp, U16 color) //显示图片
{
  U16 i, j;
  U8 dot;
  for(j=0; j<h; j++)
  {
      for(i=0; i<w; i++)
      {
        if(i%8 == 0)
          dot = *bmp++;
        if(dot & 0x80)
           Draw_Pixel(x+i, y+j, color);
        dot <<= 1;
      }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -