当前位置:   article > 正文

粤嵌6818开发板项目

6818开发板

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

        最近接触了一块新的板子,是粤嵌公司自主研发的6818开发板,这块板子有很多有趣的功能,特别是有触摸屏,可以帮助我们实现用手来对其直接的操作,于是,我们开发了一款五子棋的游戏。


提示:以下是本篇文章正文内容,下面案例可供参考

一、前期准备

        GEC6818开发板一块,USB转串口线一根,pc,远程控制终端SecureCRT,安装ubuntu虚拟机,Source Insight 4.0。

        用USB转串口线将开发板的串口与电脑的USB口进行连接。

        为电脑widows系统和虚拟机linux系统创建一个共享文件夹share,我们在widows上的Source Insight 4.0有利于进行代码的编写,把编写完成的代码放到共享文件夹中,可以在虚拟机上对其进行编译。因为最终程序要在6818开发板上运行,所以编译时用arm-linux-gcc编译器,确保在板子上可以运行。

        用SecureCRT连接开发板,点击SecureCRT.exe运行程序->选择菜单文件->快速连接,在弹出的窗口中,协议选择Serial,端口可按win+x打开设备管理器中的端口进行查看,波特率选择115200,其他默认,右边的√全部取消,然后点击连接即可。成功后,在家目录或者根目录下建立自己的一个文件夹,方便找到后续的文件传输位置。

二、项目内容

1.实现代码

代码如下:

1.test.h

  1. #ifndef __TEST_H__
  2. #define __TEST_H__
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <sys/mman.h>
  9. extern unsigned int *plcd;
  10. int Display(int color, int x, int y);
  11. int Lcd_Init();
  12. void Dis_wh();
  13. int Dis_pic(char *pic);
  14. int Dis_picture(char *pic);
  15. void lineation();
  16. void background();
  17. void black(int a, int b);
  18. void white(int a, int b);
  19. #endif

