Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / storage / storage_common.cpp
blobb6ae2d30100cc993fc5ff0f10b79a0e8cf1aa0c7
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "opentx.h"
22 #include "pulses/multi.h"
24 uint8_t storageDirtyMsk;
25 tmr10ms_t storageDirtyTime10ms;
27 #if defined(RTC_BACKUP_RAM)
28 uint8_t rambackupDirtyMsk;
29 tmr10ms_t rambackupDirtyTime10ms;
30 #endif
32 void storageDirty(uint8_t msk)
34 storageDirtyMsk |= msk;
35 storageDirtyTime10ms = get_tmr10ms();
37 #if defined(RTC_BACKUP_RAM)
38 rambackupDirtyMsk = storageDirtyMsk;
39 rambackupDirtyTime10ms = storageDirtyTime10ms;
40 #endif
43 void preModelLoad()
45 watchdogSuspend(500/*5s*/);
47 #if defined(SDCARD)
48 logsClose();
49 #endif
51 if (pulsesStarted()) {
52 pausePulses();
55 pauseMixerCalculations();
58 void postRadioSettingsLoad()
60 #if defined(PXX2)
61 if (is_memclear(g_eeGeneral.ownerRegistrationID, PXX2_LEN_REGISTRATION_ID)) {
62 setDefaultOwnerId();
64 #endif
67 #if defined(EXTERNAL_ANTENNA) && defined(INTERNAL_MODULE_PXX1)
68 void onAntennaSelection(const char * result)
70 if (result == STR_USE_INTERNAL_ANTENNA) {
71 globalData.externalAntennaEnabled = false;
73 else if (result == STR_USE_EXTERNAL_ANTENNA) {
74 globalData.externalAntennaEnabled = true;
76 else {
77 checkExternalAntenna();
81 void onAntennaSwitchConfirm(const char * result)
83 if (result == STR_OK) {
84 // Switch to external antenna confirmation
85 globalData.externalAntennaEnabled = true;
89 void checkExternalAntenna()
91 if (isModuleXJT(INTERNAL_MODULE)) {
92 if (g_eeGeneral.antennaMode == ANTENNA_MODE_EXTERNAL) {
93 globalData.externalAntennaEnabled = true;
95 else if (g_eeGeneral.antennaMode == ANTENNA_MODE_PER_MODEL && g_model.moduleData[INTERNAL_MODULE].pxx.antennaMode == ANTENNA_MODE_EXTERNAL) {
96 if (!globalData.externalAntennaEnabled) {
97 POPUP_CONFIRMATION(STR_ANTENNACONFIRM1, onAntennaSwitchConfirm);
98 SET_WARNING_INFO(STR_ANTENNACONFIRM2, sizeof(TR_ANTENNACONFIRM2), 0);
101 else if (g_eeGeneral.antennaMode == ANTENNA_MODE_ASK || (g_eeGeneral.antennaMode == ANTENNA_MODE_PER_MODEL && g_model.moduleData[INTERNAL_MODULE].pxx.antennaMode == ANTENNA_MODE_ASK)) {
102 globalData.externalAntennaEnabled = false;
103 POPUP_MENU_ADD_ITEM(STR_USE_INTERNAL_ANTENNA);
104 POPUP_MENU_ADD_ITEM(STR_USE_EXTERNAL_ANTENNA);
105 POPUP_MENU_START(onAntennaSelection);
107 else {
108 globalData.externalAntennaEnabled = false;
111 else {
112 globalData.externalAntennaEnabled = false;
115 #endif
117 void postModelLoad(bool alarms)
119 #if defined(PXX2)
120 if (is_memclear(g_model.modelRegistrationID, PXX2_LEN_REGISTRATION_ID)) {
121 memcpy(g_model.modelRegistrationID, g_eeGeneral.ownerRegistrationID, PXX2_LEN_REGISTRATION_ID);
123 #endif
125 #if defined(HARDWARE_INTERNAL_MODULE)
126 if (!isInternalModuleAvailable(g_model.moduleData[INTERNAL_MODULE].type)) {
127 memclear(&g_model.moduleData[INTERNAL_MODULE], sizeof(ModuleData));
129 #if defined(MULTIMODULE)
130 else if (isModuleMultimodule(INTERNAL_MODULE))
131 multiPatchCustom(INTERNAL_MODULE);
132 #endif
133 #endif
135 if (!isExternalModuleAvailable(g_model.moduleData[EXTERNAL_MODULE].type)) {
136 memclear(&g_model.moduleData[EXTERNAL_MODULE], sizeof(ModuleData));
138 #if defined(MULTIMODULE)
139 else if (isModuleMultimodule(EXTERNAL_MODULE))
140 multiPatchCustom(EXTERNAL_MODULE);
141 #endif
143 AUDIO_FLUSH();
144 flightReset(false);
146 customFunctionsReset();
148 restoreTimers();
150 for (int i=0; i<MAX_TELEMETRY_SENSORS; i++) {
151 TelemetrySensor & sensor = g_model.telemetrySensors[i];
152 if (sensor.type == TELEM_TYPE_CALCULATED && sensor.persistent) {
153 telemetryItems[i].value = sensor.persistentValue;
154 telemetryItems[i].timeout = 0; // make value visible even before the first new value is received)
156 else {
157 telemetryItems[i].timeout = TELEMETRY_SENSOR_TIMEOUT_UNAVAILABLE;
161 LOAD_MODEL_CURVES();
163 resumeMixerCalculations();
164 if (pulsesStarted()) {
165 #if defined(GUI)
166 if (alarms) {
167 checkAll();
168 PLAY_MODEL_NAME();
170 #endif
171 resumePulses();
174 #if defined(SDCARD)
175 referenceModelAudioFiles();
176 #endif
178 #if defined(PCBHORUS)
179 loadCustomScreens();
180 #endif
182 LOAD_MODEL_BITMAP();
183 LUA_LOAD_MODEL_SCRIPTS();
185 SEND_FAILSAFE_1S();
188 void storageFlushCurrentModel()
190 saveTimers();
192 for (int i=0; i<MAX_TELEMETRY_SENSORS; i++) {
193 TelemetrySensor & sensor = g_model.telemetrySensors[i];
194 if (sensor.type == TELEM_TYPE_CALCULATED && sensor.persistent && sensor.persistentValue != telemetryItems[i].value) {
195 sensor.persistentValue = telemetryItems[i].value;
196 storageDirty(EE_MODEL);
200 if (g_model.potsWarnMode == POTS_WARN_AUTO) {
201 for (int i=0; i<NUM_POTS+NUM_SLIDERS; i++) {
202 if (!(g_model.potsWarnEnabled & (1 << i))) {
203 SAVE_POT_POSITION(i);
206 storageDirty(EE_MODEL);