华为昇腾310嵌入式开发重力小球迷宫的部分源码

华为昇腾310开发板重力小球迷宫的部分源码

作者:爱写代码的刚子

时间:2024.1.11

前言:本篇博客是对学校嵌入式大实验的记录

void Ball_Point(int x,int y)
{
    DrawPoint(x-1,y-2);
    DrawPoint(x,y-2);
    DrawPoint(x+1,y-2);
    
    DrawPoint(x-2,y-1);
    DrawPoint(x-1,y-1);
    DrawPoint(x,y-1);
    DrawPoint(x+1,y-1);
    DrawPoint(x+2,y-1);
    
    DrawPoint(x-2,y);
    DrawPoint(x-1,y);
    DrawPoint(x,y);
    DrawPoint(x+1,y);
    DrawPoint(x+2,y);
    
    DrawPoint(x-2,y+1);
    DrawPoint(x-1,y+1);
    DrawPoint(x,y);
    DrawPoint(x+1,y+1);
    DrawPoint(x+2,y+1);
    
    DrawPoint(x-1,y+2);
    DrawPoint(x,y+2);
    DrawPoint(x+1,y+2);
}


void Ball_Clear(int x,int y)
{
    ClearPoint(x-1,y-2);
    ClearPoint(x,y-2);
    ClearPoint(x+1,y-2);
    
    ClearPoint(x-2,y-1);
    ClearPoint(x-1,y-1);
    ClearPoint(x,y-1);
    ClearPoint(x+1,y-1);
    ClearPoint(x+2,y-1);
    
    ClearPoint(x-2,y);
    ClearPoint(x-1,y);
    ClearPoint(x,y);
    ClearPoint(x+1,y);
    ClearPoint(x+2,y);
    
    ClearPoint(x-2,y+1);
    ClearPoint(x-1,y+1);
    ClearPoint(x,y);
    ClearPoint(x+1,y+1);
    ClearPoint(x+2,y+1);
    
    ClearPoint(x-1,y+2);
    ClearPoint(x,y+2);
    ClearPoint(x+1,y+2);
}


void DrawBlock(int x,int y)
{
    for(int i=x;i<x+8;i++)
    {
        for(int j=y;j<y+8;j++)
        {
            DrawPoint(i,j);
        }
    }
}

void DrawMap()
{
    DrawBlock(24,0);    
    DrawBlock(32,0);
    DrawBlock(40,0);
    DrawBlock(56,0);
        
    DrawBlock(0,8);
    DrawBlock(8,8);
    DrawBlock(56,8);

    DrawBlock(24,16);
    DrawBlock(32,16);
    DrawBlock(40,16);
    DrawBlock(56,16);

    DrawBlock(0,24);
    DrawBlock(16,24);
    DrawBlock(24,24);
    DrawBlock(56,24);

    DrawBlock(16,32);
    DrawBlock(40,32);

    DrawBlock(0,40);
    DrawBlock(16,40);
    DrawBlock(32,40);
    DrawBlock(40,40);
    DrawBlock(56,40);
    
    DrawBlock(40,48);
    DrawBlock(56,48);
    
    DrawBlock(0,56);
    DrawBlock(8,56);
    DrawBlock(16,56);
    DrawBlock(32,56);
    DrawBlock(40,56);
    DrawBlock(56,56);


}
void set_arr(int arr[][64])
{
        
    set_arr_block(arr,24,0);    
    set_arr_block(arr,32,0);
    set_arr_block(arr,40,0);
    set_arr_block(arr,56,0);


        
    set_arr_block(arr,0,8);
    set_arr_block(arr,8,8);
    set_arr_block(arr,56,8);


    set_arr_block(arr,24,16);
    set_arr_block(arr,32,16);
    set_arr_block(arr,40,16);
    set_arr_block(arr,56,16);


    set_arr_block(arr,0,24);
    set_arr_block(arr,16,24);
    set_arr_block(arr,24,24);
    set_arr_block(arr,56,24);

    set_arr_block(arr,16,32);
    set_arr_block(arr,40,32);
    
    set_arr_block(arr,0,40);
    set_arr_block(arr,16,40);
    set_arr_block(arr,32,40);
    set_arr_block(arr,40,40);
    set_arr_block(arr,56,40);
    
    set_arr_block(arr,40,48);
    set_arr_block(arr,56,48);
    
    set_arr_block(arr,0,56);
    set_arr_block(arr,8,56);
    set_arr_block(arr,16,56);
    set_arr_block(arr,32,56);
    set_arr_block(arr,40,56);
    set_arr_block(arr,56,56);
}


