From 140b5f4b4ae4fda09cd6d24887f96a00853c1dd0 Mon Sep 17 00:00:00 2001 From: Krzysiek Egzmont Date: Mon, 30 Oct 2023 23:20:32 +0100 Subject: [PATCH] Added second discharge curve for 2200mAh type battery and hidden BatTyp menu entry --- app/menu.c | 25 ++++++++++++---- board.c | 3 +- helper/battery.c | 78 ++++++++++++++++++++++++------------------------ helper/battery.h | 6 ++++ settings.c | 1 + settings.h | 2 ++ ui/menu.c | 13 +++++++- ui/menu.h | 5 ++-- 8 files changed, 84 insertions(+), 49 deletions(-) diff --git a/app/menu.c b/app/menu.c index d05ef74..fb2b16b 100644 --- a/app/menu.c +++ b/app/menu.c @@ -331,8 +331,13 @@ int MENU_GetLimits(uint8_t menu_id, int32_t *pMin, int32_t *pMax) #endif case MENU_BATCAL: - *pMin = 1600; // 0 - *pMax = 2200; // 2300 + *pMin = 1600; + *pMax = 2200; + break; + + case MENU_BATTYP: + *pMin = 0; + *pMax = 1; break; case MENU_F1SHRT: @@ -772,17 +777,21 @@ void MENU_AcceptSetting(void) #endif case MENU_BATCAL: - { + { // voltages are averages between discharge curves of 1600 and 2200 mAh gBatteryCalibration[0] = (520ul * gSubMenuSelection) / 760; // 5.20V empty, blinking above this value, reduced functionality below - gBatteryCalibration[1] = (700ul * gSubMenuSelection) / 760; // 7.00V, ~5%, 1 bars above this value - gBatteryCalibration[2] = (745ul * gSubMenuSelection) / 760; // 7.45V, ~17%, 2 bars above this value + gBatteryCalibration[1] = (689ul * gSubMenuSelection) / 760; // 6.89V, ~5%, 1 bars above this value + gBatteryCalibration[2] = (724ul * gSubMenuSelection) / 760; // 7.24V, ~17%, 2 bars above this value gBatteryCalibration[3] = gSubMenuSelection; // 7.6V, ~29%, 3 bars above this value - gBatteryCalibration[4] = (788ul * gSubMenuSelection) / 760; // 7.88V, ~65%, 4 bars above this value + gBatteryCalibration[4] = (771ul * gSubMenuSelection) / 760; // 7.71V, ~65%, 4 bars above this value gBatteryCalibration[5] = 2300; SETTINGS_SaveBatteryCalibration(gBatteryCalibration); return; } + case MENU_BATTYP: + gEeprom.BATTERY_TYPE = gSubMenuSelection; + break; + case MENU_F1SHRT: case MENU_F1LONG: case MENU_F2SHRT: @@ -1170,6 +1179,10 @@ void MENU_ShowCurrentSetting(void) gSubMenuSelection = gBatteryCalibration[3]; break; + case MENU_BATTYP: + gSubMenuSelection = gEeprom.BATTERY_TYPE; + break; + case MENU_F1SHRT: case MENU_F1LONG: case MENU_F2SHRT: diff --git a/board.c b/board.c index f4b0892..b9a5275 100644 --- a/board.c +++ b/board.c @@ -614,7 +614,8 @@ void BOARD_EEPROM_Init(void) #endif gEeprom.ROGER = (Data[1] < 3) ? Data[1] : ROGER_MODE_OFF; gEeprom.REPEATER_TAIL_TONE_ELIMINATION = (Data[2] < 11) ? Data[2] : 0; - gEeprom.TX_VFO = (Data[3] < 2) ? Data[3] : 0; + gEeprom.TX_VFO = (Data[3] < 2) ? Data[3] : 0; + gEeprom.BATTERY_TYPE = (Data[4] < BATTERY_TYPE_UNKNOWN) ? Data[4] : BATTERY_TYPE_1600_MAH; // 0ED0..0ED7 EEPROM_ReadBuffer(0x0ED0, Data, 8); diff --git a/helper/battery.c b/helper/battery.c index 13e0ec2..5394f63 100644 --- a/helper/battery.c +++ b/helper/battery.c @@ -17,6 +17,7 @@ #include "battery.h" #include "driver/backlight.h" #include "misc.h" +#include "settings.h" #include "ui/battery.h" #include "ui/menu.h" #include "ui/ui.h" @@ -34,48 +35,47 @@ uint16_t gBatteryCheckCounter; volatile uint16_t gPowerSave_10ms; -/* -Based on real measurement -Volts Percent Volts Percent Volts Percent Volts Percent -8.28 100 7.95099 73 7.7184 46 7.48116 19 -8.22 99 7.94188 72 7.71091 45 7.46364 18 -8.17 98 7.9338 71 7.70911 44 7.44789 17 -8.13632 97 7.92684 70 7.70098 43 7.43318 16 -8.12308 96 7.9178 69 7.69619 42 7.41864 15 -8.09688 95 7.90823 68 7.69018 41 7.40579 14 -8.08124 94 7.89858 67 7.68473 40 7.39289 13 -8.06912 93 7.88667 66 7.67911 39 7.37839 12 -8.05826 92 7.87673 65 7.67087 38 7.36017 11 -8.05008 91 7.86864 64 7.66601 37 7.33704 10 -8.04192 90 7.85802 63 7.6599 36 7.3079 9 -8.03866 89 7.84816 62 7.65418 35 7.26793 8 -8.03089 88 7.83744 61 7.64775 34 7.21291 7 -8.0284 87 7.82748 60 7.64065 33 7.13416 6 -8.02044 86 7.81983 59 7.63136 32 7.02785 5 -8.01832 85 7.80929 58 7.6244 31 6.89448 4 -8.01072 84 7.79955 57 7.61636 30 6.72912 3 -8.00965 83 7.79017 56 7.60738 29 6.5164 2 -8.00333 82 7.78107 55 7.597 28 6.19272 1 -7.99973 81 7.77167 54 7.5876 27 5.63138 0 -7.99218 80 7.76509 53 7.57732 26 -7.98999 79 7.75649 52 7.56563 25 -7.98234 78 7.74939 51 7.55356 24 -7.97892 77 7.7411 50 7.54088 23 -7.97043 76 7.73648 49 7.52683 22 -7.96478 75 7.72911 48 7.51285 21 -7.95983 74 7.72097 47 7.49832 20 -*/ unsigned int BATTERY_VoltsToPercent(const unsigned int voltage_10mV) { - if (voltage_10mV > 814) - return 100; - if (voltage_10mV > 756) - return ((132ul * voltage_10mV) / 100) - 974u; - if (voltage_10mV > 729) - return ((63ul * voltage_10mV) / 100) - 452u; - if (voltage_10mV > 600) - return ((52ul * voltage_10mV) / 1000) - 31u; + const uint16_t crv1600[][2] = { + {814, 100}, + {756, 24 }, + {729, 7 }, + {597, 0 }, + {0, 0} + }; + + const uint16_t crv2200[][2] = { + {823, 100}, + {740, 60}, + {707, 21}, + {680, 5}, + {505, 0}, + {0, 0} + }; + + const BATTERY_Type_t type = gEeprom.BATTERY_TYPE; + const uint16_t(*crv)[2]; + uint8_t size; + if (type == BATTERY_TYPE_2200_MAH) { + crv = crv2200; + size = ARRAY_SIZE(crv2200); + } + else { + crv = crv1600; + size = ARRAY_SIZE(crv1600); + } + + const int mulipl = 1000; + for (int i = 1; i < size; i++) { + if (voltage_10mV > crv[i][0]) { + int a = (crv[i - 1][1] - crv[i][1]) * mulipl / (crv[i - 1][0] - crv[i][0]); + int b = crv[i][1] - a * crv[i][0] / mulipl; + int p = a * voltage_10mV / mulipl + b; + return MIN(p, 100); + } + } return 0; } diff --git a/helper/battery.h b/helper/battery.h index 336207c..85ecb0a 100644 --- a/helper/battery.h +++ b/helper/battery.h @@ -33,6 +33,12 @@ extern uint16_t gBatteryCheckCounter; extern volatile uint16_t gPowerSave_10ms; +typedef enum { + BATTERY_TYPE_1600_MAH, + BATTERY_TYPE_2200_MAH, + BATTERY_TYPE_UNKNOWN +} BATTERY_Type_t; + unsigned int BATTERY_VoltsToPercent(const unsigned int voltage_10mV); void BATTERY_GetReadings(const bool bDisplayBatteryLevel); diff --git a/settings.c b/settings.c index b10c5a2..f3f1505 100644 --- a/settings.c +++ b/settings.c @@ -137,6 +137,7 @@ void SETTINGS_SaveSettings(void) State[1] = gEeprom.ROGER; State[2] = gEeprom.REPEATER_TAIL_TONE_ELIMINATION; State[3] = gEeprom.TX_VFO; + State[4] = gEeprom.BATTERY_TYPE; EEPROM_WriteBuffer(0x0EA8, State); State[0] = gEeprom.DTMF_SIDE_TONE; diff --git a/settings.h b/settings.h index 99611c5..d4bce95 100644 --- a/settings.h +++ b/settings.h @@ -21,6 +21,7 @@ #include #include "frequencies.h" +#include #include "radio.h" enum POWER_OnDisplayMode_t { @@ -230,6 +231,7 @@ typedef struct { uint8_t KEY_M_LONG_PRESS_ACTION; uint8_t BACKLIGHT_MIN; uint8_t BACKLIGHT_MAX; + BATTERY_Type_t BATTERY_TYPE; } EEPROM_Config_t; extern EEPROM_Config_t gEeprom; diff --git a/ui/menu.c b/ui/menu.c index 01d8edb..63ce4a6 100644 --- a/ui/menu.c +++ b/ui/menu.c @@ -134,6 +134,7 @@ const t_menu_item MenuList[] = {"FrCali", VOICE_ID_INVALID, MENU_F_CALI }, // reference xtal calibration #endif {"BatCal", VOICE_ID_INVALID, MENU_BATCAL }, // battery voltage calibration + {"BatTyp", VOICE_ID_INVALID, MENU_BATTYP }, // battery type 1600/2200mAh {"Reset", VOICE_ID_INITIALISATION, MENU_RESET }, // might be better to move this to the hidden menu items ? {"", VOICE_ID_INVALID, 0xff } // end of list - DO NOT delete or move this this @@ -316,6 +317,12 @@ const char gSubMenu_BAT_TXT[3][8] = "PERCENT" }; +const char gSubMenu_BATTYP[2][9] = +{ + "1600mAh", + "2200mAh" +}; + const char gSubMenu_SCRAMBLER[11][7] = { "OFF", @@ -835,7 +842,11 @@ void UI_DisplayMenu(void) sprintf(String, "%u.%02uV\n%u", vol / 100, vol % 100, gSubMenuSelection); break; } - + + case MENU_BATTYP: + strcpy(String, gSubMenu_BATTYP[gSubMenuSelection]); + break; + case MENU_F1SHRT: case MENU_F1LONG: case MENU_F2SHRT: diff --git a/ui/menu.h b/ui/menu.h index 57b8ec5..8e02816 100644 --- a/ui/menu.h +++ b/ui/menu.h @@ -120,7 +120,8 @@ enum MENU_F1LONG, MENU_F2SHRT, MENU_F2LONG, - MENU_MLONG + MENU_MLONG, + MENU_BATTYP }; extern const uint8_t FIRST_HIDDEN_MENU_ITEM; @@ -154,7 +155,7 @@ extern const char gSubMenu_RX_TX[4][6]; extern const char gSubMenu_AM_fix_test1[4][8]; #endif extern const char gSubMenu_BAT_TXT[3][8]; - +extern const char gSubMenu_BATTYP[2][9]; extern const char gSubMenu_SCRAMBLER[11][7]; typedef struct {char* name; uint8_t id;} t_sidefunction;