Skip to content

Commit

Permalink
[math] More generatic prescaler computation class
Browse files Browse the repository at this point in the history
  • Loading branch information
rleh committed May 8, 2021
1 parent ab8fa00 commit adbe4da
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/modm/math/algorithm/prescaler.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2019, Niklas Hauser
* Copyright (c) 2021, Raphael Lehmann
*
* This file is part of the modm project.
*
Expand All @@ -25,14 +26,15 @@ namespace modm
* @note For ranges the end is *inclusive*: [begin, end]!
* @ingroup modm_math_algorithm
*/
template<typename T>
class
Prescaler
GenericPrescaler
{
public:
struct Result
{
constexpr Result(modm::frequency_t input_frequency,
modm::frequency_t desired_frequency,
constexpr Result(T input_frequency,
T desired_frequency,
uint32_t index, uint32_t prescaler) :
index{index}, prescaler{prescaler},
frequency{input_frequency / prescaler},
Expand All @@ -45,11 +47,11 @@ Prescaler
/// Prescaler value
const uint32_t prescaler;
/// Generated frequency
const modm::frequency_t frequency;
const T frequency;
/// Input frequency
const modm::frequency_t input_frequency;
const T input_frequency;
/// Desired Frequency
const modm::frequency_t desired_frequency;
const T desired_frequency;
/// Relative Frequency Error
const float error;
};
Expand All @@ -59,8 +61,8 @@ Prescaler
/// @note container must have `begin()`, `end()` and `size()` function!
template<class Iterator>
static constexpr Result
from_iterator(modm::frequency_t input_frequency,
modm::frequency_t desired_frequency,
from_iterator(T input_frequency,
T desired_frequency,
Iterator prescalers)
{
const double desired = double(input_frequency) / desired_frequency;
Expand All @@ -87,8 +89,8 @@ Prescaler

/// From a initializer list.
static constexpr Result
from_list(modm::frequency_t input_frequency,
modm::frequency_t desired_frequency,
from_list(T input_frequency,
T desired_frequency,
std::initializer_list<uint32_t> prescalers)
{
return from_iterator(input_frequency, desired_frequency, prescalers);
Expand All @@ -98,8 +100,8 @@ Prescaler
/// @note the range end is *inclusive*: [begin, end].
template< typename Function >
static constexpr Result
from_function(modm::frequency_t input_frequency,
modm::frequency_t desired_frequency,
from_function(T input_frequency,
T desired_frequency,
uint32_t begin, uint32_t end,
Function prescaler_modifier)
{
Expand Down Expand Up @@ -127,8 +129,8 @@ Prescaler
/// From any linear range.
/// @note the range end is *inclusive*: [begin, end].
static constexpr Result
from_range(modm::frequency_t input_frequency,
modm::frequency_t desired_frequency,
from_range(T input_frequency,
T desired_frequency,
uint32_t begin, uint32_t end)
{
const double desired = double(input_frequency) / desired_frequency;
Expand All @@ -147,8 +149,8 @@ Prescaler
/// @param begin must be a power-of-two!
/// @param end must be a power-of-two!
static constexpr Result
from_power(modm::frequency_t input_frequency,
modm::frequency_t desired_frequency,
from_power(T input_frequency,
T desired_frequency,
uint32_t begin, uint32_t end)
{
const double desired = double(input_frequency) / desired_frequency;
Expand All @@ -159,4 +161,6 @@ Prescaler
}
};

using Prescaler = GenericPrescaler<modm::frequency_t>;

} // namespace modm

0 comments on commit adbe4da

Please sign in to comment.