《智能家居系统》2

bmp图片显示

一、文件操作的函数

   #include <unistd.h>

   off_t lseek(int fildes, off_t offset, int whence);
	参数:
	int fildes ---- 文件描述符
	off_t offset ---- 移动的字节数
	int whence ---- 移动的开始位置(移动的基准点)
		SEEK_SET
		SEEK_CUR
		SEEK_END
	返回值:
	Upon successful completion, the resulting offset, as measured in  bytes
   from  the beginning of the file, shall be returned. Otherwise, ?1 shall
   be returned, errno shall be set to indicate the  error,  and  the  file
   offset shall remain unchanged.

思考:
使用lseek函数来求个文件的大小。

注意:
局部变量存放在stack中,而在嵌入式平台上,stack是有一定的限制的。如果局部变量超出了stack的范围限制,程序在执行的时候,就会出现段错误(segment fault)。

局部变量和全局变量的区别?
生命周期、作用域、存放在内存的范围????

二、如何在液晶屏上显示一个图片

显示一张bmp格式的图片。
bmp图片的特点:
bmp图是没有经过压缩的位图,所以不需要解压
bmp图有54个字节的头文件,这个头文件不是有效的图片颜色数据,而是如分辨率等参数—>bmp图片的格式说明
bmp图片每个像素点有3个字节,分别是红绿蓝。

所以在做bmp显示的时候,需要做像素点数据的转换。将一个像素点三个字节的数据转换成一个像素点4个字节的数据。

char bmp_buf[8004803] -----> int lcd_buf[800*480]

BMP图片的处理过程

一切皆文件

1、打开图片文件

int fd_bmp;
fd_bmp = oepn("./test.bmp", O_RDONLY); // ./----当前目录(相对路径)
或者
fd_bmp = open("/test/test.bmp", O_RDONLY); //绝对路径
if(fd_bmp == -1)
{
perror(“open bmp err”);
return -1;
}

2、移动文件指针,到bmp图片的数据区

lseek(fd_bmp, 54, SEEK_SET);

思考:
char bmp_head[54];
read(fd_bmp,bmp_head,54);

3、读取bmp图片的有效数据

#include <unistd.h>
ssize_t read(int fildes, void *buf, size_t nbyte);

例:
char bmp_buf[8004803];
read(fd_bmp, bmp_buf,8004803);//sizeof(bmp_buf)

4、关闭图片文件

close(fd_bmp);

5、将图片的数据转换成显存的数据

int lcd_buf[800480];
char bmp_buf[800
480*3];

for(int i=0;i<800480;i++)
{
lcd_buf[i] = (bmp_buf[i
3+2]<<16)+(bmp_buf[i3+1]<<8)+(bmp_buf[i3]<<0);
}

三、应用


