x*1000/255
) is replaced with multiplication and bit shifts for approximate calculation.
static inline uint8_t scale_1000_to_127u8(uint16_t x)
static inline uint16_t scale_127u8_to_1000(uint8_t x)
static inline uint8_t scale_1000_to_255u8(uint16_t x)
static inline uint16_t scale_255u8_to_1000(uint8_t x)
static inline uint8_t scale_1000_to_256u8(uint16_t x)
static inline uint16_t scale_256u16_to_1000(uint16_t x)
scale_1000_to_127u8()
Scales 0..1000 to 0..127. Uses (16646*x+65000)>>17
for approximate calculation.
scale_127u8_to_1000()
Scales 0..127 to 0..1000. Uses (2064000UL*x+131072)>>18
for approximate calculation.
scale_1000_to_255u8()
Scales 0..1000 to 0..255. Uses (33423*x+65000)>>17
for approximate calculation.
scale_255u8_to_1000()
Scales 0..255 to 0..1000. Uses (1028000UL*uint32_t(x)+131072)>>18
for approximate calculation.
scale_1000_to_256u8()
Scales 0..1000 to 0..256. Uses (33554*x+66000) >> 17
for approximate calculation.
Note: For x=999,1000 the calculated value becomes 256, but returns 255 as the range of uint8_t
.
scale_256u16_to_1000()
Scales 0..256 to 0..1000. Uses (1028000UL*uint32_t(x)+131072)>>18
for approximate calculation.
Background
Values to be set in hardware are often based on binary such as 0..255, while numbers handled in user applications are easier to manage when based on decimal such as 0..1000. These scale conversions define formulas that do not use division.