Make better use of range_t methods.

This commit is contained in:
Jared Boone 2016-01-27 14:46:45 -08:00
parent ca3b1a2c5a
commit 7519b83379
8 changed files with 39 additions and 72 deletions

View File

@ -196,18 +196,16 @@ void MAX2837::set_lpf_rf_bandwidth(const uint32_t bandwidth_minimum) {
bool MAX2837::set_frequency(const rf::Frequency lo_frequency) { bool MAX2837::set_frequency(const rf::Frequency lo_frequency) {
/* TODO: This is a sad implementation. Refactor. */ /* TODO: This is a sad implementation. Refactor. */
if( lo_frequency < lo::band[0].min ) { if( lo::band[0].contains(lo_frequency) ) {
return false;
} else if( lo_frequency < lo::band[0].max ) {
_map.r.syn_int_div.LOGEN_BSW = 0b00; /* 2300 - 2399.99MHz */ _map.r.syn_int_div.LOGEN_BSW = 0b00; /* 2300 - 2399.99MHz */
_map.r.rxrf_1.LNAband = 0; /* 2.3 - 2.5GHz */ _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.syn_int_div.LOGEN_BSW = 0b01; /* 2400 - 2499.99MHz */
_map.r.rxrf_1.LNAband = 0; /* 2.3 - 2.5GHz */ _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.syn_int_div.LOGEN_BSW = 0b10; /* 2500 - 2599.99MHz */
_map.r.rxrf_1.LNAband = 1; /* 2.5 - 2.7GHz */ _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.syn_int_div.LOGEN_BSW = 0b11; /* 2600 - 2700Hz */
_map.r.rxrf_1.LNAband = 1; /* 2.5 - 2.7GHz */ _map.r.rxrf_1.LNAband = 1; /* 2.5 - 2.7GHz */
} else { } else {

View File

@ -50,10 +50,10 @@ enum class Mode {
namespace lo { namespace lo {
constexpr std::array<rf::FrequencyRange, 4> band { { constexpr std::array<rf::FrequencyRange, 4> band { {
{ .min = 2300000000, .max = 2400000000, }, { 2300000000, 2400000000 },
{ .min = 2400000000, .max = 2500000000, }, { 2400000000, 2500000000 },
{ .min = 2500000000, .max = 2600000000, }, { 2500000000, 2600000000 },
{ .min = 2600000000, .max = 2700000000, }, { 2600000000, 2700000000 },
} }; } };
} /* namespace lo */ } /* namespace lo */
@ -67,8 +67,8 @@ constexpr int8_t gain_db_max = 40;
constexpr int8_t gain_db_step = 8; constexpr int8_t gain_db_step = 8;
constexpr std::array<rf::FrequencyRange, 2> band { { constexpr std::array<rf::FrequencyRange, 2> band { {
{ .min = 2300000000, .max = 2500000000, }, { 2300000000, 2500000000 },
{ .min = 2500000000, .max = 2700000000, }, { 2500000000, 2700000000 },
} }; } };
} /* namespace lna */ } /* namespace lna */

View File

@ -22,29 +22,14 @@
#ifndef __RF_PATH_H__ #ifndef __RF_PATH_H__
#define __RF_PATH_H__ #define __RF_PATH_H__
#include "utility.hpp"
#include <cstdint> #include <cstdint>
namespace rf { namespace rf {
using Frequency = int64_t; using Frequency = int64_t;
using FrequencyRange = range_t<Frequency>;
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);
}
};
enum class Direction { enum class Direction {
/* Zero-based, used as index into table */ /* Zero-based, used as index into table */
@ -54,20 +39,9 @@ enum class Direction {
namespace path { namespace path {
constexpr FrequencyRange band_low { constexpr FrequencyRange band_low { 0, 2150000000 };
.min = 0, constexpr FrequencyRange band_high { 2750000000, 7250000000 };
.max = 2150000000, constexpr FrequencyRange band_mid { band_low.maximum, band_high.minimum };
};
constexpr FrequencyRange band_high {
.min = 2750000000,
.max = 7250000000,
};
constexpr FrequencyRange band_mid {
.min = band_low.max,
.max = band_high.min,
};
enum class Band { enum class Band {
/* Zero-based, used as index into frequency_bands table */ /* Zero-based, used as index into frequency_bands table */
@ -94,10 +68,7 @@ private:
} /* path */ } /* path */
constexpr FrequencyRange tuning_range { constexpr FrequencyRange tuning_range { path::band_low.minimum, path::band_high.maximum };
.min = path::band_low.min,
.max = path::band_high.max,
};
} /* rf */ } /* rf */

View File

@ -51,10 +51,7 @@ constexpr auto reference_frequency = rffc5072_reference_f;
namespace vco { namespace vco {
constexpr rf::FrequencyRange range { constexpr rf::FrequencyRange range { 2700000000, 5400000000 };
.min = 2700000000U,
.max = 5400000000U,
};
} /* namespace vco */ } /* 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_min = 1U << divider_log2_min;
constexpr size_t divider_max = 1U << divider_log2_max; constexpr size_t divider_max = 1U << divider_log2_max;
constexpr rf::FrequencyRange range { constexpr rf::FrequencyRange range { vco::range.minimum / divider_max, vco::range.maximum / divider_min };
.min = vco::range.min / divider_max,
.max = vco::range.max / divider_min,
};
size_t divider_log2(const rf::Frequency lo_frequency) { size_t divider_log2(const rf::Frequency lo_frequency) {
/* TODO: Error */ /* TODO: Error */

View File

@ -83,13 +83,11 @@ Config high_band(const rf::Frequency target_frequency) {
Config create(const rf::Frequency target_frequency) { Config create(const rf::Frequency target_frequency) {
/* TODO: This is some lame code. */ /* TODO: This is some lame code. */
if( target_frequency < rf::path::band_low.min ) { if( rf::path::band_low.contains(target_frequency) ) {
return { };
} else if( target_frequency < rf::path::band_low.max ) {
return low_band(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); 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); return high_band(target_frequency);
} else { } else {
return { }; return { };

View File

@ -118,13 +118,7 @@ void FrequencyField::on_focus() {
} }
rf::Frequency FrequencyField::clamp_value(rf::Frequency value) { rf::Frequency FrequencyField::clamp_value(rf::Frequency value) {
if( value > range.max ) { return range.clip(value);
value = range.max;
}
if( value < range.min ) {
value = range.min;
}
return value;
} }
/* FrequencyKeypadView ***************************************************/ /* FrequencyKeypadView ***************************************************/

View File

@ -31,8 +31,6 @@
namespace portapack { namespace portapack {
namespace persistent_memory { namespace persistent_memory {
using tuned_frequency_range_t = range_t<rf::Frequency>;
constexpr tuned_frequency_range_t tuned_frequency_range { rf::tuning_range.min, rf::tuning_range.max };
constexpr rf::Frequency tuned_frequency_reset_value { 858750000 }; constexpr rf::Frequency tuned_frequency_reset_value { 858750000 };
using ppb_range_t = range_t<ppb_t>; using ppb_range_t = range_t<ppb_t>;
@ -50,12 +48,12 @@ static_assert(sizeof(data_t) <= 0x100, "Persistent memory structure too large fo
static data_t* const data = reinterpret_cast<data_t*>(LPC_BACKUP_REG_BASE); static data_t* const data = reinterpret_cast<data_t*>(LPC_BACKUP_REG_BASE);
rf::Frequency tuned_frequency() { 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; return data->tuned_frequency;
} }
void set_tuned_frequency(const rf::Frequency new_value) { 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() { ppb_t correction_ppb() {

View File

@ -86,6 +86,20 @@ struct range_t {
value = reset_value; 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 { namespace std {