SteveA
Posts: 38
Joined: Sat Mar 14, 2015 11:18 am
Location: South Yorkshire, England

Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Mon Nov 25, 2019 11:19 am

I need to alter the screen brightness when a particular scientific project is run in a dark room/lab. The room is initially bright then turned dark when using such as UV, etc, and so the screen needs to be dimmed to match, so as still to be able to read the text/graphics. If the screen is not dimmed it prevents defects/problems being seen. There is no access to the display monitor brilliance controls as everything has to be sealed.

On the RPI3 I use openvg for all of the graphics/UI in the project. The library I use is the excellent version by Paeryn, which is based on AJ Starks openvg lib, both of which use the OpenVG lib by the Kronos group as pre-installed in all RPi OS up to the RPI4 Buster version.

To dim the screen using openvg, I create a new window layer (I call it a dim layer) above the main drawing layer/s (as openvg allows multiply drawing layers) and then alter the opacitiy of that dim layer to adjust the screen display brightness from 0 - 100% as required. It works perfectly well and finely adjusts the screen brightness without any corruption of gamma/colour.

Unfortunately, the new RPI4 does not include OpenVG/openvg and it cannot be installed due to a change of the firmware. It has been removed by the RPI software team and I have read that it is not going to be added back due to OpenVG Kronos group not updating OpenVG for many years - maybe it does not need updating as it works fine and spot on, hence that appears to be a weak reason to not include it due to it working fine!. All of the excellent OpenVG graphics handling, multi window layers, opacity control, text scaling, image handling, rotating, etc, has now been lost.

To use the RPI4 (which I need for more ram and an increase in speed because of high speed ADC data logging) I will have to completely rewrite everything using x11 windows graphics and not openvg graphics. The first obstacle I have come across is how to programmatically adjust the screen brightness as this is a prime requirement in the environment it will be used.

I have looked through the web and see that you can alter the backlight/brightness when using the small add-on RPI (touch) LCD screen (by altering the backlight, xrandr, etc), but none of the solutions seen can adjust the standard HDMI/DVI monitor displays (2 of them will be used), which I have to use due to the need a for large screen area.

Has anyone managed to programmatically (in C) alter the screen brightness in x11 graphics - perhaps by a driver which scales the pixel values as they are drawn?

I do wish Raspberry Pi foundation would reinstate OpenVG back into the latest OS versions of RPI 4.

Thanks for looking
Steve

Gfunkbus76
Posts: 9
Joined: Thu Mar 12, 2020 3:17 pm

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Fri Jan 01, 2021 4:55 pm

Any luck with this? ... i found pishade on github which seems to work on one of my displays.

W. H. Heydt
Posts: 14432
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Fri Jan 01, 2021 6:35 pm

I can't point to a general solution, but a Pi can control the backlight of an RPF 7" touchscreen through software commands. So if you can live with a fairly small screen, there's a solution. One interesting point is changing the backlight of the screen does not affect the brightness of how it is displayed over a VNC session, so you could do a remote monitor at full brightness of a darkened RPF display.

Here is some of what I have in my root crontab to do this:

Code: Select all

# m h  dom mon dow   command
00 22 * * * killall mplayer

# Evening Dim Display
00 22 * * * echo 50 > /sys/class/backlight/rpi_backlight/brightness
30 22 * * * echo 14 > /sys/class/backlight/rpi_backlight/brightness

# Normal weekday
# 35 06 * * 1-5 echo 50 > /sys/class/backlight/rpi_backlight/brightness
# 45 06 * * 1-5 echo 200 > /sys/class/backlight/rpi_backlight/brightness

# No school weekday
15 07 * * 1-5 echo 50 > /sys/class/backlight/rpi_backlight/brightness
20 07 * * 1-5 echo 200 > /sys/class/backlight/rpi_backlight/brightness

# Normal Saturday
50 07 * * 6 echo 50 > /sys/class/backlight/rpi_backlight/brightness
00 08 * * 6 echo 200 > /sys/class/backlight/rpi_backlight/brightness

