@@ -22,131 +22,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222SOFTWARE.
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
13030CfgClass 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-
15032CfgClass::CfgClass () {}
15133
15234void 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);
0 commit comments