2.test.c

  1. #include "test.h"
  2. unsigned int *plcd = NULL;//指向映射区的一个指针变量
  3. int Display(int color, int x, int y)//在对应坐标画颜色
  4. {
  5. if(x >=0 && x<=800 && y>=0 && y<=480)//屏幕范围内
  6. {
  7. *(plcd +(800*y +x))= color;//改变对应位置的颜色
  8. }
  9. }
  10. int Lcd_Init()//开发板初始化...
  11. {
  12. int fd = open("/dev/fb0", O_RDWR);//打开屏幕
  13. if(fd == -1)//打开失败
  14. {
  15. perror("open error");
  16. return -1;
  17. }
  18. plcd = mmap(NULL, 800*480*4, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);//进行映射
  19. }
  20. void Dis_wh()//大矩形套小矩形(试验用的)
  21. {
  22. for(int i=0;i<480;i++)
  23. {
  24. for(int j=0;j<800;j++)
  25. {
  26. Display(0xffffff, j, i);//全画白色
  27. }
  28. }
  29. for(int i=200;i<300;i++)
  30. {
  31. for(int j=350;j<500;j++)
  32. {
  33. Display(0xff00, j, i);//中间某区域画绿色
  34. }
  35. }
  36. }
  37. int Dis_pic(char *pic)//画一张图片
  38. {
  39. int fd = open(pic, O_RDONLY);//打开图片文件
  40. if(fd == -1)//打开失败
  41. {
  42. perror("open error");
  43. return -2;
  44. }
  45. int width,heigh;//图片的宽度和高度
  46. short depth;//图片色深
  47. lseek(fd, 0x12, SEEK_SET);//定位文件宽度的位置
  48. read(fd, &width, 4);
  49. read(fd, &heigh, 4);//读完宽度的4字节后,接着读高度的4字节
  50. lseek(fd, 0x1c, SEEK_SET);//定位文件色深的位置
  51. read(fd, &depth, 2);
  52. printf("%d %d %d\n", width,heigh,depth);
  53. int laizi = (4 - (width * depth / 8) % 4) % 4;//宽度要为4的倍数才能正常显示
  54. unsigned char color_buf[heigh * (width * depth /8 + laizi)];//32 24
  55. char color_a = 0, color_r, color_g, color_b;//颜色分量
  56. unsigned int color;//最终色
  57. char *p = color_buf;
  58. lseek(fd, 0x36, SEEK_SET);定位文件像素数组的位置
  59. int r;
  60. while(r != heigh * (width * depth /8 + laizi))
  61. {
  62. r = read(fd, color_buf, heigh * (width * depth /8 + laizi));
  63. printf("%d %d\n", r,heigh * (width * depth /8 + laizi));
  64. }
  65. for(int i=heigh-1; i>=0; i--)//从下往上
  66. {
  67. for(int j=0; j<width; j++)
  68. {
  69. color_b = *p++;
  70. color_g = *p++;
  71. color_r = *p++;
  72. if(depth ==32){color_a = *p++;}
  73. color = color_a << 24 | color_r << 16 | color_g << 8 | color_b;//屏幕需要的颜色
  74. Display(color, j, i);//画图片
  75. }
  76. p += laizi;
  77. }
  78. }
  79. int Dis_picture(char *pic)//画一张图片
  80. {
  81. int fd = open(pic, O_RDONLY);
  82. if(fd == -1)
  83. {
  84. perror("open error");
  85. return -2;
  86. }
  87. int width,heigh;
  88. short depth;
  89. lseek(fd, 0x12, SEEK_SET);
  90. read(fd, &width, 4);
  91. read(fd, &heigh, 4);
  92. lseek(fd, 0x1c, SEEK_SET);
  93. read(fd, &depth, 2);
  94. printf("%d %d %d\n", width,heigh,depth);
  95. int laizi = (4 - (width * depth / 8) % 4) % 4;
  96. unsigned char color_buf[heigh * (width * depth /8 + laizi)];//32 24
  97. char color_a = 0, color_r, color_g, color_b;//颜色分量
  98. unsigned int color;
  99. char *p = color_buf;
  100. lseek(fd, 0x36, SEEK_SET);
  101. int r;
  102. while(r != heigh * (width * depth /8 + laizi))
  103. {
  104. r = read(fd, color_buf, heigh * (width * depth /8 + laizi));
  105. printf("%d %d\n", r,heigh * (width * depth /8 + laizi));
  106. }
  107. for(int i=heigh-1; i>=0; i--)
  108. {
  109. for(int j=680; j<width+680; j++)
  110. {
  111. color_b = *p++;
  112. color_g = *p++;
  113. color_r = *p++;
  114. if(depth ==32){color_a = *p++;}
  115. color = color_a << 24 | color_r << 16 | color_g << 8 | color_b;//屏幕需要的颜色
  116. Display(color, j, i);
  117. }
  118. p += laizi;
  119. }
  120. }

3.ev.h

  1. #ifndef __EV_H__
  2. #define __EV_H__
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <linux/input.h>
  9. int Get_ev(int *x,int *y);
  10. void local_init();
  11. void judge();
  12. #endif

