Skip to content

Commit 020cde3

Browse files
committed
fix cfg load/save
1 parent c59c224 commit 020cde3

File tree

7 files changed

+66
-20
lines changed

7 files changed

+66
-20
lines changed

examples/00.HelloWorld/madflight_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ rdr_gizmo NONE // options: NONE, LD2411S, LD2413, USD1, SR04
8888
//pin_rdr_echo -1
8989

9090
//--- LED ---
91-
//led_on LOW_IS_OFF // options: LOW_IS_OFF, LOW_IS_ON
91+
//led_on LOW_IS_ON // options: LOW_IS_ON,HIGH_IS_ON
9292
//pin_led -1
9393

9494
//--- AHR --- AHRS (keep MAHONY, unless you want to experiment)

examples/01.Quadcopter/madflight_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ rdr_gizmo NONE // options: NONE, LD2411S, LD2413, USD1, SR04
8888
//pin_rdr_echo -1
8989

9090
//--- LED ---
91-
//led_on LOW_IS_OFF // options: LOW_IS_OFF, LOW_IS_ON
91+
//led_on LOW_IS_ON // options: LOW_IS_ON,HIGH_IS_ON
9292
//pin_led -1
9393

9494
//--- AHR --- AHRS (keep MAHONY, unless you want to experiment)

examples/02.QuadcopterAdvanced/madflight_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ rdr_gizmo NONE // options: NONE, LD2411S, LD2413, USD1, SR04
8888
//pin_rdr_echo -1
8989

9090
//--- LED ---
91-
//led_on LOW_IS_OFF // options: LOW_IS_OFF, LOW_IS_ON
91+
//led_on LOW_IS_ON // options: LOW_IS_ON,HIGH_IS_ON
9292
//pin_led -1
9393

9494
//--- AHR --- AHRS (keep MAHONY, unless you want to experiment)

examples/03.Plane/madflight_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ rdr_gizmo NONE // options: NONE, LD2411S, LD2413, USD1, SR04
8888
//pin_rdr_echo -1
8989

9090
//--- LED ---
91-
//led_on LOW_IS_OFF // options: LOW_IS_OFF, LOW_IS_ON
91+
//led_on LOW_IS_ON // options: LOW_IS_ON,HIGH_IS_ON
9292
//pin_led -1
9393

9494
//--- AHR --- AHRS (keep MAHONY, unless you want to experiment)

