From dfbc9738881ad6bce43e2c8e52a56de466b86f0a Mon Sep 17 00:00:00 2001 From: zogodo <742782908@qq.com> Date: Thu, 10 Oct 2019 15:23:45 +0800 Subject: [PATCH] power --- TC1/main.c | 50 +++++++++++++++++++++++++++--------------------- TC1/user_power.c | 11 +++++------ TC1/user_power.h | 2 +- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/TC1/main.c b/TC1/main.c index 08f331b..e997d18 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -27,7 +27,7 @@ void appRestoreDefault_callback(void * const user_config_data, uint32_t size) { UNUSED_PARAMETER(size); - mico_system_context_get()->micoSystemConfig.name[0] = 1; //在下次重启时使用默认名称 + mico_system_context_get()->micoSystemConfig.name[0] = 1; //在下次重启时使用默认名称 mico_system_context_get()->micoSystemConfig.name[1] = 0; user_config_t* userConfigDefault = user_config_data; @@ -51,8 +51,8 @@ void appRestoreDefault_callback(void * const user_config_data, uint32_t size) userConfigDefault->socket[i].name[5] = 0xa3; userConfigDefault->socket[i].name[6] = i + '1'; userConfigDefault->socket[i].name[7] = 0; + //sprintf(userConfigDefault->socket[i].name, "插座%d", i);//编码异常 -// sprintf(userConfigDefault->socket[i].name, "插座%d", i);//编码异常 for (j = 0; j < SOCKET_TIME_TASK_NUM; j++) { userConfigDefault->socket[i].task[j].hour = 0; @@ -62,7 +62,7 @@ void appRestoreDefault_callback(void * const user_config_data, uint32_t size) userConfigDefault->socket[i].task[j].action = 1; } } -// mico_system_context_update(sys_config); + //mico_system_context_update(sys_config); } int application_start(void) @@ -71,10 +71,9 @@ int application_start(void) os_log("Start %s",VERSION); char main_num=0; - uint32_t power_last = 0xffffffff; OSStatus err = kNoErr; - /* Create mico system context and read application's config data from flash */ + // Create mico system context and read application's config data from flash sys_config = mico_system_context_init(sizeof(user_config_t)); user_config = ((system_context_t*)sys_config)->user_config_data; require_action(user_config, exit, err = kNoMemoryErr); @@ -99,7 +98,9 @@ int application_start(void) } MicoSysLed(0); - if (user_config->version != USER_CONFIG_VERSION || user_config->socket[0].task[0].hour < 0 || user_config->socket[0].task[0].hour > 23) + if (user_config->version != USER_CONFIG_VERSION + || user_config->socket[0].task[0].hour < 0 + || user_config->socket[0].task[0].hour > 23) { os_log("WARNGIN: user params restored!"); err = mico_system_context_restore(sys_config); @@ -109,8 +110,8 @@ int application_start(void) if (sys_config->micoSystemConfig.name[0] == 1) { IPStatusTypedef para; - os_log("micoWlanGetIPStatus:%d", micoWlanGetIPStatus(¶, Station)); //mac读出来全部是0??!!! - strcpy(strMac, para.mac); + os_log("micoWlanGetIPStatus:%d", micoWlanGetIPStatus(¶, Station)); + strcpy(strMac, para.mac); //mac读出来全部是0??!!! os_log("result:%s",strMac); os_log("result:%s",para.mac); @@ -132,8 +133,15 @@ int application_start(void) WifiInit(); if (!open_ap) { - if (sys_config->micoSystemConfig.reserved != NOTIFY_STATION_UP) ApInit(); - else WifiConnect(sys_config->micoSystemConfig.ssid, sys_config->micoSystemConfig.user_key); + if (sys_config->micoSystemConfig.reserved != NOTIFY_STATION_UP) + { + ApInit(); + } + else + { + WifiConnect(sys_config->micoSystemConfig.ssid, + sys_config->micoSystemConfig.user_key); + } } user_udp_init(); KeyInit(); @@ -141,26 +149,23 @@ int application_start(void) require_noerr(err, exit); err = user_rtc_init(); require_noerr(err, exit); - user_power_init(); + PowerInit(); - /* start http server thread */ - AppHttpdStart(); + uint32_t power_last = 0xffffffff; + AppHttpdStart(); // start http server thread + char* power_buf = malloc(128); + if (!power_buf) goto exit; while (1) { main_num++; //发送功率数据 - if (power_last != power || main_num>4) + if (power_last != power || main_num > 4) { power_last = power; main_num =0; - char* power_buf = malloc(128); - if (power_buf != NULL) - { - sprintf(power_buf, "{\"mac\":\"%s\",\"power\":\"%u.%u\",\"total_time\":%u}", - strMac, (unsigned int)(power/10), (unsigned int)(power%10), (unsigned int)total_time); - user_send(0, power_buf); - free(power_buf); - } + sprintf(power_buf, "{\"mac\":\"%s\",\"power\":\"%u.%u\",\"total_time\":%u}", + strMac, (unsigned int)(power / 10), (unsigned int)(power % 10), (unsigned int)total_time); + user_send(0, power_buf); user_mqtt_hass_power(); } mico_thread_msleep(1000); @@ -168,6 +173,7 @@ int application_start(void) exit: os_log("application_start ERROR!"); + if (power_buf) free(power_buf); return 0; } diff --git a/TC1/user_power.c b/TC1/user_power.c index 3723bb6..00e770b 100644 --- a/TC1/user_power.c +++ b/TC1/user_power.c @@ -14,9 +14,8 @@ static uint32_t clock_count = 0; static uint32_t timer_count = 0; static uint32_t timer_irq_count = 0; -static void power_timer_handler(void* arg) +static void PowerTimerHandler(void* arg) { - uint32_t timer = 0; if (timer_irq_count > 1) @@ -47,21 +46,21 @@ static void power_timer_handler(void* arg) } } -static void power_irq_handler(void* arg) +static void PowerIrqHandler(void* arg) { clock_count = mico_nanosecond_clock_value(); if (timer_irq_count == 0) clock_count_last = clock_count; timer_irq_count++; } -void user_power_init(void) +void PowerInit(void) { os_log("user_power_init"); MicoGpioInitialize(POWER, INPUT_PULL_UP); - mico_rtos_init_timer(&power_timer, 1000, power_timer_handler, NULL); + mico_rtos_init_timer(&power_timer, 1000, PowerTimerHandler, NULL); mico_rtos_start_timer(&power_timer); - MicoGpioEnableIRQ(POWER, IRQ_TRIGGER_FALLING_EDGE, power_irq_handler, NULL); + MicoGpioEnableIRQ(POWER, IRQ_TRIGGER_FALLING_EDGE, PowerIrqHandler, NULL); } diff --git a/TC1/user_power.h b/TC1/user_power.h index 9013722..b4db8ff 100644 --- a/TC1/user_power.h +++ b/TC1/user_power.h @@ -1,6 +1,6 @@ #ifndef __USER_POWER_H_ #define __USER_POWER_H_ -void user_power_init(void); +void PowerInit(void); #endif