![]() |
Speeduino
|

Go to the source code of this file.
Macros | |
| #define | DIV_ROUND_DOWN -1 |
| Rounding behavior: always round down. | |
| #define | DIV_ROUND_UP 1 |
| Rounding behavior: always round up. | |
| #define | DIV_ROUND_NEAREST 0 |
| Rounding behavior: round to nearest. | |
| #define | DIV_ROUND_BEHAVIOR DIV_ROUND_NEAREST |
| Integer division rounding behavior. | |
| #define | DIV_ROUND_CORRECT(d, t) ((t)(((d)>>1U)+(t)DIV_ROUND_BEHAVIOR)) |
| Computes the denominator correction for rounding division based on our rounding behavior. | |
| #define | DIV_ROUND_CLOSEST(n, d, t) |
| Rounded integer division. | |
| #define | UDIV_ROUND_CLOSEST(n, d, t) ((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d)) |
| Rounded unsigned integer division. | |
| #define | UDIV_ROUND_UP(n, d, t) ((t)((n) + (t)((d)+1U))/(t)(d)) |
| Rounding up unsigned integer division. | |
| #define | IS_INTEGER(d) ((d) == (int32_t)(d)) |
| Test whether the parameter is an integer or not. | |
| #define | TEST_AND_APPLY(precision) |
Functions | |
| uint8_t | random1to100 (void) noexcept |
| uint16_t | pwmFreqToTicks (uint16_t frequency) |
| Convert a frequency in Hz to the equivalent number of PWM timer ticks. | |
| template<uint16_t divisor> | |
| TESTABLE_STATIC_CONSTEXPR uint16_t | div_round_closest_u16 (uint16_t n) |
| Rounded unsigned integer division optimized for compile time constants. | |
| static uint32_t | div360 (uint32_t n) |
| Optimised integer division by 360. | |
| template<uint8_t b> | |
| static uint32_t | rshift_round (uint32_t a) |
| Rounded arithmetic right shift. | |
| static uint32_t | percentageApprox (uint16_t percent, uint32_t value) |
| Integer based percentage calculation: faster, but less accurate, than percentage() | |
| static uint32_t | percentageApprox (uint8_t percent, uint32_t value) noexcept |
| Slightly faster version of percentageApprox(uint16_t, uint32_t), since we know percent<256. | |
| static uint32_t | percentage (uint16_t percent, uint32_t value) |
| Integer based percentage calculation. | |
| static uint16_t | halfPercentage (uint8_t percent, uint16_t value) |
| Integer based half-percentage calculation. | |
| static int16_t | nudge (int16_t min, int16_t max, int16_t value, int16_t nudgeAmount) |
| Make one pass at correcting the value into the range [min, max) | |
| template<typename TDividend , typename TDivisor > | |
| TESTABLE_STATIC_CONSTEXPR TDividend | fast_div_closest (TDividend dividend, TDivisor divisor) |
| Same as fast_div(), except this will round to nearest integer instead of truncating. | |
| template<class T > | |
| TESTABLE_STATIC_CONSTEXPR const T & | clamp (const T &v, const T &lo, const T &hi) |
| clamps a given value between the minimum and maximum thresholds. | |
| static uint16_t | LOW_PASS_FILTER (uint16_t input, uint8_t alpha, uint16_t prior) |
| Simple low pass IIR filter 16-bit values. | |
| static int16_t | LOW_PASS_FILTER (int16_t input, uint8_t alpha, int16_t prior) |
| Simple low pass IIR filter for S16 values. | |
| static uint8_t | scale (const uint8_t from, const uint8_t fromRange, const uint8_t toRange) |
| Scale a value from one range to another. | |
| static uint8_t | fast_map (const uint8_t from, const uint8_t fromLow, const uint8_t fromHigh, const uint8_t toLow, const uint8_t toHigh) |
| Specialist version of map(long, long, long, long, long) for performance. | |
| static uint16_t | div100 (uint16_t n) |
| Performance optimised integer division by 100. I.e. same as n/100. | |
| static int16_t | div100 (int16_t n) |
| Performance optimised integer division by 100. I.e. same as n/100. | |
| static uint32_t | div100 (uint32_t n) |
| Performance optimised integer division by 100. I.e. same as n/100. | |
| static int32_t | div100 (int32_t n) |
| Performance optimised integer division by 100. I.e. same as n/100. | |
Variables | |
| static constexpr uint32_t | MICROS_PER_SEC = UINT32_C(1000000) |
| Self-explanatory. | |
| static constexpr uint32_t | MICROS_PER_MIN = MICROS_PER_SEC*60U |
| Self-explanatory. | |
| static constexpr uint32_t | MICROS_PER_HOUR = MICROS_PER_MIN*60U |
| Self-explanatory. | |
| static constexpr uint32_t | MILLI_PER_SEC = MICROS_PER_SEC/1000 |
| Self-explanatory. | |
| static constexpr uint8_t | ONE_HUNDRED_PCT = 100U |
| This is only here to eliminate magic numbers. | |
clamps a given value between the minimum and maximum thresholds.
Uses operator< to compare the values.
| T | Any type that supports operator< |
| v | The value to clamp |
| lo | The minimum threshold |
| hi | The maximum threshold |

