AceCommon
1.1
Arduino library for low-level common functions and features with no external dependencies
|
#include <stdint.h>
Go to the source code of this file.
Functions | |
template<typename T > | |
void | ace_common::incrementMod (T &d, T m) |
Increment 'd' mod 'm', avoiding '' operator which is expensive for 8-bit processors. More... | |
template<typename T > | |
void | ace_common::incrementModOffset (T &d, T m, T offset) |
Increment 'd' mod 'm', with an offset, avoiding '' operator which is expensive for 8-bit processors. More... | |
uint8_t | ace_common::decToBcd (uint8_t val) |
Convert normal decimal numbers to binary coded decimal. | |
uint8_t | ace_common::bcdToDec (uint8_t val) |
Convert binary coded decimal to normal decimal numbers. | |
unsigned long | ace_common::udiv1000 (unsigned long n) |
Approximate division by 1000 without using integer division to avoid inefficient integer division operations on 8-bit processors. More... | |
Common functions involving arithmetics such as incrementing, addition, subtraction, division, and conversion to and from BCD (binary coded decimal).
Definition in file arithmetic.h.
void ace_common::incrementMod | ( | T & | d, |
T | m | ||
) |
Increment 'd' mod 'm', avoiding '' operator which is expensive for 8-bit processors.
For example, cycling through 0 to 9 can be done with incrementMod(count, 10)
.
Definition at line 46 of file arithmetic.h.
void ace_common::incrementModOffset | ( | T & | d, |
T | m, | ||
T | offset | ||
) |
Increment 'd' mod 'm', with an offset, avoiding '' operator which is expensive for 8-bit processors.
For example, incrementing the months of the year from 1 to 12 can be done with incrementMod(month, 12, 1)
.
Definition at line 57 of file arithmetic.h.
|
inline |
Approximate division by 1000 without using integer division to avoid inefficient integer division operations on 8-bit processors.
This can be useful for converting millis to seconds, or micros to millis, for example.
The algorithm is based on the binomial expansion of 1/(1-x):
The truncation of this series means that this function returns approximations which are always slightly smaller than the fully accurate answer. See the unit tests for concrete examples. This approximation was good enough for me in the situations where exact accuracy was not required.
More accurate algorithms exist (see for example http://www.hackersdelight.org/divcMore.pdf).
Definition at line 96 of file arithmetic.h.