嵌入式开发入门-02

嵌入式开发入门-02

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>

char *music_play_cmd[] = {"madplay one.mp3 &", "madplay two.mp3 &", "madplay three.mp3 &"};

char *jpg_play_cmd[] = {"./jpeg_show ./pic/one.jpg", "./jpeg_show ./pic/two.jpg", "./jpeg_show ./pic/three.jpg", "./jpeg_show ./pic/four.jpg", "./jpeg_show ./pic/five.jpg", "./jpeg_show ./pic/six.jpg", "./jpeg_show ./pic/seven.jpg", "./jpeg_show ./pic/eight.jpg", "./jpeg_show ./pic/nine.jpg", "./jpeg_show ./pic/ten.jpg"};

int ts_x, ts_y;
int get_ts();

//1. catch one time  touchscreen function
int get_ts() {
    //1. open touchscreen doc
    int fd = open("/dev/input/event0", O_RDWR);
    if (-1 ==fd) {
        perror("open is failed");
        return -1;
    }
    struct input_event buf;
    //int ts_x, ts_y;
    int flag = 0;
    //2. read touchscreen doc data
    while (1) {
        read(fd, &buf, sizeof(buf));
        //printf("buf:%d---%d---%d\n", buf.type, buf.code, buf.value);
        if (buf.type == EV_ABS && buf.code == ABS_X && flag == 0) {
            ts_x = buf.value;
            flag = 1;
        }
        if (buf.type == EV_ABS && buf.code == ABS_Y && flag == 1) {
            ts_y = buf.value;
            flag = 2;
        }
        if (flag == 2) {
            printf("(%d, %d)\n", ts_x, ts_y);
            flag = 0;
            break;
        }
    }
    //3. close doc
    close(fd);
    return 0;
}

//1. music play module
int music() {
//1. show music.jpg
    system("./jpeg_show music.jpg");
    //play music
    int i = 0;
    system(music_play_cmd[i]);
    //2. wait user touch screen
    while (1) {
        get_ts();
        if (ts_x > 70 && ts_x < 160 && ts_y > 370 && ts_y < 465) {
            //previous one music
            system("killall -9 madplay ");
            sleep(1);
            i--;
            if (i < 0) {
               i = 2;
            }
            system(music_play_cmd[i]);
        }
        if (ts_x > 215 && ts_x < 300 && ts_y > 370 && ts_y < 465) {
            //next  one music
            system("killall -9 madplay ");
            sleep(1);
            i++;
            if (i > 2) {
                i = 0;
            }
            system(music_play_cmd[i]);
        }
        if (ts_x > 360 && ts_x < 455 && ts_y > 370 && ts_y < 465) {
            //pause music
            system("killall -19 madplay");
        }
        if (ts_x > 510  && ts_x < 595 && ts_y > 370 && ts_y < 465) {
            //continue play  music
            system("killall -18 madplay");
        }
        if (ts_x > 650 && ts_x < 740 && ts_y > 370 && ts_y < 465) {
            //1. stop play music
            system("killall -9 madplay");
            //2. exit music play module
            break;
        }
    }
    return 0;
}

//2. video play module
int initmplayer() {
    //create a fifo doc
    int ret = mkfifo("/tmp/myfifo01", 0777);
    if (-1 == ret) {
        perror("mkfifo01 failed");
        return -1;
    }
    int fd = open("/tmp/myfifo01", O_RDWR);
    if (-1 == fd) {
        perror("open fifo failed");
        return -1;
    }
    return fd;
}

int video_play() {
    //init mplayer
    int fd = initmplayer();
    printf("%d\n", fd);
    system("./jpeg_show video.jpg");
    sleep(1);
    //2. play video
    system("./mplayer -slave -quiet -input file=/tmp/myfifo01 -geometry 90:0 -zoom -x 620 -y 480 Faded3.avi &");
    //3. get user touch msg
    while (1) {
        get_ts();
        //1. kuaitui
        if (ts_x > 0 && ts_x < 90 && ts_y > 10 && ts_y < 100) {
            write(fd, "seek -10\n", 9);
            printf("kuaitui\n");
        }
        //2. kuaijing
        if (ts_x > 710 && ts_x < 800 && ts_y > 10 && ts_y < 100) {
            write(fd, "seek 10\n", 8);
            printf("kuaijing\n");
        }
        //3. zanting
        if (ts_x > 0 && ts_x < 90 && ts_y > 100 && ts_y < 190) {
            write(fd, "pause\n", 6);
            printf("zanting\n");
        }
        //4. jixu
        if (ts_x > 710 && ts_x < 800 && ts_y > 100 && ts_y < 190) {
            write(fd, "pause\n", 6);
            printf("jixu\n");
        }
        //5. yingliang--
        if (ts_x > 0 && ts_x < 90 && ts_y > 190 && ts_y < 280) {
            write(fd, "volume -5\n", 10);
            printf("yingliang--\n");
        }
        //6. yingliang++
        if (ts_x > 710 && ts_x < 800 && ts_y > 190 && ts_y < 280) {
            write(fd, "volume +5\n", 10);
            printf("yingliang++\n");
        }
        //7. jingyin
        if (ts_x > 0 && ts_x < 90 && ts_y > 280 && ts_y < 360) {
            write(fd, "mute 1\n", 7);
            printf("jingyin\n");
        }
        //8. huifu
        if (ts_x > 710 && ts_x < 800 && ts_y > 280 && ts_y < 360) {
            write(fd, "mute 0\n", 7);
            printf("huifu\n");
        }
        //9. tuichu
        if (ts_x > 710 && ts_x < 800 && ts_y > 360 && ts_y < 460) {
            write(fd, "quit\n", 6);
            system("killall mplayer");
            printf("tuichu\n");
            break;
        }
    }
    return 0;
}

//3. picture module
int picture() {
    //1. show page
    system("./jpeg_show picture.jpg");
    int i = 0;
    //2. get user touch locate
    while (1) {
       get_ts();
       if (ts_x > 0 && ts_x < 300) {
           i--;
           if (i < 0) {
               i = 9;
           }
           system(jpg_play_cmd[i]);
      }
      if (ts_x > 500 && ts_x < 800) {
          i++;
          if (i > 9) {
              i = 0;
          }
          system(jpg_play_cmd[i]);
      }
      if (ts_x > 300 && ts_x < 500) {
          break;
       }
    }
    return 0;
}

int main() {
    //1. show main page
    system("./jpeg_show mainpage.jpg");
    while (1) {
        get_ts();
       if (ts_x > 40 && ts_x < 140 && ts_y > 220 && ts_y < 315) {
           music();    //call the music module
           system("./jpeg_show mainpage.jpg");
       }
       if (ts_x > 190 && ts_x < 290 && ts_y > 220 && ts_y < 315) {
           video_play();    //call the video module
           system("./jpeg_show mainpage.jpg");
       }
       if (ts_x > 335 && ts_x < 435 && ts_y > 220 && ts_y < 315) {
           picture();   //call the video module
           system("./jpeg_show mainpage.jpg");
       }
    }
    return
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值