# Normal Sunday
50 06 * * 7 echo 200 > /sys/class/backlight/rpi_backlight/brightness
(Local schools are on winter break at the moment, so the "Normal weekday" is commented out in favor of the "No school weekday. My wife and I look after our grandson during the week.)

SteveA
Posts: 38
Joined: Sat Mar 14, 2015 11:18 am
Location: South Yorkshire, England

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Sat Jan 02, 2021 12:29 pm

Thank you W H Heydt for your response which works for only attached LCD screens (which use the small port LCD screen port) and it does not work for the HDMI/DVI plug in monitors (of which I use 2.)

May I please refer you to my original question which specifically says 'Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens'.

Many thanks for posting it though,
Steve

SteveA
Posts: 38
Joined: Sat Mar 14, 2015 11:18 am
Location: South Yorkshire, England

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Sat Jan 02, 2021 1:35 pm

@Gfunkbus76 - I have had some success. I had looked at the pishad and because of the bad experience of having to spent 6 months rewriting a system when OpenVG was removed, I took the view of keeping it solely x11 as per my original question.

In the lower levels of placing the pixels on the screen (ie in the screen ram buffer) the value of the pixel is modified with the gamma values to match the monitor profile in use. If the gamma values could be reduced it would dim the apparent brightness. I could not find any information anywhere on where this gamma section is and so gave up.

I then altered my x11 routines to call a function to get the x11 colour number (used for lines/fill, etc - but not font/xRender colours). Enter with the required colour, eg YELLOW, and the brightness, eg 50 and the returned x11 colour number will be scaled down and will dim subsequent drawing.

Code: Select all

#define XMAXBRILLIANCE 100 // 0 off, 100 = normal brilliance

// sample rgb colours
#define YELLOW             255, 255, 0
#define SIGNYELLOW         255, 209, 22

/**************************************************************************************************
*
* Function: xRGBtox11Colour (used for std x11 line, fill colour and not fonts/xRender colours)
* Inputs:   int32_t R, int32_t G, int32_t B (use graphicsrgb.h defs)
* Returns:  uint32_t 32-bit colour
* Overview: Enter RGB graphicsrgb.h triplet colour def, scale RBG using global xBrilliance var
*           (0..XMAXBRILLIANCE) and return u32-bit colour number, msb..lsb = 0RGB
*
*           Used in x11 lines, fill, fore/background colours but not xFont or xRender colours
*
**************************************************************************************************/
uint32_t xRGBtox11Colour (int32_t r, int32_t g, int32_t b)
{
  uint32_t rbgcolour;

  if (xBrilliance>XMAXBRILLIANCE) xBrilliance = XMAXBRILLIANCE;      // Clamp
  if (xBrilliance<0) xBrilliance = 0;

  rbgcolour = (uint32_t)((b * xBrilliance)/XMAXBRILLIANCE);          // lsb
  rbgcolour += (uint32_t)((g * xBrilliance)/XMAXBRILLIANCE) * 256;
  rbgcolour += (uint32_t)((r * xBrilliance)/XMAXBRILLIANCE) * 65536; // msb
  return rbgcolour;
} // xRGBtox11Colour
I hope this helps

Steve

W. H. Heydt
Posts: 14432
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Sat Jan 02, 2021 6:04 pm

SteveA wrote:
Sat Jan 02, 2021 12:29 pm
Thank you W H Heydt for your response which works for only attached LCD screens (which use the small port LCD screen port) and it does not work for the HDMI/DVI plug in monitors (of which I use 2.)

May I please refer you to my original question which specifically says 'Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens'.

Many thanks for posting it though,
Steve
I made the suggestion to use the DSI attached display because, so far as I know, a Pi can't control the backlight of a display attached through the HDMI port. So...if you really have to have the Pi control the backlight, I would suggest using a display where that is possible, rather than beating your head against an intractable problem.

SteveA
Posts: 38
Joined: Sat Mar 14, 2015 11:18 am
Location: South Yorkshire, England

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Sun Jan 03, 2021 3:33 pm

rather than beating your head against an intractable problem.
Sorry to have to reply again to correct your misunderstanding. I am not trying to alter the back light of an attached HDMI monitor (which can be DVI or even a TV screen, all of which could be LED, OLED, Plasma types, etc). To repeat, I needed to reduce the brilliance of the the large sealed displays (in an industrial lab hostile enviroment) and you can in fact do it by altering the gamma curves in the kernel - if you know how to access them from userland. Because I could not access them I posed the specifically worded question. The original monitor's backlight (if it is a backlight LCD) remains untouched. The original monitor's plasma ratio settings (if it is a Plasma display) remains untouched. The original monitors LED or OLED drives (if it is a LED or organic LED display which has no backlight) remains untouched. Respectfully, had I wanted to reduce a backlight on a totally different type of small display from the question I posed, I would have simply asked. The method I sought was to reduce the brilliance on the attached HDMI port and to do this I needed to reduce the intensity of each pixel to be drawn.

Subsequently I found by drawing the pixels (for lines, squares, fonts, etc) with a reduced RGB and alpha value (eg where initlal full brilliance for a colour is 200, 100 160, multiply each by 0.5 to give 100, 50, 80 to draw again and it will naturally be dimmer.) Then the next problem is to dim loaded in picture jpgs, png, icon files. These can be dimmed by first drawing them into a pixmap, placing the pixmap on the screen then overdrawing with an alpha shaded rectangle the size of the whole screen using XRenderFillRectangle(), then calling XFlush() - you could actually do this for lines and shapes if you wished. All of this is using STANDARD HDMI connected monitors (LCD, PLASMA, LED, OLED), programmatically using C (not bash scripts) in x11 and does not use any other imported graphics libs (to ensure that the RPI5/6... does not remove them as the RPI4 removed OpenVG without warning.

It is not an intractable problem. And I do thank you for your time.

mattdha
Posts: 1
Joined: Fri Feb 19, 2021 7:17 am

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Fri Feb 19, 2021 7:35 am

Hi
on the 7" Pi screen I found a command that switched the backlight off. At first I thought my Pi 4 had died, but with a strong torch on the screen, I could still see it was on. The same command switched it back on.
My only solution so far is adding colour correction gels. The red cuts most of the blue light out, adding another would make it even darker. It doesn't seem to affect the touch screen sensitivity. (see image)
Attachments
20210219_080430.jpg
20210219_080430.jpg (233.36 KiB) Viewed 275 times
Last edited by mattdha on Fri Feb 19, 2021 7:39 am, edited 1 time in total.

aBUGSworstnightmare
Posts: 2570
Joined: Tue Jun 30, 2015 1:35 pm

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Sat Feb 20, 2021 6:31 am

I would have a look at xrandr help to see if there is --gamma and --brightness option shown
Then look how ro implement them for your use case

SteveA
Posts: 38
Joined: Sat Mar 14, 2015 11:18 am
Location: South Yorkshire, England

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Sat Feb 20, 2021 4:56 pm

Thanks, I had looked and tried xrandr and it appears to only work for attached LCD screens (panels) and not for HDMI plugged in monitors (single or dual). If anyone sees an update to be able to use xrandr on HDMI attached monitors then please share for future use.

I have used the technique I described by drawing the pixels/alpha at reduced/scaled values and this nicely adjusts the overall brilliance very well. I use a small encoder to allow the user in the scientific booth to turn up/down the brilliance scaling factor. As I explained users cannot get to the actual large displays to adjust them directly as the equipment is sealed and so I must do it programmatically.


SteveA
Posts: 38
Joined: Sat Mar 14, 2015 11:18 am
Location: South Yorkshire, England

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Sat Feb 20, 2021 7:22 pm

The monitors are plugged in via the 2 mini HDMI connectors on the RPI

aBUGSworstnightmare
Posts: 2570
Joined: Tue Jun 30, 2015 1:35 pm

Re: Adjust Screen Brightness programmatically x11 for HDMI and not LCD screens

Sun Feb 21, 2021 6:15 am

SteveA wrote:
Sat Feb 20, 2021 4:56 pm
Thanks, I had looked and tried xrandr and it appears to only work for attached LCD screens (panels) and not for HDMI plugged in monitors (single or dual). If anyone sees an update to be able to use xrandr on HDMI attached monitors then please share for future use.
and
SteveA wrote: The monitors are plugged in via the 2 mini HDMI connectors on the RPI
sorry, but I still don't understand your feedback! What is the difference between 'attached LCD screens' and 'monitors plugged in via HDMI'?
Is your 'monitor' a CRT monitor? Is that the reason why you say 'works on LCD screen only'?

SteveA
Posts: 38
Joined: Sat Mar 14, 2015 11:18 am
Location: South Yorkshire, England

Re: Adjust Screen Brightness programmatically x11 for HDMI monitors and not the small LCD panels

Sun Feb 21, 2021 5:10 pm

Please see the The Raspberry Pi notes/handbook/specs which can be downloaded.

They explain the difference between

a) HDMI/DVI monitors (such as used on standard PCs and can typically be 17" to 60" in size - these are the ones I use and can use a 1 or 2m video cable to drive them) and

b) LCD panels, which are connected to a RPI via the small ribbon cable port (and are usually small in size, typically 7" to 12") and usually have to be positioned very near to the RPI4 due to the limitations of the ribbon cable. Incidentally some of these LCD panels can be referred to as screens and some are touch panels.

The rest is in the above posts in which I also explain how I fixed it to be able to adjust the brilliance programmatically (in C/C++) for the attached HDMI monitors and not LCD (ribbon attached) small panels, which was the question I originally asked.

Return to “Beginners”