Fix vbat max cell voltage CMS menu entry range
[betaflight.git] / src / main / cms / cms_menu_power.c
blob20287f85fee634e8f5725a05e9a65ed06a682fff
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <ctype.h>
26 #include "platform.h"
28 #ifdef USE_CMS
30 #include "cms/cms.h"
31 #include "cms/cms_types.h"
32 #include "cms/cms_menu_power.h"
34 #include "config/feature.h"
36 #include "sensors/battery.h"
37 #include "sensors/current.h"
38 #include "sensors/voltage.h"
40 #include "fc/config.h"
42 voltageMeterSource_e batteryConfig_voltageMeterSource;
43 currentMeterSource_e batteryConfig_currentMeterSource;
45 uint16_t batteryConfig_vbatmaxcellvoltage;
47 uint8_t voltageSensorADCConfig_vbatscale;
49 int16_t currentSensorADCConfig_scale;
50 int16_t currentSensorADCConfig_offset;
52 #ifdef USE_VIRTUAL_CURRENT_METER
53 int16_t currentSensorVirtualConfig_scale;
54 int16_t currentSensorVirtualConfig_offset;
55 #endif
57 static long cmsx_Power_onEnter(void)
59 batteryConfig_voltageMeterSource = batteryConfig()->voltageMeterSource;
60 batteryConfig_currentMeterSource = batteryConfig()->currentMeterSource;
62 batteryConfig_vbatmaxcellvoltage = batteryConfig()->vbatmaxcellvoltage;
64 voltageSensorADCConfig_vbatscale = voltageSensorADCConfig(0)->vbatscale;
66 currentSensorADCConfig_scale = currentSensorADCConfig()->scale;
67 currentSensorADCConfig_offset = currentSensorADCConfig()->offset;
69 #ifdef USE_VIRTUAL_CURRENT_METER
70 currentSensorVirtualConfig_scale = currentSensorVirtualConfig()->scale;
71 currentSensorVirtualConfig_offset = currentSensorVirtualConfig()->offset;
72 #endif
74 return 0;
77 static long cmsx_Power_onExit(const OSD_Entry *self)
79 UNUSED(self);
81 batteryConfigMutable()->voltageMeterSource = batteryConfig_voltageMeterSource;
82 batteryConfigMutable()->currentMeterSource = batteryConfig_currentMeterSource;
84 batteryConfigMutable()->vbatmaxcellvoltage = batteryConfig_vbatmaxcellvoltage;
86 voltageSensorADCConfigMutable(0)->vbatscale = voltageSensorADCConfig_vbatscale;
88 currentSensorADCConfigMutable()->scale = currentSensorADCConfig_scale;
89 currentSensorADCConfigMutable()->offset = currentSensorADCConfig_offset;
91 #ifdef USE_VIRTUAL_CURRENT_METER
92 currentSensorVirtualConfigMutable()->scale = currentSensorVirtualConfig_scale;
93 currentSensorVirtualConfigMutable()->offset = currentSensorVirtualConfig_offset;
94 #endif
96 return 0;
99 static OSD_Entry cmsx_menuPowerEntries[] =
101 { "-- POWER --", OME_Label, NULL, NULL, 0},
103 { "V METER", OME_TAB, NULL, &(OSD_TAB_t){ &batteryConfig_voltageMeterSource, VOLTAGE_METER_COUNT - 1, voltageMeterSourceNames }, 0 },
104 { "I METER", OME_TAB, NULL, &(OSD_TAB_t){ &batteryConfig_currentMeterSource, CURRENT_METER_COUNT - 1, currentMeterSourceNames }, 0 },
106 { "VBAT CLMAX", OME_UINT16, NULL, &(OSD_UINT16_t) { &batteryConfig_vbatmaxcellvoltage, VBAT_CELL_VOTAGE_RANGE_MIN, VBAT_CELL_VOTAGE_RANGE_MAX, 1 }, 0 },
108 { "VBAT SCALE", OME_UINT8, NULL, &(OSD_UINT8_t){ &voltageSensorADCConfig_vbatscale, VBAT_SCALE_MIN, VBAT_SCALE_MAX, 1 }, 0 },
110 { "IBAT SCALE", OME_INT16, NULL, &(OSD_INT16_t){ &currentSensorADCConfig_scale, -16000, 16000, 5 }, 0 },
111 { "IBAT OFFSET", OME_INT16, NULL, &(OSD_INT16_t){ &currentSensorADCConfig_offset, -16000, 16000, 5 }, 0 },
113 #ifdef USE_VIRTUAL_CURRENT_METER
114 { "IBAT VIRT SCALE", OME_INT16, NULL, &(OSD_INT16_t){ &currentSensorVirtualConfig_scale, -16000, 16000, 5 }, 0 },
115 { "IBAT VIRT OFFSET", OME_INT16, NULL, &(OSD_INT16_t){ &currentSensorVirtualConfig_offset, -16000, 16000, 5 }, 0 },
116 #endif
118 { "BACK", OME_Back, NULL, NULL, 0 },
119 { NULL, OME_END, NULL, NULL, 0 }
122 CMS_Menu cmsx_menuPower = {
123 #ifdef CMS_MENU_DEBUG
124 .GUARD_text = "MENUPWR",
125 .GUARD_type = OME_MENU,
126 #endif
127 .onEnter = cmsx_Power_onEnter,
128 .onExit = cmsx_Power_onExit,
129 .entries = cmsx_menuPowerEntries
132 #endif