Performance optimised integer division by 100. I.e. same as n/100.
Uses the rounding behaviour controlled by DIV_ROUND_BEHAVIOR
| n | Dividend to divide by 100 |

Performance optimised integer division by 100. I.e. same as n/100.
Uses the rounding behaviour controlled by DIV_ROUND_BEHAVIOR
| n | Dividend to divide by 100 |

Performance optimised integer division by 100. I.e. same as n/100.
Uses the rounding behaviour controlled by DIV_ROUND_BEHAVIOR
| n | Dividend to divide by 100 |


Performance optimised integer division by 100. I.e. same as n/100.
Uses the rounding behaviour controlled by DIV_ROUND_BEHAVIOR
| n | Dividend to divide by 100 |

Optimised integer division by 360.
| n | The numerator (dividee) (an integer) |


| TESTABLE_STATIC_CONSTEXPR TDividend fast_div_closest | ( | TDividend | dividend, |
| TDivisor | divisor | ||
| ) |
Same as fast_div(), except this will round to nearest integer instead of truncating.
Minor performance drop compared to non-rounding version.

|
inlinestatic |
Specialist version of map(long, long, long, long, long) for performance.
Maps a value from one range to another.
| from | Value to map [fromLow, fromHigh] |
| fromLow | Lower bound of the from range |
| fromHigh | Upper bound of the from range |
| toLow | Lower bound of the to range |
| toHigh | Upper bound of the to range |

Integer based half-percentage calculation.
| percent | The percent to calculate ([0, 100]) |
| value | The value to operate on |


Simple low pass IIR filter for S16 values.
Simple low pass IIR filter 16-bit values.
This is effectively implementing the smooth filter from playground.arduino.cc/Main/Smooth But removes the use of floats and uses 8 bits of fixed precision.
| input | incoming unfiltered value |
| alpha | filter factor. 0=off, 255=full smoothing (0.00 to 0.99 in float, 0-99%) |
| prior | previous filtered value. |

Make one pass at correcting the value into the range [min, max)
| min | Minimum value (inclusive) |
| max | Maximum value (exclusive) |
| value | Value to nudge |
| nudgeAmount | Amount to change value by |

Integer based percentage calculation.
| percent | The percent to apply to value |
| value | The value to operate on |


Integer based percentage calculation: faster, but less accurate, than percentage()
Recommended use case is when dealing with percentages >50%.
| percent | The percent to apply to value |
| value | The value to operate on |
| Percent | Maximum Error | Example |
|---|---|---|
| 1%-6% | 9% | percentage(4, 563) -> 23 |
percentageApprox(4, 563) -> 21 | ||
| 7%-40% | 1% | percentage(10, 1806) -> 181 |
percentageApprox(10, 1806) -> 179 | ||
| 41%+ | <0.3% | percentage(79, 2371) -> 1873 |
percentageApprox(79, 2371) -> 1870 |

Slightly faster version of percentageApprox(uint16_t, uint32_t), since we know percent<256.
Convert a frequency in Hz to the equivalent number of PWM timer ticks.


Rounded arithmetic right shift.
Right shifting throws away bits. When use for fixed point division, this effectively rounds down (towards zero). To round-to-the-nearest-integer when right-shifting by S, just add in 2 power b−1 (which is the fixed-point equivalent of 0.5) first
| b | number of bits to shift by |
| a | value to shift |
|
inlinestatic |
Scale a value from one range to another.
Takes a value from a range of [0, fromRange] and scales it to a range of [0, toRange].
| from | Value to scale [0, fromRange] |
| fromRange | Zero based range of the from value |
| toRange | Zero based range of the to value |

|
staticconstexpr |
Self-explanatory.
|
staticconstexpr |
Self-explanatory.
|
staticconstexpr |
Self-explanatory.