You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
5.1 KiB
C

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
//open lseek
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//close write read lseek
#include <unistd.h>
//mmap
#include <sys/mman.h>
//输入子系统头文件
#include <linux/input.h>
//函数申明
//打开触摸屏设备文件
void ts_open();
//打开模拟器LCD屏设备文件
void lcd_open();
//关闭触摸屏设备文件
void ts_close();
//关闭模拟器LCD屏设备文件
void lcd_close();
//获取坐标
void get_xy(int *x,int *y);
//图片显示,显示某个大小某一个位置某张图片
int bmp_show(char *pic_path,int x,int y,int w,int h);
//功能一:相册
void photo();
//全局变量
int ts_fd;
int lcd_fd;
unsigned *FB;
int pos_x,pos_y;
int main(int argc, char const *argv[])
{
/****************一、打开设备文件*******************/
ts_open();
lcd_open();
/****************二、程序逻辑代码*******************/
bmp_show("./main.bmp",0,0,800,480);
while (1)
{
get_xy(&pos_x,&pos_y);
if(pos_x > 84 && pos_x < 296 && pos_y > 213 && pos_y < 424) //坐标 68 278 218 428
{
printf("进入相册功能\n");
photo();
}
// printf("(%d,%d)\n",pos_x,pos_y);
}
/****************三、关闭设备文件*******************/
ts_close();
lcd_close();
return 0;
}
//打开触摸屏设备文件
void ts_open()
{
// 1.打开模拟器的触摸屏设备文件("/dev/ubuntu_event"
ts_fd = open("/dev/ubuntu_event",O_RDWR);
if(ts_fd == -1)
{
perror("打开模拟器的触摸屏设备文件失败");
}
}
//打开模拟器LCD屏设备文件
void lcd_open()
{
// 1.打开LCD屏幕设备文件"/dev/fb0"
lcd_fd=open("/dev/ubuntu_lcd",O_RDWR);
if(lcd_fd == -1)
{
perror("打开LCD屏失败");
}
//映射内存
FB = mmap(NULL,4*800*480,PROT_READ|PROT_WRITE,MAP_SHARED,lcd_fd,0);
}
//关闭触摸屏设备文件
void ts_close()
{
// 3.关闭模拟器的触摸屏设备文件
close(ts_fd);
}
//关闭模拟器LCD屏设备文件
void lcd_close()
{
//解除映射
munmap(FB,4*800*480);
// 6.关闭LCD设备文件的文件描述符
close(lcd_fd);
}
//获取坐标
void get_xy(int *x,int *y)
{
// 2.读取坐标信息,存储在信息结构体中
struct input_event ts_buf;
int x_read = 0;
int y_read = 0;
while (1)
{
//产生堵塞获取xy的值
read(ts_fd,&ts_buf,sizeof(ts_buf));
if(ts_buf.type == EV_ABS && ts_buf.code == ABS_X)
{
*x = ts_buf.value;
x_read = 1;
}
else if (ts_buf.type == EV_ABS && ts_buf.code == ABS_Y && y_read == 0)
{
*y = ts_buf.value;
y_read = 1;
}
else if(x_read == 1 && y_read == 1)
{
break;
}
}
}
//图片显示,显示某个大小某一个位置某张图片
int bmp_show(char *pic_path,int x,int y,int w,int h)
{
//映射新的显存
unsigned *new_FB = FB + (x+y*800);
// 2.打开BMP文件
int bmp_fd = open(pic_path,O_RDWR);
if (bmp_fd == -1)
{
perror("打开图片失败");
return -1;
}
// 3.读取BMP中的RGB的数据
//将图片文件头和信息头读完54个字节
unsigned char bmp_info[54]={0};
read(bmp_fd,bmp_info,sizeof(bmp_info));
//读取BMP中的RGB数据
unsigned char bmp_buf[w*h*3];
read(bmp_fd,bmp_buf,sizeof(bmp_buf));
// 4.将aRGB颜色写入到LCD屏中
//将3字节RGB数据转换为4字节aRGB数据
unsigned int lcd_buf[w*h];
for(int i = 0;i<w*h;i++)
{
//像素 蓝分量 绿分量 红分量 灰度
lcd_buf[i] = bmp_buf[i*3] << 0 | bmp_buf[i*3+1] << 8 | bmp_buf[i*3+2] << 16 | 0x00 << 24;
}
int n ,m;
//图片上下颠倒
unsigned int lcd_buf1[w*h];
for(m=0;m<h;m++)
{
for(n=0;n<w;n++)
{
lcd_buf1[n+m*w] = lcd_buf[n+(h-m)*w];
}
}
//显示图片
for(m=0;m<h;m++)
{
for(n=0;n<w;n++)
{
*(new_FB+n+m*800) = lcd_buf1[n+m*w];
}
}
// 5.关闭BMP文件
close(bmp_fd);
return 0;
}
//功能一:相册
void photo()
{
char *pic[3]={"./1.bmp","./2.bmp","./3.bmp"};
bmp_show("./photo.bmp",0,0,800,480);
int i = -1;
while (1)
{
get_xy(&pos_x,&pos_y);
if(pos_x > 0 && pos_x < 80 && pos_y > 0 && pos_y < 480)
{
printf("显示上一张图片\n");
i--;
if(i < 0)
{
i=2;
}
bmp_show(pic[i],80,0,640,480);
}
else if(pos_x > 720 && pos_x < 800 && pos_y > 0 && pos_y < 480)
{
printf("显示下一张图片\n");
i++;
if(i>2)
{
i=0;
}
bmp_show(pic[i],80,0,640,480);
}
else if(pos_x > 300 && pos_x < 510 && pos_y > 165 && pos_y < 365)
{
printf("返回主界面\n");
bmp_show("./main.bmp",0,0,800,480);
break;
}
}
}