backend/omnia: fix brightness conversion to and from percent value
The global brightness on Omnia in sysfs (and in hardware) is represented by percents (range [0,100], let's call this the P value), but the rainbow.all.brightness UCI setting uses range [0,255] (let's call this the B value).
The get_brightness() and preapply() functions in Omnia's backend use the formulas: PtoB(B) = B * 100 // 255 BtoP(P) = P * 255 // 100
These formulas convert between ranges [0,100] and [0,255], and after the conversion they floor the result. This is wrong, because then the function BtoP is not an inverse of PtoB. For example BtoP(PtoB(1)) = 0, or BtoP(PtoB(99)) = 98.
This is bad for example in the following situation:
-
The button_sync.sh script periodically reads the P value from sysfs, converts it via PtoB and updates the rainbow.all.brightness UCI value. Let's say the user uses the front button to configure brightness to P=1 (out of 100). The button_sync.sh script uses the PtoB formula and sets rainbow.all.brightness = PtoB(1) = 2.
-
The user then uses the following command: rainbow wan green to change the color of the WAN LED to green. The preapply() function is called, which uses the BtoP formula and configures sysfs value of brightness to BtoP(rainbow.all.brightness) = BtoP(2) = 0. This disables all LEDs.
So by setting the WAN LED to green color, we have managed to disable all LEDs.
Fix this issue by changing the formulas to round the value after the range conversion, instead of flooring it. This is done by setting PtoB(B) = (B * 100 + 127) // 255 BtoP(P) = (P * 255 + 50) // 100