As it was stated in The Affine Space chapter,
every measurement can (and probably should) be modelled as a quantity_point. This is
a perfect example of such a use case.
This example implements a simplified scenario of measuring voltage read from hardware through
a mapped 16-bits register. The actual voltage range of [-10 V, 10 V] is mapped to [-32767, 32767]
on hardware. Translation of the value requires not only scaling of the value but also applying
of an offset.
// hardware encoding of voltageusingvoltage_hw_t=std::uint16_t;inlineconstexprvoltage_hw_tvoltage_hw_error=std::numeric_limits<voltage_hw_t>::max();inlineconstexprvoltage_hw_tvoltage_hw_min=0;inlineconstexprvoltage_hw_tvoltage_hw_max=voltage_hw_error-1;inlineconstexprvoltage_hw_tvoltage_hw_range=voltage_hw_max-voltage_hw_min;inlineconstexprvoltage_hw_tvoltage_hw_zero=voltage_hw_range/2;
Finally, we define a quantity point origin, an offset unit that scales the value and uses this
origin to offset the zero of the sale, and a dedicated quantity point alias using those:
Now, when everything is ready, we can simulate mapping of our hardware register, and provide
a helper function that will read the value and construct a quantity point from the obtained copy:
In the main function we simulate setting of 3 values by our hardware. Each of them is read
and printed in the voltage unit used on the hardware as well as in the standard SI unit:
intmain(){// simulate reading of 3 values from the hardwarehw_voltage_value=voltage_hw_min;quantity_pointqp1=read_hw_voltage().value();hw_voltage_value=voltage_hw_zero;quantity_pointqp2=read_hw_voltage().value();hw_voltage_value=voltage_hw_max;quantity_pointqp3=read_hw_voltage().value();print(qp1);print(qp2);print(qp3);}
The above program results with the following text output: