scaling function between 0..1000 and 0..255.
Scale (expand and contract) with 8-bit values (0..25, etc.) and user-friendly 0..1000 (thousandths, ‰) values. Approximate computation by multiplication and bit shift instead of division (x*1000/255
) for low computational cost.
Scale 0..1000 to 0..127. Approximate calculation using (16646*x+65000)>>17
.
Scale 0..127 to 0..1000. Approximate calculation using (2064000UL*x+131072)>>18
.
Scales 0..1000 to 0..255. Approximate calculation using (33423*x+65000)>>17
.
Scales 0..255 to 0..1000. Approximate calculation using (1028000UL*uint32_t(x)+131072)>>18
.
Scales 0..1000 to 0..256. Approximate calculation using (33554*x+66000) >> 17
.
Note: x=999,1000 returns 255 as the range of uint8_t
even though the calculated value is 256.
Scale 0..256 to 0..1000. Approximate calculation using (1028000UL*uint32_t(x)+131072)>>18
.
Values to be set in hardware are often based on binary numbers such as 0...255, while numbers to be handled in user applications are easier to handle based on decimal numbers such as 0...1000. We defined an expression that does not perform division for these scale conversions.