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. Libhal does not use
std::byte
because it has a number of annoyances that results in more verbose code, usually a lot of static_casts, without much benefit. Thus hal::byte was created.
-
using u8 = std::uint8_t#
-
using u16 = std::uint16_t#
-
using u32 = std::uint32_t#
-
using u64 = std::uint64_t#
-
using i8 = std::int8_t#
-
using i16 = std::int16_t#
-
using i32 = std::int32_t#
-
using i64 = std::int64_t#
-
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.
-
enumerator none#
-
namespace literals#
Namespace containing user defined literals for the hal standard units.
Functions
-
using time_duration = std::chrono::nanoseconds#
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, hal::byte const &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
-
constexpr std::int64_t cycles_per(hertz p_source, hal::time_duration p_duration)#