背景
嵌入式linux课设,在此仅作记录
基本信息
-
姓名:xxx
-
学号:xxxxxxxxxx
-
班级:xxxxxxxxxx
-
项目名称:在ARM9开发板上实现井字棋游戏
-
运行说明:代码经测试可在以下环境中运行
-
系统:Ubuntu18.04.2LTS
-
开发板:FriendlyARM Cortex-A9开发板
-
编译器:gcc version 4.4.3 (ctng-1.6.1)
运行步骤
-
sudo ifconfig xxx 192.168.1.200
将PC端有线网IP地址改为与开发板相同的子网下,“xxx”为需要更改的端口名字。 -
telnet 192.168.1.230
telnet连接开发板,不同开发板IP地址不同,此处仅作参考。 -
mount -o nolock -t nfs 192.168.1.200:/home/stk-yaoyao/nfs /mntnfs挂载,将PC端文件夹/nfs挂载到开发板mnt/,这样在/nfs中编译的代码在/mnt中就可以运行了 。 -
cd mnt/ -
cd Arm_Cheese/ -
上面两条指令运行于开发板终端。
-
cd nfs/ -
cd Arm_Cheese/ -
arm-linux-gcc Arm_cheese.c -o Arm_cheese -
上面三条指令运行于PC终端,每次修改源码都需要跑一遍9。
-
./Arm_Cheese
开发板终端运行编译好的代码,同理,每次修改源码11要再跑一遍。 -
开发板出现井字棋棋盘,点击键盘上数字键盘不同数将在棋盘上同样的位置出现蓝色或红色的棋子,直到一方三子成线或棋盘塞满,游戏结束。终端显示两名玩家实时分数,并提示是否再来一局,用while循环重复。
-
实际运行时开发板图
-
实际运行时PC开发板终端图
源代码(Arm_Cheese.c)
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "linuxbmp.h"
#define RED_COLOR 0x00FF0000
#define GREEN_COLOR 0x0000FF00
#define BLUE_COLOR 0x000000FF
#define len 800
#define wid 1280
char *fbp = 0;
/*
* framebuffer application code, the start code of Linux GUI application
* compile :
* $/usr/local/arm/4.6.5/bin/arm-linux-gcc -o Arm_cheese Arm_cheese.c
* $cp Arm_cheese /tftpboot/examples
* run in target:
* #mount 192.168.1.200:/tftpboot/ /mnt/nfs
* #cd /mnt/nfs/examples
* #./Arm_Cheese
*/
int main(int argc,char** argv)
{
int player = 0;
int winner = 0;
int choice = 0;
int row = 0;
int column = 0;
int line = 0;
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
int x = 0, y = 0;
int i = 0, j = 0;
int out = 1;
int role[2]={0};
//long int location = 0;
// Open the file for reading and writing
fbfd = open("/dev/fb0", O_RDWR);
if (!fbfd) {
printf("Error: cannot open framebuffer device.\n");
exit(1);
}
printf("The framebuffer device was opened successfully.\n");
// Get fixed screen information
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
printf("Error reading fixed information.\n");
exit(2);
}
// Get variable screen information
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
printf("Error reading variable information.\n");
exit(3);
}
// Figure out the size of the screen in bytes
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
printf("%dx%d, %dbpp, screensize = %ld\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel, screensize );
// Map the device to memory
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
fbfd, 0);
if ((int)fbp == -1) {
printf("Error: failed to map framebuffer device to memory.\n");
exit(4);
}
printf("The framebuffer device was mapped to memory successfully.\n");
/**************************************************************************************************************************/
x = 0; y = 0; // Where we are going to put the pixel
if(vinfo.bits_per_pixel == 32) // 32bpp only
{
// 16bpp framebuffer test
printf("32bpp framebuffer test\n");
printf("four bytes in fbp is a pixel of LCD, just set the value of fbp to put color to LCD\n");
printf("byte format:\n");
printf(" bit:| 3 | 2 | 1 | 0 |\n");
printf(" | null | red | green | blue |\n");
//开发板1280*800 四字节分别是:null R G B
// Red Screen
printf("White Screen\n");
while(out==1)
{
printf("********************************************GOOD LUCK*********************************************\n");
printf("*******************************************PLAYER1: %d********************************************\n", role[0]);
printf("*******************************************PLAYER2: %d********************************************\n", role[1]);
char board[3][3] = {
{'7','8','9'},
{'4','5','6'},
{'1','2','3'}};
//设置600*600正方形区域,颜色为米黄色
for(y = 0; y < 600; y++)
{
for(x = 0; x < 600; x++)
{
*(fbp + y * len*4 + x*4) = 0x00;
*(fbp + y * len*4 + x*4 +1) = 0xB3;
*(fbp + y * len*4 + x *4 +2) = 0xDE;
*(fbp + y * len*4 + x *4 +3) = 0xF5;
}
}
//在正方形区域画线,井字棋盘
for(y = 200; y < 205; y++)
{
for(x = 0; x < 600; x++)
{
*(fbp + y * len*4 + x*4) = 0x00;
*(fbp + y * len*4 + x*4 +1) = 0x00;
*(fbp + y * len*4 + x *4 +2) = 0x00;
*(fbp + y * len*4 + x *4 +3) = 0x00;
}
}
for(y = 400; y < 405; y++)
{
for(x = 0; x < 600; x++)
{
*(fbp + y * len*4 + x*4) = 0x00;
*(fbp + y * len*4 + x*4 +1) = 0x00;
*(fbp + y * len*4 + x *4 +2) = 0x00;
*(fbp + y * len*4 + x *4 +3) = 0x00;
}
}
for(x = 200; x < 205; x++)
{
for(y = 0; y < 600; y++)
{
*(fbp + y * len*4 + x*4) = 0x00;
*(fbp + y * len*4 + x*4 +1) = 0x00;
*(fbp + y * len*4 + x *4 +2) = 0x00;
*(fbp + y * len*4 + x *4 +3) = 0x00;
}
}
for(x = 400; x < 405; x++)
{
for(y = 0; y < 600; y++)
{
*(fbp + y * len*4 + x*4) = 0x00;
*(fbp + y * len*4 + x*4 +1) = 0x00;
*(fbp + y * len*4 + x *4 +2) = 0x00;
*(fbp + y * len*4 + x *4 +3) = 0x00;
}
}
//sleep(2);
int w=0,winner=0;
for(w = 0;w<9 && winner==0;w++)
{
printf("\n\n");
printf(" %c | %c | %c\n",board[0][0],board[0][1],board[0][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n",board[1][0],board[1][1],board[1][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n",board[2][0],board[2][1],board[2][2]);
player = w%2+1;
//Get valid player square selection
do{
printf("\nPlayer %d, please enter the number of the square "
"where you want to place your %c: ",
player,(player == 1)?'x':'o');
scanf("%d",&choice);
choice--;
row = 2-(choice/3);
column = choice%3;
}while(choice<0 || choice>9 || board[row][column]>'9');
//insert player symbol
board[row][column] = (player == 1)?'x':'o';
for(row=0;row<3;row++)
{
for(column=0;column<3;column++)
{
if(board[row][column] == 'x')
{
for(y = 0;y <wid;y++)
{
for(x = 0;x <len;x++)
{
if(((x-((2-row)*200+100))*(x-((2-row)*200+100)) + (y-(column*200+100))*(y-(column*200+100)))<=8100)
{
*(fbp + y * len*4 + x *4) = 0xFF;
*(fbp + y * len*4 + x *4 +1) = 0x00;
*(fbp + y * len*4 + x *4 +2) = 0x00;
*(fbp + y * len*4 + x *4 +3) = 0x00;
}
}
}
}
else if(board[row][column] == 'o')
{
for(y = 0;y <wid;y++)
{
for(x = 0;x <len;x++)
{
if(((x-((2-row)*200+100))*(x-((2-row)*200+100)) + (y-(column*200+100))*(y-(column*200+100)))<=8100)
{
*(fbp + y * len*4 + x *4) = 0x00;
*(fbp + y * len*4 + x *4 +1) = 0x00;
*(fbp + y * len*4 + x *4 +2) = 0xFF;
*(fbp + y * len*4 + x *4 +3) = 0x00;
}
}
}
}
}
}
//Check rows and columns for a winning line
for(line = 0;line <= 2;line++)
{
if((board[line][0]==board[line][1] &&board[line][0]==board[line][2]) ||
(board[0][line]==board[1][line] &&board[0][line]==board[2][line]))
{ winner = player;
role[(player-1)]++;
break;
}
//Check for a winning line--diagonals
else if((board[0][0]==board[1][1] && board[0][0]==board[2][2]) ||
(board[0][2]==board[1][1] && board[0][2]==board[2][0]))
{
winner = player;
role[(player-1)]++;
break;
}
}
}
//Game is over so display the final board
printf("\n\nGame over!!Here is the result:\n");
/*for(y = 0; y < 600; y++)
{
for(x = 0; x < 600; x++)
{
*(fbp + y * len*4 + x*4) = 0x00;
*(fbp + y * len*4 + x*4 +1) = 0xB3;
*(fbp + y * len*4 + x *4 +2) = 0xDE;
*(fbp + y * len*4 + x *4 +3) = 0xF5;
}
}*/
printf(" %c | %c | %c\n",board[0][0],board[0][1],board[0][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n",board[1][0],board[1][1],board[1][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n",board[2][0],board[2][1],board[2][2]);
//Dispay result message
if(winner == 0)
printf("\nHow boring,it is a draw\n");
else
{
printf("\nCongrations,player %d,YOU ARE THE WINNER!\n",winner);
//printf("%d,%d", player1, player2);
printf("*******************************************PLAYER1: %d********************************************\n", role[0]);
printf("*******************************************PLAYER2: %d********************************************\n", role[1]);
}
printf("Would you like to try again?(0:No||1:Yes)\n");
scanf("%d", &out);
}
}
else
{
printf("32bpp only!!!\n");
}
munmap(fbp, screensize);
close(fbfd);
return 0;
}
linuxbmp.h
因为源码太长,故分享如下链接:
链接: https://pan.baidu.com/s/1VDJbKWzDnOm29BjnZRjW0Q 提取码: gvx3
参考文献
https://blog.csdn.net/hanshuilingyue/article/details/11942987