src/madflight.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ void madflight_setup() {
123123
cfg.loadFromEeprom(); //load parameters from EEPROM
124124
if(madflight_config) {
125125
if(cfg.load_madflight_param(madflight_config)) {
126-
Serial.println("CFG: madflight_config - Loaded");
126+
Serial.println("CFG: Loading MADFLIGHT_CONFIG - OK");
127127
}else{
128-
Serial.println("CFG: madflight_config - Skipped (was aready stored in eeprom)");
128+
Serial.println("CFG: Loading MADFLIGHT_CONFIG - Skipped (EEPROM is newer than MADFLIGHT_CONFIG)");
129129
}
130130
}
131131

src/madflight/cfg/cfg.cpp

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ void CfgClass::printNameAndValue(uint16_t i, const char* comment) {
137137
printValue(i);
138138
if(comment) Serial.printf(" # %s", comment);
139139
const char *options = Cfg::param_list[i].options;
140-
if(options && options[0] != 0) Serial.printf(" # options: %s", options);
140+
if(options && options[0] != 0) {
141+
Serial.printf(" # options: ");
142+
print_options(options);
143+
}
141144
Serial.println();
142145
}
143146

@@ -208,7 +211,7 @@ void CfgClass::printPins() {
208211

209212
//CLI set a parameter value, returns true on success
210213
bool CfgClass::setParam(String namestr, String val) {
211-
//erial.printf("cfg.setParam %s %s\n", namestr.c_str(), val.c_str());
214+
//Serial.printf("cfg.setParam %s %s\n", namestr.c_str(), val.c_str());
212215
namestr.trim();
213216
val.trim();
214217
if(namestr == "") return false;
@@ -228,7 +231,9 @@ bool CfgClass::setParam(String namestr, String val) {
228231
param_int32_t[i] = enum_idx;
229232
return true;
230233
}else{
231-
Serial.printf("CFG: WARNING - Param '%s' has no '%s' option. Available options: %s\n", namestr.c_str(), val.c_str(), Cfg::param_list[i].options);
234+
Serial.printf("CFG: WARNING - Param '%s' has no '%s' option. Available options: ", namestr.c_str(), val.c_str());
235+
print_options(Cfg::param_list[i].options);
236+
Serial.println();
232237
return false;
233238
}
234239
break;
@@ -283,21 +288,30 @@ void CfgClass::clear() {
283288
//read parameters from eeprom/flash
284289
void CfgClass::loadFromEeprom() {
285290
//Serial.printf("mf=%d all=%d i[17]=%d\n", Cfg::mf_param_cnt, Cfg::param_cnt, param_int32_t[16]);
286-
Serial.print("CFG: Loading EEPROM ... ");
291+
Serial.print("CFG: Loading EEPROM - ");
287292

288-
//load header
289-
uint8_t *buf = (uint8_t*)&hdr;
290-
for(uint32_t i=0; i<sizeof(CfgHeader); i++) {
293+
cfg.clear();
294+
295+
//load header into buffer
296+
CfgHeader hdr_new;
297+
uint8_t *buf = (uint8_t*)&hdr_new;
298+
for(uint32_t i = 0; i < sizeof(CfgHeader); i++) {
291299
buf[i] = hal_eeprom_read(i);
300+
//Serial.printf("%02X ",buf[i]);
292301
}
293302

294303
//check header
295-
if(hdr.header0 != CFG_HDR0 || hdr.header1 != CFG_HDR1 || hdr.header2 != CFG_HDR2 || hdr.header3 != CFG_HDR3 || hdr.len<sizeof(CfgHeader)+8 || hdr.len>4096) {
304+
if(hdr_new.header0 != CFG_HDR0
305+
|| hdr_new.header1 != CFG_HDR1
306+
|| hdr_new.header2 != CFG_HDR2
307+
|| hdr_new.header3 != CFG_HDR3
308+
|| hdr_new.len<sizeof(CfgHeader)+8
309+
|| hdr_new.len>4096) {
296310
Serial.println("Header invalid, using defaults");
297311
return;
298312
}
299-
uint32_t datalen = hdr.len - 4; //lenght of header+param (4=crc)
300-
uint32_t paramlen = datalen - sizeof(CfgHeader); //lenght of param
313+
uint32_t datalen = hdr_new.len - 4; //length of header+param (4=crc)
314+
uint32_t paramlen = datalen - sizeof(CfgHeader); //length of param
301315

302316
//check crc
303317
uint32_t crc = 0xFFFFFFFF;
@@ -315,6 +329,9 @@ void CfgClass::loadFromEeprom() {
315329
return;
316330
}
317331

332+
//load header from eeprom
333+
memcpy(&hdr, &hdr_new, sizeof(CfgHeader));
334+
318335
//load param from eeprom
319336
CfgParam *param = this;
320337
uint8_t *param_buf = (uint8_t*)param;
@@ -332,8 +349,17 @@ void CfgClass::writeToEeprom() {
332349
uint32_t pos = 0;
333350
uint32_t crc = 0xFFFFFFFF;
334351

352+
//setup header
353+
hdr.header0 = CFG_HDR0;
354+
hdr.header1 = CFG_HDR1;
355+
hdr.header2 = CFG_HDR2;
356+
hdr.header3 = CFG_HDR3;
357+
hdr.len = sizeof(CfgHeader) + sizeof(CfgParam) + 4; //number of bytes for hdr+param+crc
358+
hdr._reserved0 = 0;
359+
//hdr.madflight_param_crc; //this was set in load_madflight_param()
360+
//hdr._reserved1;
361+
335362
//write header
336-
hdr.len = sizeof(CfgHeader) + sizeof(CfgParam) + 4; //4=crc
337363
for(uint32_t i=0; i<sizeof(CfgHeader); i++) {
338364
uint8_t byte = ((uint8_t*)&hdr)[i];
339365
hal_eeprom_write(pos, byte);
@@ -412,9 +438,12 @@ bool CfgClass::load_cmdline(String cmdline) {
412438
return setParam(name, value);
413439
}
414440

441+
415442
//get enum index from key string, return -1 if not found
416443
int CfgClass::get_enum_index(const char* key, const char* options) {
417-
String skey = String("mf_") + key;
444+
String skey = key;
445+
skey.toUpperCase();
446+
skey = String("mf_") + skey;
418447
const char *k = skey.c_str();
419448
int klen = strlen(k);
420449
int len = strlen(options);
@@ -430,3 +459,19 @@ int CfgClass::get_enum_index(const char* key, const char* options) {
430459
}
431460
return -1;
432461
}
462+
463+
//print options without "mf_" prefix
464+
void CfgClass::print_options(const char *str)
465+
{
466+
const char *p = str;
467+
const char *p2;
468+
while(*p) {
469+
p2 = strstr(p, "mf_");
470+
if(!p2) {
471+
Serial.print(p);
472+
return;
473+
}
474+
for(const char *c = p; c < p2; c++) Serial.print(*c);
475+
p = p2 + 3;
476+
}
477+
}

src/madflight/cfg/cfg.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ class CfgClass : public CfgParam {
297297
uint8_t header2 = CFG_HDR2;
298298
uint8_t header3 = CFG_HDR3;
299299
uint16_t len = 0; //number of bytes for hdr+param+crc
300-
uint16_t _reserved0;
301-
uint32_t madflight_param_crc;
300+
uint16_t _reserved0 = 0;
301+
uint32_t madflight_param_crc = 0;
302302
uint8_t _reserved1[28] = {0};
303303
} hdr;
304304

@@ -329,6 +329,7 @@ class CfgClass : public CfgParam {
329329
private:
330330
bool load_cmdline(String cmdline);
331331
int get_enum_index(const char* k, const char* values);
332+
void print_options(const char *str); //print option list without "mf_"
332333
};
333334

334335
extern CfgClass cfg;

0 commit comments

Comments
 (0)