major: refactor

- Introduced new enums for LoRa bandwidth and coding rate to improve clarity.
- Updated default values for preamble length, frequency, bandwidth, and coding rate.
- Added new structures for modulation and packet parameters, encapsulating related settings.
- Refactored functions to utilize the new structures, improving parameter management in the llcc68 component.
This commit is contained in:
2025-05-30 16:15:41 +08:00
parent 4602468822
commit ee1c1a0497
2 changed files with 124 additions and 165 deletions

View File

@ -11,19 +11,86 @@
#include "radiolib_definitions.hpp"
namespace app::driver::llcc68 {
using freq_t = float;
using freq_t = float;
enum LoRaBandwidth : uint8_t {
BW_10_4 = 0x08,
BW_15_6 = 0x01,
BW_20_8 = 0x09,
BW_31_25 = 0x02,
BW_41_7 = 0x0A,
BW_62_5 = 0x03,
BW_125_0 = 0x04,
BW_250_0 = 0x05,
BW_500_0 = 0x06,
};
enum LoRaCodingRate : uint8_t {
CR_4_5 = 0x01,
CR_4_6 = 0x02,
CR_4_7 = 0x03,
CR_4_8 = 0x04,
};
constexpr uint16_t DEFAULT_PREAMBLE_LEN = 8;
constexpr uint8_t DEFAULT_SYNC_WORD = RADIOLIB_SX126X_SYNC_WORD_PRIVATE;
constexpr uint16_t DEFAULT_PREAMBLE_LEN = 6; // recommended is 8 symbols though
constexpr uint8_t DEFAULT_CRC_TYPE = RADIOLIB_SX126X_LORA_CRC_OFF;
constexpr uint8_t DEFAULT_HEADER_TYPE = RADIOLIB_SX126X_LORA_HEADER_EXPLICIT;
constexpr uint8_t DEFAULT_IQ_TYPE = RADIOLIB_SX126X_LORA_IQ_STANDARD;
constexpr uint8_t PACKET_TYPE = RADIOLIB_SX126X_PACKET_TYPE_LORA;
constexpr auto DEFAULT_BW = RADIOLIB_SX126X_LORA_BW_125_0;
constexpr auto DEFAULT_SF = 9;
constexpr auto DEFAULT_CR = RADIOLIB_SX126X_LORA_CR_4_6;
constexpr auto DEFAULT_LDR_OPTIMIZE = RADIOLIB_SX126X_LORA_LOW_DATA_RATE_OPTIMIZE_ON;
constexpr auto DEFAULT_FREQUENCY = freq_t{433.2};
constexpr auto DEFAULT_SF = 7;
constexpr auto DEFAULT_FREQUENCY = freq_t{433.05};
constexpr auto DEFAULT_BW = BW_250_0;
constexpr auto DEFAULT_CR = CR_4_5;
constexpr auto DEFAULT_LDR_OPTIMIZE = RADIOLIB_SX126X_LORA_LOW_DATA_RATE_OPTIMIZE_OFF;
struct modulation_params_t {
uint8_t bw;
uint8_t sf;
uint8_t cr;
uint8_t ldr_optimize;
static modulation_params_t Default() {
return modulation_params_t{
.bw = DEFAULT_BW,
.sf = DEFAULT_SF,
.cr = DEFAULT_CR,
.ldr_optimize = DEFAULT_LDR_OPTIMIZE,
};
}
};
struct packet_params_t {
uint16_t preamble_len;
uint8_t payload_len;
uint8_t crc_type;
uint8_t hdr_type;
static packet_params_t Default() {
return packet_params_t{
.preamble_len = DEFAULT_PREAMBLE_LEN,
.payload_len = 0xff,
.crc_type = DEFAULT_CRC_TYPE,
.hdr_type = DEFAULT_HEADER_TYPE,
};
}
};
struct lora_parameters_t {
modulation_params_t mod_params;
packet_params_t packet_params;
freq_t frequency;
uint8_t sync_word;
static lora_parameters_t Default() {
return lora_parameters_t{
.mod_params = modulation_params_t::Default(),
.packet_params = packet_params_t::Default(),
.frequency = DEFAULT_FREQUENCY,
.sync_word = DEFAULT_SYNC_WORD,
};
}
};
enum class ChipType {
Unknown,
@ -116,32 +183,6 @@ struct __attribute__((packed)) status_t {
};
static_assert(sizeof(status_t) == 1);
struct parameters_t {
uint8_t bw;
uint8_t sf;
uint8_t cr;
uint8_t ldr_optimize;
uint16_t preamble_len;
uint8_t sync_word;
freq_t frequency;
static parameters_t Default() {
return parameters_t{
.bw = DEFAULT_BW,
.sf = DEFAULT_SF,
.cr = DEFAULT_CR,
.ldr_optimize = DEFAULT_LDR_OPTIMIZE,
.preamble_len = DEFAULT_PREAMBLE_LEN,
.sync_word = DEFAULT_SYNC_WORD,
.frequency = DEFAULT_FREQUENCY,
};
}
constexpr static size_t size() {
return sizeof(parameters_t);
}
} __attribute__((packed));
struct __attribute__((packed)) lora_packet_status_t {
// Average over last packet received of RSSI
@ -209,25 +250,6 @@ struct __attribute__((packed)) op_error_t {
static_assert(sizeof(op_error_t) == 2);
struct calc_time_on_air_t {
uint8_t bw;
uint8_t sf;
uint8_t cr;
uint16_t preamble_length;
static constexpr uint8_t ldro = DEFAULT_LDR_OPTIMIZE;
static constexpr uint8_t crc_type = DEFAULT_CRC_TYPE;
static constexpr uint8_t header_type = DEFAULT_HEADER_TYPE;
static constexpr uint8_t iq_type = DEFAULT_IQ_TYPE;
static constexpr uint8_t sync_word = DEFAULT_SYNC_WORD;
static calc_time_on_air_t from_parameters(const parameters_t &params) {
return calc_time_on_air_t{
.bw = params.bw,
.sf = params.sf,
.cr = params.cr,
.preamble_length = params.preamble_len};
}
};
constexpr bool in_range(const auto v, const auto min, const auto max) {
return v >= min && v <= max;
}
@ -332,60 +354,6 @@ constexpr bool valid_sf(const uint8_t bw, const uint8_t sf) {
constexpr bool valid_freq(const freq_t freq) {
return in_range(freq, freq_t{150.0}, freq_t{960.0});
}
/**
* \brief check if the parameters are valid. if not, set them to default.
* \param params the parameters to check
* \return if the parameters are valid, and the modified parameters
*/
constexpr std::tuple<bool, parameters_t>
check_fix_params(parameters_t params) {
bool ok = true;
if (not valid_bw(params.bw)) {
params.bw = DEFAULT_BW;
ok = false;
}
if (not valid_sf(params.bw, params.sf)) {
params.sf = DEFAULT_SF;
ok = false;
}
if (not valid_cr(params.cr)) {
params.cr = DEFAULT_CR;
ok = false;
}
if (not valid_freq(params.frequency)) {
params.frequency = DEFAULT_FREQUENCY;
ok = false;
}
if (not valid_ldr_optimize(params.ldr_optimize)) {
params.ldr_optimize = DEFAULT_LDR_OPTIMIZE;
ok = false;
}
return {ok, params};
}
/**
* \brief only do the check
*/
constexpr bool check_params(const parameters_t &params) {
if (not valid_bw(params.bw)) {
return false;
}
if (not valid_sf(params.bw, params.sf)) {
return false;
}
if (not valid_cr(params.cr)) {
return false;
}
if (not valid_freq(params.frequency)) {
return false;
}
if (not valid_ldr_optimize(params.ldr_optimize)) {
return false;
}
return true;
}
}
#endif // LLCC68_DEFINITIONS_H