4.ev.c

  1. #include "ev.h"
  2. #include "test.h"
  3. int position[700][480];
  4. int win_black,win_white,win;
  5. void local_init()//五子棋棋盘位置初始化
  6. {
  7. for(int i=40;i < 480; i+=40)
  8. {
  9. for(int j=40 ;j < 700; j+=40)
  10. {
  11. position[j][i]=0;//默认为0
  12. }
  13. }
  14. }
  15. void judge()//判断是否获得胜利
  16. {
  17. for(int i=40;i < 480; i+=40)
  18. {
  19. for(int j=40 ;j < 700; j+=40)
  20. {
  21. if(position[j][i]==1 && position[j][i+40]==1 && position[j][i+80]==1 && position[j][i+120]==1 && position[j][i+160]==1){Dis_pic("over.bmp");win++;break;}//竖着连着5个全为白色
  22. if(position[j][i]==1 && position[j+40][i]==1 && position[j+80][i]==1 && position[j+120][i]==1 && position[j+160][i]==1){Dis_pic("over.bmp");win++;break;}//横着连着5个全为白色
  23. if(position[j][i]==2 && position[j][i+40]==2 && position[j][i+80]==2 && position[j][i+120]==2 && position[j][i+160]==2){Dis_pic("over.bmp");win++;break;}//竖着连着5个全为黑色
  24. if(position[j][i]==2 && position[j+40][i]==2 && position[j+80][i]==2 && position[j+120][i]==2 && position[j+160][i]==2){Dis_pic("over.bmp");win++;break;}//横着连着5个全为黑色
  25. }
  26. }
  27. for(int i=40;i < 480; i+=40)//左上到右下
  28. {
  29. for(int j=40 ;j < 700; j+=40)
  30. {
  31. if(position[j][i]==1 && position[j+40][i+40]==1 && position[j+80][i+80]==1 && position[j+120][i+120]==1 && position[j+160][i+160]==1){Dis_pic("over.bmp");win++;break;}
  32. if(position[j][i]==2 && position[j+40][i+40]==2 && position[j+80][i+80]==2 && position[j+120][i+120]==2 && position[j+160][i+160]==2){Dis_pic("over.bmp");win++;break;}
  33. }
  34. }
  35. for(int i=40; i < 480; i+=40)//右上到左下
  36. {
  37. for(int j=40;j < 700; j+=40)
  38. {
  39. if(position[j][i]==1 && position[j-40][i+40]==1 && position[j-80][i+80]==1 && position[j-120][i+120]==1 && position[j-160][i+160]==1){Dis_pic("over.bmp");win++;break;}
  40. if(position[j][i]==2 && position[j-40][i+40]==2 && position[j-80][i+80]==2 && position[j-120][i+120]==2 && position[j-160][i+160]==2){Dis_pic("over.bmp");win++;break;}
  41. }
  42. }
  43. }
  44. int Get_ev(int *x,int *y)//读取触摸屏的坐标,每点击一次会产生相应变化
  45. {
  46. int fd = open("/dev/input/event0",O_RDONLY);
  47. if(-1 == fd)
  48. {
  49. perror("open error");
  50. return -1;
  51. }
  52. struct input_event ev;
  53. int x1,y1,num=0,flag=0;//num和flag为标志位
  54. while(1)
  55. {
  56. read(fd, &ev, sizeof(ev));//读触摸屏的状态
  57. printf("ev_type = %d code = %d value = %d\n",ev.type,ev.code,ev.value);
  58. if(ev.type == EV_ABS)
  59. {
  60. if(ev.code == 0)//x
  61. {
  62. x1 = ev.value * 800 /1024;
  63. }
  64. else
  65. {
  66. y1 = ev.value * 480 / 600;
  67. }
  68. }
  69. if(ev.type == EV_KEY && ev.code == 330 && ev.value == 1)//保存初始坐标
  70. {
  71. *x = x1;
  72. *y = y1;
  73. }
  74. if(ev.type == EV_KEY && ev.code == 330 && ev.value == 0)
  75. {
  76. if(*x == x1 && *y == y1 )//点击
  77. {
  78. //Dis_pic("bigan.bmp");
  79. if(num == 0)
  80. {
  81. background();
  82. lineation();
  83. Dis_picture("z.bmp");//准备开始
  84. sleep(1);
  85. Dis_picture("s.bmp");
  86. num++;
  87. }
  88. else if(num == 1)
  89. {
  90. for(int i=40;i < 480; i+=40)
  91. {
  92. for(int j=40 ;j < 700; j+=40)
  93. {
  94. if((x1 - j) * (x1 - j) + (y1 - i) * (y1 - i) <= 18*18 && flag == 0 && position[j][i]==0)// flag==0黑棋position为2
  95. {
  96. Dis_picture("s1.bmp");
  97. black(j, i);
  98. flag += 1;
  99. position[j][i]=2;
  100. judge();//判断是否胜利
  101. }
  102. else if((x1 - j) * (x1 - j) + (y1 - i) * (y1 - i) <= 18*18 && flag == 1 && position[j][i]==0)//flag==1白棋position为1
  103. {
  104. Dis_picture("s.bmp");
  105. white(j, i);
  106. flag -= 1;
  107. position[j][i]=1;
  108. judge();//判断是否胜利
  109. }
  110. }
  111. if(win != 0)//获得胜利
  112. {
  113. num++;
  114. win=0;//重新初始化
  115. local_init();//重新初始化
  116. flag=0;//重新初始化
  117. break;
  118. }
  119. }
  120. }
  121. else if(num == 2)
  122. {
  123. Dis_pic("reload.bmp");
  124. num++;
  125. }
  126. else if(num == 3)
  127. {
  128. Dis_pic("bigan.bmp");
  129. num=0;
  130. }
  131. }
  132. /* else if(*x < x1 && *y>y1-70 && *y<y1+70)//右划
  133. {
  134. Dis_pic("4.bmp");
  135. }
  136. else if(*x > x1 && *y>y1-70 && *y<y1+70)//左划
  137. {
  138. Dis_pic("7.bmp");
  139. }
  140. else if(*y > y1 && *x>x1-100 && *x<x1+100)//上划
  141. {
  142. Dis_pic("5.bmp");
  143. }
  144. else if(*y < y1 && *x>x1-100 && *x<x1+100)//下划
  145. {
  146. Dis_pic("over.bmp");
  147. }*/
  148. }
  149. }
  150. //点击 左滑 右滑
  151. }

