From bfae22ddd616350fb1743891fc4181e715deabdb Mon Sep 17 00:00:00 2001 From: nhkefus Date: Wed, 19 Mar 2025 09:55:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B7=B2=E7=9F=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TC1/http_server/web/index.html | 2 +- TC1/main.h | 4 +- TC1/mqtt_server/user_mqtt_client.c | 28 ++--- TC1/user_gpio.c | 158 +++++++++++++---------------- 4 files changed, 88 insertions(+), 104 deletions(-) diff --git a/TC1/http_server/web/index.html b/TC1/http_server/web/index.html index 53f2e46..e40edb8 100644 --- a/TC1/http_server/web/index.html +++ b/TC1/http_server/web/index.html @@ -519,7 +519,7 @@
- 提示:长按电源按钮5s可开启热点重新配网,10s恢复出厂设置 + 提示:长按电源按钮5s,指示灯闪烁3次可开启热点重新配网,10s指示灯保持闪烁恢复出厂设置
diff --git a/TC1/main.h b/TC1/main.h index b783158..ee47a26 100644 --- a/TC1/main.h +++ b/TC1/main.h @@ -19,9 +19,9 @@ #define VERSION "v2.1.6" #define TYPE 1 -#define TYPE_NAME "zTC1" +#define TYPE_NAME "TC1" -#define ZTC1_NAME "zTC1-%s" +#define ZTC1_NAME "TC1-%s" #define USER_CONFIG_VERSION 9 #define SETTING_MQTT_STRING_LENGTH_MAX 32 //必须4字节对齐。 diff --git a/TC1/mqtt_server/user_mqtt_client.c b/TC1/mqtt_server/user_mqtt_client.c index 110895c..c66ac52 100644 --- a/TC1/mqtt_server/user_mqtt_client.c +++ b/TC1/mqtt_server/user_mqtt_client.c @@ -517,12 +517,12 @@ void UserMqttHassAuto(char socket_id) { "\"pl_off\":\"set socket %s %d 0\"," "\"device\":{" "\"identifiers\":[\"tc1_%s\"]," - "\"name\":\"TC1_%s\"," + "\"name\":\"TC1\"," "\"model\":\"TC1\"," "\"manufacturer\":\"PHICOMM\"}}", user_config->socket_names[(int)socket_id], str_mac, socket_id,str_mac, socket_id, str_mac, socket_id, str_mac, - socket_id, str_mac, socket_id, str_mac, str_mac); + socket_id, str_mac, socket_id, str_mac); UserMqttSendTopic(topic_buf, send_buf, 1); } if (send_buf) @@ -548,10 +548,10 @@ void UserMqttHassAutoLed(void) { "\"pl_off\":\"set led %s 0\"," "\"device\":{" "\"identifiers\":[\"tc1_%s\"]," - "\"name\":\"TC1_%s\"," + "\"name\":\"TC1\"," "\"model\":\"TC1\"," "\"manufacturer\":\"PHICOMM\"}}", - str_mac,str_mac,str_mac, str_mac, str_mac, str_mac, str_mac); + str_mac,str_mac,str_mac, str_mac, str_mac, str_mac); UserMqttSendTopic(topic_buf, send_buf, 1); } if (send_buf) @@ -577,10 +577,10 @@ void UserMqttHassAutoTotalSocket(void) { "\"pl_off\":\"set total_socket %s 0\"," "\"device\":{" "\"identifiers\":[\"tc1_%s\"]," - "\"name\":\"TC1_%s\"," + "\"name\":\"TC1\"," "\"model\":\"TC1\"," "\"manufacturer\":\"PHICOMM\"}}", - str_mac, str_mac, str_mac, str_mac, str_mac, str_mac, str_mac); + str_mac, str_mac, str_mac, str_mac, str_mac, str_mac); UserMqttSendTopic(topic_buf, send_buf, 1); } if (send_buf) @@ -606,10 +606,10 @@ void UserMqttHassAutoPower(void) { "\"icon\":\"mdi:gauge\"," "\"value_template\":\"{{ value_json.power }}\",""\"device\":{" "\"identifiers\":[\"tc1_%s\"]," - "\"name\":\"TC1_%s\"," + "\"name\":\"TC1\"," "\"model\":\"TC1\"," "\"manufacturer\":\"PHICOMM\"}}", - str_mac,str_mac, str_mac, str_mac, str_mac); + str_mac,str_mac, str_mac, str_mac); UserMqttSendTopic(topic_buf, send_buf, 1); sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumption/config", str_mac); sprintf(send_buf, @@ -621,10 +621,10 @@ void UserMqttHassAutoPower(void) { "\"icon\":\"mdi:fence-electric\"," "\"value_template\":\"{{ value_json.powerConsumption }}\",""\"device\":{" "\"identifiers\":[\"tc1_%s\"]," - "\"name\":\"TC1_%s\"," + "\"name\":\"TC1\"," "\"model\":\"TC1\"," "\"manufacturer\":\"PHICOMM\"}}", - str_mac, str_mac, str_mac, str_mac, str_mac); + str_mac, str_mac, str_mac, str_mac); UserMqttSendTopic(topic_buf, send_buf, 1); sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionToday/config", str_mac); @@ -637,10 +637,10 @@ void UserMqttHassAutoPower(void) { "\"icon\":\"mdi:fence-electric\"," "\"value_template\":\"{{ value_json.powerConsumptionToday }}\",""\"device\":{" "\"identifiers\":[\"tc1_%s\"]," - "\"name\":\"TC1_%s\"," + "\"name\":\"TC1\"," "\"model\":\"TC1\"," "\"manufacturer\":\"PHICOMM\"}}", - str_mac,str_mac, str_mac, str_mac, str_mac); + str_mac,str_mac, str_mac, str_mac); UserMqttSendTopic(topic_buf, send_buf, 1); sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionYesterday/config", str_mac); @@ -653,10 +653,10 @@ void UserMqttHassAutoPower(void) { "\"icon\":\"mdi:fence-electric\"," "\"value_template\":\"{{ value_json.powerConsumptionYesterday }}\",""\"device\":{" "\"identifiers\":[\"tc1_%s\"]," - "\"name\":\"TC1_%s\"," + "\"name\":\"TC1\"," "\"model\":\"TC1\"," "\"manufacturer\":\"PHICOMM\"}}", - str_mac,str_mac, str_mac, str_mac, str_mac); + str_mac,str_mac, str_mac, str_mac); UserMqttSendTopic(topic_buf, send_buf, 1); } if (send_buf) free(send_buf); diff --git a/TC1/user_gpio.c b/TC1/user_gpio.c index 3ff0a7e..b455130 100644 --- a/TC1/user_gpio.c +++ b/TC1/user_gpio.c @@ -4,11 +4,10 @@ #include "user_gpio.h" #include "mqtt_server/user_mqtt_client.h" -mico_gpio_t relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 }; -char socket_status[32] = { 0 }; +mico_gpio_t relay[Relay_NUM] = {Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5}; +char socket_status[32] = {0}; -void UserLedSet(char x) -{ +void UserLedSet(char x) { if (x == -1) MicoGpioOutputTrigger(Led); else if (x) @@ -17,43 +16,37 @@ void UserLedSet(char x) MicoGpioOutputLow(Led); } -bool RelayOut(void) -{ +bool RelayOut(void) { int i; - for (i = 0; i < SOCKET_NUM; i++) - { - if (user_config->socket_status[i] != 0) - { + for (i = 0; i < SOCKET_NUM; i++) { + if (user_config->socket_status[i] != 0) { return true; } } return false; } -char* GetSocketStatus() -{ +char *GetSocketStatus() { sprintf(socket_status, "%d,%d,%d,%d,%d,%d", - user_config->socket_status[0], - user_config->socket_status[1], - user_config->socket_status[2], - user_config->socket_status[3], - user_config->socket_status[4], - user_config->socket_status[5]); + user_config->socket_status[0], + user_config->socket_status[1], + user_config->socket_status[2], + user_config->socket_status[3], + user_config->socket_status[4], + user_config->socket_status[5]); return socket_status; } -void SetSocketStatus(char* socket_status) -{ +void SetSocketStatus(char *socket_status) { sscanf(socket_status, "%d,%d,%d,%d,%d,%d,", - (int*)&user_config->socket_status[0], - (int*)&user_config->socket_status[1], - (int*)&user_config->socket_status[2], - (int*)&user_config->socket_status[3], - (int*)&user_config->socket_status[4], - (int*)&user_config->socket_status[5]); + (int *) &user_config->socket_status[0], + (int *) &user_config->socket_status[1], + (int *) &user_config->socket_status[2], + (int *) &user_config->socket_status[3], + (int *) &user_config->socket_status[4], + (int *) &user_config->socket_status[5]); int i = 0; - for (i = 0; i < SOCKET_NUM; i++) - { + for (i = 0; i < SOCKET_NUM; i++) { UserRelaySet(i, user_config->socket_status[i]); UserMqttSendSocketState(i); } @@ -66,27 +59,20 @@ void SetSocketStatus(char* socket_status) * i:编号 0-5 * on:开关 0:关 1:开 */ -void UserRelaySet(unsigned char i, unsigned char on) -{ +void UserRelaySet(unsigned char i, unsigned char on) { if (i < 0 || i >= SOCKET_NUM) return; - if (on == Relay_ON) - { + if (on == Relay_ON) { MicoGpioOutputHigh(relay[i]); - } - else - { + } else { MicoGpioOutputLow(relay[i]); } user_config->socket_status[i] = on; - if (RelayOut()&&user_config->power_led_enabled) - { + if (RelayOut() && user_config->power_led_enabled) { UserLedSet(1); - } - else - { + } else { UserLedSet(0); } } @@ -96,110 +82,108 @@ void UserRelaySet(unsigned char i, unsigned char on) * y: 0:全部关 1:全部开 * */ -void UserRelaySetAll(char y) -{ +void UserRelaySetAll(char y) { int i; for (i = 0; i < SOCKET_NUM; i++) UserRelaySet(i, y); } -static void KeyLong5sPress(void) -{ +static void KeyLong5sPress(void) { key_log("WARNGIN: wifi ap started!"); sys_config->micoSystemConfig.ssid[0] = 0; mico_system_context_update(mico_system_context_get()); } -static void KeyLong10sPress(void) -{ +static void KeyLong10sPress(void) { key_log("WARNGIN: user params restored!"); mico_system_context_restore(sys_config); // appRestoreDefault_callback(user_config, sizeof(user_config_t)); // sys_config->micoSystemConfig.ssid[0] = 0; // mico_system_context_update(mico_system_context_get()); } -static void KeyShortPress(void) -{ + +static void KeyShortPress(void) { char i; - if (RelayOut()) - { + if (RelayOut()) { UserRelaySetAll(0); - } - else - { + } else { UserRelaySetAll(1); } - for (i = 0; i < SOCKET_NUM; i++) - { + for (i = 0; i < SOCKET_NUM; i++) { UserMqttSendSocketState(i); } UserMqttSendTotalSocketState(); } + mico_timer_t user_key_timer; uint16_t key_time = 0; #define BUTTON_LONG_PRESS_TIME 10 //100ms*10=1s -static void KeyTimeoutHandler(void* arg) -{ +static void KeyTimeoutHandler(void *arg) { static char key_trigger, key_continue; //按键扫描程序 char tmp = ~(0xfe | MicoGpioInputGet(Button)); key_trigger = tmp & (tmp ^ key_continue); key_continue = tmp; if (key_trigger != 0) key_time = 0; //新按键按下时,重新开始按键计时 - if (key_continue != 0) - { + if (key_continue != 0) { //any button pressed key_time++; - if (key_time > BUTTON_LONG_PRESS_TIME) - { - key_log("button long pressed:%d",key_time); + if (key_time > BUTTON_LONG_PRESS_TIME) { key_log("button long pressed:%d", key_time); - if (key_time == 50) - { + if (key_time == 50) { KeyLong5sPress(); - } - else if (key_time == 100) - { + } else if (key_time > 50 && key_time < 57) { + switch (key_time) { + case 51: + UserLedSet(1); + break; + case 52: + UserLedSet(0); + break; + case 53: + UserLedSet(1); + break; + case 54: + UserLedSet(0); + break; + case 55: + UserLedSet(1); + break; + case 56: + UserLedSet(0); + break; + } + } else if (key_time == 57) { + UserLedSet(RelayOut() && user_config->power_led_enabled); + } else if (key_time == 100) { KeyLong10sPress(); - } - else if (key_time == 102) - { + } else if (key_time == 102) { UserLedSet(1); - } - else if (key_time == 103) - { + } else if (key_time == 103) { UserLedSet(0); key_time = 101; } } - } - else - { + } else { //button released - if (key_time < BUTTON_LONG_PRESS_TIME) - { //100ms*10=1s 大于1s为长按 - key_time = 0; - key_log("button short pressed:%d",key_time); + if (key_time < BUTTON_LONG_PRESS_TIME) { //100ms*10=1s 大于1s为长按 + key_time = 0;key_log("button short pressed:%d", key_time); KeyShortPress(); - } - else if (key_time > 100) - { + } else if (key_time > 100) { MicoSystemReboot(); } mico_rtos_stop_timer(&user_key_timer); } } -static void KeyFallingIrqHandler(void* arg) -{ +static void KeyFallingIrqHandler(void *arg) { mico_rtos_start_timer(&user_key_timer); } -void KeyInit(void) -{ +void KeyInit(void) { MicoGpioInitialize(Button, INPUT_PULL_UP); mico_rtos_init_timer(&user_key_timer, 100, KeyTimeoutHandler, NULL);