|
template<typename T > |
constexpr T | constrain (T value, T min, T max) |
| Constrain value to be greater than or equal to min and lower than or equal to max . More...
|
|
template<typename TI , typename TO > |
constexpr TO | map (TI value, TI input_min, TI input_max, TO output_min, TO output_max) |
| Linearly transform value from range [input_min ; input_max ] to range [output_min ; output_max ]. More...
|
|
template<typename T > |
constexpr T | min (T a, T b) |
| Compute the min of 2 integral values. More...
|
|
template<typename T > |
constexpr T | max (T a, T b) |
| Compute the max of 2 integral values. More...
|
|
constexpr uint32_t | power_of_10 (int8_t n) |
| Calculate a power of 10 at compile-time, provided that n is a constant at call time. More...
|
|
constexpr int16_t | map_raw_to_physical (int16_t value, UnitPrefix prefix, int16_t range, uint8_t precision_bits) |
| Convert the raw value , obtained from an electronics device, using precision_bit number of bits (that defines the input range) into a physical measure for which range defines the complete output range for such value, adjusted according to the unit prefix that we want in the resulting measure. More...
|
|
constexpr int16_t | map_physical_to_raw (int16_t value, UnitPrefix prefix, int16_t range, uint8_t precision_bits) |
| Convert an absolute physical value , expressed in some given measurement unit, scaled with prefix , into a raw measurement as if obtained from a electronics device, using precision_bit number of bits (that defines the device raw measure range); for this device, physical measures are within range . More...
|
|
constexpr uint8_t | low_byte (uint16_t word) |
| Extract the low order byte of a 16-bits word. More...
|
|
constexpr uint8_t | high_byte (uint16_t word) |
| Extract the high order byte of a 16-bits word. More...
|
|
constexpr uint16_t | as_uint16_t (uint8_t high, uint8_t low) |
| Convert 2 bytes into an unsigned int. More...
|
|
template<typename T > |
constexpr T | is_zero (T value, T default_value) |
| Replace value by default_value if not "true" (also known as "Elvis
operator"). More...
|
|
template<typename T > |
void | set_mask (volatile T ®, T mask, T value) |
| Common utility to force a part of the value of a register, designated by a bit mask. More...
|
|
template<typename T > |
constexpr bool | is_mask_equal (T actual, T mask, T expected) |
| Common utility to check if 2 values are equal according to a mask. More...
|
|
uint8_t | bcd_to_binary (uint8_t bcd) |
| Convert Binary-coded decimal byte (each nibble is a digit from 0 to 9) into a natural byte. More...
|
|
uint8_t | binary_to_bcd (uint8_t binary) |
| Convert a natural integers to a BCD byte (2 digits). More...
|
|
void | swap_bytes (uint16_t &value) |
| Swap 2 bytes of a 2-bytes integer. More...
|
|
void | swap_bytes (int16_t &value) |
| Swap 2 bytes of a 2-bytes integer. More...
|
|
void | swap_bytes (uint32_t &value) |
| Reverse 4 bytes of a 4-bytes integer. More...
|
|
void | swap_bytes (int32_t &value) |
| Reverse 4 bytes of a 4-bytes integer. More...
|
|
void | swap_bytes (uint64_t &value) |
| Reverse 8 bytes of a 8-bytes integer. More...
|
|
void | swap_bytes (int64_t &value) |
| Reverse 8 bytes of a 8-bytes integer. More...
|
|
template<typename T > |
void | swap (T &a, T &b) |
| Swap the values of 2 variables passed by reference. More...
|
|
template<typename T > |
constexpr uint8_t | as_uint8_t (T input) |
| Cast a one byte long bit-fields struct into a byte. More...
|
|
template<typename T , typename U = uint8_t[sizeof(T)]> |
constexpr void | as_array (const T &input, U output) |
| Cast an instance of type T to an array of uint8_t of the size of T . More...
|
|
constexpr uint8_t | calculate_delay1_count (float time_us) |
| Calculate the count to pass to delay1() in order to reach time_us microseconds delay. More...
|
|
constexpr uint8_t | num_bits (uint8_t mask, uint8_t num=0) |
| Calculate the number of 1 bits in a byte. More...
|
|
template<typename T > |
T | change_endianness (const T &value) |
| Change endianness of any integral type (from big to small or small to big). More...
|
|
Contains all generic utility methods.
constexpr int16_t utils::map_raw_to_physical |
( |
int16_t |
value, |
|
|
UnitPrefix |
prefix, |
|
|
int16_t |
range, |
|
|
uint8_t |
precision_bits |
|
) |
| |
|
constexpr |
Convert the raw value
, obtained from an electronics device, using precision_bit
number of bits (that defines the input range) into a physical measure for which range
defines the complete output range for such value, adjusted according to the unit prefix
that we want in the resulting measure.
This is useful when you need to display real measurement from raw values provided by a sensor. Note however, that in most cases, this method will be evaluated at runtime only, and thus will drag all arithmetic computation libraries. If you need to know the physical measure from a raw value, only to compare it against some constant physical value, then it is preferable to convert the latter, with map_raw_to_physical()
which will be evaluated at compile time, and only compare raw values in your program: that will help decrease code size and improve code speed (no runtime conversions needed).
static constexpr const GyroRange GYRO_RANGE = GyroRange::RANGE_250;
static constexpr const AccelRange ACCEL_RANGE = AccelRange::RANGE_2G;
inline int16_t gyro(int16_t value)
{
}
inline int16_t accel(int16_t value)
{
}
Defines API for magnetic sensors for direction, speed and acceleration properties.
AccelRange
The full-scale range of the accelerometer in g (datasheet §6.2).
GyroRange
The full-scale range of the gyroscope in dps (datasheet §6.1).
Contains all generic utility methods.
constexpr int16_t map_raw_to_physical(int16_t value, UnitPrefix prefix, int16_t range, uint8_t precision_bits)
Convert the raw value, obtained from an electronics device, using precision_bit number of bits (that ...
- Parameters
-
value | the raw value to convert |
prefix | the unit scale prefix to use to compute the physical value |
range | the physical measure matching the maximum raw value |
precision_bits | the number of significant bits of the raw value; only positive values are accounted, hence if a raw measure can be any value in [-32768;+32767], then precision_bits is 15 . |
- Returns
- the physical value calculated from
value
, scaled according to prefix
- See also
- map_physical_to_raw()
Definition at line 219 of file utilities.h.
constexpr int16_t utils::map_physical_to_raw |
( |
int16_t |
value, |
|
|
UnitPrefix |
prefix, |
|
|
int16_t |
range, |
|
|
uint8_t |
precision_bits |
|
) |
| |
|
constexpr |
Convert an absolute physical value
, expressed in some given measurement unit, scaled with prefix
, into a raw measurement as if obtained from a electronics device, using precision_bit
number of bits (that defines the device raw measure range); for this device, physical measures are within range
.
Computations done by this method will be performed at compile-time as long as all provided arguments are constants; this is important as this will help optimize code size and execution time. This is useful when you want to compare physical values against meaningful limits, and perform actions based on these comparisons; instead of always converting measured raw values into physical ones and then compare with a physical limit, you do the opposite, compare the measured raw values against the raw limits (converted, at compile-time, from physical limits constants).
static constexpr const AccelRange ACCEL_RANGE = AccelRange::RANGE_2G;
static constexpr const uint16_t ACCEL_RANGE_IN_G = ACCEL_RANGE_G(ACCEL_RANGE);
static constexpr const int16_t ACCEL_1 =
map_physical_to_raw(500, UnitPrefix::MILLI, ACCEL_RANGE_IN_G, 15);
static constexpr const int16_t ACCEL_2 =
map_physical_to_raw(1000, UnitPrefix::MILLI, ACCEL_RANGE_IN_G, 15);
void check_accel(int16_t raw)
{
if (raw < 0) raw = -raw;
if (raw < ACCEL_1)
act_when_low_accel();
else if (raw < ACCEL_2)
act_when_mid_accel();
else if (raw < ACCEL_2)
act_when_high_accel();
}
constexpr int16_t map_physical_to_raw(int16_t value, UnitPrefix prefix, int16_t range, uint8_t precision_bits)
Convert an absolute physical value, expressed in some given measurement unit, scaled with prefix,...
- Parameters
-
value | the physical value to convert |
prefix | the unit scale prefix in which value is expressed |
range | the physical measure matching the maximum raw value |
precision_bits | the number of significant bits of the raw value; only positive values are accounted, hence if a raw measure can be any value in [-32768;+32767], then precision_bits is 15 . |
- Returns
- the raw value calculated from
value
, as if directly returned by the device sensor
- See also
- map_raw_to_physical()
Definition at line 292 of file utilities.h.