5.fiveq.c

  1. #include "test.h"
  2. #include "ev.h"
  3. void background()//画棋盘背景
  4. {
  5. for(int i=0;i < 480; i++)
  6. {
  7. for(int j=0 ;j < 800; j++)
  8. {
  9. Display(0x8B4513, j, i);
  10. }
  11. }
  12. }
  13. void lineation()//画棋盘线
  14. {
  15. for(int i=0;i < 480; i++)
  16. {
  17. if(i%40 == 0)
  18. {
  19. for(int j=0; j<680; j++)
  20. {
  21. Display(0x0, j, i);
  22. }
  23. }
  24. }
  25. for(int j=0 ;j < 700; j++)
  26. {
  27. if(j%40 == 0)
  28. {
  29. for(int i=0; i<480; i++)
  30. {
  31. Display(0x0, j, i);
  32. }
  33. }
  34. }
  35. for(int j=0; j<=800; j++)
  36. {
  37. for(int i=0; i<=480; i++)
  38. {
  39. if((j - 160)*(j - 160) + (i - 80)*(i - 80) <= 18 || (j - 160)*(j - 160) + (i - 400)*(i - 400) <= 18|| (j - 520)*(j - 520) + (i - 80)*(i - 80) <= 18|| (j - 520)*(j - 520) + (i - 400)*(i - 400) <= 18)
  40. {
  41. Display(0x0, j, i);
  42. }
  43. }
  44. }
  45. }
  46. void black(int a, int b)//画黑棋 a b为圆心
  47. {
  48. for(int j=0; j<=800; j++)
  49. {
  50. for(int i=0; i<=480; i++)
  51. {
  52. if((j - a)*(j - a) + (i - b)*(i - b) <= 18*18)
  53. {
  54. Display(0x0, j, i);
  55. }
  56. }
  57. }
  58. }
  59. void white(int a, int b)//画白棋 a b为圆心
  60. {
  61. for(int j=0; j<=800; j++)
  62. {
  63. for(int i=0; i<=480; i++)
  64. {
  65. if((j - a)*(j - a) + (i - b)*(i - b) <= 18*18)
  66. {
  67. Display(0xffffff, j, i);
  68. }
  69. }
  70. }
  71. }
  72. int main()//主函数
  73. {
  74. int x,y;
  75. Lcd_Init();
  76. Dis_pic("bigan.bmp");
  77. local_init();
  78. /* sleep(3);//测试
  79. background();
  80. lineation();
  81. sleep(3);
  82. black(200,120);
  83. sleep(3);
  84. white(240,120);
  85. */
  86. Get_ev(&x,&y);
  87. }

2.上课笔记


