parameters.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #pragma once
  2. #include <stdint.h>
  3. #include "board_config.h"
  4. #define MAX_PUBLIC_KEYS 5
  5. #define PUBLIC_KEY_LEN 32
  6. #define PARAM_NAME_MAX_LEN 16 // 参数名最长
  7. #define PARAM_FLAG_NONE 0
  8. #define PARAM_FLAG_PASSWORD (1U<<0)
  9. #define PARAM_FLAG_HIDDEN (1U<<1)
  10. class Parameters {
  11. public:
  12. #if defined(PIN_CAN_TERM)
  13. uint8_t can_term = !CAN_TERM_EN; // 是否用CAN终端电阻
  14. #endif
  15. int8_t lock_level; // 固件锁等级(-1=无锁,0-2=不同安全级别)
  16. uint8_t can_node; // CAN总线节点ID
  17. uint8_t bcast_powerup; // 上电即广播(1=是,0=等待有效位置)
  18. uint32_t baudrate = 57600; // 串口波特率
  19. uint8_t ua_type; // 无人机类型
  20. uint8_t id_type; // ID类型
  21. char uas_id[21] = "ABCD123456789";// 无人机ID(20字符)
  22. // 第二个ID(可选,用于多架次)
  23. uint8_t ua_type_2;
  24. uint8_t id_type_2;
  25. char uas_id_2[21] = "ABCD123456789";
  26. float wifi_nan_rate; // WiFi NAN广播频率(Hz)
  27. float wifi_beacon_rate; // WiFi Beacon广播频率(Hz)
  28. float wifi_power; // WiFi发射功率(dBm)
  29. float bt4_rate; // 经典蓝牙广播频率
  30. float bt4_power; // 经典蓝牙功率
  31. float bt5_rate; // BLE长距离广播频率
  32. float bt5_power; // BLE长距离功率
  33. uint8_t done_init; // 首次初始化完成标志
  34. uint8_t protocol; // 协议选择:0=国际,1=国标2023 2 GB2025
  35. uint8_t com_port; // 0 can 1 uart
  36. uint8_t webserver_enable; // 启用Web服务器
  37. uint8_t mavlink_sysid; // mavlink系统id默认值通常是 0 或 245
  38. char wifi_ssid[21] = ""; // WiFi名称
  39. char wifi_password[21] = "ArduRemoteID"; // WiFi密码
  40. uint8_t wifi_channel = 6;// WiFi信道
  41. uint8_t to_factory_defaults = 0; // 恢复出厂设置
  42. uint8_t options; // 功能选项(位掩码)
  43. struct {
  44. char b64_key[64]; // Base64编码的公钥
  45. } public_keys[MAX_PUBLIC_KEYS]; // 最多5个公钥
  46. // 参数类型枚举
  47. enum class ParamType {
  48. NONE=0, // 无
  49. UINT8=1, // 8位无符号整数
  50. UINT32=2, // 32位无符号整数
  51. FLOAT=3, // 浮点数
  52. CHAR20=4, // 20字符字符串
  53. CHAR64=5, // 64字符字符串
  54. INT8=6, // 8位有符号整数
  55. };
  56. // 参数描述结构体
  57. struct Param {
  58. char name[PARAM_NAME_MAX_LEN+1];// 参数名(如"WIFI_SSID")
  59. ParamType ptype; // 参数类型
  60. const void *ptr; // 指向实际变量的指针
  61. float default_value; // 默认值
  62. float min_value; // 最小值
  63. float max_value; // 最大值
  64. uint16_t flags; // 标志位(密码、隐藏等)
  65. uint8_t min_len; // 最小长度(字符串用)
  66. // 操作方法
  67. // 设置值
  68. void set_float(float v) const;
  69. void set_uint8(uint8_t v) const;
  70. void set_int8(int8_t v) const;
  71. void set_uint32(uint32_t v) const;
  72. void set_char20(const char *v) const;
  73. void set_char64(const char *v) const;
  74. // 获取值
  75. uint8_t get_uint8() const;
  76. int8_t get_int8() const;
  77. uint32_t get_uint32() const;
  78. float get_float() const;
  79. const char *get_char20() const;
  80. const char *get_char64() const;
  81. bool get_as_float(float &v) const;
  82. void set_as_float(float v) const;
  83. };
  84. static const struct Param params[]; // 只是声明,不分配内存
  85. // 参数查找
  86. static const Param *find(const char *name); // 按名字找
  87. static const Param *find_by_index(uint16_t idx); // 按索引找
  88. static const Param *find_by_index_float(uint16_t idx); // 找可转浮点的参数
  89. void init(void); // 初始化
  90. bool have_basic_id_info(void) const; // 检查是否有id信息
  91. bool have_basic_id_2_info(void) const; // 检查是否有第2个id信息
  92. bool set_by_name_uint8(const char *name, uint8_t v); // 设置UINT8
  93. bool set_by_name_int8(const char *name, int8_t v); // 设置INT8
  94. bool set_by_name_char64(const char *name, const char *s);// 设置字符串
  95. bool set_by_name_string(const char *name, const char *s); // 通用字符串设置
  96. /*
  97. return a public key
  98. */
  99. bool get_public_key(uint8_t i, uint8_t key[32]) const; // 获取公钥
  100. bool set_public_key(uint8_t i, const uint8_t key[32]);// 设置公钥
  101. bool remove_public_key(uint8_t i); // 删除公钥
  102. bool no_public_keys(void) const; // 检查是否有公钥
  103. static uint16_t param_count_float(void); // 统计可浮点化的参数数量 排除字符串等非数值类型
  104. static int16_t param_index_float(const Param *p); // 获取参数的浮点索引 返回它在浮点参数列表中的索引位置(不是原始参数表的索引)
  105. private:
  106. void load_defaults(void); // 加载默认参数
  107. };
  108. // bits for OPTIONS parameter
  109. #define OPTIONS_FORCE_ARM_OK (1U<<0)
  110. #define OPTIONS_DONT_SAVE_BASIC_ID_TO_PARAMETERS (1U<<1)
  111. #define OPTIONS_PRINT_RID_MAVLINK (1U<<2)
  112. // protocol
  113. #define PROTOCOL_EU 0
  114. #define PROTOCOL_CN2023 1
  115. #define PROTOCOL_GB2025 2
  116. // com_port
  117. #define CAN_PORT 0
  118. #define UART_PORT 1
  119. extern Parameters g;