Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1361

Python • Re: PI5, Bookworm and GPIO control

$
0
0
Python is totally fine for this level of control, though you could also just use the command line tools in the gpiod package if that is all you are doing.
Thank you - I hadnt / didnt realise this could be done - certainly could be an easier option for me, however nothing wrong with me learning something along the way too..
Note that whether the value set by a process sticks after that process exits depends on the GPIO driver in your kernel. For old kernels it will reset to the default on exit, but for recent kernels, and I'm pretty sure that includes all Pi5 kernels, it will remain as set. This is a function of the GPIO driver and there is nothing gpiod or the tools can do about it.

If you want to go the Python route, the latest gpiod available via PyPI is version 2, while the version packaged by Pi OS, and used in the Tom's Hardware example, is still version 1.
gpiod version 2 has a totally different API, as it was impossible to add new features to the old API in a sane fashion so it got a major rework.

For gpiod version 2 there are examples of common use cases in the source tree - https://github.com/brgl/libgpiod/tree/m ... n/examples. If those aren't sufficient then feel free to suggest additions. And, as I've written in viewtopic.php?p=2207369&hilit=gpiod#p2207369, you can use pydoc to browse the module documentation.
Currently if i dont cleanup after the python code the relay remains on. Being a PI5 on bookworm I assume thats how its meant to behave and also the behaviour I need.

I did read that the latest GPIOD had broken the toms example - now I know why and appreciate the link to the examples and the docs (or creation of)

As i mentioned doing python at least I can try and learn something which having a 7 year old son - may come in quite handy soon.. for various projects we may attempt!
Yeah, value and bias are independent, and the function is called set_value(), not toggle_value() or do_magical_things_based_on the_line_state() . It is stateless and sets the line to the logical level you asked, which is typically the same as the physical level, or the reverse if the line is configured active low.
I still do not understand why software isnt made to my level - rather than do_magical_things_based_on the_line_state() I'd prefer - do_exactly_as_I_imagine_they_should_work() - would make my life so much easier :D

Thanks for clarifying it - it makes sense and after i had tested it - i kinda thought - yes thats logical. Unfortunately my brain and logic sometimes dont see eye to eye.
In Python, using the latest gpiod, and using active low, might look something like this:
Code: Select all

import gpiod
from gpiod.line import Direction, Value

chip = "/dev/gpiochip4"
line = 5
config={line: gpiod.LineSettings(direction=Direction.OUTPUT, active_low=True, output_value=Value.ACTIVE )}
req = gpiod.request_lines(chip, config=config)
The req is captured in case you want to subsequently change the value - else you can just exit.
Thankyou! Again my skill is based on my ability to google solutions - and given there is not much info out there that makes much sense to me - my skill set is not great.. this will help greatly as I assume that GPIOD v2 will become the default very shortly.
Can't help you with gpiozero - never used it, and not sure why you would in this case - gpiod is sufficient and more portable.
This was based on my reading that the latest GPIOD broke things again.. so my script wouldnt work and that GPIOzero seemed to work regardless and was documented fairly clearly and was included in the pi lite image of bookworm.

So I will now take the time to read and read and read what you have replied. Absorb and try and understand as much as I can.. and also read up on the links you provided. I appreciate the time you took for the response. It has helped me greatly!

Steve

Statistics: Posted by 5teve — Wed Apr 03, 2024 11:56 am



Viewing all articles
Browse latest Browse all 1361

Trending Articles