move battery measurement code

This commit is contained in:
Michele Balistreri 2024-02-16 15:03:20 +01:00
parent b4635f8f38
commit 79b0211052
No known key found for this signature in database
GPG Key ID: E9567DA33A4F791A
4 changed files with 26 additions and 22 deletions

View File

@ -22,7 +22,7 @@ APP_DEF_TASK(ui, UI_STACK_SIZE);
#define FW_MAJOR 0
#define FW_MINOR 6
#define FW_PATCH 1
#define FW_PATCH 2
__attribute__((section(".fw_signature"))) __attribute__((__used__)) const uint8_t FW_SIGNATURE[64];
__attribute__((section(".fw_version"))) __attribute__((__used__)) const uint8_t FW_VERSION[4] = { FW_MAJOR, FW_MINOR, FW_PATCH, 0};

View File

@ -8,6 +8,10 @@
#include "pwr.h"
#include "usb/usb.h"
#define VBAT_MIN 3200
#define VBAT_MAX 4100
#define VBAT_USB 4600
static void pwr_graceful_shutdown() {
while(hal_flash_busy()) {
;
@ -55,3 +59,18 @@ void pwr_smartcard_removed() {
void pwr_inactivity_timer_elapsed() {
pwr_shutdown();
}
uint8_t pwr_battery_level() {
uint32_t vbat;
hal_adc_read(ADC_VBAT, &vbat);
if (vbat > VBAT_USB) {
return PWR_BATTERY_CHARGING;
} else if (vbat > VBAT_MAX) {
vbat = VBAT_MAX;
} else if (vbat < VBAT_MIN) {
vbat = VBAT_MIN;
}
return ((vbat - VBAT_MIN) * 100) / (VBAT_MAX - VBAT_MIN);
}

View File

@ -3,6 +3,8 @@
#include <stdbool.h>
#define PWR_BATTERY_CHARGING 255
void pwr_reboot();
void pwr_shutdown();
@ -14,4 +16,6 @@ void pwr_smartcard_removed();
void pwr_inactivity_timer_elapsed();
uint8_t pwr_battery_level();
#endif

View File

@ -4,6 +4,7 @@
#include "app_tasks.h"
#include "core/settings.h"
#include "pwr.h"
#include "qrcode/qrout.h"
#include "qrcode/qrscan.h"
#include "screen/screen.h"
@ -16,26 +17,6 @@
struct ui_cmd g_ui_cmd;
struct ui_ctx g_ui_ctx;
#define VBAT_MIN 3200
#define VBAT_MAX 4000
#define VBAT_USB 4600
static void ui_read_battery() {
uint32_t vbat;
hal_adc_read(ADC_VBAT, &vbat);
if (vbat > VBAT_USB) {
g_ui_ctx.battery = 255;
return;
} else if (vbat > VBAT_MAX) {
vbat = VBAT_MAX;
} else if (vbat < VBAT_MIN) {
vbat = VBAT_MIN;
}
g_ui_ctx.battery = ((vbat - VBAT_MIN) * 100) / (VBAT_MAX - VBAT_MIN);
}
void ui_task_entry(void* pvParameters) {
if (screen_init() != HAL_SUCCESS) {
vTaskSuspend(NULL);
@ -49,7 +30,7 @@ void ui_task_entry(void* pvParameters) {
hal_inactivity_timer_set(g_settings.shutdown_timeout);
while(1) {
ui_read_battery();
g_ui_ctx.battery = pwr_battery_level();
if (!g_ui_cmd.received && ((ui_wait_event(portMAX_DELAY) & UI_CMD_EVT) == 0)) {
continue;