样图如下:
通过Linux平台(ubuntu-18.04)用终端命令进行交叉编译(arm-linux-gcc)下列代码:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
//屏幕宽长
#define ICTwide 480
#define ICTlength 800
//圆参数
#define r 150
#define x 400
#define y 240
//左鼻参数
#define rl 30
#define xl 350
#define yl 240
//右鼻参数
#define rr 30
#define xr 450
#define yr 240
//左耳参数
#define rel 50
#define xel 300
#define yel 100
//右耳参数
#define rer 50
#define xer 500
#define yer 100
//变色差
#define Coloe_Rate 8
//原始色
#define Original_Color 0x000000
//饱和色
#define Saturated_Color 0xffffff
//刷新率
#define Refresh_Coloe_Rate 1000000
颜色设置
//#define Ear_color 0xff0000 //耳朵
//#define Nose_color 0xee0000 //鼻子
//#define Orth_Color 0xcd0000 //矩形
//#define EarIn_color 0xffff00 //耳内
//#define Circle_color 0xffff00 //圆
//#define Bottom_color 0xff0000 //圆底
//#define Background_color 0xee4000 //背景
//矩形边框参数
#define L_wide 40
#define W_wide 40
#define W_Dvalue ICTwide-W_wide
#define L_Dvalue ICTlength-L_wide
//颜色设置
int Ear_color=0xff0000; //耳朵
int Nose_color=0xee0000; //鼻子
int Orth_Color=0xcd0000; //矩形
int EarIn_color=0xffff00; //耳内
int Circle_color=0xffff00; //圆
int Bottom_color=0xff0000; //圆底
int Background_color=0xee4000; //背景
int main()
{
//1.打开lcd屏幕
int lcd_fd = open("/dev/fb0",O_RDWR);
if (lcd_fd == -1)
{
perror("open lcd fail");
}
//2.打开映射
int *plcd = mmap(NULL,ICTlength*ICTwide*4,PROT_READ|PROT_WRITE,
MAP_SHARED,lcd_fd,0);
while(1)
{
//背景颜色设置
for(int i = 0;i < ICTwide;i++)
{
for(int j = 0;j < ICTlength;j++)
{
*(plcd + ICTlength*i + j) = Background_color;
}
}
//圆颜色设置
for(int i = 0;i < ICTwide;i++)
{
for(int j = 0;j < ICTlength; j++)
{
//左耳朵
if((j-xel)*(j-xel) +(i-yel)*(i-yel) <= rel*rel)
{
*(plcd + ICTlength*i + j) = Ear_color;
}
//右耳朵
if((j-xer)*(j-xer) +(i-yer)*(i-yer) <= rer*rer)
{
*(plcd + ICTlength*i + j) = Ear_color;
}
//左内耳朵
if((j-xel)*(j-xel) +(i-yel)*(i-yel) <= (rel-10)*(rel-10))
{
*(plcd + ICTlength*i + j) = EarIn_color;
}
//右内耳朵
if((j-xer)*(j-xer) +(i-yer)*(i-yer) <= (rer-10)*(rer-10))
{
*(plcd + ICTlength*i + j) = EarIn_color;
}
//底圆
if((j-x)*(j-x) +(i-y)*(i-y) <= (r+10)*(r+10))
{
*(plcd + ICTlength*i + j) = Bottom_color;
}
//上圆
if((j-x)*(j-x) +(i-y)*(i-y) <= r*r)
{
*(plcd + ICTlength*i + j) = Circle_color;
}
//左眼
if((j-xl)*(j-xl) +(i-yl)*(i-yl) <= rl*rl)
{
*(plcd + ICTlength*i + j) = Nose_color;
}
//右眼
if((j-xr)*(j-xr) +(i-yr)*(i-yr) <= rr*rr)
{
*(plcd + ICTlength*i + j) = Nose_color;
}
}
}
//矩形边框设置
for(int i = 0; i < ICTwide;i++)
{
for(int j = 0;j < ICTlength;j++)
{
//长
if((i>=0&&i<W_wide)&&(j<ICTlength))
{
*(plcd + ICTlength*i + j) =Orth_Color;
}
if((i>=W_Dvalue&&i<ICTwide)&&(j<ICTlength))
{
*(plcd + ICTlength*i + j) =Orth_Color;
}
//宽
if((i<ICTwide)&&(j>=0&&j<L_wide))
{
*(plcd + ICTlength*i + j) =Orth_Color;
}
if((i<ICTwide)&&(j>=L_Dvalue&&j<ICTlength))
{
*(plcd + ICTlength*i + j) =Orth_Color;
}
}
}
usleep(Refresh_Coloe_Rate);
//颜色设置
Ear_color+=Coloe_Rate; //耳朵
Nose_color+=Coloe_Rate; //鼻子
Orth_Color+=Coloe_Rate; //矩形
EarIn_color+=Coloe_Rate; //耳内
Circle_color+=Coloe_Rate; //圆
Bottom_color+=Coloe_Rate; //圆底
Background_color+=Coloe_Rate; //背景
if(Ear_color>Saturated_Color)
{
Ear_color=Original_Color;
}
if(Nose_color>Saturated_Color)
{
Nose_color=Original_Color;
}
if(Orth_Color>Saturated_Color)
{
Orth_Color=Original_Color;
}
if(EarIn_color>Saturated_Color)
{
EarIn_color=Original_Color;
}
if(Circle_color>Saturated_Color)
{
Circle_color=Original_Color;
}
if(Bottom_color>Saturated_Color)
{
Bottom_color=Original_Color;
}
if(Background_color>Saturated_Color)
{
Background_color=Original_Color;
}
usleep(Refresh_Coloe_Rate);
}
//3.关闭lcd 解除映射
munmap(plcd,ICTlength*ICTwide*4);
close(lcd_fd);
return 0;
}
该代码采用映射实现图案,可随时间不断刷新图案颜色,可自定义刷新速率。