Use simple scheme with direct model slot to model filename

This commit is contained in:
Raphael Coeffic
2021-11-10 11:07:00 +01:00
parent 96b256cbdb
commit c696313a4e
21 changed files with 284 additions and 659 deletions
+2 -19
View File
@@ -170,23 +170,6 @@ void eeConvertModel(int id, int version)
uint8_t currModel = g_eeGeneral.currModel;
g_eeGeneral.currModel = id;
#if !defined(EEPROM)
char filename[LEN_MODEL_FILENAME+1];
memset(filename, 0, sizeof(filename));
char* s = strAppend(filename, MODEL_FILENAME_PREFIX);
s = strAppendUnsigned(s, id + 1, 2);
s = strAppend(s, MODEL_FILENAME_SUFFIX);
memcpy(g_eeGeneral.currModelFilename, filename, sizeof(g_eeGeneral.currModelFilename));
if (!modelslist.getCurrentCategory()) {
ModelsCategory* cat = modelslist.createCategory("Models", false);
modelslist.setCurrentCategory(cat);
}
ModelCell* model = modelslist.addModel(modelslist.getCurrentCategory(),
filename, false);
model->setModelName(g_model.header.name);
#endif
storageDirty(EE_MODEL);
storageCheck(true);
@@ -233,7 +216,7 @@ bool eeConvert()
storageDirty(EE_GENERAL);
storageCheck(true);
#if !defined(EEPROM)
#if defined(STORAGE_MODELSLIST)
modelslist.clear();
#endif
@@ -265,7 +248,7 @@ bool eeConvert()
}
}
#if !defined(EEPROM)
#if defined(STORAGE_MODELSLIST)
modelslist.save();
#endif
+1 -61
View File
@@ -56,37 +56,6 @@ void eeLoadModel(uint8_t index)
}
}
uint8_t eeFindEmptyModel(uint8_t id, bool down)
{
uint8_t i = id;
for (;;) {
i = (MAX_MODELS + (down ? i+1 : i-1)) % MAX_MODELS;
if (!eeModelExists(i)) break;
if (i == id) return 0xff; // no free space in directory left
}
return i;
}
void selectModel(uint8_t sub)
{
#if !defined(COLORLCD)
showMessageBox(STR_LOADINGMODEL);
#endif
storageFlushCurrentModel();
storageCheck(true); // force writing of current model data before this is changed
g_eeGeneral.currModel = sub;
storageDirty(EE_GENERAL);
eeLoadModel(sub);
}
ModelHeader modelHeaders[MAX_MODELS];
void eeLoadModelHeaders()
{
for (uint32_t i=0; i<MAX_MODELS; i++) {
eeLoadModelHeader(i, &modelHeaders[i]);
}
}
void storageClearRadioSettings()
{
memclear(&g_eeGeneral, sizeof(RadioData));
@@ -102,7 +71,7 @@ bool storageReadRadioSettings(bool allowFixes)
storageEraseAll(true);
}
else {
eeLoadModelHeaders();
loadModelHeaders();
}
for (uint8_t i=0; languagePacks[i]; i++) {
@@ -191,32 +160,3 @@ void checkModelIdUnique(uint8_t index, uint8_t module)
POPUP_WARNING(STR_MODELIDUSED, reusableBuffer.moduleSetup.msg);
}
}
uint8_t findNextUnusedModelId(uint8_t index, uint8_t module)
{
uint8_t usedModelIds[(MAX_RXNUM + 7) / 8];
memset(usedModelIds, 0, sizeof(usedModelIds));
for (uint8_t modelIndex = 0; modelIndex < MAX_MODELS; modelIndex++) {
if (modelIndex == index)
continue;
uint8_t id = modelHeaders[modelIndex].modelId[module];
if (id == 0)
continue;
uint8_t mask = 1u << (id & 7u);
usedModelIds[id >> 3u] |= mask;
}
for (uint8_t id = 1; id <= getMaxRxNum(module); id++) {
uint8_t mask = 1u << (id & 7u);
if (!(usedModelIds[id >> 3u] & mask)) {
// found free ID
return id;
}
}
// failed finding something...
return 0;
}
-5
View File
@@ -32,16 +32,11 @@ void eeLoadModel(uint8_t id);
uint8_t eeFindEmptyModel(uint8_t id, bool down);
void selectModel(uint8_t sub);
extern ModelHeader modelHeaders[MAX_MODELS];
void eeLoadModelHeader(uint8_t id, ModelHeader *header);
void eeLoadModelHeaders();
void storageClearRadioSettings();
bool storageReadRadioSettings(bool allowFixes = true);
void storageReadCurrentModel();
void checkModelIdUnique(uint8_t index, uint8_t module);
uint8_t findNextUnusedModelId(uint8_t index, uint8_t module);
#if defined(EEPROM_RLC)
#include "eeprom_rlc.h"
+19 -17
View File
@@ -26,10 +26,6 @@
#include "conversions/conversions.h"
#include "model_init.h"
//#if !defined(EEPROM_SDCARD)
ModelHeader modelHeaders[MAX_MODELS];
//#endif
// defined either in sdcard_raw.cpp or sdcard_yaml.cpp
void storageCreateModelsList();
@@ -71,7 +67,9 @@ void storageFormat()
{
sdCheckAndCreateDirectory(RADIO_PATH);
sdCheckAndCreateDirectory(MODELS_PATH);
#if defined(STORAGE_MODELSLIST)
storageCreateModelsList();
#endif
generalDefault();
}
@@ -96,6 +94,7 @@ void storageCheck(bool immediately)
}
}
#if defined(STORAGE_MODELSLIST)
const char * createModel()
{
preModelLoad();
@@ -108,6 +107,7 @@ const char * createModel()
if (index > 0) {
setModelDefaults(index);
memcpy(g_eeGeneral.currModelFilename, filename, sizeof(g_eeGeneral.currModelFilename));
storageDirty(EE_GENERAL);
storageDirty(EE_MODEL);
storageCheck(true);
@@ -116,6 +116,7 @@ const char * createModel()
return g_eeGeneral.currModelFilename;
}
#endif
const char * loadModel(const char * filename, bool alarms)
{
@@ -150,13 +151,20 @@ void storageReadAll()
{
TRACE("storageReadAll");
#if defined(STORAGE_MODELSLIST)
// Wipe models list in case
// it's being reloaded after USB connection
modelslist.clear();
#endif
if (loadRadioSettings() != nullptr) {
storageEraseAll(true);
}
#if !defined(STORAGE_MODELSLIST)
else {
loadModelHeaders();
}
#endif
for (uint8_t i = 0; languagePacks[i] != nullptr; i++) {
if (!strncmp(g_eeGeneral.ttsLanguage, languagePacks[i]->id, 2)) {
@@ -166,6 +174,7 @@ void storageReadAll()
}
}
#if defined(STORAGE_MODELSLIST)
// and reload the list
modelslist.load();
@@ -185,24 +194,17 @@ void storageReadAll()
if (loadModel(g_eeGeneral.currModelFilename, false) != nullptr) {
TRACE("No current model or SD card error");
}
#else
if (loadModel(g_eeGeneral.currModel, false) != nullptr) {
TRACE("No current model or SD card error");
}
#endif
}
#if !defined(COLORLCD)
void checkModelIdUnique(uint8_t index, uint8_t module)
{
if (isModuleXJTD8(module))
return;
char * warn_buf = reusableBuffer.moduleSetup.msg;
// cannot rely exactly on WARNING_LINE_LEN so using WARNING_LINE_LEN-2
size_t warn_buf_len = sizeof(reusableBuffer.moduleSetup.msg) - WARNING_LINE_LEN - 2;
if (!modelslist.isModelIdUnique(module,warn_buf,warn_buf_len)) {
if (warn_buf[0] != 0) {
POPUP_WARNING(STR_MODELIDUSED);
SET_WARNING_INFO(warn_buf, sizeof(reusableBuffer.moduleSetup.msg), 0);
}
}
//TODO
}
#endif
+13
View File
@@ -53,6 +53,19 @@ const char * loadModel(const char * filename, bool alarms=true);
const char * createModel();
const char * writeModel();
#if !defined(STORAGE_MODELSLIST)
// index storage vs modelslist
void selectModel(uint8_t idx);
const char* loadModel(uint8_t idx, bool alarms=true);
bool modelExists(uint8_t idx);
bool copyModel(uint8_t dst, uint8_t src);
void swapModels(uint8_t id1, uint8_t id2);
int8_t deleteModel(uint8_t idx);
uint8_t findEmptyModel(uint8_t id, bool down);
#endif
const char * loadRadioSettings();
const char * writeGeneralSettings();
+91 -1
View File
@@ -20,6 +20,7 @@
*/
#include "opentx.h"
#include "opentx_helpers.h"
#include "storage.h"
#include "sdcard_common.h"
#include "sdcard_raw.h"
@@ -67,6 +68,7 @@ const char * readYamlFile(const char* fullpath, const YamlParserCalls* calls, vo
// Generic storage interface
//
#if !defined(EEPROM_RLC)
void storageCreateModelsList()
{
modelslist.clear();
@@ -74,6 +76,7 @@ void storageCreateModelsList()
cat->addModel(DEFAULT_MODEL_FILENAME);
modelslist.save();
}
#endif
//
// SDCARD storage interface
@@ -244,7 +247,94 @@ const char * writeModelYaml(const char* filename)
return NULL;
}
#if !defined(STORAGE_MODELSLIST)
// EEPROM slot simulation based on file names:
// - /MODELS/model[00-99].yml
constexpr uint8_t MODELIDX_STRLEN = sizeof(MODEL_FILENAME_PREFIX "00");
static void getModelNumberStr(uint8_t idx, char* model_idx)
{
memcpy(model_idx, MODEL_FILENAME_PREFIX, sizeof(MODEL_FILENAME_PREFIX));
model_idx[sizeof(MODEL_FILENAME_PREFIX)-1] = '0' + idx / 10;
model_idx[sizeof(MODEL_FILENAME_PREFIX)] = '0' + idx % 10;
model_idx[sizeof(MODEL_FILENAME_PREFIX)+1] = '\0';
}
#endif
const char * writeModel()
{
return writeModelYaml(g_eeGeneral.currModelFilename);
#if defined(STORAGE_MODELSLIST)
return writeModelYaml(g_eeGeneral.currModelFilename);
#else
char fname[MODELIDX_STRLEN + sizeof(YAML_EXT)];
getModelNumberStr(g_eeGeneral.currModel, fname);
strcat(fname, YAML_EXT);
return writeModelYaml(fname);
#endif
}
#if !defined(STORAGE_MODELSLIST)
void loadModelHeader(uint8_t id, ModelHeader* header)
{
PartialModel partial;
memclear(&partial, sizeof(PartialModel));
if (modelExists(id)) {
uint8_t dummy;
char fname[MODELIDX_STRLEN + sizeof(YAML_EXT)];
getModelNumberStr(id, fname);
strcat(fname, YAML_EXT);
readModelYaml(fname, reinterpret_cast<uint8_t*>(&partial), sizeof(partial), &dummy);
memcpy(header, &partial, sizeof(ModelHeader));
}
}
const char * loadModel(uint8_t idx, bool alarms)
{
char fname[MODELIDX_STRLEN + sizeof(YAML_EXT)];
getModelNumberStr(idx, fname);
strcat(fname, YAML_EXT);
return loadModel(fname, alarms);
}
bool modelExists(uint8_t idx)
{
char model_idx[MODELIDX_STRLEN];
getModelNumberStr(idx, model_idx);
GET_FILENAME(fname, MODELS_PATH, model_idx, YAML_EXT);
FILINFO fno;
return f_stat(fname, &fno) == FR_OK;
}
bool copyModel(uint8_t dst, uint8_t src)
{
// TODO: overwrite possible?
char model_idx_src[MODELIDX_STRLEN];
char model_idx_dst[MODELIDX_STRLEN];
getModelNumberStr(src, model_idx_src);
getModelNumberStr(dst, model_idx_dst);
GET_FILENAME(fname_src, MODELS_PATH, model_idx_src, YAML_EXT);
GET_FILENAME(fname_dst, MODELS_PATH, model_idx_dst, YAML_EXT);
return sdCopyFile(fname_src, fname_dst);
}
void swapModels(uint8_t id1, uint8_t id2)
{
//TODO
}
int8_t deleteModel(uint8_t idx)
{
char model_idx[MODELIDX_STRLEN];
getModelNumberStr(idx, model_idx);
GET_FILENAME(fname, MODELS_PATH PATH_SEPARATOR MODEL_FILENAME_PREFIX,
model_idx, YAML_EXT);
return f_unlink(fname) == FR_OK ? 0 : -1;
}
#endif
+9
View File
@@ -63,6 +63,15 @@ void preModelLoad();
void postModelLoad(bool alarms);
void checkExternalAntenna();
#if !defined(STORAGE_MODELSLIST)
extern ModelHeader modelHeaders[MAX_MODELS];
void loadModelHeader(uint8_t id, ModelHeader *header);
void loadModelHeaders();
uint8_t findNextUnusedModelId(uint8_t index, uint8_t module);
#endif
#if defined(EEPROM)
#include "eeprom_common.h"
#endif
+63
View File
@@ -237,3 +237,66 @@ void storageFlushCurrentModel()
storageDirty(EE_MODEL);
}
}
#if !defined(STORAGE_MODELSLIST)
void selectModel(uint8_t idx)
{
#if !defined(COLORLCD)
showMessageBox(STR_LOADINGMODEL);
#endif
storageFlushCurrentModel();
storageCheck(true); // force writing of current model data before this is changed
g_eeGeneral.currModel = idx;
storageDirty(EE_GENERAL);
loadModel(idx);
}
uint8_t findEmptyModel(uint8_t id, bool down)
{
uint8_t i = id;
for (;;) {
i = (MAX_MODELS + (down ? i+1 : i-1)) % MAX_MODELS;
if (!modelExists(i)) break;
if (i == id) return 0xff; // no free space in directory left
}
return i;
}
ModelHeader modelHeaders[MAX_MODELS];
void loadModelHeaders()
{
for (uint32_t i=0; i<MAX_MODELS; i++) {
loadModelHeader(i, &modelHeaders[i]);
}
}
uint8_t findNextUnusedModelId(uint8_t index, uint8_t module)
{
uint8_t usedModelIds[(MAX_RXNUM + 7) / 8];
memset(usedModelIds, 0, sizeof(usedModelIds));
for (uint8_t modelIndex = 0; modelIndex < MAX_MODELS; modelIndex++) {
if (modelIndex == index)
continue;
uint8_t id = modelHeaders[modelIndex].modelId[module];
if (id == 0)
continue;
uint8_t mask = 1u << (id & 7u);
usedModelIds[id >> 3u] |= mask;
}
for (uint8_t id = 1; id <= getMaxRxNum(module); id++) {
uint8_t mask = 1u << (id & 7u);
if (!(usedModelIds[id >> 3u] & mask)) {
// found free ID
return id;
}
}
// failed finding something...
return 0;
}
#endif
+4 -1
View File
@@ -3,9 +3,12 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set(SRC ${SRC} storage/yaml/yaml_tree_walker.cpp
storage/yaml/yaml_parser.cpp
storage/yaml/yaml_bits.cpp
storage/yaml/yaml_modelslist.cpp
storage/yaml/yaml_datastructs.cpp)
if(STORAGE_MODELSLIST)
set(SRC ${SRC} storage/yaml/yaml_modelslist.cpp)
endif()
# Warning: this file must be kept in sync with 'yaml_datastructs.cpp'
# in the same directory.