按键控制运动(方向)

This commit is contained in:
james 2024-08-04 20:14:20 +08:00
parent 59dccf3e4c
commit 5f0093f4e8
5 changed files with 17 additions and 6 deletions

View File

@ -434,7 +434,7 @@ CONFIG_RT_USING_ULOG=y
# CONFIG_ULOG_OUTPUT_LVL_I is not set
CONFIG_ULOG_OUTPUT_LVL_D=y
CONFIG_ULOG_OUTPUT_LVL=7
# CONFIG_ULOG_USING_ISR_LOG is not set
CONFIG_ULOG_USING_ISR_LOG=y
CONFIG_ULOG_ASSERT_ENABLE=y
CONFIG_ULOG_LINE_BUF_SIZE=128
# CONFIG_ULOG_USING_ASYNC_OUTPUT is not set

View File

@ -4,14 +4,17 @@
#define LOG_TAG "pin.irq"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
#include <rtatomic.h>
#include "my_func.h"
#define KEY_UP GET_PIN(C, 5)
#define KEY_DOWN GET_PIN(C, 1)
#define KEY_LEFT GET_PIN(C, 0)
#define KEY_RIGHT GET_PIN(C, 4)
extern rt_atomic_t now_direction ;
void key_up_callback(void *args)
{
if(rt_atomic_load(&now_direction) != 2) rt_atomic_store(&now_direction, 0);
int value = rt_pin_read(KEY_UP);
LOG_I("key up value: %d\n", value);
}
@ -19,17 +22,20 @@ void key_up_callback(void *args)
void key_down_callback(void *args)
{
int value = rt_pin_read(KEY_DOWN);
if(rt_atomic_load(&now_direction) != 0) rt_atomic_store(&now_direction, 2);
LOG_I("key down value: %d\n", value);
}
void key_left_callback(void *args)
{
if(rt_atomic_load(&now_direction) != 3) rt_atomic_store(&now_direction, 1);
int value = rt_pin_read(KEY_LEFT);
LOG_I("key left value: %d\n", value);
}
void key_right_callback(void *args)
{
if(rt_atomic_load(&now_direction) != 1) rt_atomic_store(&now_direction, 3);
int value = rt_pin_read(KEY_RIGHT);
LOG_I("key right value: %d\n", value);
}

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#include <malloc.h>
// 当前方向
void mytime();
void xy_round(int x, int y, int x2, int y2, int r, int ii);
void my_round(int r);

View File

@ -4,10 +4,12 @@
#include "my_func.h"
#include "mysnake.h"
#include <time.h>
#include <rtatomic.h>
#define LCD_MAX 240
#define SNAKE_SIZE 20
#define SNAKE_MAX LCD_MAX / SNAKE_SIZE
rt_atomic_t now_direction=3 ;
// bool snake_table[SNAKE_MAX][SNAKE_MAX] = {0};
// struct My_snake
@ -20,6 +22,7 @@
void snake_entry(void *parameter)
{
system("irq");
time_t t;
/* 初始化随机数发生器 */
srand((unsigned)time(&t));
@ -28,7 +31,7 @@ void snake_entry(void *parameter)
char snake_dirshow[4][7] = {"upup", "left", "down", "rigt"};
char tmp[10];
int snake_head = 2, snake_tail = 0, former_head; // 蛇头,蛇尾
int now_direction = 3; // 当前方向
snake_list[1][0] = SNAKE_MAX / 2;
snake_list[1][1] = SNAKE_MAX / 2;
snake_list[0][0] = snake_list[1][0] - 1;
@ -44,8 +47,8 @@ void snake_entry(void *parameter)
int new_direction = 0;
while (1)
{
new_direction = rand() % 3;
now_direction = (now_direction+3+new_direction)%4;//防止反向,走回头路
// new_direction = rand() % 3;
// now_direction = (now_direction+3+new_direction)%4;//防止反向,走回头路
former_head = snake_head;
snake_head = (snake_head + 1) % (SNAKE_MAX);
@ -57,7 +60,7 @@ void snake_entry(void *parameter)
// rt_kprintf("head:%d,%d\n", snake_list[snake_head][0], snake_list[snake_head][1]);
lcd_show_string(20, 20, 16,snake_dirshow[now_direction]);
lcd_show_string(20+16*4, 20, 16,tmp);
rt_thread_mdelay(1000);
rt_thread_mdelay(500);
snake_address(snake_list[snake_head][0], snake_list[snake_head][1], SNAKE_SIZE, BLACK);
snake_address(snake_list[snake_tail][0], snake_list[snake_tail][1], SNAKE_SIZE, WHITE);

View File

@ -276,6 +276,7 @@
#define RT_USING_ULOG
#define ULOG_OUTPUT_LVL_D
#define ULOG_OUTPUT_LVL 7
#define ULOG_USING_ISR_LOG
#define ULOG_ASSERT_ENABLE
#define ULOG_LINE_BUF_SIZE 128