diff --git a/EPD/EPD_driver.c b/EPD/EPD_driver.c index 25967f6..1e837b3 100644 --- a/EPD/EPD_driver.c +++ b/EPD/EPD_driver.c @@ -244,7 +244,7 @@ void EPD_LED_BLINK(void) { } } -float EPD_ReadVoltage(void) { +uint16_t EPD_ReadVoltage(void) { #if defined(S112) volatile int16_t value = 0; NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_10bit; @@ -286,7 +286,7 @@ float EPD_ReadVoltage(void) { NRF_ADC->ENABLE = 0; #endif NRF_LOG_DEBUG("ADC value: %d\n", value); - return (value * 3.6) / (1 << 10); + return (value * 3600) / (1 << 10); } // EPD models diff --git a/EPD/EPD_driver.h b/EPD/EPD_driver.h index 97e3904..2cd4b11 100644 --- a/EPD/EPD_driver.h +++ b/EPD/EPD_driver.h @@ -218,7 +218,7 @@ void EPD_LED_Toggle(void); void EPD_LED_BLINK(void); // VDD voltage -float EPD_ReadVoltage(void); +uint16_t EPD_ReadVoltage(void); epd_model_t* epd_init(epd_model_id_t id); diff --git a/GUI/GUI.c b/GUI/GUI.c index 65cdada..b9fe13e 100644 --- a/GUI/GUI.c +++ b/GUI/GUI.c @@ -129,12 +129,26 @@ static void DrawTimeSyncTip(Adafruit_GFX* gfx, gui_data_t* data) { GFX_printf(gfx, url); } -static void DrawBattery(Adafruit_GFX* gfx, int16_t x, int16_t y, uint8_t iw, float voltage) { +static uint8_t batt_cal(uint16_t voltage) { + uint16_t adc_sample = (voltage * 2047) / 3600; + if (adc_sample > 1705) + return 100; + else if (adc_sample <= 1705 && adc_sample > 1584) + return 28 + (uint8_t)(((((adc_sample - 1584) << 16) / (1705 - 1584)) * 72) >> 16); + else if (adc_sample <= 1584 && adc_sample > 1360) + return 4 + (uint8_t)(((((adc_sample - 1360) << 16) / (1584 - 1360)) * 24) >> 16); + else if (adc_sample <= 1360 && adc_sample > 1136) + return (uint8_t)(((((adc_sample - 1136) << 16) / (1360 - 1136)) * 4) >> 16); + else + return 0; +} + +static void DrawBattery(Adafruit_GFX* gfx, int16_t x, int16_t y, uint8_t iw, uint16_t voltage) { x -= iw; - uint8_t level = (uint8_t)(voltage * 100 / 3.6f); + uint8_t level = batt_cal(voltage); GFX_setFont(gfx, u8g2_font_wqy9_t_lunar); GFX_setCursor(gfx, x - GFX_getUTF8Width(gfx, "3.2V") - 2, y + 9); - GFX_printf(gfx, "%.1fV", voltage); + GFX_printf(gfx, "%d.%dV", voltage / 1000, (voltage % 1000) / 100); GFX_fillRect(gfx, x, y, iw, 10, GFX_WHITE); GFX_drawRect(gfx, x, y, iw, 10, GFX_BLACK); GFX_fillRect(gfx, x + iw, y + 4, 2, 2, GFX_BLACK); diff --git a/GUI/GUI.h b/GUI/GUI.h index fec92f3..33e261d 100644 --- a/GUI/GUI.h +++ b/GUI/GUI.h @@ -17,7 +17,7 @@ typedef struct { uint32_t timestamp; uint8_t week_start; // 0: Sunday, 1: Monday int8_t temperature; - float voltage; + uint16_t voltage; char ssid[20]; } gui_data_t; diff --git a/emulator.c b/emulator.c index 36f2548..f09efc0 100644 --- a/emulator.c +++ b/emulator.c @@ -225,7 +225,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) .timestamp = g_display_time, .week_start = g_week_start, .temperature = 25, - .voltage = 3.2f, + .voltage = 2920, .ssid = "NRF_EPD_84AC", };