diff --git a/applications/init.c b/applications/init.c index 5729679..eafb3d6 100644 --- a/applications/init.c +++ b/applications/init.c @@ -4,6 +4,7 @@ #include #include #include +#include "my_func.h" static int board_init(void) { @@ -48,7 +49,12 @@ INIT_APP_EXPORT(app_init); // extern int wifi_join(int argc, char *argv[]); int main_init(void) { - + + char str[] = "wifi join Dong abcd07691234"; + my_round(20); + rt_wlan_config_autoreconnect(RT_TRUE); + rt_wlan_connect("Dong", "abcd07691234"); + system(str); // rt_thread_mdelay(18000); // char *argv[] = {"wifi", "join", "Dong", "abcd07691234"}; // wifi_join(4, argv); diff --git a/applications/main.c b/applications/main.c index 5f67466..e199e38 100644 --- a/applications/main.c +++ b/applications/main.c @@ -16,7 +16,7 @@ #define DBG_TAG "main" #define DBG_LVL DBG_LOG #include - +#include "init.h" #include #include "my_func.h" #include @@ -25,92 +25,17 @@ /* 配置 LED 灯引脚 */ #define PIN_LED_B GET_PIN(F, 11) // PF11 : LED_B --> LED #define PIN_LED_R GET_PIN(F, 12) // PF12 : LED_R --> LED -#define LCD_MAX 240 -int roundxy[4][2] = { - {0, 0}, - {0, LCD_MAX}, - {LCD_MAX, 0}, - {LCD_MAX, LCD_MAX}, -}; -int xymove[4][2] = { - {1, 1}, - {1, -1}, - {-1, 1}, - {-1, -1}, -}; -extern void wlan_autoconnect_init(void); -void mytime() -{ - rt_thread_mdelay(10000); - time_t cur_time; - cur_time = ntp_get_time(RT_NULL); - if( cur_time) - { - rt_kprintf("NTP Server Time: %s", ctime((const time_t *)&cur_time)); - } - lcd_show_string(2, 2, 16, ctime((const time_t *)&cur_time)); -} -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); - for (int i = x; i != x2; i += xymove[ii][0]) - { - for (int j = y; j != y2; j += xymove[ii][1]) - { - int newi = x2 - i; - int newj = y2 - j; - // rt_kprintf("(%d,%d,%d)",(newi * newi + newj * newj), newi, newj); - if ((newi * newi + newj * newj) > (r * r)) - { - // rt_kprintf("x:%d,y:%d\n", i, j); - lcd_black(i, j); - } - } - } -} -void my_round(int r) -{ - // 这个范围涂黑 - lcd_fill(0, 0, roundxy[2][0], roundxy[2][1],BLACK); - lcd_write_half_word(BLACK); - - for (int i = 0; i < 4; i++) - { - xy_round(roundxy[i][0], roundxy[i][1], roundxy[i][0] + r * xymove[i][0], roundxy[i][1] + r * xymove[i][1], r, i); - } -} -void xy_sink() -{ - for (int i = 0; i < 240; i++) - { - for (int j = 0; j <= 240; j++) - { - lcd_black(j, 240 - i); - rt_thread_mdelay(1); - } - // rt_kprintf("(%d,...) Blacked\n", i); - } -} -// void mylvgl() -// { -// lv_init(); -// lv_demo_music(); -// } int main(void) -{ +{ + main_init(); rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT); - rt_pin_write(PIN_LED_B, PIN_HIGH); + rt_pin_write(PIN_LED_B, PIN_LOW); system("snake game"); - char str[] = "wifi join Dong abcd07691234"; - my_round(20); - rt_wlan_config_autoreconnect(RT_TRUE); - rt_wlan_connect("Dong", "abcd07691234"); - system(str); mytime(); /* init Wi-Fi auto connect feature */ diff --git a/dayandnight/my_func.c b/dayandnight/my_func.c index 38ea534..9e8c67f 100644 --- a/dayandnight/my_func.c +++ b/dayandnight/my_func.c @@ -3,7 +3,9 @@ #include #include #include +#include +#define LCD_MAX 240 void lcd_black(int x, int y) { lcd_address_set(x, y, x, y); @@ -15,11 +17,82 @@ void lcd_white(int x, int y) lcd_address_set(x, y, x, y); lcd_write_half_word(WHITE); } +int roundxy[4][2] = { + {0, 0}, + {0, LCD_MAX}, + {LCD_MAX, 0}, + {LCD_MAX, LCD_MAX}, +}; +int xymove[4][2] = { + {1, 1}, + {1, -1}, + {-1, 1}, + {-1, -1}, +}; +void mytime() +{ + rt_thread_mdelay(10000); + time_t cur_time; + + cur_time = ntp_get_time(RT_NULL); + if (cur_time) + { + rt_kprintf("NTP Server Time: %s", ctime((const time_t *)&cur_time)); + } + lcd_show_string(20, 2, 16, ctime((const time_t *)&cur_time)); +} + +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); + for (int i = x; i != x2; i += xymove[ii][0]) + { + for (int j = y; j != y2; j += xymove[ii][1]) + { + int newi = x2 - i; + int newj = y2 - j; + // rt_kprintf("(%d,%d,%d)",(newi * newi + newj * newj), newi, newj); + if ((newi * newi + newj * newj) > (r * r)) + { + // rt_kprintf("x:%d,y:%d\n", i, j); + lcd_black(i, j); + } + } + } +} +void my_round(int r) +{ + // 这个范围涂黑 + lcd_fill(0, 0, roundxy[2][0], roundxy[2][1], BLACK); + lcd_write_half_word(BLACK); + + for (int i = 0; i < 4; i++) + { + xy_round(roundxy[i][0], roundxy[i][1], roundxy[i][0] + r * xymove[i][0], roundxy[i][1] + r * xymove[i][1], r, i); + } +} +void xy_sink() +{ + for (int i = 0; i < 240; i++) + { + for (int j = 0; j <= 240; j++) + { + lcd_black(j, 240 - i); + rt_thread_mdelay(1); + } + // rt_kprintf("(%d,...) Blacked\n", i); + } +} void snake_address(int x, int y, int r, const rt_uint16_t da) { - for (int i = x * r; i < x * r + r; i++) + int f = 0; // 使蛇身成节 + if (r > 5) { - for (int j = y * r; j < y * r + r; j++) + f = 1; + } + for (int i = x * r + f; i < x * r + r - f; i++) + { + for (int j = y * r + f; j < y * r + r - f; j++) { lcd_address_set(i, j, i, j); lcd_write_half_word(da); diff --git a/dayandnight/my_func.h b/dayandnight/my_func.h index 148d0ca..267e931 100644 --- a/dayandnight/my_func.h +++ b/dayandnight/my_func.h @@ -3,10 +3,15 @@ #include #include +void mytime(); +void xy_round(int x, int y, int x2, int y2, int r, int ii); +void my_round(int r); +void xy_sink(); 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 diff --git a/dayandnight/mysnake.c b/dayandnight/mysnake.c index c35487d..3a8ecda 100644 --- a/dayandnight/mysnake.c +++ b/dayandnight/mysnake.c @@ -3,6 +3,7 @@ #include #include "my_func.h" #include "mysnake.h" +#include #define LCD_MAX 240 #define SNAKE_SIZE 20 @@ -17,11 +18,14 @@ // /* rt_event_t 是指向事件结构体的指针类型 */ // typedef struct My_snake* my_snake_t; -void snake_entry(void) +void snake_entry(void *parameter) { + time_t t; + /* 初始化随机数发生器 */ + srand((unsigned)time(&t)); int snake_list[SNAKE_MAX][2] = {0}; - int snake_direction[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; // 上,下,左,右 - + int snake_direction[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; // 上,左,下,右 + char snake_dirshow[4][7] = {"upup", "left", "down", "rigt"}; int snake_head = 2, snake_tail = 0, former_head; // 蛇头,蛇尾 int now_direction = 3; // 当前方向 snake_list[1][0] = SNAKE_MAX / 2; @@ -36,16 +40,20 @@ void snake_entry(void) 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; + int new_direction = 0; while (1) { - rt_thread_mdelay(500); + + new_direction = rand() % 3; + now_direction = (now_direction+3+new_direction)%4;//防止反向,走回头路 + lcd_show_string(20, 20, 16,snake_dirshow[now_direction]); + rt_thread_mdelay(1000); 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_list[snake_head][0] = (snake_list[former_head][0] + snake_direction[now_direction][0]) % (SNAKE_MAX); + snake_list[snake_head][1] = (snake_list[former_head][1] + snake_direction[now_direction][1]) % (SNAKE_MAX); 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 index ccf5606..9d733fa 100644 --- a/dayandnight/mysnake.h +++ b/dayandnight/mysnake.h @@ -3,4 +3,4 @@ #include #include "my_func.h" -void snake_entry(void); \ No newline at end of file +void snake_entry(void *parameter); \ No newline at end of file