粤嵌——电子相册代码实现

这个代码是实现了上下左右滑动功能。使用的板子是800*480大小的

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define UP    1
#define DOWN  2
#define LEFT  3
#define RIGHT 4

#define RGB_SIZE 800*480*3
#define LCD_SIZE 800*480
int lcd_fd;
int main()
{
	lcdinit();
	char *p[8]= {"/home/czz/00.bmp","/home/czz/1.bmp","/home/czz/2.bmp","/home/czz/3.bmp","/home/czz/4.bmp","/home/czz/5.bmp",
	             "/home/czz/7.bmp","/home/czz/88.bmp"
	            };
	int i=0;
	while(1)
	{
		display(p[i]);
		int dirt=GetDirection();

		if(dirt==1) //up
		{
			i=(i+7)%8;
		}
		else if(dirt==2)  //down
		{
			i=(i+1)%8;
		}
		else if(dirt==3)  //left
		{
			i=(i+7)%8;
		}
		else
		{
			i=(i+1)%8;
		}
		display(p[i]);
	}
	lcdclose();
	return 0;
}
int lcdinit()
{
	lcd_fd = open("/dev/fb0", O_RDWR);
	if (lcd_fd == -1)
	{
		printf("Open lcd failed!!\n");
		return -1;
	}
}
int lcdclose()
{
	close(lcd_fd);
}
int display(char *p)
{

	lseek(lcd_fd,0,SEEK_SET);
	int bmp_fd = open(p, O_RDWR);
	if (bmp_fd == -1)
	{
		printf("Open bmp filed\n");
		return -1;
	}


	off_t offset = lseek(bmp_fd, 54, SEEK_SET);
	if (offset == -1)
	{
		printf("Offset failed!\n");
		return -1;
	}
	char bmp_buf[RGB_SIZE];
	size_t re_ret = read(bmp_fd, bmp_buf, RGB_SIZE);
	if (re_ret == -1)
	{
		printf("Read failed!\n");
		return -1;
	}

	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;
	}


	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];
		}
	}


	size_t wr_fd = write(lcd_fd, fli_buf, LCD_SIZE*4);
	if (wr_fd == -1)
	{
		printf("Write data into lcd failed!\n");
		return -1;
	}

	close(bmp_fd);


	return 0;
}
int GetDirection()
{
	int fd = open("/dev/input/event0",O_RDWR);
	if(fd == -1)
	{
		printf("Open Error!\n");
		return -1;
	}
	struct input_event event0;
	int res = 0;
	int x_start = -1;
	int y_start = -1;
	int x_end = -1;
	int y_end = -1;

	while(1)
	{
		res = read(fd,&event0,sizeof(event0));
		if(res != sizeof(event0))
		{
			continue;
		}
		if(event0.type == EV_KEY && event0.code == BTN_TOUCH && event0.value == 0)
		{
			break;
		}
		if(event0.type == EV_ABS)
		{
			if(event0.code == ABS_PRESSURE && event0.value == 0)
			{
				break;
			}
			if(event0.code == ABS_X)
			{
				if(x_start == -1)
				{
					x_start = event0.value;
				}
				x_end = event0.value;
			}
			if(event0.code == ABS_Y)
			{
				if(y_start == -1)
				{
					y_start = event0.value;
				}
				y_end = event0.value;
			}
		}
	}
	if(abs(x_end - x_start) > abs(y_end - y_start))
	{
		if(x_end - x_start > 0)
		{
			return RIGHT;
		}
		else
			return LEFT;
	}
	if(abs(x_end - x_start) < abs(y_end - y_start))
	{
		if(y_end - y_start > 0)
		{
			return DOWN;
		}
		else
			return UP;
	}
}

你可能感兴趣的:(随笔,嵌入式)