I'm looking for a way to control the screen brightness from code. I saw this post but this just turns the screen on and off. How can I dim the screen before it goes to sleep instead of just turning it off and wake up the screen in code (later using I/O) by brightening it gradually?
Possible answer to your question can probably be found in the answer to this Adjusting the Brightness of the Official Touchscreen Display post. I clearly says a display brightness can be adjusted by sending a value to /sys/class/backlight/rpi_backlight/brightness as shown below.
echo n > /sys/class/backlight/rpi_backlight/brightness