SI Units¶
lbuild module: modm:math:units
modm uses a couple of common SI units for configuration of peripherals:
frequency_t
in Hertz:Hz
,kHz
andMHz
.baudrate_t
in Baud:Bd
,kBd
,MBd
.bitrate_t
in bit/s,bps
.
These are integral units, so 1 Hz/Bd/bps cannot be split further, and are cast
directly to uint32_t
type, so they can be used as a non-type template argument.
Conversion can be done via constexpr functions from any numerical type:
modm::Hz(T value)
,modm::kHz(T value)
,modm::MHz(T value)
.modm::Bd(T value)
,modm::kBd(T value)
,modm::MBd(T value)
.modm::bps(T value)
,modm::kbps(T value)
,modm::Mbps(T value)
.
In addition, user-defined literals are provided in the modm::literals
namespace:
using namespace modm::literals;
frequency_t frequency = modm::Mhz(10.5);
frequency = 10.5_MHz;
baudrate_t baudrate = 115.2_kBd;
baudrate = modm::kBd(115.2);
bitrate_t bitrate = modm::kbps(125);
bitrate = 125_kbps;
frequency = 4295_MHz; // OVERFLOW at 2^32 units!
Integral Percentages¶
Since float
cannot be used as a non-type template argument, an integer type
for providing tolerances in percent_t
is available.
Note that percent_t
is implemented as an enum class, which prevents implicit
conversions, since the base for this is not 1.
You must therefore use the modm::pct(T value)
or _pct
constructors.
using namespace modm::literals;
percent_t tolerance = modm::pct(10);
tolerance = 10_pct;
// convert back to float. *internal use only*
float percent = modm::pct2f(tolerance);
This type is not guaranteed to hold more than 100 percent!