diff --git a/firmware/application/max2837.cpp b/firmware/application/max2837.cpp index 7456a1bc..e1c759c2 100644 --- a/firmware/application/max2837.cpp +++ b/firmware/application/max2837.cpp @@ -196,18 +196,16 @@ void MAX2837::set_lpf_rf_bandwidth(const uint32_t bandwidth_minimum) { bool MAX2837::set_frequency(const rf::Frequency lo_frequency) { /* TODO: This is a sad implementation. Refactor. */ - if( lo_frequency < lo::band[0].min ) { - return false; - } else if( lo_frequency < lo::band[0].max ) { + if( lo::band[0].contains(lo_frequency) ) { _map.r.syn_int_div.LOGEN_BSW = 0b00; /* 2300 - 2399.99MHz */ _map.r.rxrf_1.LNAband = 0; /* 2.3 - 2.5GHz */ - } else if( lo_frequency < lo::band[1].max ) { + } else if( lo::band[1].contains(lo_frequency) ) { _map.r.syn_int_div.LOGEN_BSW = 0b01; /* 2400 - 2499.99MHz */ _map.r.rxrf_1.LNAband = 0; /* 2.3 - 2.5GHz */ - } else if( lo_frequency < lo::band[2].max ) { + } else if( lo::band[2].contains(lo_frequency) ) { _map.r.syn_int_div.LOGEN_BSW = 0b10; /* 2500 - 2599.99MHz */ _map.r.rxrf_1.LNAband = 1; /* 2.5 - 2.7GHz */ - } else if( lo_frequency < lo::band[3].max ) { + } else if( lo::band[3].contains(lo_frequency) ) { _map.r.syn_int_div.LOGEN_BSW = 0b11; /* 2600 - 2700Hz */ _map.r.rxrf_1.LNAband = 1; /* 2.5 - 2.7GHz */ } else { diff --git a/firmware/application/max2837.hpp b/firmware/application/max2837.hpp index d4666c35..43ffe8b2 100644 --- a/firmware/application/max2837.hpp +++ b/firmware/application/max2837.hpp @@ -50,10 +50,10 @@ enum class Mode { namespace lo { constexpr std::array band { { - { .min = 2300000000, .max = 2400000000, }, - { .min = 2400000000, .max = 2500000000, }, - { .min = 2500000000, .max = 2600000000, }, - { .min = 2600000000, .max = 2700000000, }, + { 2300000000, 2400000000 }, + { 2400000000, 2500000000 }, + { 2500000000, 2600000000 }, + { 2600000000, 2700000000 }, } }; } /* namespace lo */ @@ -67,8 +67,8 @@ constexpr int8_t gain_db_max = 40; constexpr int8_t gain_db_step = 8; constexpr std::array band { { - { .min = 2300000000, .max = 2500000000, }, - { .min = 2500000000, .max = 2700000000, }, + { 2300000000, 2500000000 }, + { 2500000000, 2700000000 }, } }; } /* namespace lna */ diff --git a/firmware/application/rf_path.hpp b/firmware/application/rf_path.hpp index 97cbaa08..84a0a0b3 100644 --- a/firmware/application/rf_path.hpp +++ b/firmware/application/rf_path.hpp @@ -22,29 +22,14 @@ #ifndef __RF_PATH_H__ #define __RF_PATH_H__ +#include "utility.hpp" + #include namespace rf { using Frequency = int64_t; - -struct FrequencyRange { - Frequency min; - Frequency max; - /* TODO: static_assert low < high? */ - - bool below_range(const Frequency f) const { - return f < min; - } - - bool contains(const Frequency f) const { - return (f >= min) && (f < max); - } - - bool out_of_range(const Frequency f) const { - return !contains(f); - } -}; +using FrequencyRange = range_t; enum class Direction { /* Zero-based, used as index into table */ @@ -54,20 +39,9 @@ enum class Direction { namespace path { -constexpr FrequencyRange band_low { - .min = 0, - .max = 2150000000, -}; - -constexpr FrequencyRange band_high { - .min = 2750000000, - .max = 7250000000, -}; - -constexpr FrequencyRange band_mid { - .min = band_low.max, - .max = band_high.min, -}; +constexpr FrequencyRange band_low { 0, 2150000000 }; +constexpr FrequencyRange band_high { 2750000000, 7250000000 }; +constexpr FrequencyRange band_mid { band_low.maximum, band_high.minimum }; enum class Band { /* Zero-based, used as index into frequency_bands table */ @@ -94,10 +68,7 @@ private: } /* path */ -constexpr FrequencyRange tuning_range { - .min = path::band_low.min, - .max = path::band_high.max, -}; +constexpr FrequencyRange tuning_range { path::band_low.minimum, path::band_high.maximum }; } /* rf */ diff --git a/firmware/application/rffc507x.cpp b/firmware/application/rffc507x.cpp index 6d466ea0..e15265e4 100644 --- a/firmware/application/rffc507x.cpp +++ b/firmware/application/rffc507x.cpp @@ -51,10 +51,7 @@ constexpr auto reference_frequency = rffc5072_reference_f; namespace vco { -constexpr rf::FrequencyRange range { - .min = 2700000000U, - .max = 5400000000U, -}; +constexpr rf::FrequencyRange range { 2700000000, 5400000000 }; } /* namespace vco */ @@ -66,10 +63,7 @@ constexpr size_t divider_log2_max = 5; constexpr size_t divider_min = 1U << divider_log2_min; constexpr size_t divider_max = 1U << divider_log2_max; -constexpr rf::FrequencyRange range { - .min = vco::range.min / divider_max, - .max = vco::range.max / divider_min, -}; +constexpr rf::FrequencyRange range { vco::range.minimum / divider_max, vco::range.maximum / divider_min }; size_t divider_log2(const rf::Frequency lo_frequency) { /* TODO: Error */ diff --git a/firmware/application/tuning.cpp b/firmware/application/tuning.cpp index b662fbc3..2519fc82 100644 --- a/firmware/application/tuning.cpp +++ b/firmware/application/tuning.cpp @@ -83,13 +83,11 @@ Config high_band(const rf::Frequency target_frequency) { Config create(const rf::Frequency target_frequency) { /* TODO: This is some lame code. */ - if( target_frequency < rf::path::band_low.min ) { - return { }; - } else if( target_frequency < rf::path::band_low.max ) { + if( rf::path::band_low.contains(target_frequency) ) { return low_band(target_frequency); - } else if( target_frequency < rf::path::band_mid.max ) { + } else if( rf::path::band_mid.contains(target_frequency) ) { return mid_band(target_frequency); - } else if( target_frequency < rf::path::band_high.max ) { + } else if( rf::path::band_high.contains(target_frequency) ) { return high_band(target_frequency); } else { return { }; diff --git a/firmware/application/ui_receiver.cpp b/firmware/application/ui_receiver.cpp index f8bdf748..e45c720e 100644 --- a/firmware/application/ui_receiver.cpp +++ b/firmware/application/ui_receiver.cpp @@ -118,13 +118,7 @@ void FrequencyField::on_focus() { } rf::Frequency FrequencyField::clamp_value(rf::Frequency value) { - if( value > range.max ) { - value = range.max; - } - if( value < range.min ) { - value = range.min; - } - return value; + return range.clip(value); } /* FrequencyKeypadView ***************************************************/ diff --git a/firmware/common/portapack_persistent_memory.cpp b/firmware/common/portapack_persistent_memory.cpp index 63ace663..de4ce64a 100644 --- a/firmware/common/portapack_persistent_memory.cpp +++ b/firmware/common/portapack_persistent_memory.cpp @@ -31,8 +31,6 @@ namespace portapack { namespace persistent_memory { -using tuned_frequency_range_t = range_t; -constexpr tuned_frequency_range_t tuned_frequency_range { rf::tuning_range.min, rf::tuning_range.max }; constexpr rf::Frequency tuned_frequency_reset_value { 858750000 }; using ppb_range_t = range_t; @@ -50,12 +48,12 @@ static_assert(sizeof(data_t) <= 0x100, "Persistent memory structure too large fo static data_t* const data = reinterpret_cast(LPC_BACKUP_REG_BASE); rf::Frequency tuned_frequency() { - tuned_frequency_range.reset_if_outside(data->tuned_frequency, tuned_frequency_reset_value); + rf::tuning_range.reset_if_outside(data->tuned_frequency, tuned_frequency_reset_value); return data->tuned_frequency; } void set_tuned_frequency(const rf::Frequency new_value) { - data->tuned_frequency = tuned_frequency_range.clip(new_value); + data->tuned_frequency = rf::tuning_range.clip(new_value); } ppb_t correction_ppb() { diff --git a/firmware/common/utility.hpp b/firmware/common/utility.hpp index fc8a3b3c..2a2dc9f4 100644 --- a/firmware/common/utility.hpp +++ b/firmware/common/utility.hpp @@ -86,6 +86,20 @@ struct range_t { value = reset_value; } } + + bool below_range(const T& value) const { + return value < minimum; + } + + bool contains(const T& value) const { + // TODO: Subtle gotcha here! Range test doesn't include maximum! + return (value >= minimum) && (value < maximum); + } + + bool out_of_range(const T& value) const { + // TODO: Subtle gotcha here! Range test in contains() doesn't include maximum! + return !contains(value); + } }; namespace std {