25#include "boards/board.h"
26#include <util/atomic.h>
46#define synchronized _Pragma("GCC diagnostic ignored \"-Wreturn-type\"") ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
88 template<
typename TI,
typename TO>
89 constexpr TO
map(TI value, TI input_min, TI input_max, TO output_min, TO output_max)
91 return output_min + (value - input_min) * (output_max - output_min) / (input_max - input_min);
127 template<
typename T>
constexpr T
min(T a, T b)
129 return (a < b ? a : b);
143 template<
typename T>
constexpr T
max(T a, T b)
145 return (a > b ? a : b);
222 const int8_t prefix_value = int8_t(prefix);
223 if (prefix_value > 0)
224 return (int32_t(value) * int32_t(
range) / int32_t(
power_of_10(prefix_value))) >> precision_bits;
226 return (int32_t(value) * int32_t(
range) * int32_t(
power_of_10(prefix_value))) >> precision_bits;
231 constexpr int32_t map_physical_to_raw_(int32_t value, int8_t prefix, int32_t range, uint8_t precision_bits)
235 return (value << precision_bits) * int32_t(
power_of_10(prefix)) / range;
237 return (value << precision_bits) / int32_t(
power_of_10(prefix)) / range;
295 const int32_t output = map_physical_to_raw_(value, int8_t(prefix),
range, precision_bits);
297 if (output > INT16_MAX)
299 else if (output <= INT16_MIN)
310 return uint8_t(word & 0xFF);
318 return uint8_t(word >> 8);
329 return uint16_t(uint16_t(high) << 8) | uint16_t(low);
339 template<
typename T>
constexpr T
is_zero(T value, T default_value)
341 return (value ? value : default_value);
352 template<
typename T>
void set_mask(
volatile T& reg, T mask, T value)
354 reg = (reg & ~mask) | (value & mask);
364 template<
typename T>
constexpr bool is_mask_equal(T actual, T mask, T expected)
366 return (actual & mask) == (expected & mask);
377 const uint8_t tens = bcd / 16;
379 return (tens << 3) + (tens << 1) + (bcd & 0x0F);
407 value = (value >> 8) | (value << 8);
427 uint16_t value1 = uint16_t(value >> 16);
428 uint16_t value2 = uint16_t(value & 0xFFFFU);
431 value = uint32_t(value1) | (uint32_t(value2) << 16);
451 uint32_t value1 = uint32_t(value >> 32);
452 uint32_t value2 = uint32_t(value & 0xFFFF'FFFFUL);
455 value = uint64_t(value1) | (uint64_t(value2) << 32);
475 template<
typename T>
void swap(T& a, T&b)
483 template<
typename T>
union ToUint8
485 static_assert(
sizeof(T) == 1,
"T must be a one-byte size type");
486 explicit ToUint8(T value) : value_(value) {}
491 template<
typename T>
union ToArray
493 explicit ToArray(
const T& value): value_{value} {}
495 uint8_t as_array_[
sizeof(T)];
508 return ToUint8<T>(input).as_uint8_;
517 template<
typename T,
typename U = u
int8_t[sizeof(T)]>
520 memcpy(output, ToArray<T>(input).as_array_,
sizeof(T));
533 return uint8_t(INST_PER_US / 3.0 * time_us);
542 constexpr uint8_t
num_bits(uint8_t mask, uint8_t num = 0)
547 return num_bits(mask >> 1, num + 1);
568 uint16_t temp = value;
574 int16_t temp = value;
580 uint32_t temp = value;
586 int32_t temp = value;
Iterable class that can embed arrays or initializer lists through implicit conversion.
Useful defines GCC specific attributes.
Contains all generic utility methods.
constexpr T min(T a, T b)
Compute the min of 2 integral values.
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.
T change_endianness(const T &value)
Change endianness of any integral type (from big to small or small to big).
constexpr T max(T a, T b)
Compute the max of 2 integral values.
constexpr T is_zero(T value, T default_value)
Replace value by default_value if not "true" (also known as "Elvis operator").
void swap(T &a, T &b)
Swap the values of 2 variables passed by reference.
void swap_bytes(uint16_t &value)
Swap 2 bytes of a 2-bytes integer.
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.
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].
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 ...
UnitPrefix
Common prefixes for measurement units.
constexpr uint8_t calculate_delay1_count(float time_us)
Calculate the count to pass to delay1() in order to reach time_us microseconds delay.
constexpr bool is_mask_equal(T actual, T mask, T expected)
Common utility to check if 2 values are equal according to a mask.
uint8_t binary_to_bcd(uint8_t binary)
Convert a natural integers to a BCD byte (2 digits).
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,...
constexpr uint8_t as_uint8_t(T input)
Cast a one byte long bit-fields struct into a byte.
constexpr uint8_t high_byte(uint16_t word)
Extract the high order byte of a 16-bits word.
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.
constexpr uint16_t as_uint16_t(uint8_t high, uint8_t low)
Convert 2 bytes into an unsigned int.
constexpr uint8_t num_bits(uint8_t mask, uint8_t num=0)
Calculate the number of 1 bits in a byte.
constexpr uint8_t low_byte(uint16_t word)
Extract the low order byte of a 16-bits word.
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.
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.