粤嵌gec6818LED屏幕上画图 太极图 图片显示 电子相册 2048小游戏 实现识别触摸坐标的识别 电子自助点餐设计等项目
Posted qq_735754647
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了粤嵌gec6818LED屏幕上画图 太极图 图片显示 电子相册 2048小游戏 实现识别触摸坐标的识别 电子自助点餐设计等项目相关的知识,希望对你有一定的参考价值。
交叉开发
在一个有编辑/编译功能的PC机上进行编辑/编译,生成的可执行文件通过
交叉开发工具下载到目标机(GEC-6818)
开发板 --- Linux内核 --- Linux指令
首先创建自己的工作目录
mkdir xxx
下载交叉编译生成的可执行文件:
rx 可执行文件名
传输 --- 发送xmodem --- 浏览到我们所要发送的文件 --- 选中 --- 发送
如果发送的是一个可执行文件,没有可执行的权限
chmod +x 可执行文件名 --- 再去运行
!!! 下载可执行文件必须是交叉编译生成的
arm-linux-gcc 源文件名 -o 可执行文件名
2 屏幕操作
屏幕分辨率:800*480
800 一行有800个像素点 480行
像素点:显示颜色的最小单位
颜色:ARGB --- 每个分量一个字节
A:透明度
R:红色分量 0 - ff
G:绿色分量
B:蓝色分量
绿色:0x0000ff00
如果我们想要绿屏:
每个像素点全部显示绿色:0x0000ff00
打开屏幕
int lcd_fd = open("/dev/fb0",O_RDWR);
if(lcd_fd == -1)
perror("open lcd fail");
return -1;
操作屏幕
//写入数据
int color[800*480]=0;
for(int i=0;i<480;i++)
for(int j=0;j<800;j++)
color[i*800+j]=0x0000ff00;
write(lcd_fd,color,800*480*4);
关闭屏幕
close(lcd_fd);
因为引用了 #include<math.h>
切记 切记 Liunx 编译时 要加 -lm
类似 arm-linux-gcc 1.c -lm
画太极图
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <sys/mman.h>
#include<unistd.h>
#include<math.h>
int *plcd = NULL;
#define WHITE 0x00FFFFFF
#define BLAK 0x00000000
void draw_point(int x, int y, int color)
if (x >= 0 && x<800 && y >= 0 && y<480)
*(plcd + y * 800 + x) = color;
void draw_circle(int x, int y,double r ,int color)
if (x >= 0 && x<480 && y >= 0 && y<800)
for (double i = 0; i < 480; i++)
for (double j = 0; j < 800; j++)
double all=(i-x)*(i-x)+(j-y)*(j-y);
double fc=sqrt(all);
if(r>fc)
draw_point(j, i, color);
// printf("fc=%lf\\n",fc);
void draw_circle_b(int x, int y,double r ,int color)
if (x >= 0 && x<480 && y >= 0 && y<800)
for (double i = 0; i < 480; i++)
for (double j = 0; j < 800; j++)
if(i<x)
double all=(i-x)*(i-x)+(j-y)*(j-y);
double fc=sqrt(all);
if(r>fc)
draw_point(j, i, color);
// printf("fc=%lf\\n",fc);
void clear(int color)
int x,y;
for(y=0;y<480;y++)
for(x=0;x<800;x++)
draw_point(x,y,color);
int main()
int lcd_fd = open("/dev/fb0",O_RDWR);
if (lcd_fd == -1)
perror("open lcd fail");
plcd = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED,lcd_fd,0);
if (plcd==NULL)
perror("mmao fail");
int color = 0x0000FFFF;
clear(0x00666666);
draw_circle(240, 400,200, BLAK);
draw_circle_b(240, 400,200, WHITE);
draw_circle(240, 300,100, WHITE);
draw_circle(240, 500,100, BLAK);
draw_circle(240, 300,25, BLAK);
draw_circle(240, 500,25, WHITE);
// draw_circle(240, 400,50, color);
close(lcd_fd);
munmap(plcd,800*480*4);
return 0;
画笑脸
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <sys/mman.h>
#include<unistd.h>
#include<math.h>
#define WHITE 0x00FFFFFF
#define BLAK 0x00000000
#define org 0x00CDAD00
#define gray 0x00CD853F
int *plcd = NULL;
void draw_point(int x, int y, int color)
if (x >= 0 && x<800 && y >= 0 && y<480)
*(plcd + y * 800 + x) = color;
void draw_circle(int x, int y,double r ,int color)//HUAYUAN
if (x >= 0 && x<480 && y >= 0 && y<800)
for (double i = 0; i < 480; i++)
for (double j = 0; j < 800; j++)
double all=(i-x)*(i-x)+(j-y)*(j-y);
double fc=sqrt(all);
if(r>fc)
draw_point(j, i, color);
// printf("fc=%lf\\n",fc);
void draw_circle_b(int x, int y,double r ,int color)//BANYUAN
if (x >= 0 && x<480 && y >= 0 && y<800)
for (double i = 0; i < 480; i++)
for (double j = 0; j < 800; j++)
if(i<x)
double all=(i-x)*(i-x)+(j-y)*(j-y);
double fc=sqrt(all);
if(r>fc)
draw_point(j, i, color);
// printf("fc=%lf\\n",fc);
void draw_circle_c(int x, int y,double r ,int color)//BANYUAN
if (x >= 0 && x<480 && y >= 0 && y<800)
for (double i = 0; i < 480; i++)
for (double j = 0; j < 800; j++)
if(i>x)
double all=(i-x)*(i-x)+(j-y)*(j-y);
double fc=sqrt(all);
if(r>fc)
draw_point(j, i, color);
// printf("fc=%lf\\n",fc);
void clear(int color)
int x,y;
for(y=0;y<480;y++)
for(x=0;x<800;x++)
draw_point(x,y,color);
int main()
int lcd_fd = open("/dev/fb0",O_RDWR);
if (lcd_fd == -1)
perror("open lcd fail");
plcd = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED,lcd_fd,0);
if (plcd==NULL)
perror("mmap fail");
int color = 0x0000FFFF;
clear(0x00666666);
draw_circle(240, 400,200, org);//画橙底
//draw_circle(180, 480,30, HUI);//眼睛1
//draw_circle(180, 320,30, HUI); //眼睛2
draw_circle_b(170, 300,50, gray);
draw_circle_b(170, 300,40, org);
draw_circle_b(170, 500,50, gray);
draw_circle_b(170, 500,40, org);
//draw_circle(240, 300,25, BLAK);
//draw_circle(240, 500,25, WHITE);
draw_circle_c(250, 400,150, WHITE);
// draw_circle(240, 400,50, color);
int x,y;
for(y=0;y<480;y++)
for(x=0;x<800;x++)
if (x >=400 && x<402 && y >=250 && y<400 )
draw_point(x,y,BLAK);
if (x >=300 && x<302 && y >=250 && y<362 )
draw_point(x,y,BLAK);
if (x >=500 && x<502 && y >=250 && y<362 )
draw_point(x,y,BLAK);
close(lcd_fd);
munmap(plcd,800*480*4);
return 0;
画微笑
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <sys/mman.h>
#include<unistd.h>
#include<math.h>
#define WHITE 0x00FFFFFF
#define BLAK 0x00000000
#define org 0x00CDAD00
#define HUI 0x00CD853F
int *plcd = NULL;
void draw_point(int x, int y, int color)
if (x >= 0 && x<800 && y >= 0 && y<480)
*(plcd + y * 800 + x) = color;
void draw_circle(int x, int y,double r ,int color)//HUAYUAN
if (x >= 0 && x<480 && y >= 0 && y<800)
for (double i = 0; i < 480; i++)
for (double j = 0; j < 800; j++)
double all=(i-x)*(i-x)+(j-y)*(j-y);
double fc=sqrt(all);
if(r>fc)
draw_point(j, i, color);
// printf("fc=%lf\\n",fc);
void draw_circle_b(int x, int y,double r ,int color)//BANYUAN
if (x >= 0 && x<480 && y >= 0 && y<800)
for (double i = 0; i < 480; i++)
for (double j = 0; j < 800; j++)
if(i>x)
double all=(i-x)*(i-x)+(j-y)*(j-y);
double fc=sqrt(all);
if(r>fc)
draw_point(j, i, color);
// printf("fc=%lf\\n",fc);
void clear(int color)
int x,y;
for(y=0;y<480;y++)
for(x=0;x<800;x++)
draw_point(x,y,color);
int main()
int lcd_fd = open("/dev/fb0",O_RDWR);
if (lcd_fd == -1)
perror("open lcd fail");
plcd = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED,lcd_fd,0);
if (plcd==NULL)
perror("mmao fail");
int color = 0x0000FFFF;
clear(0x00666666);
draw_circle(240, 400,200, org);//画橙底
draw_circle(180, 480,30, HUI);//眼睛1
draw_circle(180, 320,30, HUI); //眼睛2
draw_circle_b(270, 400,50, HUI);
draw_circle_b(270, 400,40, org);
//draw_circle(240, 300,25, BLAK);
//draw_circle(240, 500,25, WHITE);
// draw_circle(240, 400,50, color);
close(lcd_fd);
munmap(plcd,800*480*4);
return 0;
四叶草 彩虹
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <sys/mman.h>
#include<unistd.h>
#include<math.h>
#define WHITE 0x00FFFFFF
#define BLAK 0x00000000
#define org 0x00CDAD00
#define HUI 0x00CD853F
int *plcd = NULL;
void draw_point(int x, int y, int color)
if (x >= 0 && x<800 && y >= 0 && y<480)
*(plcd + y * 800 + x) = color;
void draw_circle(int x, int y,double r ,int color)//HUAYUAN
if (x >= 0 && x<480 && y >= 0 && y<800)
for (double i = 0; i < 480; i++)
for (double j = 0; j < 800; j++)
double all=(i-x)*(i-x)+(j-y)*(j-y);
double fc=sqrt(all);
if(r>fc)
draw_point(j, i, color);
// printf("fc=%lf\\n",fc);
void draw_circle_b(int x, int y,double r ,int color)//BANYUAN
if (x >= 0 && x<480 && y >= 0 && y<800)
for (double i = 0; i < 480; i++)
for (double j = 0; j < 800; j++)
if(i>x)
double all=(i-x)*(i-x)+(j-y)*(j-y);
double fc=sqrt(all);
if(r>fc)
draw_point(j, i, color);
// printf("fc=%lf\\n",fc);
void clear(int color)
int x,y;
for(y=0;y<480;y++)
for(x=0;x<800;x++)
draw_point(x,y,color);
int main()
int lcd_fd = open("/dev/fb0",O_RDWR);
if (lcd_fd == -1)
perror("open lcd fail");
plcd = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED,lcd_fd,0);
if (plcd==NULL)
perror("mmao fail");
int color = 0x0000FFFF;
clear(0x00666666);
while(1)
int x,y;
for(x=0;x<800;x++)
for(y=0;y<480;y++)
if(y<160)
*(plcd + y * 800 + x) = 0x0000ff00;
else if(y<320)
*(plcd + y * 800 + x) = 0x000000ff;
else
*(plcd + y * 800 + x) = 0x00ff0000;
draw_circle(240, 400,200, org);//画橙底
draw_circle_b(270, 400,50, HUI);
draw_circle_b(270, 400,40, org);
draw_circle(180, 480,30, HUI);//眼睛1
draw_circle(180, 320,30, HUI); //眼睛2
int i,j;
int cir_color[480][800];
for(i=0;i<480;i++)
for(j=0;j<800;j++)
if((i-480)*(i-480) + (j-400)*(j-400)<51*51)
cir_color[i][j]=0x00FF0033;
else if((i-480)*(i-480) + (j-400)*(j-400)<107*107)
cir_color[i][j]=0x00FF6600;
else if((i-480)*(i-480) + (j-400)*(j-400)<168*168)
cir_color[i][j]=0x00FFFF00;
else if((i-480)*(i-480) + (j-400)*(j-400)<234*234)
cir_color[i][j]=0x0000FF00;
else if((i-480)*(i-480) + (j-400)*(j-400)<307*307)
cir_color[i][j]=0x0000FFFF;
else if((i-480)*(i-480) + (j-400)*(j-400)<385*385)
cir_color[i][j]=0x000000FF;
else
cir_color[i][j]=0x00FF00CC;
lcd_fd = open("/dev/fb0",O_RDWR);
if(-1 == lcd_fd)
printf("open lcd error\\n");
write(lcd_fd,cir_color,800*480*4);
sleep(2);
int si_color[480][800];
for(i=0;i<480;i++)
//遍历二维数组每一行的每一列
for(j=0;j<800;j++)
int a = (i-150)*(i-150) + (j-300)*(j-300);
int b = (i-330)*(i-330) + (j-300)*(j-300);
int c = (i-150)*(i-150) + (j-500)*(j-500);
int d = (i-330)*(i-330) + (j-500)*(j-500);
int r2 = 150*150;
if (a<r2 && b<r2)
si_color[i][j]=0x00FF0033;
else if(a<r2 && c<r2)
si_color[i][j]=0x00FF6600;
else if(b<r2 && d<r2)
si_color[i][j]=0x00FFFF00;
else if(c<r2 && d<r2)
si_color[i][j]=0x0000FF00;
else
si_color[i][j]=0x00FF00CC;
lcd_fd = open("/dev/fb0",O_RDWR);
if(-1 == lcd_fd)
printf("open lcd error\\n");
write(lcd_fd,si_color,800*480*4);
sleep(1);
close(lcd_fd);
munmap(plcd,800*480*4);
return 0;
切记 切记 Liunx 编译时 要加 -lm
电子相册代码实现
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <math.h>
#include <stdlib.h>
int * p = NULL ;
void draw_point(int x,int y,int color)
if(x>=0 && x<800 && y>=0 && y<480 )
*(p+800*y+x) = color ;
void show_bmp (char * pathname ,int x ,int y)
int fd = open(pathname,O_RDONLY);
if(fd == -1)
perror("open error\\n");
return ;
int fd1 = open("/dev/fb0",O_RDWR);
if(fd1 == -1)
perror("open error\\n");
return ;
printf("open success\\n");
p = mmap(NULL,800*480*4,PROT_READ | PROT_WRITE,MAP_SHARED ,fd1,0);
if(p == NULL )
perror("mmap error\\n");
return ;
int width,height;
short depth;
unsigned char buf[4] ;
//读取宽度
lseek(fd,0x12,SEEK_SET);
read(fd,buf,4);
width = buf[3]<<24 | buf[2]<< 16 | buf[1] << 8 | buf[0];
//读取高度
read(fd,buf,4);
height = buf[3]<<24 | buf[2]<< 16 | buf[1] << 8 | buf[0];
//读取色深
lseek(fd,0x1c,SEEK_SET);
read(fd,buf,2);
depth = buf[1] << 8 | buf[0];
//打印信息
printf("width = %d height = %d depth = %d \\n",width,height,depth);
int line_valid_bytes = abs(width) * depth / 8 ;
int laizi=0;
if( (line_valid_bytes % 4) !=0 )
laizi = 4 - line_valid_bytes%4;
int line_bytes = line_valid_bytes + laizi ;
int total_bytes = line_bytes * abs(height) ;
unsigned char * p1 = malloc(total_bytes);
lseek(fd,54,SEEK_SET);
read(fd,p1,total_bytes);
unsigned char a ,r ,g, b ;
int i = 0;
int x0=0,y0=0;
int color ;
for(y0=0;y0<abs(height);y0++)
for(x0=0;x0<abs(width);x0++)
b = p1[i++];
g = p1[i++];
r = p1[i++];
if(depth == 32)
a=p1[i++];
if(depth == 24)
a = 0;
color = a << 24 | r << 16 | g << 8 | b ;
draw_point(width>0?x+x0:abs(width)+x-1-x0,
height>0? y+height-1-y0 : y+y0,color);
i = i +laizi ;
free(p1);
close(fd1);
munmap(p,800*480*4);
close(fd);
int main()
while(1)
show_bmp("1.bmp",0 ,0);// 自己存储的图片
sleep(2);
show_bmp("2.bmp",0 ,0);
sleep(2);
show_bmp("3.bmp",0 ,0);
sleep(2);
show_bmp("4.bmp",0 ,0);
sleep(2);
show_bmp("5.bmp",0 ,0);
sleep(2);
return 0;
横行打开
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <math.h>
#include <stdlib.h>
#define RGB_SIZE 800*480*3
#define LCD_SIZE 800*480
int *plcd=NULL;
int photo(char *a)
// 1. 打开触摸屏文件
int lcd_fd = open("/dev/fb0", O_RDWR);
if (lcd_fd == -1)
printf("Open lcd failed!!\\n");
return -1;
// 2. 打开bmp图片
int bmp_fd = open(a, O_RDWR);
// 3. 偏移bmp格式头, 54个字节
off_t offset = lseek(bmp_fd, 54, SEEK_SET);
if (offset == -1)
printf("Offset failed!\\n");
return -1;
// 4. 读取bmp图片的RGB值,将读到的值存进bmp_rgb数组中
char bmp_buf[RGB_SIZE] = ;
size_t re_ret = read(bmp_fd, bmp_buf, RGB_SIZE);
// 5. 24位数据-->32位数据:bmp图片rgb占3个字节,lcdargb占4个字节. char占1个字节大小,int占4个字节大小
int lcd_buf[LCD_SIZE] = ;
int i;
for (i=0; i<LCD_SIZE; i++)
lcd_buf[i] = bmp_buf[i*3+2]<<16 | bmp_buf[i*3+1]<<8 | bmp_buf[i*3+0]<<0;
// 6. 翻转180°
int fli_buf[LCD_SIZE];
int x, y;
for(y = 0; y < 480; y++)
for(x = 0; x < 800; x++)
fli_buf[y*800+x] = lcd_buf[(479-y)*800+x];
// 7. 写入LCD
plcd=mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd_fd,0);
if(plcd == NULL)
perror("mmap fail");
for(int x=0;x<800;x++)
for(int y=0;y<480;y++)
*(plcd+x+y*800)=fli_buf[x+y*800];
sleep(0.1);
// 8. 关闭文件
close(lcd_fd);
close(bmp_fd);
return 0;
int photo2(char *a)
// 1. 打开触摸屏文件
int lcd_fd = open("/dev/fb0", O_RDWR);
if (lcd_fd == -1)
printf("Open lcd failed!!\\n");
return -1;
// 2. 打开bmp图片
int bmp_fd = open(a, O_RDWR);
// 3. 偏移bmp格式头, 54个字节
off_t offset = lseek(bmp_fd, 54, SEEK_SET);
if (offset == -1)
printf("Offset failed!\\n");
return -1;
// 4. 读取bmp图片的RGB值,将读到的值存进bmp_rgb数组中
char bmp_buf[RGB_SIZE] = ;
size_t re_ret = read(bmp_fd, bmp_buf, RGB_SIZE);
// 5. 24位数据-->32位数据:bmp图片rgb占3个字节,lcdargb占4个字节. char占1个字节大小,int占4个字节大小
int lcd_buf[LCD_SIZE] = ;
int i;
for (i=0; i<LCD_SIZE; i++)
lcd_buf[i] = bmp_buf[i*3+2]<<16 | bmp_buf[i*3+1]<<8 | bmp_buf[i*3+0]<<0;
// 6. 翻转180°
int fli_buf[LCD_SIZE];
int x, y;
for(y = 0; y < 480; y++)
for(x = 0; x < 800; x++)
fli_buf[y*800+x] = lcd_buf[(479-y)*800+x];
// 7. 写入LCD
plcd=mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd_fd,0);
if(plcd == NULL)
perror("mmap fail");
for(int y=0;y<480;y++)
for(int x=0;x<800;x++)
*(plcd+x+y*800)=fli_buf[x+y*800];
sleep(0.1);
// 8. 关闭文件
close(lcd_fd);
close(bmp_fd);
return 0;
int photo3(char *a)
// 1. 打开触摸屏文件
int lcd_fd = open("/dev/fb0", O_RDWR);
if (lcd_fd == -1)
printf("Open lcd failed!!\\n");
return -1;
// 2. 打开bmp图片
int bmp_fd = open(a, O_RDWR);
// 3. 偏移bmp格式头, 54个字节
off_t offset = lseek(bmp_fd, 54, SEEK_SET);
if (offset == -1)
printf("Offset failed!\\n");
return -1;
// 4. 读取bmp图片的RGB值,将读到的值存进bmp_rgb数组中
char bmp_buf[RGB_SIZE] = ;
size_t re_ret = read(bmp_fd, bmp_buf, RGB_SIZE);
// 5. 24位数据-->32位数据:bmp图片rgb占3个字节,lcdargb占4个字节. char占1个字节大小,int占4个字节大小
int lcd_buf[LCD_SIZE] = ;
int i;
for (i=0; i<LCD_SIZE; i++)
lcd_buf[i] = bmp_buf[i*3+2]<<16 | bmp_buf[i*3+1]<<8 | bmp_buf[i*3+0]<<0;
// 6. 翻转180°
int fli_buf[LCD_SIZE];
int x, y;
for(y = 0; y < 480; y++)
for(x = 0; x < 800; x++)
fli_buf[y*800+x] = lcd_buf[(479-y)*800+x];
// 7. 写入LCD
plcd=mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd_fd,0);
if(plcd == NULL)
perror("mmap fail");
for(int x=0;x<400;x++)
for(int y=0;y<240;y++)
*(plcd+x+y*800)=fli_buf[x+y*800];
sleep(0.1);
for(int x=400;x<800;x++)
for(int y=0;y<240;y++)
*(plcd+x+y*800)=fli_buf[x+y*800];
sleep(0.1);
for(int x=0;x<400;x++)
for(int y=240;y<480;y++)
*(plcd+x+y*800)=fli_buf[x+y*800];
sleep(0.1);
for(int x=400;x<800;x++)
for(int y=240;y<480;y++)
*(plcd+x+y*800)=fli_buf[x+y*800];
sleep(0.1);
// 8. 关闭文件
close(lcd_fd);
close(bmp_fd);
return 0;
void main()
while(1)
photo3("./1.bmp");
sleep(0.5);
photo3("./2.bmp");
sleep(0.5);
photo("./3.bmp");
sleep(0.5);
如何使用触摸屏
Linux为输入事件提供了一个专门的结构体
#include <linux/input.h>
struct input_event
struct timeval time;//记录事件发生的时间
_u16 type; //记录事件的类型
type == EV_ABS 触摸屏事件
type == EV_KEY 按键事件
_u16 code; //随着type的改变而改变
type == EV_ABS
code == ABS_X x轴
code == ABS_y y轴
type == EV_KEY
code == BTN_TOUCH
_s32 value; //随着code的变化而变化
code == ABS_X
value = x x值
code == ABS_Y
value = y y值
code == BTN_TOUCH
value == 0 表示松开
value == 1 表示按下
触摸屏的获取
获取坐标
打开设备 --- 读取内容 --- 解析 --- 关闭设备
int touch_fd = open("/dev/input/event0",O_RDONLY);
if(touch_fd == -1)
perror("open touch fail");
return -1;
struct input_event ev;
int x=-1,y=-1;
while(1)
read(touch_fd,&ev,sizeof(ev));
if(ev.type == EV_ABS && ev.code == ABS_X)
x = ev.value;
if(ev.type == EV_ABS && ev.code == ABS_Y)
y = ev.value;
if(ev.type == EV_KEY && ev.code == BTN_TOUCH)
if(ev.value == 0)
printf("Release\\n");
break;
else if(ev.value == 1)
printf("Press\\n");
printf("x=%d y=%d\\n",x,y);
close(touch_fd);
#include <stdio.h>//printf
#include <linux/input.h>//struct input_event
#include <sys/types.h>//open
#include <sys/stat.h>//open
#include <fcntl.h>//open
#include <unistd.h>//read
#include <stdlib.h>
#include <sys/mman.h>
#include<linux/fb.h>
#include <math.h>
int * p = NULL ;
void draw_point(int x,int y,int color)
if(x>=0 && x<800 && y>=0 && y<480 )
*(p+800*y+x) = color ;
void show_bmp (char * pathname ,int x ,int y)
int fd = open(pathname,O_RDONLY);
if(fd == -1)
perror("open error\\n");
return ;
int fd1 = open("/dev/fb0",O_RDWR);
if(fd1 == -1)
perror("open error\\n");
return ;
printf("open success\\n");
p = mmap(NULL,800*480*4,PROT_READ | PROT_WRITE,MAP_SHARED ,fd1,0);
if(p == NULL )
perror("mmap error\\n");
return ;
int width,height;
short depth;
unsigned char buf[4] ;
//读取宽度
lseek(fd,0x12,SEEK_SET);
read(fd,buf,4);
width = buf[3]<<24 | buf[2]<< 16 | buf[1] << 8 | buf[0];
//读取高度
read(fd,buf,4);
height = buf[3]<<24 | buf[2]<< 16 | buf[1] << 8 | buf[0];
//读取色深
lseek(fd,0x1c,SEEK_SET);
read(fd,buf,2);
depth = buf[1] << 8 | buf[0];
//打印信息
printf("width = %d height = %d depth = %d \\n",width,height,depth);
int line_valid_bytes = abs(width) * depth / 8 ;
int laizi=0;
if( (line_valid_bytes % 4) !=0 )
laizi = 4 - line_valid_bytes%4;
int line_bytes = line_valid_bytes + laizi ;
int total_bytes = line_bytes * abs(height) ;
unsigned char * p1 = malloc(total_bytes);
lseek(fd,54,SEEK_SET);
read(fd,p1,total_bytes);
unsigned char a ,r ,g, b ;
int i = 0;
int x0=0,y0=0;
int color ;
for(y0=0;y0<abs(height);y0++)
for(x0=0;x0<abs(width);x0++)
b = p1[i++];
g = p1[i++];
r = p1[i++];
if(depth == 32)
a=p1[i++];
if(depth == 24)
a = 0;
color = a << 24 | r << 16 | g << 8 | b ;
draw_point(width>0?x+x0:abs(width)+x-1-x0,
height>0? y+height-1-y0 : y+y0,color);
i = i +laizi ;
free(p1);
close(fd1);
munmap(p,800*480*4);
close(fd);
int main()
int touch_fd = open("/dev/input/event0",O_RDONLY);
if(touch_fd == -1)
perror("open touch fail");
return -1;
struct input_event ev;
int x=-1,y=-1;
while(1)
while(1)
read(touch_fd,&ev,sizeof(ev));
if(ev.type == EV_ABS && ev.code == ABS_X)
x = ev.value;
if(ev.type == EV_ABS && ev.code == ABS_Y)
y = ev.value;
if(ev.type == EV_KEY && ev.code == BTN_TOUCH)
if(ev.value == 0)
printf("Release\\n");
break;
else if(ev.value == 1)
printf("Press\\n");
printf("x=%d y=%d\\n",x,y);
if (x>0&& y<400)
show_bmp("1.bmp", 0, 0);
if (x>400 && y<800)
show_bmp("2.bmp", 0, 0);
close(touch_fd);
return 0;
2048小游戏c语言代码
2048游戏规则:每次可以选择上下左右其中一个方向去滑动,每滑动一次,所有的数字方块都会往滑动的方向靠拢外,系统也会在空白的地方乱数出现一个数字方块,相同数字的方块在靠拢、相撞时会相加。不断的叠加最终拼凑出2048这个数字就算成功。
参考思路 :
第一步
给屏幕上背景色(红、蓝....)
第二步
中心画一个正方形400*400(这里也可以画大一点,每张图之间保留一点空隙,使游戏更美观)
第三步
找11张100*100的图片(2,4,8,16...2048)
第四步
初始化棋盘,游戏开始,在16个位置上随机出现2,4,8中的一张图片,共出现3张
(概率自己弄,如出现2的概率50%,4个概率25%,8个概率25%)
第五步
手指滑屏怎么实现图片的上下左右移动,同时相同的图片相加变成一张图片
如,两张图片2变更成一张图片4
同时,滑动后在没有图片的地方生成一张新的图片(同第四步,只是这里出现一张图)
第六步
判断游戏结束
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include<linux/fb.h>
#include <math.h>
#include <errno.h>
#include <time.h>
int lcd_fd, ts_fd;
int *lcd_ptr;
int game_over;
int get_finger_direction();
struct bmp_header //14
unsigned char type[2];
unsigned long size;
unsigned short reserverd1;
unsigned short reserverd2;
unsigned long offbit;
__attribute__((packed)); //字节对齐
struct bmp_info //40
unsigned int size;
unsigned int width;
unsigned int height;
unsigned short planes;
unsigned short bitcount;
unsigned int bitcompression;
unsigned int sizeimg;
unsigned int xpelspermeter;
unsigned int ypelspermeter;
unsigned int biclrused;
unsigned int important;
__attribute__((packed)); //字节对齐
//数组初始化,将所有的图片保存在一个数组中
const char *bmp_files[] =
"/pic/bmp/digit_2.bmp", "/pic/bmp/digit_4.bmp",
"/pic/bmp/digit_8.bmp", "/pic/bmp/digit_16.bmp",
"/pic/bmp/digit_32.bmp", "/pic/bmp/digit_64.bmp",
"/pic/bmp/digit_128.bmp", "/pic/bmp/digit_256.bmp",
"/pic/bmp/digit_512.bmp", "/pic/bmp/digit_1024.bmp",
"/pic/bmp/digit_2048.bmp", "/pic/bmp/digit_4096.bmp",
"/pic/bmp/digit_8192.bmp", "/pic/bmp/digit_16384.bmp",
"/pic/bmp/digit_32768.bmp", "/pic/bmp/digit_65536.bmp"
;
//棋盘矩阵的初始化
int array[4][4] = 0;
//根据要显示的数字来返回对应的文件名的下标
int get_bmp_files_index(int x)
if (x == 2)
return 0;
else if (x == 4)
return 1;
else if (x == 8)
return 2;
else if (x == 16)
return 3;
else if (x == 32)
return 4;
else if (x == 64)
return 5;
else if (x == 128)
return 6;
else if (x == 256)
return 7;
else if (x == 512)
return 8;
else if (x == 1024)
return 9;
else if (x == 2048)
return 10;
else if (x == 4096)
return 11;
else if (x == 8192)
return 12;
else if (x == 16384)
return 13;
else if (x == 32768)
return 14;
else if (x == 65536)
return 15;
return -1;
//求棋盘矩阵里面有多少个0
int rectangle_get_zero_num()
int i, j, count = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (array[i][j] == 0)
count++;
return count;
int rectangle_set_value(int z, int value)
int i, j, count = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (array[i][j] == 0)
count++;
if (count == z)
array[i][j] = value;
return 0;
//触摸屏滑屏算法实现
int get_xy_v2()
struct input_event ts;
int x1 = -1, y1 = -1;
int x2, y2;
int x_read = 0, y_read =1;
// x1, y1, x2,y2
//2, read coord
while (1)
read(ts_fd, &ts, sizeof(ts));
if (ts.type == EV_ABS && ts.code == ABS_X && x_read == 0)
if (x1 == -1)
x1 = ts.value;
x2 = ts.value;
x_read = 1;
y_read = 0;
if (ts.type == EV_ABS && ts.code == ABS_Y && y_read == 0)
if (y1 == -1)
y1 = ts.value;
y2 = ts.value;
x_read = 0;
y_read = 1;
if (x_read && y_read)
break;
if (ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == KEY_RESERVED)
int num1 = x2-x1;
int num2 = y2-y1;
if (num1 > 0)
return 1;
else if (num1 < 0)
return 2;
if (num2 < 0)
return 3;
else if (num2 > 0)
return 4;
break;
/*
fin_left:手指左划后棋子移动及合并的方式
*/
void fin_left()
int i, j;//i为矩阵行下标,j为矩阵列下标
int value, save_zero;
for(i = 0; i < 4; i++)
value = 0;
save_zero= 0;
for(j = 0; j < 4 ; j++)
if (array[i][j] == 0)
continue;
if (value == 0)
value = array[i][j];
else
if (value == array[i][j])
array[i][save_zero++] = value * 2;
value = 0;
else
array[i][save_zero++] = value;
value = array[i][j];
array[i][j] = 0;
if (value != 0)
array[i][save_zero] = value;
//draw_matrix();
/*
fin_right:手指上划后棋子移动及合并的方式
*/
void fin_right()
int i, j;//i为矩阵行下标,j为矩阵列下标
int value;
int save_zero;
for (i = 0; i < 4; i++)
value = 0;
save_zero = 4 -1;
for (j = 4 - 1; j >= 0 ; j--)
if(array[i][j] == 0)
continue;
if(value == 0)
value = array[i][j];
else
if(value == array[i][j])
array[i][save_zero--] = 2 * value;
value = 0;
else
array[i][save_zero--] = value;
value = array[i][j];
array[i][j] = 0;
if(value != 0)
array[i][save_zero] = value;
/*
fin_up:手指上划后棋子移动及合并的方式
*/
void fin_up()
int i, j;//i为矩阵行下标,j为矩阵列下标
int value;
int save_zero;
for(j = 0; j < 4; j++)
value = 0;
save_zero= 0;
for(i = 0; i < 4 ; i++)
if(array[i][j] == 0)
continue;
if(value == 0)
value = array[i][j];
else
if(value == array[i][j])
array[save_zero++][j] =2 * value;
value = 0;
else
array[save_zero++][j] = value;
value = array[i][j];
array[i][j] = 0;
if(value != 0)
array[save_zero][j] = value;
//draw_matrix();
/*
fin_down:手指上划后棋子移动及合并的方式
*/
void fin_down()
int i, j;//i为矩阵行下标,j为矩阵列下标
int value;
int save_zero;
for(j = 0; j < 4; j++)
value = 0;
save_zero = 4 - 1;
for(i = 4 - 1; i >= 0 ; i--)
if(array[i][j] == 0)
continue;
if(value == 0)
value = array[i][j];
else
if(value == array[i][j])
array[save_zero--][j] = 2 * value;
value = 0;
else
array[save_zero--][j] = value;
value = array[i][j];
array[i][j] = 0;
if(value != 0)
array[save_zero][j] = value;
/*
change_matrix:根据手指滑动(direction),
变换棋盘矩阵
*/
int change_matrix()
int direction = get_finger_direction();
if (direction == 1)
fin_left();
else if (direction == 2)
fin_right();
else if (direction == 3)
fin_up();
else
fin_down();
/*
lcd_draw_point:在屏幕坐为(x, y)这个点,填充color
这个颜色值。
@x: x轴坐标
@y:y轴坐标
@color:要填充的辨色值
返回值:
无返回值。
*/
void lcd_draw_point(int x, int y, int color)
int *p = lcd_ptr;
if (x >= 0 && x < 800 && y>=0 && y < 480)
*(p +800*y + x) = color;
/*
lcd_draw_dect: 在屏幕上画一个矩形,并且用
color这种颜色填充该矩形。
@x0: 该矩形的左上角的那个点x轴坐标
@y0:该矩形的左上角的那个点y轴坐标
@w:该矩形的宽
@h:该矩形的高
@color:该矩形要填充的辨色值
返回值:
无返回值。
*/
void lcd_draw_dect(int x0, int y0, int w, int h, int color)
if (x0 < 0 || y0 < 0 || w < 0 || h <0)
return;
if ((x0 + w >800) || (y0+h) > 480)
return;
int x, y;
for (y = y0; y < y0 + h; y++)
for (x = x0; x < x0 + w; x++)
lcd_draw_point(x, y , color);
/*
draw_bmp_byname:把一张bmp图片显示在屏幕上特定的位置
@bmpfile:要显示的bmp图片的文件名
@x0: 在屏幕上显示的左上角顶点的x轴坐标
@y0: 在屏幕上显示的左上角顶点的y轴坐标
@w: 位图宽度
@h: 位图高度
返回值:
无返回值.
*/
void draw_bmp_byname(const char *bmpfile, int x0, int y0, int w, int h)
int fd;
int x, y;
fd = open(bmpfile, O_RDONLY);
if (fd == -1)
perror("open bmpfile error:");
return ;
char bmpdata[w*h*3];
lseek(fd, 54, SEEK_SET);
read(fd, bmpdata, w*h*3);
close(fd);
int i = 0;
for (y = 0; y < h; y++)
unsigned char r,g ,b;
int color;
for (x = 0; x < w; x++)
b = bmpdata[i++];
g = bmpdata[i++];
r = bmpdata[i++];
color = (r << 16) | (g << 8) | b;
lcd_draw_point(x0+ x, y0 + (h -1 - y) ,color);
/*
draw_matrix:把棋盘矩阵在屏幕上显示出来
*/
void draw_matrix()
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4;j++)
int x0, y0;
x0 = 185;//棋盘矩阵左上角那个点的x轴坐标
y0 = 25;//棋盘矩阵左上角那个点的y轴坐标
if (array[i][j] == 0)
lcd_draw_dect(x0+j*110, y0+i*110, 100, 100,
0xff8080);//如果此处元素的值为0,那么
//就显示
else
int f_index = get_bmp_files_index(array[i][j]);
draw_bmp_byname(bmp_files[f_index],
x0+j*110, y0+i*110,
100,100);
/*
init_matrix:初始化棋盘矩阵
在任意x个位置,填充x个数字(2,4,8)
*/
void init_matrix()
//规则x >= 1,x <= 3
int x = (random() % 3) + 1;
int i;
/*
step1:随机产生x个数字,并填充到棋盘矩阵中去
*/
for(i = 0; i < x; i++)
int pos = (random() % rectangle_get_zero_num()) + 1;
int s[] = 2, 4, 8, 2;
int s_i = (random() % 3);
rectangle_set_value(pos, s[s_i]);
/*
step 2: 绘制棋盘矩阵
*/
draw_matrix();
/*
rand1_matrix:移动之后随机产生一个数字填充到
任意一个0的位置上
*/
void rand_matrix()
int pos = (random() % rectangle_get_zero_num()) + 1;
int s[] = 2, 4, 8, 2;
int s_i = (random() % 4);
rectangle_set_value(pos, s[s_i]);
draw_matrix();
/*
get_finger_direction:获取手指在触摸屏上面的滑动方向
返回值:
MOVE_LEFT:手指向左移动
MOVE_RIGHT:手指向右移动
MOVE_UP:手指向上移动
MOVE_DOWN:手指向下移动
*/
int get_finger_direction()
int ret;
int fd = open("/dev/input/event0", O_RDWR);
if (fd == -1)
perror("open event failed:");
return -1;
struct input_event ev;
int x1 = -1; //在滑动过程中第一个点的x轴坐标
int x2; //在滑动过程中最后一个点的x轴坐标
int y1 = -1;//在滑动过程中第一个点的y轴坐标
int y2;//在滑动过程中最后一个点的y轴坐标
while (1)
ret = read(fd, &ev, sizeof(ev));
if(ret != sizeof(ev))
continue;
if (ev.type == EV_ABS && ev.code == ABS_X)//是x轴坐标
if (x1 == -1)//x1重来没有赋过值,那么肯定是第一个点
x1 = ev.value;
x2 = ev.value;
if (ev.type == EV_ABS && ev.code == ABS_Y)//是y轴坐标
if ( y1 == -1)//y1重来没有赋过值,那么肯定是第一个点
y1 = ev.value;
y2 = ev.value;
// if (ev.type == EV_ABS && ev.code == ABS_PRESSURE//手指弹起,再计算滑动方向
// && ev.value == 0)//触摸屏压力值为0, press up
if (ev.type == EV_KEY && ev.code == BTN_TOUCH)
if(ev.value==0)
int x_cz;//x轴的位移
int y_cz;//y轴的位移
int abs_x;
int abs_y;
x_cz = x2 - x1;
y_cz = y2 - y1;
abs_x = abs(x_cz);
abs_y = abs(y_cz);
if((x_cz > 30) && (abs_x > 2 * abs_y))
close(fd);
return 2;
else if((x_cz < -30) && (abs_x > 2 * abs_y))
close(fd);
return 1;
else if((y_cz > 30) && (abs_y > 2 * abs_x))
close(fd);
return 4;
else if((y_cz < -30) && (abs_y > 2 * abs_x))
close(fd);
return 3;
else
x1 = y1 = -1;
continue;
break;
close(fd);
/*
move_judge:判断是否还能移动
return value:
1 game over
0 continue
*/
int move_judge()
int i, j;
if(rectangle_get_zero_num() != 0)
return 0;
for(i = 0; i < 4; i++)
for(j = 0; j < 4 ; j++)
if (j != 4 -1)
if (array[i][j] == array[i][j+1])
return 0;
if (i != 4 - 1)
if (array[i][j] == array[i+1][j])
return 0;
return 1;
int main()
/*step 1: 打开屏幕*/
lcd_fd = open("/dev/fb0", O_RDWR);
if (lcd_fd == -1)
perror("open fb0 failed:");
return -1;
/*step 2: mmap*/
lcd_ptr = mmap(
NULL, //第一个参数,为映射后的内存地址,
//为NULL,表示让操作系统自行分配
800*480*4, //第二个参数,为映射的长度。
PROT_WRITE, //第三个参数,为映射内存区域的权限
MAP_SHARED, //第四个参数,为映射标志,shared
lcd_fd, //第五个参数,为文件描述符,表示您要
//映射哪个文件
0 //第六个参数为偏移量,表示您要从文件的
//哪个位置开始映射
);
lcd_draw_dect(0, 0, 800, 480, 0x0080ff);//清屏
srandom( time(NULL) ); //设置随机数种子,种子一样,产生的
//随机数是一样的
init_matrix();
while (game_over == 0) //游戏没结束
//用来保存原来的矩阵值
int matrix_v1[4][4];
int i, j, flag = 0;
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
matrix_v1[i][j] = array[i][j];
/*
step 1: 变换矩阵
*/
change_matrix();
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
if (matrix_v1[i][j] != array[i][j])
flag = 1;
i = j = 4;
if (flag)
rand_matrix();
draw_matrix();
else
draw_matrix();
game_over = move_judge();
printf("Game Over~~");
munmap(lcd_ptr, 800*480*4);
close(lcd_fd);
电子相册+2048小游戏
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include<linux/fb.h>
#include <math.h>
#include <errno.h>
#include <time.h>
#include<string.h>
int lcd_fd, ts_fd;
int *lcd_ptr;
int game_over;
int get_finger_direction();
struct bmp_header //14
unsigned char type[2];
unsigned long size;
unsigned short reserverd1;
unsigned short reserverd2;
unsigned long offbit;
__attribute__((packed)); //字节对齐
struct bmp_info //40
unsigned int size;
unsigned int width;
unsigned int height;
unsigned short planes;
unsigned short bitcount;
unsigned int bitcompression;
unsigned int sizeimg;
unsigned int xpelspermeter;
unsigned int ypelspermeter;
unsigned int biclrused;
unsigned int important;
__attribute__((packed)); //字节对齐
int dev_init()
//1,lcd device init
lcd_fd = open("/dev/fb0", O_RDWR);
if (lcd_fd == -1)
printf("open lcd device failed!\\n");
return -1;
//建立映射(走到镜子)
lcd_ptr = mmap(NULL, //内存映射的地址,默认填NULL,由系统来分配
800*480*4,//内存映射区的大小
PROT_READ | PROT_WRITE,//操作标志
MAP_SHARED,//共享标志
lcd_fd, //将要进行映射的文件的别名
0 //偏移量
);
if (lcd_ptr == MAP_FAILED)
printf("mmap failed!\\n");
return -2;
ts_fd = open("/dev/input/event0", O_RDONLY);
if (ts_fd == -1)
printf("open lcd device failed!\\n");
return -3;
return 0;
void dev_uninit()
munmap(lcd_ptr, 800*480*4);
close(lcd_fd);
close(ts_fd);
int show_bmp(int x, int y, const char* pathname)
//bmp file
int bmp_fd = open(pathname, O_RDWR);
if (bmp_fd == -1)
printf("open bmp file failed!\\n");
return -1;
struct bmp_header b_header;
struct bmp_info b_info;
read(bmp_fd, &b_header, 14);
read(bmp_fd, &b_info, 40);
int w, h;
w = b_info.width;
h = b_info.height;
//b) 读取图片颜色数据
char rgb_buf[w*h*3];
int tmp_buf[w*h];
int lcd_buf[800*480];
read(bmp_fd, rgb_buf, w*h*3);
// 24 --- > 32
int i, j;
int color;
for (i = 0; i < w*h; i++)
/*
color = rgb_buf[0]; //b
color = (rgb_buf[1] << 8); //g
color = (rgb_buf[2] << 16); //r
lcd_buf[0] = color;
color = rgb_buf[3]; //b
color = (rgb_buf[4] << 8); //g
color = (rgb_buf[5] << 16); //r
lcd_buf[1] = color;
*/
color = rgb_buf[3*i]; //b
color |= (rgb_buf[3*i+1] << 8); //g
color |= (rgb_buf[3*i+2] << 16); //r
tmp_buf[i] = color;
// up down
for (i = 0; i < h; i++)
for (j = 0; j < w; j++)
lcd_ptr[(i+y)*800+(j+x)] = tmp_buf[(h-1-i)*w+j];
//c) 将图片颜色数据写入到lcd屏幕
//write(lcd_fd, lcd_buf, 800*480*4);
close(bmp_fd);
return 0;
int get_xy(int *x, int *y)
struct input_event ts;
int x_read = 0, y_read =1;
// x1, y1, x2,y2
//2, read coord
while (1)
read(ts_fd, &ts, sizeof(ts));
if (ts.type == EV_ABS && ts.code == ABS_X && x_read == 0)
*x = ts.value;
x_read = 1;
y_read = 0;
if (ts.type == EV_ABS && ts.code == ABS_Y && y_read == 0)
*y = ts.value;
x_read = 0;
y_read = 1;
if (ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == KEY_RESERVED)
printf("手指已经离开\\n");
break;
return 0;
int log_on()
int xing_x[6] = 175,255,325,395,475,545;
char number[7] = 0;
char vc[7] = 0;
//获取随机数种子
srandom(time(NULL));
show_bmp(0, 0, "/pic/bmp/vc_login.bmp");
int i;
bzero(number, 6);
for (i = 0; i < 6; i++)
number[i] = (random()%10)+48;
printf("%d ", number[i]);
printf("\\n");
number[7] = '\\0';
printf("number : %s\\n", number);
int x, y;
int j = 0;
while (1)
get_xy(&x, &y);
if (y > 160 && y <= 275 && j < 6)
//1 2 3
if (x > 170 && x < 385)
vc[j] = '1';
// /pic/bmp/
show_bmp(xing_x[j], 62, "/pic/bmp/star.bmp");
j++;
printf("111\\n");
else if (x > 390 && x < 605)
vc[j] = '2';
show_bmp(xing_x[j], 62, "/pic/bmp/star.bmp");
j++;
printf("222\\n");
else if (x > 610 && x < 800)
vc[j] = '3';
show_bmp(xing_x[j], 62, "/pic/bmp/star.bmp");
j++;
printf("333\\n");
else if (y > 280 && y <= 395 && j < 6)
//4 5 6
if (x > 170 && x < 385)
vc[j] = '4';
// /pic/bmp/
show_bmp(xing_x[j], 62, "/pic/bmp/star.bmp");
j++;
printf("444\\n");
else if (x > 390 && x < 605)
vc[j] = '5';
show_bmp(xing_x[j], 62, "/pic/bmp/star.bmp");
j++;
printf("555\\n");
else if (x > 610 && x < 800)
vc[j] = '6';
show_bmp(xing_x[j], 62, "/pic/bmp/star.bmp");
j++;
printf("666\\n");
else if (y > 400 && y <= 465 && j < 6)
//7 8 9
if (x > 170 && x < 385)
vc[j] = '7';
// /pic/bmp/
show_bmp(xing_x[j], 62, "/pic/bmp/star.bmp");
j++;
printf("777\\n");
else if (x > 390 && x < 605)
vc[j] = '8';
show_bmp(xing_x[j], 62, "/pic/bmp/star.bmp");
j++;
printf("888\\n");
else if (x > 610 && x < 800)
vc[j] = '9';
show_bmp(xing_x[j], 62, "/pic/bmp/star.bmp");
j++;
printf("999\\n");
else if (y > 470 && y <= 615)
if (x > 170 && x < 385 && j > 0)
j--;
vc[j] = '0';
show_bmp(xing_x[j], 62, "/pic/bmp/white.bmp");
printf("delete!\\n");
else if (x > 390 && x < 605 && j < 6)
vc[j] = '0';
show_bmp(xing_x[j], 62, "/pic/bmp/star.bmp");
j++;
printf("000\\n");
else if (x > 610 && x < 800)
vc[7] = '\\0';
printf("%s\\n", vc);
if (strncmp(number, vc, 6) == 0)
show_bmp(200, 120, "/pic/bmp/correct.bmp");
sleep(1);
break;
else
show_bmp(200, 120, "/pic/bmp/error.bmp");
sleep(1);
j = 0;
bzero(vc, 6);
/* for (i = 0; i < 6; i++)
show_bmp(xing_x[i], 62, "/pic/bmp/white.bmp");
*/
show_bmp(0, 0, "/pic/bmp/vc_login.bmp");
continue;
int show_pic(void)
//1, device init
int rt = dev_init();
if (rt != 0)
printf("rt : %d\\n", rt);
return -1;
//2, oper
//gif
int i;
char buf[1024] = 0; //bzero
for (i = 0; i < 10; i++)
sprintf(buf, "/pic/gif/%d.bmp", i);
show_bmp(0, 0, buf);
usleep(100000);
//log on
log_on();
//main
//3, rmmod to device
dev_uninit();
return 0;
//数组初始化,将所有的图片保存在一个数组中
const char *bmp_files[] =
"/pic/bmp/digit_2.bmp", "/pic/bmp/digit_4.bmp",
"/pic/bmp/digit_8.bmp", "/pic/bmp/digit_16.bmp",
"/pic/bmp/digit_32.bmp", "/pic/bmp/digit_64.bmp",
"/pic/bmp/digit_128.bmp", "/pic/bmp/digit_256.bmp",
"/pic/bmp/digit_512.bmp", "/pic/bmp/digit_1024.bmp",
"/pic/bmp/digit_2048.bmp", "/pic/bmp/digit_4096.bmp",
"/pic/bmp/digit_8192.bmp", "/pic/bmp/digit_16384.bmp",
"/pic/bmp/digit_32768.bmp", "/pic/bmp/digit_65536.bmp"
;
//棋盘矩阵的初始化
int array[4][4] = 0;
//根据要显示的数字来返回对应的文件名的下标
int get_bmp_files_index(int x)
if (x == 2)
return 0;
else if (x == 4)
return 1;
else if (x == 8)
return 2;
else if (x == 16)
return 3;
else if (x == 32)
return 4;
else if (x == 64)
return 5;
else if (x == 128)
return 6;
else if (x == 256)
return 7;
else if (x == 512)
return 8;
else if (x == 1024)
return 9;
else if (x == 2048)
return 10;
else if (x == 4096)
return 11;
else if (x == 8192)
return 12;
else if (x == 16384)
return 13;
else if (x == 32768)
return 14;
else if (x == 65536)
return 15;
return -1;
//求棋盘矩阵里面有多少个0
int rectangle_get_zero_num()
int i, j, count = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (array[i][j] == 0)
count++;
return count;
int rectangle_set_value(int z, int value)
int i, j, count = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (array[i][j] == 0)
count++;
if (count == z)
array[i][j] = value;
return 0;
//触摸屏滑屏算法实现
int get_xy_v2()
struct input_event ts;
int x1 = -1, y1 = -1;
int x2, y2;
int x_read = 0, y_read =1;
// x1, y1, x2,y2
//2, read coord
while (1)
read(ts_fd, &ts, sizeof(ts));
if (ts.type == EV_ABS && ts.code == ABS_X && x_read == 0)
if (x1 == -1)
x1 = ts.value;
x2 = ts.value;
x_read = 1;
y_read = 0;
if (ts.type == EV_ABS && ts.code == ABS_Y && y_read == 0)
if (y1 == -1)
y1 = ts.value;
y2 = ts.value;
x_read = 0;
y_read = 1;
if (x_read && y_read)
break;
if (ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == KEY_RESERVED)
int num1 = x2-x1;
int num2 = y2-y1;
if (num1 > 0)
return 1;
else if (num1 < 0)
return 2;
if (num2 < 0)
return 3;
else if (num2 > 0)
return 4;
break;
/*
fin_left:手指左划后棋子移动及合并的方式
*/
void fin_left()
int i, j;//i为矩阵行下标,j为矩阵列下标
int value, save_zero;
for(i = 0; i < 4; i++)
value = 0;
save_zero= 0;
for(j = 0; j < 4 ; j++)
if (array[i][j] == 0)
continue;
if (value == 0)
value = array[i][j];
else
if (value == array[i][j])
array[i][save_zero++] = value * 2;
value = 0;
else
array[i][save_zero++] = value;
value = array[i][j];
array[i][j] = 0;
if (value != 0)
array[i][save_zero] = value;
//draw_matrix();
/*
fin_right:手指上划后棋子移动及合并的方式
*/
void fin_right()
int i, j;//i为矩阵行下标,j为矩阵列下标
int value;
int save_zero;
for (i = 0; i < 4; i++)
value = 0;
save_zero = 4 -1;
for (j = 4 - 1; j >= 0 ; j--)
if(array[i][j] == 0)
continue;
if(value == 0)
value = array[i][j];
else
if(value == array[i][j])
array[i][save_zero--] = 2 * value;
value = 0;
else
array[i][save_zero--] = value;
value = array[i][j];
array[i][j] = 0;
if(value != 0)
array[i][save_zero] = value;
/*
fin_up:手指上划后棋子移动及合并的方式
*/
void fin_up()
int i, j;//i为矩阵行下标,j为矩阵列下标
int value;
int save_zero;
for(j = 0; j < 4; j++)
value = 0;
save_zero= 0;
for(i = 0; i < 4 ; i++)
if(array[i][j] == 0)
continue;
if(value == 0)
value = array[i][j];
else
if(value == array[i][j])
array[save_zero++][j] =2 * value;
value = 0;
else
array[save_zero++][j] = value;
value = array[i][j];
array[i][j] = 0;
if(value != 0)
array[save_zero][j] = value;
//draw_matrix();
/*
fin_down:手指上划后棋子移动及合并的方式
*/
void fin_down()
int i, j;//i为矩阵行下标,j为矩阵列下标
int value;
int save_zero;
for(j = 0; j < 4; j++)
value = 0;
save_zero = 4 - 1;
for(i = 4 - 1; i >= 0 ; i--)
if(array[i][j] == 0)
continue;
if(value == 0)
value = array[i][j];
else
if(value == array[i][j])
array[save_zero--][j] = 2 * value;
value = 0;
else
array[save_zero--][j] = value;
value = array[i][j];
array[i][j] = 0;
if(value != 0)
array[save_zero][j] = value;
/*
change_matrix:根据手指滑动(direction),
变换棋盘矩阵
*/
int change_matrix()
int direction = get_finger_direction();
if (direction == 1)
fin_left();
else if (direction == 2)
fin_right();
else if (direction == 3)
fin_up();
else
fin_down();
/*
lcd_draw_point:在屏幕坐为(x, y)这个点,填充color
这个颜色值。
@x: x轴坐标
@y:y轴坐标
@color:要填充的辨色值
返回值:
无返回值。
*/
void lcd_draw_point(int x, int y, int color)
int *p = lcd_ptr;
if (x >= 0 && x < 800 && y>=0 && y < 480)
*(p +800*y + x) = color;
/*
lcd_draw_dect: 在屏幕上画一个矩形,并且用
color这种颜色填充该矩形。
@x0: 该矩形的左上角的那个点x轴坐标
@y0:该矩形的左上角的那个点y轴坐标
@w:该矩形的宽
@h:该矩形的高
@color:该矩形要填充的辨色值
返回值:
无返回值。
*/
void lcd_draw_dect(int x0, int y0, int w, int h, int color)
if (x0 < 0 || y0 < 0 || w < 0 || h <0)
return;
if ((x0 + w >800) || (y0+h) > 480)
return;
int x, y;
for (y = y0; y < y0 + h; y++)
for (x = x0; x < x0 + w; x++)
lcd_draw_point(x, y , color);
/*
draw_bmp_byname:把一张bmp图片显示在屏幕上特定的位置
@bmpfile:要显示的bmp图片的文件名
@x0: 在屏幕上显示的左上角顶点的x轴坐标
@y0: 在屏幕上显示的左上角顶点的y轴坐标
@w: 位图宽度
@h: 位图高度
返回值:
无返回值.
*/
void draw_bmp_byname(const char *bmpfile, int x0, int y0, int w, int h)
int fd;
int x, y;
fd = open(bmpfile, O_RDONLY);
if (fd == -1)
perror("open bmpfile error:");
return ;
char bmpdata[w*h*3];
lseek(fd, 54, SEEK_SET);
read(fd, bmpdata, w*h*3);
close(fd);
int i = 0;
for (y = 0; y < h; y++)
unsigned char r,g ,b;
int color;
for (x = 0; x < w; x++)
b = bmpdata[i++];
g = bmpdata[i++];
r = bmpdata[i++];
color = (r << 16) | (g << 8) | b;
lcd_draw_point(x0+ x, y0 + (h -1 - y) ,color);
/*
draw_matrix:把棋盘矩阵在屏幕上显示出来
*/
void draw_matrix()
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4;j++)
int x0, y0;
x0 = 185;//棋盘矩阵左上角那个点的x轴坐标
y0 = 25;//棋盘矩阵左上角那个点的y轴坐标
if (array[i][j] == 0)
lcd_draw_dect(x0+j*110, y0+i*110, 100, 100,
0xff8080);//如果此处元素的值为0,那么
//就显示
else
int f_index = get_bmp_files_index(array[i][j]);
draw_bmp_byname(bmp_files[f_index],
x0+j*110, y0+i*110,
100,100);
/*
init_matrix:初始化棋盘矩阵
在任意x个位置,填充x个数字(2,4,8)
*/
void init_matrix()
//规则x >= 1,x <= 3
int x = (random() % 3) + 1;
int i;
/*
step1:随机产生x个数字,并填充到棋盘矩阵中去
*/
for(i = 0; i < x; i++)
int pos = (random() % rectangle_get_zero_num()) + 1;
int s[] = 2, 4, 8, 2;
int s_i = (random() % 3);
rectangle_set_value(pos, s[s_i]);
/*
step 2: 绘制棋盘矩阵
*/
draw_matrix();
/以上是关于粤嵌gec6818LED屏幕上画图 太极图 图片显示 电子相册 2048小游戏 实现识别触摸坐标的识别 电子自助点餐设计等项目的主要内容,如果未能解决你的问题,请参考以下文章
粤嵌GEC6818,基于LVGL和mplayer的音视频播放器
GEC6818开发板JPG图像显示,科大讯飞离线语音识别包Linux_aitalk_exp1227_1398d7c6运行demo程序,开发板实现录音