From aee981d847d4631e95eeb8413e8ba7046874d949 Mon Sep 17 00:00:00 2001 From: james <1943357252@qq.com> Date: Thu, 8 Aug 2024 12:32:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=A5=E6=8E=A7=E9=A1=B5=E9=9D=A2=E6=9B=B4?= =?UTF-8?q?=E6=8D=A2=E5=8F=8A=E6=9A=82=E5=81=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/main.c | 1 + my_pro/myinfrared.c | 28 +++++++++++++++++++--------- my_pro/myproject.c | 26 ++++++++++++++++++-------- my_pro/mysnake.c | 23 +++++++++++++++++++---- 4 files changed, 57 insertions(+), 21 deletions(-) diff --git a/applications/main.c b/applications/main.c index 1500f18..9e15692 100644 --- a/applications/main.c +++ b/applications/main.c @@ -39,6 +39,7 @@ int main(void) rt_wlan_config_autoreconnect(RT_TRUE); rt_wlan_connect("Dong", "abcd07691234"); system("myproject"); + // system("mqtt"); mytime(); /* init Wi-Fi auto connect feature */ diff --git a/my_pro/myinfrared.c b/my_pro/myinfrared.c index 2dc5dbc..ad66da4 100644 --- a/my_pro/myinfrared.c +++ b/my_pro/myinfrared.c @@ -16,30 +16,40 @@ extern rt_atomic_t now_direction; extern rt_atomic_t snake_pressed; -extern rt_atomic_t page_chosen ; +extern rt_atomic_t page_chosen; +extern rt_atomic_t page_first; +extern rt_atomic_t page_stop; extern int snake_max; extern char tmp[10]; -void snake_compare(rt_uint8_t key) +void snake_compare(rt_uint8_t key, rt_uint8_t repeat) { rt_sprintf(tmp, "%02X", key); rt_atomic_store(&snake_pressed, snake_max + 1); - if (rt_strcmp(tmp, "30") == 0|| rt_strcmp(tmp, "53") == 0) + if (rt_strcmp(tmp, "30") == 0 || rt_strcmp(tmp, "53") == 0) if (rt_atomic_load(&now_direction) != 2) rt_atomic_store(&now_direction, 0); - if (rt_strcmp(tmp, "E8") == 0|| rt_strcmp(tmp, "99") == 0) + if (rt_strcmp(tmp, "E8") == 0 || rt_strcmp(tmp, "99") == 0) if (rt_atomic_load(&now_direction) != 3) rt_atomic_store(&now_direction, 1); - if (rt_strcmp(tmp, "B0") == 0|| rt_strcmp(tmp, "4B") == 0) + if (rt_strcmp(tmp, "B0") == 0 || rt_strcmp(tmp, "4B") == 0) if (rt_atomic_load(&now_direction) != 0) rt_atomic_store(&now_direction, 2); - if (rt_strcmp(tmp, "68") == 0|| rt_strcmp(tmp, "83") == 0) + if (rt_strcmp(tmp, "68") == 0 || rt_strcmp(tmp, "83") == 0) if (rt_atomic_load(&now_direction) != 1) rt_atomic_store(&now_direction, 3); - // if (rt_strcmp(tmp, "88") == 0) - // page_chosen = (page_chosen % PAGE_MAX) + 1; + if (repeat == 0 && (rt_strcmp(tmp, "88") == 0 || rt_strcmp(tmp, "11") == 0)) + { + page_chosen = (page_chosen % PAGE_MAX) + 1; + page_first = 1; + rt_kprintf("page_chosen = %d\n", page_chosen); + } + if (repeat == 0 && (rt_strcmp(tmp, "73") == 0 )) + { + page_stop = (page_stop + 1) % 2; + } } void myir_entry(void *parameter) @@ -67,7 +77,7 @@ void myir_entry(void *parameter) rt_pin_write(PIN_LED_R, PIN_LOW); LOG_I("RECEIVE OK: addr:0x%02X key:0x%02X repeat:%d", infrared_data.data.nec.addr, infrared_data.data.nec.key, infrared_data.data.nec.repeat); - snake_compare(infrared_data.data.nec.key); + snake_compare(infrared_data.data.nec.key, infrared_data.data.nec.repeat); } rt_thread_mdelay(50); diff --git a/my_pro/myproject.c b/my_pro/myproject.c index 9d42032..1cec2dc 100644 --- a/my_pro/myproject.c +++ b/my_pro/myproject.c @@ -31,6 +31,8 @@ char buffer[1026] = {}; char tmp[1026]; extern int snake_len; rt_atomic_t page_chosen = 1; +rt_atomic_t page_first = 1; +rt_atomic_t page_stop = 0; #define PAGE_MAX 2 @@ -199,14 +201,21 @@ void tmp_payload(void) // sprintf(tmp, "Temp: %.1f;Humi: %.1f;Count: %d\n", Temp, Humi,++cnt); // rt_kprintf("\n%f %f tmp:%s\n",Humi,Temp,tmp); // make_file(); - // if (page_chosen == 2) - // { - // show_lcd(); - // } - // if (ps_data > 14) - // { - // page_chosen = (page_chosen % PAGE_MAX) + 1; - // } + if (page_chosen == 2 && !page_stop) + { + if (page_first) + { + my_round(20); + page_first = 0; + } + + show_lcd(); + } + if (ps_data > 14) + { + page_chosen = (page_chosen % PAGE_MAX) + 1; + page_first = 1; + } sprintf(tmp, "{\"params\":{\"temperature\":%.2f,\"humidity\":%.2f,\"LightLux\":%.2f,\"Psdata\":%d,\"Snakelen\":%d}}", Temp, Humi, brightness, ps_data, snake_len); return; } @@ -338,6 +347,7 @@ void mqt_init(void) rt_kprintf("MQTT Thread Create Failed!\n"); } } +// MSH_CMD_EXPORT_ALIAS(mqt_init, mqtt, run my mqtt); int ap3_init(void) { const char *i2c_bus_name = "i2c2"; diff --git a/my_pro/mysnake.c b/my_pro/mysnake.c index 861b1a0..37cd68a 100644 --- a/my_pro/mysnake.c +++ b/my_pro/mysnake.c @@ -8,10 +8,12 @@ #define LCD_MAX 240 #define SNAKE_SIZE 20 -#define SNAKE_MAX LCD_MAX / SNAKE_SIZE +#define SNAKE_MAX (LCD_MAX / SNAKE_SIZE) rt_atomic_t now_direction = 3; rt_atomic_t snake_pressed = 0; extern rt_atomic_t page_chosen; +extern rt_atomic_t page_first; +extern rt_atomic_t page_stop; int snake_max = SNAKE_MAX * 3; int snake_len = 3; @@ -51,13 +53,26 @@ void snake_entry(void *parameter) snake_address(snake_list[2][0], snake_list[2][1], SNAKE_SIZE, BLACK); snake_food[0] = rand() % SNAKE_MAX; snake_food[1] = rand() % SNAKE_MAX; - snake_address(snake_food[0], snake_food[1], SNAKE_SIZE, GREEN); + // snake_address(snake_food[0], snake_food[1], SNAKE_SIZE, GREEN); int new_head_x = 0, new_head_y = 0; - int new_direction = 0; + int new_direction = 0,snake_now=0; while (1) { - if (page_chosen == 1) + if (page_chosen == 1&&!page_stop) { + if (page_first == 1) + { + page_first = 0; + lcd_fill(0, 0, 240, 240, WHITE); + snake_address(snake_food[0], snake_food[1], SNAKE_SIZE, GREEN); + snake_now = snake_tail-1; + // for (int i = 0; i = snake_len && i < SNAKE_MAX - 1; i++) + do{ + snake_now=(snake_now+1)%SNAKE_MAX; + snake_address(snake_list[snake_now][0], snake_list[snake_now][1], SNAKE_SIZE, BLACK); + }while(snake_now!=snake_head); + } + if (!snake_pressed) { // 50%的概率保持当前方向,20%的概率随机改变方向