From 3761dacd69454ba36f3bf61a10f25e939ad563fc Mon Sep 17 00:00:00 2001 From: zogodo <742782908@qq.com> Date: Sat, 2 Nov 2019 17:14:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TC1/main.c | 20 +++++++++----------- TC1/user_power.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/TC1/main.c b/TC1/main.c index 6f536bf..bed0ea7 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -155,20 +155,18 @@ int application_start(void) AppHttpdStart(); // start http server thread char* power_buf = malloc(128); if (!power_buf) goto exit; + + uint32_t last_p_count = p_count; while (1) { - main_num++; //发送功率数据 - if (power_last != power || main_num > 4) - { - SetPowerRecord(&power_record, power); - power_last = power; - main_num =0; - 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(); - } + uint32_t power2 = 171 * (p_count - last_p_count) / 10; + last_p_count = p_count; + //SetPowerRecord(&power_record, power2); + sprintf(power_buf, "{\"mac\":\"%s\",\"power\":\"%u.%u\",\"total_time\":%u}", + strMac, (unsigned int)(power2 / 10), (unsigned int)(power2 % 10), (unsigned int)total_time); + user_send(0, power_buf); + user_mqtt_hass_power(); mico_thread_msleep(1000); } diff --git a/TC1/user_power.c b/TC1/user_power.c index d8011be..436afb2 100644 --- a/TC1/user_power.c +++ b/TC1/user_power.c @@ -73,12 +73,46 @@ static void PowerTimerHandler(void* arg) } } +float n_1s = 0; +mico_time_t t_x = 0; +mico_time_t past_ms = 0; +mico_time_t rest_ms = 1000; +mico_time_t rest_x_ms = 0; +mico_time_t rest_y_ms = 0; + static void PowerIrqHandler(void* arg) { clock_count = mico_nanosecond_clock_value(); if (timer_irq_count == 0) clock_count_last = clock_count; timer_irq_count++; p_count++; + + mico_time_get_time(&past_ms); + if (t_x == 0) + { + t_x = past_ms - 1; + } + rest_x_ms = past_ms - t_x; + if (rest_x_ms < 1000) + { + n_1s += 1; + rest_y_ms = t_x + 1000 - past_ms; + } + else if (rest_x_ms > 1000 && rest_x_ms < 2000) + { + n_1s += (float)rest_y_ms / (rest_x_ms - 1000 + rest_y_ms); + rest_y_ms = 2000 - t_x; + t_x = past_ms / 1000 * 1000; + + float power2 = 17.1 * n_1s; + SetPowerRecord(&power_record, (int)power2); + n_1s = (float)(rest_x_ms - 1000) / (rest_x_ms - 1000 + rest_y_ms); + } + else + { + SetPowerRecord(&power_record, 123456); + } + } void PowerInit(void)