This commit is contained in:
zogodo
2019-11-02 17:14:59 +08:00
parent fd51255396
commit 3761dacd69
2 changed files with 43 additions and 11 deletions

View File

@@ -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);
}

View File

@@ -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)