#include<stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include<stdlib.h>
#include<string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <sys/mman.h>
#include <jpeglib.h>
char Cover
[]="04.bmp";
char Cover2
[]="1.bmp";
char Cover3
[]="2.bmp";
char Cover4
[]="3.bmp";
int *plcd
= NULL;
int x
;
typedef char Elemtype
;
struct node
{
Elemtype
*data
;
int time
;
struct node
*next
,*prev
;
};
typedef struct linkedlist
{
struct node
*first
;
struct node
*last
;
int num
;
}LinkedList
;
LinkedList
*create_doublelist_head()
{
LinkedList
*l
= (LinkedList
*)malloc(sizeof(LinkedList
));
l
->first
= NULL;
l
->last
= NULL;
l
->num
= 0;
return l
;
}
struct node
*print_double_list_display01(struct node
*q
);
void Add_Elem_To_Double_List(LinkedList
*l
,Elemtype
*x
,int TIME
)
{
struct node
*p
= (struct node
*)malloc(sizeof(struct node
));
p
->data
= x
;
p
->next
= NULL;
p
->prev
= NULL;
p
->time
= TIME
;
if(l
->first
== NULL )
{
l
->first
= p
;
l
->last
= p
;
l
->num
=1;
}else
{
struct node
*pk
= l
->first
;
while(pk
==l
->last
)
{
if(pk
->time
> p
->time
)
{
break;
}
pk
=pk
->next
;
}
if(pk
== NULL)
{
l
->last
->next
= p
;
p
->prev
=l
->last
;
l
->last
= p
;
l
->num
++;
}else if(pk
== l
->first
)
{
p
->next
= l
->first
;
l
->first
->prev
= p
;
l
->first
= p
;
l
->num
++;
}else
{
p
->next
= pk
;
p
->prev
= pk
->prev
;
pk
->prev
->next
= p
;
pk
->prev
= p
;
l
->num
++;
}
l
->last
->next
=l
->first
;
l
->first
->prev
=l
->last
;
}
}
LinkedList
*InitializePicture()
{
LinkedList
*lIst
= NULL;
lIst
= create_doublelist_head();
DIR
*list
;
list
= opendir("./");
if(list
== NULL)
{
perror("open list failed\n");
}
struct dirent
*list_name
;
struct stat buf
;
int i
;
while(1)
{
list_name
= readdir(list
);
if(list_name
== NULL)
{
break;
}
int i
=findfilebmp(list_name
->d_name
);
int j
=findfilejpg(list_name
->d_name
);
if(i
==-1)
{
if(j
==-1)
{
continue;
}else
{
i
=stat(list_name
->d_name
,&buf
);
if(i
==-1)
{
perror("stat");
return 0;
}
Add_Elem_To_Double_List(lIst
,list_name
->d_name
,(int)buf
.st_atime
);
}
}else
{
i
=stat(list_name
->d_name
,&buf
);
if(i
==-1)
{
perror("stat");
return 0;
}
Add_Elem_To_Double_List(lIst
,list_name
->d_name
,(int)buf
.st_atime
);
}
}
closedir(list
);
return lIst
;
}
int findfilebmp(Elemtype
*p
)
{
int i
,l
=strlen(p
);
if(p
[l
-1]=='p'&&p
[l
-2]=='m'&&p
[l
-3]=='b'&&p
[l
-4]=='.'&&p
[0]=='a')
{
return 1;
}
return -1;
}
int findfilejpg(Elemtype
*p
)
{
int i
,l
=strlen(p
);
if(p
[l
-1]=='g'&&p
[l
-2]=='p'&&p
[l
-3]=='j'&&p
[l
-4]=='.'&&p
[0]=='a')
{
return 1;
}
return -1;
}
void select1(Elemtype
*p
)
{
int i
=findfilebmp(p
);
if(i
==-1)
{
display_jpeg(p
);
}else
{
lcd_display(p
);
}
}
void print_double_list_display(LinkedList
*l
)
{
if(l
== NULL)
{
return ;
}
int times
=0;
struct node
*p
= l
->first
;
while(times
<l
->num
)
{
select1(p
->data
);
usleep(1000000);
p
=p
->next
;
times
++;
}
}
void print_double_list_display1(LinkedList
*l
)
{
if(l
== NULL)
{
return ;
}
int times
=0;
struct node
*p
= l
->last
;
while(times
<l
->num
)
{
select1(p
->data
);
printf("time:%d \n",p
->time
);
usleep(1000000);
p
=p
->prev
;
times
++;
}
}
LinkedList
*delete_all_x(LinkedList
*l
,Elemtype
*x
)
{
char *r
=x
;
struct node
*px
= l
->first
;
struct node
*pf
= l
->first
;
int T
=ts_event(),times
=0;
if(T
==1||T
==2)
{
px
= pf
;
while(times
<l
->num
)
{
if(px
->data
== x
)
{
break;
}
px
= px
->next
;
times
++;
}
if(px
== NULL)
{
return NULL;
}
pf
= px
->next
;
if(px
== l
->first
)
{
if(l
->first
->next
== NULL)
{
l
->first
= NULL;
l
->last
= NULL;
l
->num
--;
free(px
);
return l
;
}
l
->first
= px
->next
;
px
->next
= NULL;
l
->first
->prev
= l
->last
;
l
->num
--;
free(px
);
}else if(px
== l
->last
)
{
l
->last
= px
->prev
;
l
->last
->next
= l
->first
;
px
->prev
= NULL;
l
->num
--;
free(px
);
}else
{
px
->prev
->next
= px
->next
;
px
->next
->prev
= px
->prev
;
px
->next
= NULL;
px
->prev
= NULL;
l
->num
--;
free(px
);
}
printf("delete %s success\n",r
);
}else
{
printf("delete %s bashing\n",r
);
}
return l
;
}
void print_double_list_display11(LinkedList
*l
)
{
printf("link:\n");
if(l
== NULL)
{
perror("link");
return ;
}
printf("\nl->num=%d\n",l
->num
);
int times
=0;
struct node
*p
= l
->last
;
while(times
<l
->num
)
{
printf("name:%s \n",p
->data
);
p
=p
->prev
;
times
++;
}
}
void FunctionRealization(LinkedList
*l
)
{
struct node
*p
=l
->first
;
struct node
*q
=l
->last
;
struct node
*pre
=NULL;
LinkedList
*s
=NULL;
int x
,i
=0;
print_double_list_display11(l
);
while(1)
{
x
=ts_event();
switch(x
)
{
case 1:
{
p
=p
->next
;
select1(p
->data
);
printf(" display:%s\n",p
->data
);
continue;
}
case 2:
{
p
=p
->prev
;
select1(p
->data
);
continue;
}
}
if(x
==4)
{
pre
=p
;
s
=delete_all_x(l
,p
->data
);
print_double_list_display11(s
);
p
=s
->first
;
q
=s
->last
;
}
if(x
==3)
{
break;
}
}
}
int lcd_display(Elemtype
*file
)
{
int lcd_fd
;
printf("%s\n",file
);
lcd_fd
= open("/dev/fb0",O_RDWR
);
if(lcd_fd
< 0)
{
perror("lcd failed");
}
FILE
*fp
;
puts(file
);
fp
=fopen(file
,"r");
if(fp
== NULL)
{
perror("open bmp failed");
return -1;
}
fseek(fp
,54,SEEK_SET);
char bmp_buf
[800*480*3];
int lcd_buf
[800*480];
int ret
;
ret
=fread(bmp_buf
,800*480,3,fp
);
if(ret
<3)
{
perror("read failed\n");
}
int *p
;
p
=mmap(NULL,800*480*4, PROT_READ
|PROT_WRITE
, MAP_SHARED
,lcd_fd
,0);
int i
,j
;
for(i
=0,j
=0;i
<800*480;i
++,j
=j
+3)
{
lcd_buf
[i
]= bmp_buf
[j
] | bmp_buf
[j
+1] << 8 | bmp_buf
[j
+2] << 16 | 0 << 24 ;
}
int show_bmp
[480*800];
int x
,y
;
for(y
=0;y
<480;y
++)
{
for(x
=0;x
<800;x
++)
{
show_bmp
[x
+800*(479-y
)]=lcd_buf
[x
+800*y
];
}
}
for(i
=0;i
<800*480;i
++)
{
*(p
+i
)=show_bmp
[i
];
}
int k
;
for(k
=0;k
<60;k
++)
{
for(i
=k
;i
<480;i
+=60)
{
for(j
=0;j
<800;j
++)
{
*(p
+800*i
+j
)=lcd_buf
[j
+800*(479-i
)];
}
}
}
munmap(p
,800*480*4);
close(lcd_fd
);
fclose(fp
);
return 0;
}
int display_jpeg( char *jpegfile
)
{
int lcd_fd
;
printf("%s\n",jpegfile
);
lcd_fd
= open("/dev/fb0",O_RDWR
);
if(lcd_fd
< 0)
{
perror("lcd failed");
}
FILE
*fp
;
struct jpeg_decompress_struct dinfo
;
struct jpeg_error_mgr jerr
;
int width
, height
, components
;
int x
, y
;
unsigned char a
, r
, g
, b
;
int color
;
jpeg_create_decompress(&dinfo
);
dinfo
.err
= jpeg_std_error(&jerr
);
fp
= fopen( jpegfile
, "r" );
if( NULL == fp
)
{
perror(" open jpeg file");
return 1;
}
jpeg_stdio_src(&dinfo
, fp
);
jpeg_read_header(&dinfo
, TRUE
);
jpeg_start_decompress(&dinfo
);
width
= dinfo
.output_width
;
height
= dinfo
.output_height
;
components
= dinfo
.output_components
;
printf(" jpeg : width = %d, height = %d, components = %d\n", width
, height
, components
);
unsigned char *pixel
= ( unsigned char *) malloc( width
*components
);
plcd
=mmap(NULL,800*480*4, PROT_READ
|PROT_WRITE
, MAP_SHARED
,lcd_fd
,0);
while( dinfo
.output_scanline
< height
)
{
y
= dinfo
.output_scanline
;
jpeg_read_scanlines(&dinfo
,
&pixel
,
1
);
unsigned char *p
= pixel
;
for( x
=0; x
<width
; x
++ )
{
if ( 4 == components
)
a
= *p
++;
else
a
= 0x0;
r
= *p
++;
g
= *p
++;
b
= *p
++;
color
= ( a
<<24 ) | (r
<<16) | (g
<<8) | b
;
*(plcd
+ 800*y
+ x
) = color
;
}
}
jpeg_finish_decompress( &dinfo
);
jpeg_destroy_decompress
(&dinfo
);
munmap(plcd
,800*480*4);
free(pixel
);
fclose(fp
);
}
int ts_event()
{
int fdts
,i
;
int x1
=-1,y1
=-1,x2
=-1,y2
=-1;
int ret
=0;
struct input_event ts
;
x
=0;
fdts
= open("/dev/input/event0",O_RDONLY
);
if(fdts
< 0)
{
perror("ts open fail");
return -1;
}
while(1)
{
read(fdts
,&ts
,sizeof(struct input_event
));
if(ts
.type
== EV_KEY
&& ts
.code
== 0x14a && ts
.value
== 1)
{
for(i
=0;i
<3;i
++)
{
read(fdts
,&ts
,sizeof(struct input_event
));
if(ts
.type
== EV_ABS
)
{
if(ts
.code
== ABS_X
)
{
x1
=ts
.value
;
}
else if(ts
.code
== ABS_Y
)
{
y1
=ts
.value
;
}
}
}
}
if(ts
.type
== EV_ABS
)
{
if(ts
.code
== ABS_X
)
{
x2
=ts
.value
;
}
else if(ts
.code
== ABS_Y
)
{
y2
=ts
.value
;
}
}
if(ts
.type
== EV_KEY
&& ts
.code
== 0x14a && ts
.value
== 0)
{
close(fdts
);
break;
}
}
if(y1
-y2
>10&&(abs(x1
-x2
)<abs(y1
-y2
)))
{
ret
=3;
}
else if(y2
-y1
>10&&(abs(x1
-x2
)<abs(y1
-y2
)))
{
ret
=4;
}
else if(((x2
-x1
)>20)&&((abs(x1
-x2
))>(abs(y1
-y2
))))
{
ret
=1;
}
else if(((x1
-x2
)>1)&&((abs(x1
-x2
))>(abs(y1
-y2
))))
{
ret
=2;
}
printf("\nret=%d\n",ret
);
return ret
;
}
void FunctionRealization1(LinkedList
*l
)
{
int x
;
while(1)
{
x
=ts_event();
switch(x
)
{
case 1:
{
print_double_list_display(l
);
lcd_display(Cover2
);
continue;
}
case 2:
{
print_double_list_display1(l
);
lcd_display(Cover2
);
continue;
}
case 3:
{
return;
}
}
}
}
int main()
{
LinkedList
*list
= NULL;
list
= create_doublelist_head();
list
= InitializePicture();
lcd_display(Cover
);
usleep(600000);
while(1)
{
lcd_display(Cover4
);
x
=ts_event();
if(x
==1)
{
lcd_display(Cover3
);
FunctionRealization(list
);
}
if(x
==2)
{
lcd_display(Cover2
);
FunctionRealization1(list
);
}
if(x
==3)
{
lcd_display(Cover
);
break;
}
}
free(list
);
return 0;
}
转载请注明原文地址: https://mac.8miu.com/read-408329.html