Fix doc path
[opentx.git] / radio / src / storage / eeprom_common.cpp
blob9fc5e6874a43ed9b04d46c155aaa85eb82a02d06
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 <stdint.h>
22 #include <inttypes.h>
23 #include <string.h>
24 #include "opentx.h"
25 #include "timers.h"
27 void eeLoadModel(uint8_t index)
29 if (index < MAX_MODELS) {
31 preModelLoad();
33 uint16_t size = eeLoadModelData(index);
35 #if defined(SIMU) && defined(EEPROM_ZONE_SIZE)
36 if (sizeof(uint16_t) + sizeof(g_model) > EEPROM_ZONE_SIZE) {
37 TRACE("Model data size can't exceed %d bytes (%d bytes)", int(EEPROM_ZONE_SIZE-sizeof(uint16_t)), (int)sizeof(g_model));
39 #endif
41 #if defined(SIMU)
42 if (size > 0 && size != sizeof(g_model)) {
43 TRACE("Model data read=%d bytes vs %d bytes\n", size, (int)sizeof(ModelData));
45 #endif
47 bool alarms = true;
48 if (size < EEPROM_MIN_MODEL_SIZE) { // if not loaded a fair amount
49 modelDefault(index) ;
50 storageCheck(true);
51 alarms = false;
54 postModelLoad(alarms);
58 uint8_t eeFindEmptyModel(uint8_t id, bool down)
60 uint8_t i = id;
61 for (;;) {
62 i = (MAX_MODELS + (down ? i+1 : i-1)) % MAX_MODELS;
63 if (!eeModelExists(i)) break;
64 if (i == id) return 0xff; // no free space in directory left
66 return i;
69 void selectModel(uint8_t sub)
71 #if !defined(COLORLCD)
72 showMessageBox(STR_LOADINGMODEL);
73 #endif
74 storageFlushCurrentModel();
75 storageCheck(true); // force writing of current model data before this is changed
76 g_eeGeneral.currModel = sub;
77 storageDirty(EE_GENERAL);
78 eeLoadModel(sub);
81 #if defined(CPUARM)
82 ModelHeader modelHeaders[MAX_MODELS];
83 void eeLoadModelHeaders()
85 for (uint32_t i=0; i<MAX_MODELS; i++) {
86 eeLoadModelHeader(i, &modelHeaders[i]);
89 #endif
91 void storageReadRadioSettings()
93 if (!eepromOpen() || !eeLoadGeneral()) {
94 storageEraseAll(true);
96 else {
97 eeLoadModelHeaders();
100 #if defined(CPUARM)
101 for (uint8_t i=0; languagePacks[i]!=NULL; i++) {
102 if (!strncmp(g_eeGeneral.ttsLanguage, languagePacks[i]->id, 2)) {
103 currentLanguagePackIdx = i;
104 currentLanguagePack = languagePacks[i];
107 #endif
110 void storageReadCurrentModel()
112 eeLoadModel(g_eeGeneral.currModel);
115 void storageReadAll()
117 storageReadRadioSettings();
118 storageReadCurrentModel();
121 void storageEraseAll(bool warn)
123 TRACE("storageEraseAll");
125 generalDefault();
126 modelDefault(0);
128 if (warn) {
129 ALERT(STR_STORAGE_WARNING, STR_BAD_RADIO_DATA, AU_BAD_RADIODATA);
132 RAISE_ALERT(STR_STORAGE_WARNING, STR_STORAGE_FORMAT, NULL, AU_NONE);
134 storageFormat();
135 storageDirty(EE_GENERAL|EE_MODEL);
136 storageCheck(true);