mirror of
https://github.com/No-Chicken/Power-Pico.git
synced 2026-04-03 13:02:36 +08:00
保留总显示数字4位
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#include "data_queue.h"
|
#include "data_queue.h"
|
||||||
#include "math.h"
|
#include "math.h"
|
||||||
#include "BL24C02.h" // system settings
|
#include "BL24C02.h" // system settings
|
||||||
|
#include "rtc.h" // elapsed time
|
||||||
|
|
||||||
lv_obj_t * ui_HomeScreen = NULL;
|
lv_obj_t * ui_HomeScreen = NULL;
|
||||||
static lv_obj_t * ui_ButVal = NULL;
|
static lv_obj_t * ui_ButVal = NULL;
|
||||||
@@ -48,69 +49,72 @@ void ui_main_page_key_handler(uint8_t key_id)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void fmt_sig4(char *buf, size_t size, float v)
|
||||||
|
{
|
||||||
|
if (!isfinite(v)) { snprintf(buf, size, "--"); return; }
|
||||||
|
if (fabsf(v) < 1e-9f) v = 0.0f; // 处理接近零的值
|
||||||
|
float av = fabsf(v);
|
||||||
|
int int_digits = (av >= 1.0f) ? ((int)floorf(log10f(av)) + 1) : 0;
|
||||||
|
if(int_digits >= 4) {
|
||||||
|
snprintf(buf, size, "%.0f", roundf(v));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int decimals = (int_digits == 0) ? 3 : (4 - int_digits); // 确保总数字为 4
|
||||||
|
snprintf(buf, size, "%.*f", decimals, v);
|
||||||
|
}
|
||||||
|
|
||||||
static void set_val_cur_label(float voltage, float current)
|
static void set_val_cur_label(float voltage, float current)
|
||||||
{
|
{
|
||||||
char buf[16];
|
char buf[16];
|
||||||
// voltage
|
|
||||||
sprintf(buf, "%.2f", voltage);
|
// voltage (单位: V) -> 4 位有效数字
|
||||||
|
fmt_sig4(buf, sizeof(buf), voltage);
|
||||||
lv_label_set_text(ui_LabelValt, buf);
|
lv_label_set_text(ui_LabelValt, buf);
|
||||||
// current
|
|
||||||
if (fabs(current) >= 1000000.0) {
|
// current (原始单位: uA)
|
||||||
current = current / 1000000.0;
|
float cur_uA = current;
|
||||||
sprintf(buf, "%.2f", current); // 保留 2 位小数
|
float cur_abs_uA = fabsf(cur_uA);
|
||||||
|
float cur_disp = 0.0f;
|
||||||
|
|
||||||
|
if (cur_abs_uA >= 1e6f) {
|
||||||
|
cur_disp = cur_uA / 1e6f; // A
|
||||||
|
fmt_sig4(buf, sizeof(buf), cur_disp);
|
||||||
lv_label_set_text(ui_LabelCur, buf);
|
lv_label_set_text(ui_LabelCur, buf);
|
||||||
lv_label_set_text(ui_LabelUnitCur, "A");
|
lv_label_set_text(ui_LabelUnitCur, "A");
|
||||||
} else if(fabs(current) >= 1000.0) {
|
} else if (cur_abs_uA >= 1e3f) {
|
||||||
current = current / 1000.0;
|
cur_disp = cur_uA / 1e3f; // mA
|
||||||
if(fabs(current) >= 100.0) {
|
fmt_sig4(buf, sizeof(buf), cur_disp);
|
||||||
sprintf(buf, "%.1f", current);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
sprintf(buf, "%.2f", current);
|
|
||||||
}
|
|
||||||
lv_label_set_text(ui_LabelCur, buf);
|
lv_label_set_text(ui_LabelCur, buf);
|
||||||
lv_label_set_text(ui_LabelUnitCur, "mA");
|
lv_label_set_text(ui_LabelUnitCur, "mA");
|
||||||
} else {
|
} else {
|
||||||
if(fabs(current) >= 100.0) {
|
cur_disp = cur_uA; // uA
|
||||||
sprintf(buf, "%.1f", current);
|
fmt_sig4(buf, sizeof(buf), cur_disp);
|
||||||
}
|
|
||||||
else {
|
|
||||||
sprintf(buf, "%.2f", current);
|
|
||||||
}
|
|
||||||
lv_label_set_text(ui_LabelCur, buf);
|
lv_label_set_text(ui_LabelCur, buf);
|
||||||
lv_label_set_text(ui_LabelUnitCur, "uA");
|
lv_label_set_text(ui_LabelUnitCur, "uA");
|
||||||
}
|
}
|
||||||
// set power
|
|
||||||
float power = voltage * current; // in uW
|
// power = V * A,按 W/mW/uW 选择单位,4 位有效数字
|
||||||
if(fabs(power) >= 1000000.0) {
|
float power_W = voltage * (cur_uA / 1e6f);
|
||||||
power = power / 1000000.0;
|
float p_abs = fabsf(power_W);
|
||||||
sprintf(buf, "%.2f", power);
|
|
||||||
|
if (p_abs >= 1.0f) {
|
||||||
|
fmt_sig4(buf, sizeof(buf), power_W); // W
|
||||||
lv_label_set_text(ui_LabelEnerge, buf);
|
lv_label_set_text(ui_LabelEnerge, buf);
|
||||||
lv_label_set_text(ui_LabelUnitEnerge, "W");
|
lv_label_set_text(ui_LabelUnitEnerge, "W");
|
||||||
} else if(fabs(power) >= 1000.0) {
|
} else if (p_abs >= 1e-3f) {
|
||||||
power = power / 1000.0;
|
fmt_sig4(buf, sizeof(buf), power_W * 1e3f); // mW
|
||||||
if(fabs(power) >= 100.0) {
|
|
||||||
sprintf(buf, "%.1f", power);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
sprintf(buf, "%.2f", power);
|
|
||||||
}
|
|
||||||
lv_label_set_text(ui_LabelEnerge, buf);
|
lv_label_set_text(ui_LabelEnerge, buf);
|
||||||
lv_label_set_text(ui_LabelUnitEnerge, "mW");
|
lv_label_set_text(ui_LabelUnitEnerge, "mW");
|
||||||
} else {
|
} else {
|
||||||
if(fabs(power) >= 100.0) {
|
fmt_sig4(buf, sizeof(buf), power_W * 1e6f); // uW
|
||||||
sprintf(buf, "%.1f", power);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
sprintf(buf, "%.2f", power);
|
|
||||||
}
|
|
||||||
lv_label_set_text(ui_LabelEnerge, buf);
|
lv_label_set_text(ui_LabelEnerge, buf);
|
||||||
lv_label_set_text(ui_LabelUnitEnerge, "uW");
|
lv_label_set_text(ui_LabelUnitEnerge, "uW");
|
||||||
}
|
}
|
||||||
// set time
|
|
||||||
|
// time
|
||||||
uint8_t hours = 0, minutes = 0, seconds = 0;
|
uint8_t hours = 0, minutes = 0, seconds = 0;
|
||||||
GetElapsedTime_HMS(&hours, &minutes, &seconds);
|
GetElapsedTime_HMS(&hours, &minutes, &seconds);
|
||||||
sprintf(buf, "%02d:%02d:%02d", hours, minutes, seconds);
|
snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hours, minutes, seconds);
|
||||||
lv_label_set_text(ui_LabelTime, buf);
|
lv_label_set_text(ui_LabelTime, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user