在开发板上简单显示一个相册——链表的初级运用

本文是做出一个能够实现把若干bmp格式图片有序显示在GEC6818开发版上的简单代码,由于时间紧俏,代码有些冗余。

main.h文件

#ifndef _MAIN_H
#define _MAIN_H

#include //printf
#include 
#include 
#include //open,lseek
#include //open
#include //open
#include //lseek,read,close
#include //mmap
#include 
#include 
typedef struct link_node
{
    char data[30];
    struct link_node * next;
    struct link_node * pre;
}LINK_NODE,*NODE;
 int show_bmp(char path[]);
 NODE dret();
 NODE create_node();
 int ADD_NODE(NODE head,char data[]);
 int touch();
 int touch_s();
#endif

main.c文件

#include "main.h"

int main()
{
    char path[10];
    NODE Head;
    Head = dret();

    int ifret = 5;

    int tret;
    show_bmp("../mn/huany.bmp");
    sleep(2);
    while(1)
        {
                              show_bmp("../mn/shou.bmp");

                                ifret = touch();

                                NODE new_node = Head->next;

                        if (ifret == 1)
                            {
                                while(strlen(new_node->data)!=0)
                                {
                                    strcpy(path,new_node->data);
                                    new_node = new_node->next;
                                    show_bmp(path);
                                    sleep(1);
                                }

                            }
                        if (ifret == 2)
                            {
                                strcpy(path,new_node->data);
                                show_bmp(path);

                                while(1)
                                {
                                    tret = touch_s();
                                    printf("tret=%d\n",tret);

                                    if(tret == 2)
                                    {
                                       new_node = new_node->next;
                                       strcpy(path,new_node->data);
                                       show_bmp(path);
                                    }
                                    if(tret == 1)
                                    {
                                       new_node = new_node->pre;
                                       strcpy(path,new_node->data);
                                       show_bmp(path);
                                    }
                                    if(tret == 0)
                                        printf("fail\n");
                                    if(strlen(new_node->data)== 0)
                                       break;
                                }
                            }
                        if(ifret == 3)
                            {
                                show_bmp("../mn/goodbye.bmp");
                                sleep(1);
                                break;
                            }

        }
    return 0;
}

bmp.c文件

#include "main.h"

int show_bmp(char path[])//显示图片
{
	int i;
	int ret;
	int x, y;
	int *lcd_mmap;
	int fd_bmp, fd_lcd;
	int lcd_buf[800*480];
	char bmp_buf[800*480*3];
    printf("path is %s\n",path);
	char pathm[30];
	bzero(pathm,30);
	sprintf(pathm,"/projet/pr/%s",path);   //  ../mn
	char pathmm[30];
	bzero(pathmm,30);
	strcpy(pathmm,pathm);
	printf("pathm is %s\n",pathm);
	printf("pathmm is %s\n",pathmm);
	fd_lcd = open("/dev/fb0", O_RDWR);
	if(fd_lcd == -1)
	{
		printf("open fb0 fail!\n");
		return -1;
	}
	fd_bmp = open(pathm,O_RDWR);
    if(fd_bmp == -1)
	{
		printf("open bmp fail!\n");
		return -1;
	}

	ret = lseek(fd_bmp, 54, SEEK_SET);
	if(ret == -1)
	{
		perror("lseek fail!\n");
		return -1;
	}


	bzero(bmp_buf, sizeof(bmp_buf));
	bzero(lcd_buf, sizeof(lcd_buf));

	ret = read(fd_bmp, bmp_buf, sizeof(bmp_buf));
	if(ret == -1)
	{
		printf("read bmp fail!\n");
		return -1;
	}

	for(i=0; i<800*480; i++)
	{
		lcd_buf[i] = bmp_buf[i*3]<<0 | bmp_buf[i*3+1]<<8 | bmp_buf[i*3+2]<<16;
	}

	lcd_mmap = mmap(	NULL,
						800*480*4,
						PROT_READ|PROT_WRITE,
						MAP_SHARED,
						fd_lcd,
						0);
	if(lcd_mmap == MAP_FAILED)
	{
		printf("mmap fail!\n");
		return -1;
	}

	for(y=0; y<480; y++)
	{
		for(x=0; x<800; x++)
		{
			*(lcd_mmap+800*(479-y)+x) = lcd_buf[800*y+x];
		}
	}

	close(fd_bmp);
	close(fd_lcd);
	munmap(lcd_mmap, 800*480*4);

	return 0;
}

NODE create_node()//创建节点
{
    NODE node = (NODE)malloc(sizeof(LINK_NODE));
    if(node == NULL)
    {
        return NULL;
    }

    node->next = node;
    node->pre = node;

    return node;
}


