album.h
3.21
#ifndef __ALBUM_H__
#define __ALBUM_H__
typedef char* Elemtype;
struct node
{
Elemtype data;
struct node *next;
struct node *prev;
};
typedef struct node Node;
struct head
{
Node *first;
Node *last;
int n;
};
typedef struct head Head;
void printfl(Head *l);
void tailinsert(Head* l,Elemtype x);
Head* creathead();
int opendirs(const char* path,Head* l);
int lcdInit(const char * pathname);
void lcdClose(int fd);
void displayPoint(int x,int y,int color);
void lcdClear(int color);
void displayBmp(const char * bmpPN,int x0,int y0);
void displayChar(const unsigned char *name,int x0,int y0);
int getDirection();
#endif
main.c
#include <stdio.h>
#include "album.h"
Node *pp;
char *h;
int main()
{
Head* l = creathead();
printf("请输入路径: \n");
char a[100];
scanf("%s",a);
opendirs(a,l);
printfl(l);
int fd = lcdInit("/dev/fb0");
pp =l->first;
int m = 0,n =0;
while(1)
{
printf("flag2\n");
lcdClear(0x00);
printf("flag3\n");
h = pp->data;
displayBmp(h,m,n);
printf("----%s\n",pp->data);
getDirection();
}
lcdClose(fd);
}
album.c
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "album.h"
#include <linux/input.h>
int * p;
extern Node *pp;
int opendirs(const char* path,Head* l)
{
DIR *r = opendir(path);
if(r == NULL)
{
perror(" ");
return -1;
}
struct dirent *q;
struct stat st;
int m;
int len;
char *flag;
while(1)
{
q = readdir(r);
if(q == NULL)
{
break;
}
if(strcmp(q->d_name,".")==0 ||strcmp(q->d_name,"..")==0)
{
continue;
}
char pathname[100];
strncpy(pathname,path,100);
strcat(pathname,"/");
strcat(pathname,q->d_name);
m = stat(pathname,&st);
if(m == -1)
{
continue;
}
if(S_ISREG(st.st_mode))
{
char *p = (char *)malloc(100);
strncpy(p,pathname,100);
//printf("----%s--------\n",p);
len = strlen(p);
flag = p + len - 4;
if(strcmp(flag,".bmp") == 0)
{
tailinsert(l,p);
}
}
if(S_ISDIR(st.st_mode))
{
opendirs(pathname,l);
}
}
}
Head* creathead()
{
Head *l = (Head *)malloc(sizeof(Head));
l->first = l->last =NULL;
l->n = 0;
return l;
}
void tailinsert(Head* l,Elemtype x)
{
if(l == NULL)
{
return;
}
Node * p = (Node *)malloc(sizeof(Node));
p->next = p->prev = NULL;
p->data = x;
//printf("%s\n",p->data);
if(l->n == 0)
{
l->first = l->last = p;
//printf("%s----\n",l->first->data);
}
else
{
l->last->next = p;
p->prev = l->last;
l->last = p;
l->first->prev =l->last;
l->last->next = l->first;
//printf("%s\n",l->last->data);
}
l->n++;
}
void printfl(Head *l)
{
Node *p = l->first;
//printf("%s\n",p->data);
int i = l->n;
while(i)
{
printf("%s\n",p->data);
//printf("1111\n");
p = p->next;
i--;
}
}
int lcdInit(const char * pathname)
{
int fd = open(pathname,O_RDWR);
if(fd == -1)
{
perror("open lcd failed");
return -1;
}
p = (int *)mmap(NULL,//表示由操作系统帮我们分配一段内存,用于映射
480*800*4,//文件长度,有480*800个像素点,一个像素点4个字节
PROT_READ | PROT_WRITE, //可读可写权限
MAP_SHARED,//共享
fd,//要映射的文件的文件描述符
0//偏移量为0,表示从该文件的第1个字节开始映射
);
if(p == NULL)
{
perror("mmap failed");
return -1;
}
return fd;
}
void lcdClose(int fd)
{
munmap(p,480*800*4);
close(fd);
}
void displayPoint(int x,int y,int color)
{
if(y>=0 && y<480 && x>=0 && x<800)
*(p+800*y+x)=color;
}
void lcdClear(int color)
{
int x,y;
for(y=0;y<480;y++)
{
for(x=0;x<800;x++)
{
displayPoint(x,y,color);
}
}
}
//111111111111111111
void displayBmp(const char * bmpPN,int x0,int y0)
{
//打开bmp图片
int fd = open(bmpPN,O_RDONLY);
if(fd == -1)
{
perror("");
return ;
}
//获取图片的宽度和高度
lseek(fd,0x12,SEEK_SET);
int width;
read(fd,&width,4);
x0 = (800-width)/2;
int height;
read(fd,&height,4);
y0 = (480-height)/2;
printf("width=%d,height=%d\n",width,height);
//获取图片像素数组数据
unsigned char buf[width*height*3];
lseek(fd,54,SEEK_SET);//跳过文件头
read(fd,buf,width*height*3);
//开始显示
int x,y;
int color;
int n = 0;
for(y=0;y<height;y++)
{
for(x=0;x<width;x++)
{
color = buf[n++]|buf[n++]<<8|buf[n++]<<16;
//displayPoint(x,y,color);//上下颠倒
displayPoint(x+x0,height-1-y+y0,color);
}
}
printf("flag1\n");
close(fd);
}
void displayChar(const unsigned char *name,int x0,int y0)
{
int i,j;
int x,y,color;
for(i=0;i<24*24/8;i++)//依次把数组中的元素拿出来进行判断
{
//判断 li[i]
//依次判断 li[i] 8个bit ,并且要从左到右 bit7 ~ bit0
for(j=7;j>=0;j--)
{
if(name[i]&(1<<j))
{
x = (i%3)*8+(7-j);
y = i/3;
color = 0xaabbcc;
displayPoint(x+x0,y+y0,color);
}
}
}
}
int getDirection()
{
printf("进入地址:%s\n",pp->data);
int fd = open("/dev/input/event0",O_RDONLY);
if(fd == -1)
{
perror("");
return -1;
}
int x1=-1,y1=-1;//用来保存起点坐标
int x2,y2;//用来保存终端坐标
struct input_event ev;
//1,得到起点坐标和终点坐标
while(1)
{
read(fd,&ev,sizeof(ev));
printf("type=%d,code=%d,value=%d\n",ev.type,ev.code,ev.value);
if(ev.type==3&&ev.code==0)
{
x2 = ev.value;
}
if(ev.type==3&&ev.code==0 &&x1==-1)
{
x1 = ev.value;
printf("x1 = %d\n",x1);
}
if(ev.type==3&&ev.code==1)
{
y2 = ev.value;
}
if(ev.type==3&&ev.code==1 &&y1==-1)
{
y1 = ev.value;
printf("x2 = %d\n",x2);
}
if(ev.type==1&&ev.code==0x14a&&ev.value==0)
{
break;
}
}
printf("胡波好样的\n");
printf("x1 = %d\n",x1);
printf("x2 = %d\n",x2);
if((x1-x2)>0)//显示下一张
{
//*pp = *pp->next;
pp = pp->next;
printf("123 :%s",pp->data);
printf("杨洋好样的\n");
}
else if((x1-x2)<0)//显示上一张
{
//*pp = *pp->prev;
pp = pp->prev;
}
}