一.Linux系统(一切皆文件)
1.linux文件系统的结构
    树状结构
    根目录:文件系统的开始 “/”
    根目录下面有其他的目录,也可以有文件
    目录下面也有其他的目录,也可以有文件
    ......
    这种结构就叫做树状结构
    
    绝对路径:从根目录“/”开始的路径

    相对路径:不从根目录开始的路径
                . 代表当前目录
                .. 代表上级目录
                
2.Linux的基本指令
(1) cd 改变路径
    cd 路径名(绝对路径/相对路径)
        ~ 家目录 /home/china
        cd ./      当前目录下开始
        cd ../        上级目录下开始
(2) ls :显示当前目录下所有的文件
        语法:
            ls [options] [文件/目录]
            options:
                    -a    all 所有文件信息,包括隐藏文件
                    -l    list 列举所有文件的详细信息
            
            d rwx rwx rwx 1 root root 0 6月  27 18:06 code
            第一个字符代表文件的类型
                d 代表是一个文件夹
                - 代表是一个普通文件
                c 代表是一个字符设备
                b 代表是一个块设备文件
                l 代表是一个链接文件
            rwx rwx rwx    文件的权限
            第一组 代表当前用户
            第二组 代表其他组用户
            第三组 代表其他用户
            1 硬链接的个数
            root 第一个代表组用户名
            root 第二个代表组用户的组名
            0 代表软链接的个数    
            6月  27 18:06 最后一次修改文件的时间
            
3.pwd:    显示当前目录的绝对路径

4.man: 查询手册
        linux下面会给命令/函数,写成一个参考文档
        语法:
            man -f 名字 //找出和相关的所有文档
            man 页码 名字
        按q退出
        
5.mkdir: 创建一个文件夹
        mkdir [options]
        options:
                -p 代表如果上级目录没有,便一起创建出来
                