int ADD_NODE(NODE head,char data[])//添加节点数据
{
    NODE add_node = create_node();
    if(add_node == NULL)
    {
        return -1;
    }

    NODE tmp = head;

    while(tmp->next != head)
    {
        tmp = tmp->next;
    }

    strcpy(add_node->data ,data);
    tmp->next = add_node;
    add_node->pre = tmp;
    head->pre = add_node;
    add_node->next = head;


    return 0;

}


NODE dret()//目录检索,检索到就创建节点添加数据
{
            char *ret;
            char buf[100]="./";
            struct dirent *ep;
            DIR *fp;
            fp = opendir(buf);
            if(fp == NULL)
            {
                perror("open dir failed\n");
            }

         NODE fnode  =   create_node();



            ret = getcwd(buf,100);
            if(ret == NULL)
            {
                perror("get failed\n");
            }


        while(1)
        {
            ep = readdir(fp);
            if(ep == NULL)
            {
                break;
            }
            if(strcmp(ep->d_name,".") == 0 || strcmp(ep->d_name,"..") == 0)
            {

                continue;
            }
            if(strstr(ep->d_name,".bmp"))
            {
              ADD_NODE(fnode,ep->d_name);
            }


        }
             closedir(fp);
	return fnode;
}


int touch()
{

	FILE *fp;
	fp = fopen("/dev/input/event0","r");
	if(fp == NULL)
	{
		perror("open ts failed\n");
	}

    int x,y;
	struct input_event ts_buf;
	bzero(&ts_buf,sizeof(ts_buf));

	while(1)
	{
			fread(&ts_buf, sizeof(ts_buf),1, fp);

			if(ts_buf.type == EV_ABS)
			{
				if(ts_buf.code == ABS_X)
					x = ts_buf.value;

				if(ts_buf.code == ABS_Y)
					y = ts_buf.value;

				if(ts_buf.code == ABS_PRESSURE && ts_buf.value == 0)
					break;
			}

			if(ts_buf.type == EV_KEY)
			{
				if(ts_buf.code == BTN_TOUCH && ts_buf.value == 0)
					break;
			}

	}

	if (x>400&&x<580&&y>45&&y<130)
        return 1;
    if (x>600&&x<787&&y>45&&y<130)
        return 2;
    if (x>25&&x<120&&y>430&&y<470)
        return 3;



	fclose(fp);
	return 0;


}

int touch_s()
{
    int ret;
	int ts_x, ts_y;
	int fd_ts;
	struct input_event coordinate;


	fd_ts = open("/dev/input/event0", O_RDWR);
	if(fd_ts==-1)
	{
		printf("open event0 fail!\n");
		return -1;
	}

		int ts_x1 = 0;
		int ts_y1 = 0;
		int ts_x2 = 0;
		int ts_y2 = 0;

		while(1)
		{
			ret = read(fd_ts, &coordinate, sizeof(struct input_event));
			if(ret == -1)
			{
				printf("read bmp fail!\n");
				return -1;
			}

			if(coordinate.type==3 && coordinate.code==0 && coordinate.value>0 && coordinate.value<800)
			{
				ts_x1 = coordinate.value;
			}
			if(coordinate.type==3 && coordinate.code==1 && coordinate.value>0 && coordinate.value<480)
			{
				ts_y1 = coordinate.value;
			}
			if(coordinate.type==1 && coordinate.code==330 && coordinate.value==1)
			{
				break;
			}
		}
		while(1)
		{
			ret = read(fd_ts, &coordinate, sizeof(struct input_event));
			if(ret == -1)
			{
				printf("read bmp fail!\n");
				return -1;
			}

			if(coordinate.type==3 && coordinate.code==0 && coordinate.value>0 && coordinate.value<800)
			{
				ts_x2 = coordinate.value;
			}
			if(coordinate.type==3 && coordinate.code==1 && coordinate.value>0 && coordinate.value<480)
			{
				ts_y2 = coordinate.value;
			}
			if(coordinate.type==1 && coordinate.code==330 && coordinate.value==0)
			{
				break;
			}
		}

		printf("(x1, y1):(x2,y2)(%d, %d):(%d, %d)\n", ts_x1, ts_y1,ts_x2, ts_y2);


		if (ts_x2>ts_x1)
        {
            if(((ts_y2-ts_y1)/(ts_x2-ts_x1))<0.6&&((ts_y2-ts_y1)/(ts_x2-ts_x1))>(-0.6))
                return 1;
        }
        else
        {
            if(((ts_y2-ts_y1)/(ts_x2-ts_x1))<0.6&&((ts_y2-ts_y1)/(ts_x2-ts_x1))>(-0.6))
                return 2;
        }


	close(fd_ts);

	return 0;
}


你可能感兴趣的:(在开发板上简单显示一个相册——链表的初级运用)