-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error trying PWMBacklight with ili9341 #126
Comments
What version of Luma.LCD are you using? the line numbers in the stack trace you supplied dont seem to line up with latest on master |
he mentioned 2.5.0 @rm-hull |
well ... the line numbers dont line up with whats in 2.5.0, so something is off somewhere.
@mattblovell are you saying the PWM backlight appears to work on Odroid, but not on RPi ? |
Yes. Both SBCs have the same luma.core and luma.led versions installed via pip (1.17.1, 2.5.0 respectively). My same On the Odroid C4, I can switch The code base is nominally the same, until one gets down to the RPi.GPIO module of course. So, perhaps something in the "real" RPi.GPIO is being more picky (than RPi.GPIO-Odroid)? I opened the issue just to see if it was something trivial. If anything, I was expecting execution on RPi generally to be more robust (since more people are using it). I was therefore surprised to see the error. If PWMBacklight is working for others using RPi boards, perhaps there is just something odd about my setup. I'm surprised by the line differences. On the RPi board in particular, I relied solely on pip3 for installation. I don't think I ever poked around in the installed lib files. |
I can try the RPi experiment again later this afternoon, with a fresh install of luma.* modules. |
I started with
I then tried setting
The backlight is using GPIO18, physical Pin 12. Repeating the attempt a second time, now the GPIO "channel is already in use" warning appears. |
Returning to this hiccup... If I comment out the try...except portion of
I'll keep playing. I've also seen hints googling around that RPi.GPIO may, despite its documentation to the contrary, support hardware PWM. One of the pin functions floating around seems to be That could be useful, as any jitter in PWM brightness for a backlight is readily visible. |
Adding
to that |
I observed similar flickering on the Odroid C4. (On the Odroid forums, one of the Hardkernel developers is augmenting RPi.GPIO-Odroid to make use of hardware PWM on their N2 and C4 SBCs. It's still a work in progress, but I'd like to think my gentle poking resulted in something.) RPi.GPIO uses pthreads for its PWM implementation, which unfortunately isn't particularly rock-solid. How difficult would it be, rather than treating the backlight signal as a pin handled through RPi.GPIO, to make use of the sysfs interface for hardware PWM control? The following is an extremely inelegant hack, but it produces a stable brightness. I hacked
I have existing functions that were serving as wrappers for turning the backlight on and off; those I modified to just write 1 or 0 to the above enable file. This approach requires that The sleep is unfortunately necessary to give time for the Perhaps there is a more elegant mechanism available. |
Having a mechanism to tell the I do need to google around and see if there is a nicer approach than the shell commands, but they seem to work. |
are both variations on a theme. They use @rm-hull, any thoughts on being able to (effectively) turn off the I think the root cause for the original error message is also addressed above (just need a GPIO output setup prior to trying to create EDIT: Updated license for pwpmy, per Robert's observation below. |
Makes sense, but might be worth having a chat with @dhrone and @kevinastone as the best way to proceed here (must admit I am not especially familiar with this part of the codebase), but I would have a slight preference for bringing in some form of /sys/class/pwm/pwmchip/pwm capability into Neither project has a release in pypi, so we'd have to manually include i guess, unless we can require directly from github. oh, and pwmpy looks to be 'New BSD', according to this: https://github.com/scottellis/pwmpy/blob/master/pwm.py#L7 |
So far, I've only seen two methods for taking advantage of hardware PWM on
Discussions of RPi.GPIO's software-based PWM flickering:
That gpiozero discussion is interesting. Seems like that library offers different backends for controlling the GPIO pins -- RPi.GPIO, RPIO, and PiGPIO. The documentation for the PiGPIO backend seems to imply one has to also have a daemon ( Perhaps the flicker would be better with some other choice of frequency? In my application, I've been using this when experimenting:
In general, if hardware-based PWM is available at relatively low-effort, seems like it would be nice to make use of it. FWIW, the Waveshare displays all seem to have fairly straightforward rework options that give one PWM control over the backlight via (everyone's favorite!) GPIO18. This is true for the 3.5" SPI-attached IPS panel and 4" HDMI panel. (They have a PDF available online with the rework instructions, but it doesn't have all of their displays. They were quiet speedy in replying when I inquired about the 2 displays I have on-hand.) |
I mentioned that the lack of hardware PWM in the GPiO library greatly impacts the usefulness of the PWM support in #95. Here's my workaround using from functools import cached_property
from luma.lcd.device import ili9341 as base_ili9341
from gpiozero import Device, PWMLED
class ili9341(base_ili9341):
@cached_property
def backlight_led(self) -> Device:
return PWMLED(self.backlight._pin, frequency=362) # type: ignore
def backlight(self, enabled: bool) -> None:
self.brightness(1.0 if enabled else 0.0)
def brightness(self, value: float) -> None:
self.backlight_led.value = value |
The display I'm most interested in using right at the moment is an HDMI panel. Since luma's I think one of the modules listed above will likely fit my needs. Do we want to go down the path of modifying luma.lcd to make use of something other than RPi.GPIO for the backlight pin? |
Sounds good to me. But probably means luma.core has to support it as well. |
Some questions toward that goal...
Oh, and separately, do we want to include the Thanks! |
Not sure I follow what you mean by "multiple PWM mechanisms". The |
Yes, that's actually what I meant @kevinastone. If multiple such interfaces / mechanisms are available, what's the best way to allow application code to select the mechanism to use? |
Are you still using this? I think it's def worth trying later 3.7.x, or of course 3.8 and 3.9, versions of python. |
Yes. I think its the default for python3 on the Buster release of Raspberry Pi OS. At least it's a 3.x. :) My original motivation for development was for an application running under CoreELEC on an Odroid C4. For lots of reasons, it's still on a 4.9.x Linux kernel; I believe I have Entware to thank for even getting python3 on that particular installation. |
@mattblovell Do applications need to specify their preferred mechanism? I would have expected there's a natural priority and fallback progression. (e.g. |
If the preference order is that clear, and the process of determining what's actually available is also reasonable quick and reliable (e..g., no odd error messages given back to the user, doesn't take a long time to "test" if Should it exit testing, the changes that are in-progress for RPi.GPIO-Odroid add a small wrinkle. On the Odroid N2 and C4 boards, that "RPi.GPIO" would itself be capable of hardware PWM. The choice between |
Type of Raspberry Pi
Raspberry Pi 3 Model B v1.2
Linux Kernel version
Linux raspberrypi 5.4.51-v7+ #1333 SMP Mon Aug 10 16:45:19 BST 2020 armv7l GNU/Linux
Other versions
luma.core 1.17.1
luma.lcd 2.5.0
python 3.7.3
Expected behaviour
I am attempting to enable use of PWM to control backlight brightness for my script (see mattblovell/kodi_panel on github).
The constructor for the ili9341 device is thus called in one of two ways:
I am getting my "leftover" 2.8-inch panel LCD panel working again on the RPi and thought I would give the PWM functionality a try (even though RPi.GPIO's PWM is still software-based).
The current form of
kodi_panel.py
, with USE_PWM set to True, works as-is on an Odroid C4. There is some flicker, though, given the pthreads control for the backlight. So, I was expecting somewhat the same behavior on the RPi. Instead an error message results.Actual behaviour
I should also note that physical Pin 12 (GPIO18) does work fine for backlight control without PWM on the RPi. Merely setting USE_PWM in my code, such that the ili9341() constructor is invoked with pwm_frequency set, is sufficient to get the above error.
Setting USE_PWM back to False, everything is happy again.
The text was updated successfully, but these errors were encountered: