Units#

Core Functionality#

Defined in namespace hal

#include <libhal/units.hpp>

namespace hal#

The foundation of libhal containing, interfaces, utilities and soft drivers.

Typedefs

using time_duration = std::chrono::nanoseconds#

The standard time durations in libhal std::chrono::nanoseconds.

using byte = std::uint8_t#

Standard type for bytes in libhal. hal::byte has a number of annoyances that results in more verbose code without much benefit and thus hal::byte was created.

using hertz = float#

Type for frequency represented in hertz.

using g_force = float#

Type for acceleration represented in the force applied by gravity at sea level.

using ampere = float#

Type for current represented in amps.

using volts = float#

Type for voltage represented in volts.

using celsius = float#

Type for temperature represented in celsius.

using rpm = float#

Type for rotational velocity represented in RPMs.

using meters = float#

Type for length represented in meters.

using degrees = float#

Type for angle represented in degrees.

using gauss = float#

Type for magnetic field represented in gauss.

Enums

enum class pin_resistor#

Set of possible pin mode resistor settings.

See each enumeration to get more details about when and how these should be used.

Values:

enumerator none#

No pull up. This will cause the pin to float. This may be desirable if the pin has an external resistor attached or if the signal is sensitive to external devices like resistors.

enumerator pull_down#

Pull the pin down to devices GND. This will ensure that the voltage read by the pin when there is no signal on the pin is LOW (or false).

enumerator pull_up#

See pull down explanation, but in this case the pin is pulled up to VCC, also called VDD on some systems.

namespace literals#

Namespace containing user defined literals for the hal standard units.

Functions

consteval hertz operator""_Hz(long double p_value) noexcept#
consteval hertz operator""_kHz(long double p_value) noexcept#
consteval hertz operator""_MHz(long double p_value) noexcept#
consteval hertz operator""_GHz(long double p_value) noexcept#
consteval g_force operator""_g(long double p_value) noexcept#
consteval ampere operator""_kA(long double p_value) noexcept#
consteval ampere operator""_A(long double p_value) noexcept#
consteval ampere operator""_mA(long double p_value) noexcept#
consteval ampere operator""_uA(long double p_value) noexcept#
consteval volts operator""_kV(long double p_value) noexcept#
consteval volts operator""_V(long double p_value) noexcept#
consteval volts operator""_mV(long double p_value) noexcept#
consteval volts operator""_uV(long double p_value) noexcept#
consteval celsius operator""_C(long double p_value) noexcept#
consteval celsius operator""_F(long double p_value) noexcept#
consteval celsius operator""_K(long double p_value) noexcept#
consteval rpm operator""_rpm(long double p_value) noexcept#
consteval rpm operator""_deg_per_sec(long double p_value) noexcept#
consteval degrees operator""_deg(long double p_value) noexcept#
consteval meters operator""_um(long double p_value) noexcept#
consteval meters operator""_mm(long double p_value) noexcept#
consteval meters operator""_m(long double p_value) noexcept#
consteval meters operator""_km(long double p_value) noexcept#
consteval meters operator""_inch(long double p_value) noexcept#
consteval meters operator""_yards(long double p_value) noexcept#
consteval meters operator""_miles(long double p_value) noexcept#

Utilities#

Defined in namespace hal

#include <libhal-util/units.hpp>

group UnitsUtils

Functions

constexpr std::int64_t cycles_per(hertz p_source, hal::time_duration p_duration)#

Calculate the number of cycles of this frequency within the time duration. This function is meant for timers to determine how many count cycles are needed to reach a particular time duration at this frequency.

Parameters:
  • p_source – - source frequency

  • p_duration – - the amount of time to convert to cycles

Returns:

std::int64_t - number of cycles

template<typename Period>
constexpr std::chrono::duration<int64_t, Period> wavelength(hertz p_source)#

Calculates and returns the wavelength in seconds.

Calculates and returns the wavelength in seconds as a float.

Template Parameters:
  • Period – - desired period (defaults to std::femto for femtoseconds).

  • float_t – - float type

  • Period – - desired period

Parameters:
  • p_source – - source frequency to convert to wavelength

  • p_source – - source frequency to convert to wavelength

Returns:

std::chrono::duration<int64_t, Period> - time based wavelength of the frequency.

Returns:

constexpr float - float representation of the time based wavelength of the frequency.

inline std::optional<std::chrono::nanoseconds> duration_from_cycles(hertz p_source, uint32_t p_cycles)#

Calculate the amount of time it takes a frequency to oscillate a number of cycles.

Parameters:
  • p_source – - the frequency to compute the cycles from

  • p_cycles – - number of cycles within the time duration

Returns:

std::optional<std::chrono::nanoseconds> - time duration based on this frequency and the number of cycles. Will return std::nullopt if the duration exceeds

template<class CharT, class Traits>
inline std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &p_ostream, const hal::byte &p_byte)#

print byte type using ostreams

Meant for unit testing, testing and simulation purposes C++ streams, in general, should not be used for any embedded project that will ever have to be used on an MCU due to its memory cost.

Template Parameters:
  • CharT – - character type

  • Traits – - ostream traits type

Parameters:
  • p_ostream – - the ostream

  • p_byte – - object to convert to a string

Returns:

std::basic_ostream<CharT, Traits>& - reference to the ostream