[root@GEC6818 ~/test]#mount
rootfs on / type rootfs (rw)
/dev/root on / type ext4 (rw,sync,relatime,data=ordered)
devtmpfs on /dev type devtmpfs (rw,relatime,size=405396k,nr_inodes=101349,mode=755)
proc on /proc type proc (rw,relatime)
devpts on /dev/pts type devpts (rw,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /dev/shm type tmpfs (rw,relatime,mode=777)
tmpfs on /tmp type tmpfs (rw,relatime)
tmpfs on /run type tmpfs (rw,nosuid,nodev,relatime,mode=755)
sysfs on /sys type sysfs (rw,relatime)
sda1 on /mnt/udisk type vfat (rw,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro)
------>U盘挂载到/mnt/udisk目录下。

[root@GEC6818 ~/test]#df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root               492.1M    484.8M      7.3M  99% /
devtmpfs                395.9M         0    395.9M   0% /dev
tmpfs                   404.0M         0    404.0M   0% /dev/shm
tmpfs                   404.0M         0    404.0M   0% /tmp
tmpfs                   404.0M         0    404.0M   0% /run
sda1                      1.7G    212.9M      1.5G  12% /mnt/udisk


四、触摸屏显示颜色


#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
//arm-linux-gcc -o test test.c -std=c99
int main(int argc, char const *argv[]) // ./test hello world
{	
	int lp=0;
	while(1)
	{
		int lcd_buf[800*480];
		for ( int i = 0; i < 800*480; i++)/*初始化颜色数组*/
		{
			switch(lp)
			{
				case 1 : 
						lcd_buf[i]=0x00FF0000; // 红
						break;
				case 2 : 
						lcd_buf[i]=0x0000FF00; //绿 
						break;
				case 3 : 
						lcd_buf[i]=0x000000FF; //蓝
						break;
				case 4 : 
						lcd_buf[i]=0x00000000; //黑
						break;
				default : 
						lcd_buf[i]=0x00FFFFFF; //白
						break;
			}
		}

		int fd_lcd;

		fd_lcd= open("/dev/fb0",O_WRONLY);
		if (fd_lcd== -1)
		{
			perror("open /dev/fb0");
			return -1 ;
		}
		write(fd_lcd,lcd_buf,800*480*4); //sizeof(lcd_buf)

		close(fd_lcd);
		if (lp==4)
		{
			lp=0;
		}
		else lp++;
		sleep(3);
	}

	return 0;
}

图片显示
按钮的图片
获取触摸屏的xy坐标
struct ts_xy get_ts(void)
当获取了之后
判断xy去打开
显示单色,显示图片,相册

五、触摸屏的访问流程

1、打开触摸屏
应用程序访问驱动程序,是通过设备文件来访问的,如:LCD----/dev/fb0

触摸屏的设备:

[root@GEC6818 /dev]#ls /dev/input/ -l
total 0
crw-rw----    1 root     root       13,  64 Jan  1  1970 event0
crw-rw----    1 root     root       13,  65 Jan  1  1970 event1
crw-rw----    1 root     root       13,  66 Jan  1  1970 event2
crw-rw----    1 root     root       13,  67 Jan  1  1970 event3
crw-rw----    1 root     root       13,  68 Jan  1  1970 event4
crw-rw----    1 root     root       13,  63 Jan  1  1970 mice
crw-rw----    1 root     root       13,  32 Jan  1  1970 mouse0
crw-rw----    1 root     root       13,  33 Jan  1  1970 mouse1


[root@GEC6818 /dev]#cat /proc/bus/input/devices 
I: Bus=0018 Vendor=12fa Product=2143 Version=0100
N: Name="ft5x0x"   ------>触摸屏控制器芯片
P: Phys=input(mt)
S: Sysfs=/devices/virtual/input/input0
U: Uniq=
H: Handlers=mouse0 event0 
B: PROP=0
B: EV=b
B: KEY=400 0 0 0 0 0 0 0 0 0 0
B: ABS=1000003


[root@GEC6818 /]#cat /dev/input/event0

所以,设备文件:/dev/input/event0

例:

int fd_ts;
fd_ts = open("/dev/input/event0", O_RDONLY);
fi(fd_ts < 0)
{
	perror("open ts error");
	return -1;
}

2、读取触摸屏数据
从触摸屏中读出的数据是一个固定的结构体。我们读到结构体后,需要对数据进行分析。
这个结构体的原型不需要我们定义,但是我们需要包含一个定义这个结构体原型的头文件


#include <linux/input.h>

 23 struct input_event {
 24         struct timeval time;
 25         __u16 type;
 26         __u16 code;
 27         __s32 value;
 28 };

例:

	struct input_event gec6818_ts;
	read(fd_ts, &gec6818_ts, sizeof(struct input_event));

3、分析触摸屏数据—得到触摸屏的状态和坐标

vi /usr/include/linux/input.h

 23 struct input_event {
 24         struct timeval time;
 25         __u16 type;
 26         __u16 code;
 27         __s32 value;
 28 };

成员:
struct timeval time; ---- 输入事件的时间
__u16 type; ---->输入事件的类型—输入设备的类型:键盘、鼠标、触摸屏、…
#define EV_SYN 0x00 ---->同步信号
#define EV_KEY 0x01 ---->按键、键盘
#define EV_REL 0x02 ---->相对坐标(鼠标)
#define EV_ABS 0x03 ---->绝对坐标(触摸屏)

__u16 code;----->输入事件的编码。type不同,这code就会不同。
	如果type == EV_ABS,则code是触摸屏的坐标轴。
		#define ABS_X           0x00 ---->X轴
		#define ABS_Y           0x01 ---->Y轴

__s32 value;----->输入事件的值
	如果type == EV_ABS,code==ABS_X,则value是x轴的坐标。

点击一下触摸屏的左上角,得到的数据:

type=3, code=0, value=100 ---->触摸屏x轴坐标值是100
type=3, code=1, value=70 ---->触摸屏y轴坐标
值是70

type=1, code=330, value=1 ---->触摸屏按下 #define BTN_TOUCH 0x14a
type=0, code=0, value=0
type=1, code=330, value=0 ---->触摸屏松开
type=0, code=0, value=0

4、关闭触摸屏
close(fd_ts);

===================================================================================================

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WeSiGJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值