pimoroni / st7789-python

Python library to control an ST7789 240x240 1.3" TFT LCD display.
MIT License
193 stars 51 forks source link

Dimming / backlight control on/off #8

Open kokosowy opened 3 years ago

kokosowy commented 3 years ago

Hey!

I'm using pirate audio and it's fun! I have a question, is it possible to allow dimming on this display or turning of backlight completely?

Thanks for letting know! K

Sebwap commented 2 years ago

No return on this since nearly one year ? I'm asking myself the same question for the new display hat mini which is driven by the same library. For me, you can shutdown backlight by pulling down the GPIO used for backlight.

Howerver, I don't know if it is possible to do some dimming.

tomtastic commented 2 years ago

There's a method to set the backlight on/off :

https://github.com/pimoroni/st7789-python/blob/5c16baecb8844b1807da221b40d03dec500b3a9e/library/ST7789/__init__.py#L172

Though I've yet to try it, and there appears no current way to adjust the level or dimming of the backlight, but with some code changes, I think it should be possible to set the backlight GPIO pin to PWM mode (https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/) and change the duty cycle accordingly to vary the brightness.

Caveat: I've no idea what impact this would have as far as the current being passed and may fry things?!

tomtastic commented 2 years ago

From a quick look at the ST7789 datasheet, it seems there might even be functions to vary brightness that just aren't implemented in the current driver here : https://www.rhydolabz.com/documents/33/ST7789.pdf

0x51 = Write display brightness 0x52 = Read display brightness value

image

Gadgetoid commented 2 years ago

For all of our display boards, the backlight pin can be PWM'd for brightness control. Any "brightness" registers in the display driver itself will probably do something but since they can't vary the intensity of the backlight they wont have the intended effect.

PWM from Python (RPi.GPIO) is... dicey... I haven't tried it for a while, but it wasn't traditionally stable enough for a display backlight. You might see some flicker, which isn't great on a display. Might be worth looking into again to see if the results are acceptable but since Linux is moving toward Libgpio (slow-ish) and the Pi is moving away from traditional register bitbanging (fast) it's probably going to break even more in future anyway.

Any other method of PWM is unfortunately outside the scope of this library.

The most correct approach is to bring up the pwm-backlight kernel module with the appropriate config. Though since the Pi's PWM slices are often used for analogue audio there are tradeoffs even here. Some instructions on how to do this- pending figuring out how to do it again- might not go amiss in this repository.

TLDR: It's complicated.

tomtastic commented 2 years ago

I had a look at RPi.GPIO for the PWM details, it's still software based, but an overlay for the device tree to allow hardware PWM (via sysfs - https://jumpnowtek.com/rpi/Using-the-Raspberry-Pi-Hardware-PWM-timers.html) looks straightforward enough and would at least avoid burning user cycles and would hopefully equate to less flicker during garbage collection, etc.

ThecaTTony commented 2 years ago

This is a good example of dimming:

https://github.com/pimoroni/pirate-audio/blob/master/examples/backlight-pwm.py

Wikinaut commented 1 year ago

I use the PWM method but noticed annoying flickering - where does it stem from? How can this be overcome?

Wikinaut commented 1 year ago

Using pigpioworks perfectly.

I will publish test script using this method later here.

bjohas commented 1 year ago

@Wikinaut - would love to see the example!

Wikinaut commented 1 year ago

@bjohas Hi, here is the updated code using pigpio. Make sure to run pigpiod with -t 0 option, otherwise you cannot play audio.

Testscript PWM without Audio:

Testscript PWM plus Audio:

bjohas commented 1 year ago

Thank you so much!