当前位置:   article > 正文

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

粤嵌电子相册代码

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

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#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;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/47598
推荐阅读
相关标签
  

闽ICP备14008679号