From c7b78edbf8ec26e841e233ec7baeed3e6ec558fd Mon Sep 17 00:00:00 2001 From: james <1943357252@qq.com> Date: Sun, 4 Aug 2024 14:43:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AA=E5=90=83=E8=9B=87=E4=BD=86=E9=A2=9C?= =?UTF-8?q?=E8=89=B2=E4=BD=8D=E7=BD=AE=E6=9C=89=E7=82=B9=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/main.c | 13 +++++------ dayandnight/my_func.c | 30 ++++++++++++++++++++++++ dayandnight/my_func.h | 17 ++++++++++++++ dayandnight/myproject.c | 16 +++++++++++++ dayandnight/mysnake.c | 51 +++++++++++++++++++++++++++++++++++++++++ dayandnight/mysnake.h | 6 +++++ 6 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 dayandnight/my_func.c create mode 100644 dayandnight/my_func.h create mode 100644 dayandnight/mysnake.c create mode 100644 dayandnight/mysnake.h diff --git a/applications/main.c b/applications/main.c index 7cbb63d..79b67e5 100644 --- a/applications/main.c +++ b/applications/main.c @@ -18,6 +18,7 @@ #include #include +#include "my_func.h" /* 配置 LED 灯引脚 */ @@ -26,9 +27,9 @@ #define LCD_MAX 240 int roundxy[4][2] = { - {0, 16}, + {0, 0}, {0, LCD_MAX}, - {LCD_MAX, 16}, + {LCD_MAX, 0}, {LCD_MAX, LCD_MAX}, }; int xymove[4][2] = { @@ -52,11 +53,7 @@ void mytime() } lcd_show_string(2, 2, 16, ctime((const time_t *)&cur_time)); } -void lcd_black(int x, int y) -{ - lcd_address_set(x, y, x, y); - lcd_write_half_word(BLACK); -} + void xy_round(int x, int y, int x2, int y2, int r, int ii) { // rt_kprintf("x:%d,y:%d,x2:%d,y2:%d,r:%d\n", x, y, x2, y2, r); @@ -77,6 +74,7 @@ void xy_round(int x, int y, int x2, int y2, int r, int ii) } void my_round(int r) { + // 这个范围涂黑 lcd_fill(0, 0, roundxy[2][0], roundxy[2][1],BLACK); lcd_write_half_word(BLACK); @@ -104,6 +102,7 @@ void xy_sink() // } int main(void) { + // system("snake game"); char str[] = "wifi join Dong abcd07691234"; my_round(20); rt_wlan_config_autoreconnect(RT_TRUE); diff --git a/dayandnight/my_func.c b/dayandnight/my_func.c new file mode 100644 index 0000000..40739c5 --- /dev/null +++ b/dayandnight/my_func.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +void lcd_black(int x, int y) +{ + lcd_address_set(x, y, x, y); + lcd_write_half_word(BLACK); +} + +void lcd_white(int x, int y) +{ + lcd_address_set(x, y, x, y); + lcd_write_half_word(WHITE); + +} +void snake_address(int x, int y,int r,const rt_uint16_t da) +{ + lcd_fill(x*r, y*r, x*r+r-1, y*r+r-1,da); + lcd_write_half_word(da); +} +// bt 命令行 +int color_cmd(int argc, char **argv) +{ + snake_address(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]),BLACK); + return 1; +} +MSH_CMD_EXPORT_ALIAS(color_cmd, snk, bt 命令行); \ No newline at end of file diff --git a/dayandnight/my_func.h b/dayandnight/my_func.h new file mode 100644 index 0000000..148d0ca --- /dev/null +++ b/dayandnight/my_func.h @@ -0,0 +1,17 @@ +#include +#include +#include +#include + +void lcd_black(int x, int y); +void lcd_white(int x, int y); +void snake_address(int x, int y, int r, const rt_uint16_t da); + +// typedef int QDataType; + +// typedef struct QListNode +// { +// QDataType data; +// struct QListNode* next; + +// } QueueNode; diff --git a/dayandnight/myproject.c b/dayandnight/myproject.c index b8ce4b7..623d2d4 100644 --- a/dayandnight/myproject.c +++ b/dayandnight/myproject.c @@ -9,6 +9,7 @@ #include #include #include +#include "mysnake.h" char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0}; @@ -282,6 +283,7 @@ static void mqtt_example_main(void *parameter) #define THREAD_TIMESLICE 5 rt_thread_t MQTT_Thread = RT_NULL; +rt_thread_t Snake_Thread = RT_NULL; void ath_init(void) { @@ -313,6 +315,20 @@ int ap3_init(void){ return 0; } +void snk_init(void) +{ + Snake_Thread = rt_thread_create("Snake_Thread", snake_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); + + if (Snake_Thread != RT_NULL) + { + rt_thread_startup(Snake_Thread); + } + else + { + rt_kprintf("Snake Thread Create Failed!\n"); + } +} +MSH_CMD_EXPORT_ALIAS(snk_init, snake, "snake game"); // void i20_init(void) // { // const char* i2c_bus_name = "i2c2"; diff --git a/dayandnight/mysnake.c b/dayandnight/mysnake.c new file mode 100644 index 0000000..c35487d --- /dev/null +++ b/dayandnight/mysnake.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include "my_func.h" +#include "mysnake.h" + +#define LCD_MAX 240 +#define SNAKE_SIZE 20 +#define SNAKE_MAX LCD_MAX / SNAKE_SIZE + +// bool snake_table[SNAKE_MAX][SNAKE_MAX] = {0}; +// struct My_snake +// { +// int x; +// int y; +// }snake_list[SNAKE_MAX*SNAKE_MAX] = {0}; +// /* rt_event_t 是指向事件结构体的指针类型 */ +// typedef struct My_snake* my_snake_t; + +void snake_entry(void) +{ + int snake_list[SNAKE_MAX][2] = {0}; + int snake_direction[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; // 上,下,左,右 + + 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; + snake_list[0][1] = snake_list[1][1]; + snake_list[2][0] = snake_list[1][0] + 1; + snake_list[2][1] = snake_list[1][1]; + // snake_table[snake_list[0][0]][snake_list[0][1]] = 1; + // snake_table[snake_list[1][0]][snake_list[1][1]] = 1; + // snake_table[snake_list[2][0]][snake_list[2][1]] = 1; + snake_address(snake_list[0][0], snake_list[0][1], SNAKE_SIZE, BLACK); + snake_address(snake_list[1][0], snake_list[1][1], SNAKE_SIZE, BLACK); + snake_address(snake_list[2][0], snake_list[2][1], SNAKE_SIZE, BLACK); + int xx = 5; + while (1) + { + rt_thread_mdelay(500); + snake_address(snake_list[snake_tail][0], snake_list[snake_tail][1], SNAKE_SIZE, WHITE); + former_head = snake_head; + snake_head = (snake_head + 1) % (SNAKE_MAX); + snake_tail = (snake_tail + 1) % (SNAKE_MAX); + snake_list[snake_head][0] = snake_list[former_head][0] + snake_direction[now_direction][0]; + snake_list[snake_head][1] = snake_list[former_head][1] + snake_direction[now_direction][1]; + snake_address(snake_list[snake_head][0], snake_list[snake_head][1], SNAKE_SIZE, BLACK); + } +} diff --git a/dayandnight/mysnake.h b/dayandnight/mysnake.h new file mode 100644 index 0000000..ccf5606 --- /dev/null +++ b/dayandnight/mysnake.h @@ -0,0 +1,6 @@ +#include +#include +#include +#include "my_func.h" + +void snake_entry(void); \ No newline at end of file