mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-12 21:18:13 +08:00
93 lines
2.0 KiB
C
93 lines
2.0 KiB
C
#define os_log(format, ...) custom_log("OTA", format, ##__VA_ARGS__)
|
|
#include "TimeUtils.h"
|
|
|
|
#include "mico.h"
|
|
#include "main.h"
|
|
#include "user_udp.h"
|
|
#include "user_mqtt_client.h"
|
|
#include "user_function.h"
|
|
#include "user_power.h"
|
|
|
|
mico_timer_t power_timer;
|
|
|
|
PowerRecord power_record = { 1, { 0 } };
|
|
|
|
static uint32_t clock_count_last = 0;
|
|
static uint32_t clock_count = 0;
|
|
static uint32_t timer_count = 0;
|
|
static uint32_t timer_irq_count = 0;
|
|
|
|
char power_record_str[1101] = { 0 };
|
|
|
|
void SetPowerRecord(PowerRecord* pr, uint32_t pw)
|
|
{
|
|
pr->powers[(++pr->idx)% PW_NUM] = pw;
|
|
}
|
|
|
|
char* GetPowerRecord(int idx)
|
|
{
|
|
if (idx > power_record.idx) return "";
|
|
|
|
int i = idx > 0 ? idx : (power_record.idx - PW_NUM + 1);
|
|
i = i < 0 ? 0 : i;
|
|
char* tmp = power_record_str;
|
|
for (; i <= power_record.idx; i++)
|
|
{
|
|
sprintf(tmp, "%u,", (unsigned int)power_record.powers[i%PW_NUM]);
|
|
tmp += strlen(tmp);
|
|
}
|
|
*(--tmp) = 0;
|
|
return power_record_str;
|
|
}
|
|
|
|
static void PowerTimerHandler(void* arg)
|
|
{
|
|
uint32_t timer = 0;
|
|
|
|
if (timer_irq_count > 1)
|
|
{
|
|
timer = (clock_count - clock_count_last);
|
|
|
|
if (timer_count > 3)
|
|
{
|
|
timer /= 1000;
|
|
timer += 4294967; //0xffffffff/1000;
|
|
}
|
|
else if (clock_count < clock_count_last)
|
|
{
|
|
timer += 0xffffffff;
|
|
timer /= 1000;
|
|
}
|
|
else
|
|
{
|
|
timer /= 1000;
|
|
}
|
|
power = 17100000 * (timer_irq_count - 1) / timer;
|
|
timer_count = 0;
|
|
timer_irq_count = 0;
|
|
}
|
|
else
|
|
{
|
|
timer_count++;
|
|
}
|
|
}
|
|
|
|
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 PowerInit(void)
|
|
{
|
|
os_log("user_power_init");
|
|
|
|
MicoGpioInitialize(POWER, INPUT_PULL_UP);
|
|
mico_rtos_init_timer(&power_timer, 1000, PowerTimerHandler, NULL);
|
|
mico_rtos_start_timer(&power_timer);
|
|
|
|
MicoGpioEnableIRQ(POWER, IRQ_TRIGGER_FALLING_EDGE, PowerIrqHandler, NULL);
|
|
}
|
|
|