Skip to content

Commit 902be5a

Browse files
committed
cfg.cpp + cli.cpp
1 parent 95d70b4 commit 902be5a

File tree

25 files changed

+985
-997
lines changed

25 files changed

+985
-997
lines changed

examples/00.HelloWorld/00.HelloWorld.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Copyright (c) 2023-2025 https://madflight.com
1515

1616
void setup() {
1717
//setup madflight components: Serial.begin(115200), imu, rcin, led, etc. See src/madflight/interface.h for full interface description of each component.
18-
madflight_setup();
18+
madflight_setup();
1919
}
2020

2121
void loop() {

src/madflight.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,15 @@ const char* madflight_config = MADFLIGHT_BOARD MADFLIGHT_CONFIG;
4343

4444
// include all "_cpp.h" modules which have compile time config options
4545
#define MF_ALLOW_INCLUDE_CCP_H
46-
#include "madflight/cfg/cfg_cpp.h" //include this first, because cfg.xxx parameters are used by other "_cpp.h" modules
47-
#include "madflight/ahr/ahr_cpp.h"
48-
#include "madflight/alt/alt_cpp.h"
49-
#include "madflight/cli/cli_cpp.h"
46+
#include "madflight/ahr/ahr_cpp.h" //TODO - convert to use gizmos
47+
#include "madflight/alt/alt_cpp.h" //TODO - convert to use gizmos
5048
#include "madflight/hal/hal_cpp.h"
51-
#include "madflight/imu/imu_cpp.h"
52-
#include "madflight/rcl/rcl_cpp.h"
49+
#include "madflight/imu/imu_cpp.h" //for IMU_EXEC
5350
#undef MF_ALLOW_INCLUDE_CCP_H
5451

5552
// include all other modules without compile time config options
53+
#include "madflight/cfg/cfg.h"
54+
#include "madflight/cli/cli.h"
5655
#include "madflight/bar/bar.h"
5756
#include "madflight/bat/bat.h"
5857
#include "madflight/bbx/bbx.h"
@@ -61,6 +60,7 @@ const char* madflight_config = MADFLIGHT_BOARD MADFLIGHT_CONFIG;
6160
#include "madflight/mag/mag.h"
6261
#include "madflight/out/out.h"
6362
#include "madflight/pid/pid.h"
63+
#include "madflight/rcl/rcl.h"
6464
#include "madflight/rdr/rdr.h"
6565
#include "madflight/veh/veh.h"
6666

src/madflight/ahr/ahr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ SOFTWARE.
2424

2525
#pragma once
2626

27+
#include <stdint.h>
28+
2729
class Ahr {
2830
public:
2931
float gx = 0, gy = 0, gz = 0; //corrected and filtered imu gyro measurements in [deg/sec]

src/madflight/bar/bar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ SOFTWARE.
3535
Bar bar;
3636

3737
int Bar::setup() {
38-
Cfg::printModule(MF_MOD);
38+
cfg.printModule(MF_MOD);
3939

4040
_samplePeriod = 1000000 / config.sampleRate;
4141

src/madflight/bat/bat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ SOFTWARE.
3434
Bat bat;
3535

3636
int Bat::setup() {
37-
Cfg::printModule(MF_MOD);
37+
cfg.printModule(MF_MOD);
3838

3939
//create gizmo
4040
delete gizmo;

src/madflight/bbx/BinLogWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ namespace BinLogWriter {
254254
String name;
255255
float value = 0;
256256
int i = 0;
257-
while(Cfg::getNameAndValue(i,&name,&value)) {
257+
while(cfg.getNameAndValue(i,&name,&value)) {
258258
log_header_parm(name.c_str(), value, 0);
259259
queueFlush();
260260
i++;

src/madflight/bbx/bbx.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Bbx bbx;
8484

8585

8686
int Bbx::setup() {
87-
Cfg::printModule(MF_MOD);
87+
cfg.printModule(MF_MOD);
8888

8989
//create gizmo
9090
delete gizmo;
@@ -239,7 +239,6 @@ void Bbx::log_att() {
239239

240240
//raw (unfiltered but corrected) IMU data
241241
void Bbx::log_imu() {
242-
/*
243242
BinLog bl("IMU");
244243
bl.keepFree = QUEUE_LENGTH/4; //keep 25% of queue free for other messages
245244
bl.TimeUS(imu.ts);
@@ -265,7 +264,6 @@ void Bbx::log_imu() {
265264
bl.i16("roll",ahr.roll*100, 1e-2, "deg"); //deg -180 to 180
266265
bl.i16("pitch",ahr.pitch*100, 1e-2, "deg");; //deg -90 to 90
267266
bl.i16("yaw",ahr.yaw*100, 1e-2, "deg");; //deg -180 to 180
268-
*/
269267
}
270268

271269
void Bbx::log_mode(uint8_t fm, const char* name) {
Lines changed: 1 addition & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -22,131 +22,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
===========================================================================================*/
2424

25-
// Make sure this file is included from madflight.h and not from somewhere else
26-
#ifndef MF_ALLOW_INCLUDE_CCP_H
27-
#error "Only include this file from madflight.h"
28-
#endif
29-
//#pragma once //don't use here, we want to get an error if included twice
30-
31-
#ifndef USER_PARAM_LIST
32-
#define USER_PARAM_LIST
33-
#endif
34-
3525
#include "cfg.h" //MF_PARAM_LIST and enums for options
3626
#include "../hal/hal.h"
3727
#include "../tbx/tbx_crc.h"
3828

39-
#define CFG_HDR0 'm'
40-
#define CFG_HDR1 'a'
41-
#define CFG_HDR2 'd'
42-
#define CFG_HDR3 '2'
43-
44-
45-
namespace Cfg {
46-
//list of parameters (generate from MF_PARAM_LIST and USER_PARAM_LIST)
47-
#define MF_PARAM(name, defval, datatype, type, ...) {#name, defval, type, #__VA_ARGS__},
48-
struct param_list_t {
49-
const char* name;
50-
const float defval;
51-
const char type;
52-
const char* options;
53-
};
54-
const param_list_t param_list[] = { MF_PARAM_LIST USER_PARAM_LIST };
55-
#undef MF_PARAM
56-
57-
//enums for user parameters, generated from USER_PARAM_LIST
58-
//NOTE: MF_PARAM_LIST already expanded in cfg.h
59-
#define MF_PARAM(name, defval, datatype, type, ...) enum class name##_enum { __VA_ARGS__ };
60-
USER_PARAM_LIST
61-
#undef MF_PARAM
62-
63-
//count number of parameters, generated from MF_PARAM_LIST
64-
#define MF_PARAM(name, defval, datatype, type, ...) + 1
65-
//count number of madflight library parameters
66-
const uint16_t mf_param_cnt = 0 MF_PARAM_LIST ;
67-
//count number of all (madflight+user) parameters
68-
const uint16_t param_cnt = 0 MF_PARAM_LIST USER_PARAM_LIST ;
69-
#undef MF_PARAM
70-
};
71-
72-
73-
//struct CfgParam for parameters, generated from MF_PARAM_LIST
74-
#define MF_PARAM(name, defval, datatype, type, ...) datatype name = defval;
75-
struct __attribute__((packed)) CfgParam {
76-
union __attribute__((packed)) {
77-
struct __attribute__((packed)) {
78-
MF_PARAM_LIST
79-
USER_PARAM_LIST
80-
};
81-
float param_float[Cfg::param_cnt];
82-
int32_t param_int32_t[Cfg::param_cnt];
83-
};
84-
};
85-
#undef MF_PARAM
86-
87-
class CfgClass : public CfgParam {
88-
private:
89-
//keep CfgHeader 40 bytes long!!!
90-
struct __attribute__((packed)) CfgHeader {
91-
uint8_t header0 = CFG_HDR0;
92-
uint8_t header1 = CFG_HDR1;
93-
uint8_t header2 = CFG_HDR2;
94-
uint8_t header3 = CFG_HDR3;
95-
uint16_t len = 0; //number of bytes for hdr+param+crc
96-
uint16_t mf_param_cnt; //number of madflight library parameters
97-
uint32_t madflight_param_crc;
98-
uint8_t _reserved[28] = {0};
99-
} hdr;
100-
101-
public:
102-
CfgClass();
103-
void begin();
104-
uint16_t paramCount(); //get number of parameters
105-
bool getNameAndValue(uint16_t index, String* name, float* value); //get parameter name and value for index
106-
void list(const char* filter = nullptr); //CLI print all config values
107-
bool setParam(String namestr, String val); //CLI set a parameter value, returns true on success
108-
bool setParamMavlink(String namestr, float val); //set a parameter value, returns true on success
109-
int getIndex(String namestr); //get parameter index for a parameter name
110-
void clear(); //load defaults from param_list
111-
void loadFromEeprom(); //read parameters from eeprom/flash
112-
void loadFromString(const char *batch); //load text unconditional
113-
bool load_madflight_param(const char *batch); //load text if crc is different, returns true when loaded
114-
void writeToEeprom(); //write config to flash
115-
float getValue(String namestr, float default_value);
116-
117-
//print
118-
void printParamOption(const int32_t* param);
119-
bool getOptionString(uint16_t param_idx, int32_t param_val, char out_option[20]);
120-
void printPins();
121-
void printModule(const char* module_name);
122-
void printNameAndValue(uint16_t i, const char* comment = nullptr);
123-
void printValue(uint16_t i);
124-
125-
private:
126-
bool load_cmdline(String cmdline);
127-
int get_enum_index(const char* k, const char* values);
128-
};
129-
29+
//create global module instance
13030
CfgClass cfg;
13131

132-
namespace Cfg {
133-
void printModule(const char* modulename) {
134-
cfg.printModule(modulename);
135-
}
136-
int getIndex(String namestr) {
137-
return cfg.getIndex(namestr);
138-
}
139-
uint16_t paramCount(){
140-
return cfg.paramCount();
141-
}
142-
bool setParamMavlink(String namestr, float val){
143-
return cfg.setParamMavlink(namestr, val);
144-
}
145-
bool getNameAndValue(uint16_t index, String* name, float* value) {
146-
return cfg.getNameAndValue(index, name, value);
147-
}
148-
};
149-
15032
CfgClass::CfgClass() {}
15133

15234
void CfgClass::begin() {
@@ -452,7 +334,6 @@ void CfgClass::writeToEeprom() {
452334

453335
//write header
454336
hdr.len = sizeof(CfgHeader) + sizeof(CfgParam) + 4; //4=crc
455-
hdr.mf_param_cnt = Cfg::mf_param_cnt;
456337
for(uint32_t i=0; i<sizeof(CfgHeader); i++) {
457338
uint8_t byte = ((uint8_t*)&hdr)[i];
458339
hal_eeprom_write(pos, byte);

src/madflight/cfg/cfg.h

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ SOFTWARE.
2424

2525
#pragma once
2626

27-
//NOTE: class Cfg is kept "private" (header only) to enable injection of user parameters via USER_PARAM_LIST
28-
2927
//=========================================================================================
3028
// Parameter List
3129
//=========================================================================================
@@ -234,18 +232,95 @@ SOFTWARE.
234232
//end MF_PARAM_LIST
235233

236234

235+
#include <Arduino.h> //String
237236

238-
#include <Arduino.h>
239-
237+
//=========================================================================================
238+
// Generate parameter count, enums, lookup table, and CfgParam member variables from MF_PARAM_LIST
239+
//=========================================================================================
240240
namespace Cfg {
241+
//count number of parameters, generated from MF_PARAM_LIST
242+
#define MF_PARAM(name, defval, datatype, type, ...) + 1
243+
const uint16_t param_cnt = 0 MF_PARAM_LIST ;
244+
#undef MF_PARAM
245+
241246
//enums for madflight library parameters, generated from MF_PARAM_LIST
242247
#define MF_PARAM(name, defval, datatype, type, ...) enum class name##_enum { __VA_ARGS__ };
243248
MF_PARAM_LIST
244249
#undef MF_PARAM
250+
251+
//list of parameters (generate from MF_PARAM_LIST)
252+
#define MF_PARAM(name, defval, datatype, type, ...) {#name, defval, type, #__VA_ARGS__},
253+
struct param_list_t {
254+
const char* name;
255+
const float defval;
256+
const char type;
257+
const char* options;
258+
};
259+
const param_list_t param_list[] = { MF_PARAM_LIST };
260+
#undef MF_PARAM
261+
};
262+
263+
//struct CfgParam for parameters, generated from MF_PARAM_LIST
264+
#define MF_PARAM(name, defval, datatype, type, ...) datatype name = defval;
265+
struct __attribute__((packed)) CfgParam {
266+
union __attribute__((packed)) {
267+
struct __attribute__((packed)) {
268+
MF_PARAM_LIST
269+
};
270+
float param_float[Cfg::param_cnt];
271+
int32_t param_int32_t[Cfg::param_cnt];
272+
};
273+
};
274+
#undef MF_PARAM
275+
276+
277+
278+
#define CFG_HDR0 'm'
279+
#define CFG_HDR1 'a'
280+
#define CFG_HDR2 'd'
281+
#define CFG_HDR3 '2'
282+
283+
class CfgClass : public CfgParam {
284+
private:
285+
//keep CfgHeader 40 bytes long!!!
286+
struct __attribute__((packed)) CfgHeader {
287+
uint8_t header0 = CFG_HDR0;
288+
uint8_t header1 = CFG_HDR1;
289+
uint8_t header2 = CFG_HDR2;
290+
uint8_t header3 = CFG_HDR3;
291+
uint16_t len = 0; //number of bytes for hdr+param+crc
292+
uint16_t _reserved0;
293+
uint32_t madflight_param_crc;
294+
uint8_t _reserved1[28] = {0};
295+
} hdr;
296+
297+
public:
298+
CfgClass();
299+
void begin();
300+
uint16_t paramCount(); //get number of parameters
301+
bool getNameAndValue(uint16_t index, String* name, float* value); //get parameter name and value for index
302+
void list(const char* filter = nullptr); //CLI print all config values
303+
bool setParam(String namestr, String val); //CLI set a parameter value, returns true on success
304+
bool setParamMavlink(String namestr, float val); //set a parameter value, returns true on success
305+
int getIndex(String namestr); //get parameter index for a parameter name
306+
void clear(); //load defaults from param_list
307+
void loadFromEeprom(); //read parameters from eeprom/flash
308+
void loadFromString(const char *batch); //load text unconditional
309+
bool load_madflight_param(const char *batch); //load text if crc is different, returns true when loaded
310+
void writeToEeprom(); //write config to flash
311+
float getValue(String namestr, float default_value);
312+
313+
//print
314+
void printParamOption(const int32_t* param);
315+
bool getOptionString(uint16_t param_idx, int32_t param_val, char out_option[20]);
316+
void printPins();
317+
void printModule(const char* module_name);
318+
void printNameAndValue(uint16_t i, const char* comment = nullptr);
319+
void printValue(uint16_t i);
320+
321+
private:
322+
bool load_cmdline(String cmdline);
323+
int get_enum_index(const char* k, const char* values);
324+
};
245325

246-
void printModule(const char* modulename);
247-
int getIndex(String namestr);
248-
uint16_t paramCount();
249-
bool setParamMavlink(String namestr, float val);
250-
bool getNameAndValue(uint16_t index, String* name, float* value);
251-
}; //namespace Cfg
326+
extern CfgClass cfg;

0 commit comments

Comments
 (0)