void check_change(int*buff,float*buff_cur,int arr[128][64])
{
    if(abs((int)buff_cur[1]-(int)buff[1])>3||abs((int)buff_cur[0]-(int)buff[0])>3)
    {
        if(buff_cur[0]>buff[0])
        {
            buff_cur[0]=buff[0]+2;
        }
        else
        {
            buff_cur[0]=buff[0]-2;
        }
        if(buff_cur[1]>buff[1])
        {
            buff_cur[1]=buff[1]+2;
        }
        else
        {
            buff_cur[1]=buff[1]-2;
        }
    }
    int flag=0;
    int flag2=0;
    
    if((int)buff_cur[1]-3<0)//x
    {
        int a=(int)buff_cur[1]-3;
        buff[1]=(int)buff_cur[1]-a;
        buff[0]=(int)buff_cur[0];
        flag=1;
    }
    if((int)buff_cur[0]-3<0)//y
    {
        int a=(int)buff_cur[0]-3;
        buff[0]=(int)buff_cur[0]-a;
        buff[1]=(int)buff_cur[1];
        flag=1;
    }
    if((int)buff_cur[1]+3>=128)
    {
        int a=(int)buff_cur[1]-127;
        buff[1]=(int)buff_cur[1]-a;
        buff[0]=(int)buff_cur[0];
        flag=1;
    }   
    if((int)buff_cur[0]+3>=64)
    {
        int a=(int)buff_cur[0]-63
                  buff[0]=(int)buff_cur[0]-a;
        buff[1]=(int)buff_cur[1];
        flag=1;
    }


    if(flag)return;

    if(arr[(int)buff_cur[1]][(int)buff_cur[0]-3]==1)
    {
        while(1)
        {
            (int)buff_cur[0]++;
            if(arr[(int)buff_cur[1]][(int)buff_cur[0]-3]==0)break;  

        }
        buff[0]=(int)buff_cur[0];
        buff[1]=(int)buff_cur[1];
        flag2=1;
    }

    if(arr[(int)buff_cur[1]][(int)buff_cur[0]+3]==1)
    {
        while(1)
        {
            (int)buff_cur[0]--;
            if(arr[(int)buff_cur[1]][(int)buff_cur[0]+3]==0)break;  

        }
        buff[0]=(int)buff_cur[0];
        buff[1]=(int)buff_cur[1];
        flag2=1;
    }

    if(arr[(int)buff_cur[1]-3][(int)buff_cur[0]]==1)
    {
        while(1)
        {
            (int)buff_cur[1]++;
            if(arr[(int)buff_cur[1]-3][(int)buff_cur[0]]==0)break;  

        }
        buff[1]=(int)buff_cur[1];
        buff[0]=(int)buff_cur[0];
        flag2=1;
    }

    if(arr[(int)buff_cur[1]+3][(int)buff_cur[0]]==1)
    {
        while(1)
        {
            (int)buff_cur[1]--;
            if(arr[(int)buff_cur[1]+3][(int)buff_cur[0]]==0)break;  

        }
        buff[1]=(int)buff_cur[1];
        buff[0]=(int)buff_cur[0];
        flag2=1;
    }

    if(flag2)return;        
    
    buff[0]=buff_cur[0];
    buff[1]=buff_cur[1];
}
void Labyrinth()
{
    oled_init();
    adxl345_Init();
    int arr[128][64];
    set_arr(arr);
    int buff[3]={5,3,0};
    float buff_cur[3];
    while(1)
    {
        ColorTurn(TURNOVER_COLOR);
        DisplayTurn(NORMAL_DISPLAY);
        DrawMap();
        Ball_Point((int)buff[1],(int)buff[0]);
        Refresh();
        Ball_Clear((int)buff[1],(int)buff[0]);
        adxl345_GetData(buff_cur);
        //修正
        check_change(buff,buff_cur,arr);
    }
}

给出部分源码是因为这个实验是在Ascend310项目之上的,调用了一些外设的接口,给出来是在是太麻烦,主要是提供思路。

作者其实还有一个实验,但是这个实验有参考价值。

你可能感兴趣的:(FPGA,华为,java,算法)