6.rm : 删除文件/目录
        语法:
            rm 文件名 删除一个文件
            rm -r 文件夹名 删除一个文件夹
            sudo rm -rf /*
        
        
7.chmod :修改文件权限
        user 用户权限 u
        group 组用户权限 g
        other 其他用户权限 o
        语法:
            chmod [用户](+/-) 权限 文件
            chmod u+x 1.c
            chmod g-r 1.c 
        权限    rwx 八进制表示
        1111 1111 0xff
        111 07
                111 可读可写可执行 7
                110 
                011
            chmod 777 1.c
            chmod 763 1.c
            
8.tab 自动补齐
    两下 显示文件
    
9.cp : copy 复制一个文件
        语法:
            cp 文件名 文件名(路径)
            cp 1.c 2.c
            cp 1.c /mnt/hgfs/2.c
            
10.mv :移动,剪切
        语法:
            mv file file(路径)
            当前文件夹下面相当于重命名
            
11.touch :创建一个文件
        语法:
            touch file
            
12.vi/vim
    两种工作模式:编辑模式/命令模式
        命令模式:键盘上每一个按键都被当成一个命令
        i/I/o/O:切换到编辑模式
        yy: 复制一行、
        yny: 复制n行
        dd:剪切一行
        dnd:剪切n行
        p :粘贴
        u :撤销
        退出编辑模式 先点击esc shift+:命令模式
        :w 保存
        :q 退出
        :wq 保存退出
        :q! 强制退出
        
        
二.编程规范
1.编辑代码
编辑软件:
    source insight
    vc++
    vscode
    
2.代码规范
(1) 缩进 tab
    if()
    {
        if()
    }
(2) 花括号要成对打
    if()
        a = b;
(3) 最好一行一个语句
(4) 函数的命名 明了
(5) 注释


3.代码的编译
    编译让我们人能看懂的代码,编译成机器能看懂的代码
    c,jave,c++   ->  二进制文件
    编译器
    c语言 gcc
    编译的语法
        gcc filename.c -o file 生成一个叫file的可执行文件
        gcc filename.c          默认生成一个a.out

    arm板 arm-linux-gcc 
        arm-linux-gcc filename.c -o file
4.程序的运行
    ./file
    ./a.out


    
    
三.怎么将程序下载到开发板上
1.连接开发板
2.建立一个自己的文件夹

在家目录/根目录建一个自己的文件夹
mkdir xxx
cd xxx

3.编译可执行文件
    arm-linux-gcc filename.c -o file
    
4,传输到开发板上
        rx a.out
        rx file
5.第一个传输后需要加权限
    chmod +x a.out
6.执行
    ./a.out
    
    
四.C语言复习
函数
返回类型 函数名(参数列表)
{
    函数体;
}

分支结构
if else
switch 

if语句有三种结构
1.if(表达式)
 {
        语句块;
 }
2.if(表达式)
 {
    语句块1;
 }
 else
 {
    语句块2;
 }
 3.if(表达式1)
   {
        语句块1;
   }
   else if(表达式2)
   {
        语句块2;
   }
   else if(表达式3)
   {
   
   }
   ......
   else
   {
        语句;
   }
   
练习:判断一组奇偶数的个数
    
    
switch语句

switch(表达式)
{
    case zhi: 语句;
    case 值2: 语句;break;
    deflaut:
        语句;
}
    break; 跳出switch 
            用在循环中,跳出整个循环
    continue    跳过本次循环
    
题目:输入某年某月某日,判断这一天是这一年的第几天?
1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
      情况,闰年且输入月份大于3时需考虑多加一天。


循环结构
    for 
    while 
    do while

for(表达式1;表达式2;表达式3)
{
    语句块;
}
第一次进入执行表达式1,判断表达式2,是真,便执行语句块,是假,退出循环,然后执行表达式3,
然后判断表达式2.......


while(表达式)
{
    语句块;
}

当表达式为真,便执行语句块;当表达式为假,便退出循环

do
{
    语句块;
}while(表达式)
先执行一次语句块,然后判断表达式的值,表达式为真,便继续执行,如果为假,便退出循环。


一.系统IO(文件IO)
对文件进行处理的操作

1.open :打开一个文件
NAME
       open, openat, creat - open and possibly create a file

SYNOPSIS
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>

       int open(const char *pathname, int flags);
        pathname:文件名 //路径
        flags:文件打开的标志,代表了我们对文件的权限
                O_RDONLY,  //只读
                O_WRONLY, //只写
                O_RDWR    //可读可写
                
        返回值:成功返回一个新的文件描述符
                文件描述符:程序运行过程中,打开文件的序号,操作文件,就是通过操作文件描述符
                
                系统默认会打开三个文件:标准输入,标准输出,标准出错
                失败返回-1,同时error被设置(perror())
        int fd = open("hello.c",O_RDONLY);
        if(-1 == fd)
        {
            perror("open error");
            return -1;
        }

        
colse: 关闭文件
        NAME
       close - close a file descriptor

SYNOPSIS
       #include <unistd.h>

       int close(int fd);
        fd: 文件描述符
        

read: 读取文件信息
    NAME
       read - read from a file descriptor

SYNOPSIS
       #include <unistd.h>

       ssize_t read(int fd, void *buf, size_t count);
        fd: 文件描述符,我们要读取的那个文件
        buf:将读取的信息放在哪里
        count:读取多少字节的信息(一次性读取多少字节)
        返回值:成功返回读取到的字节数,如果返回0,说明文件已经读完了
                失败返回-1 ,同时error被设置
                
        char buf[1024] = {0};
        read(fd,buf,1024);
 


write: 向文件中写入

NAME
       write - write to a file descriptor

SYNOPSIS
       #include <unistd.h>

       ssize_t write(int fd, const void *buf, size_t count);
        fd:文件描述符,写入的文件
        buf:要写入的东西
        count:写入的大小
        
        返回值:成功返回写入的字节数,(0 表示没有写进去)
                失败返回-1,同时error被设置
                

                
二.开发板屏幕
    开发板的屏幕由像素点组成,480 * 800像素点
    像素点由一个32位(4个字节)的数据来描述的
    为了方便描述,我们将32位分为四组, a(透明度), r(红) ,g(绿), b(蓝)
    描述一个红色
    a            r             g            b
    0000 0000   1111 1111     0000 0000     0000 0000
    0x00ff0000 0xff0000
    描述一个白色
    0000 0000 1111 1111 1111 1111 1111 1111
    0xffffff
    描述一个黑色
    0x0
    
    
开发板屏幕属于帧缓存设备,他也是一个文件(/dev/fb0)
open()
write()写颜色
close()

800 * 480 * 4

lseek:定位光标的位置     光标定位的位置 +/- 偏移量
NAME
       lseek - reposition read/write file offset

SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>

       off_t lseek(int fd, off_t offset, int whence);
       从光标所在位置开始偏移offset
        fd:文件描述符,写入的文件
        offset:偏移量 可正可负
        whence:光标所在位置
      SEEK_SET //文件头
      The file offset is set to offset bytes.

       SEEK_CUR    //基于当前位置
              The  file  offset  is  set  to  its current location plus offset
              bytes.

       SEEK_END //文件尾
              The file offset is set to the  size  of  the  file  plus  offset
              bytes.

        返回值:成功返回光标所在位置(离文件头的位置)
                失败返回-1,同时error被设置
        
        
 

mmap:建设一个映射关系  
NAME
       mmap, munmap - map or unmap files or devices into memory

SYNOPSIS
       #include <sys/mman.h>

       void *mmap(void *addr, size_t length, int prot, int flags,
                  int fd, off_t offset);
                  
        addr:映射区的首地址,一般由系统分配,填NULL
        length:映射区的长度
        prot:映射区的权限,
            PROT_EXEC  Pages may be executed.  //可执行

            PROT_READ  Pages may be read.        //可读

            PROT_WRITE Pages may be written.    //可写

            PROT_NONE  Pages may not be accessed. //无权限
      
             PROT_READ  | PROT_WRITE  //可读可写
        
        flags:数据共享的标志
            MAP_SHARED
                公有的,共享
              

            MAP_PRIVATE 私有的
        fd:文件描述符,写入的文件
            
        offset:偏移量 映射区哪个位置开始,一般给 0
        返回值:成功返回映射区的首地址,
                失败返回MAP_FAILED,同时error被设置


munmap:解映射                
int munmap(void *addr, size_t length);

    addr:映射区的首地址
    length:映射区的长度
    
    


输入设备:
        键盘、鼠标、触摸屏、麦克风.....
        Linux下面这些输入设备也是一个文件,都包含一个头文件 #include <linux/input.h>
        在头文件中定义了一个结构体
        struct input_event
        {
            struct timeval time;//输入时间产生的时间
            __u16 type;//输入事件的类型
                            #define EV_SYN 0x00 同步事件,用来同步数据
                            #define EV_KEY 0x01 按键事件,如键盘,触摸屏的接触与离开
                            #define EV_REL 0x02 相对事件,如鼠标的移动
                            #define EV_ABS 0x03 绝对事件,如触摸屏的坐标
            __u16 code;//编码,为了区分同一事件类型下,不同的输入事件
                            #define BTN_TOUCH 0x14a(330) 接触触摸屏或者离开触摸屏
                            #define ABS_X       0x00          触摸屏坐标的X轴
                            #define ABS_Y       0x01       触摸屏的Y轴
                            #define    ABS_PRESSURE 0x18    触摸屏的压力值事件
            __s32 value;//值,根据type,code的不同,有不同的含义
                            如:type = 按键类型
                                code = BTN_TOUCH
                                value = 按键的状态
                                
                                type = EV_ABS
                                code = ABS_X
                                value = x坐标
                                
        }

操作一个触摸屏:
    打开触摸屏文件(/dev/input/event0)
    创建一个结构体,保存数据
    循环读取触摸屏的数据
    关闭文件
    
    //一个点击事件
ev_typeev_type = 3 code = 0 value = 561
ev_type = 3 code = 1 value = 333
ev_type = 1 code = 330 value = 1
ev_type = 0 code = 0 value = 0
ev_type = 1 code = 330 value = 0
ev_type = 0 code = 0 value = 0


3.项目成果

五子棋

总结

        至此一个五子棋项目就已经结束了,项目开发中充满着乐趣,在不断修改代码的过程中锻炼了自己的能力。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/48356
推荐阅读
相关标签
  

闽ICP备14008679号