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

Python • Re: What is the official Raspberry Pi library for accessing GPIO ports ?

$
0
0
The AI made me this code for my gpiod v1 and it works. And it does not launch any threads ! :) Of course, I need to wrap the code in a thread for every pin I monitor for edge changes, but at least those threads will be dormat and not consumes 4% of the CPU in idle like "the official raspberry pi gpiozero library made for kids" does, because this block the thread execution while waiting for edge to change. Now I just need to figure out how to install v2 as system wide... and I will be happy.

Code:

import gpiodSDReqPin = 26def SDRequest(event):    print('SDRequest triggered!')# Open the GPIO chipchip = gpiod.Chip('/dev/gpiochip0')# Get the GPIO line for SDReqPin and configure it for edge detection with pull-up resistorline = chip.get_line(SDReqPin)line.request(consumer='sd_req', type=gpiod.LINE_REQ_EV_RISING_EDGE, flags=gpiod.LINE_REQ_FLAG_BIAS_PULL_UP)try:    print("Waiting for SDReqPin to change state...")    while True:        # Block execution until an event occurs on the line        line.event_wait()  # This will block the program until an event is detected        event = line.event_read()        SDRequest(event)except KeyboardInterrupt:    passfinally:    chip.close()    print("Exiting cleanly!")
warthog618, thanks for the hint with "With gpiod v2 you can monitor multiple lines in the one request, so you only need the one monitoring thread." from the other topic I posted in by mistake... I will investigate. But for now AI is helping me to install v2 system wide from source... :lol: Lets see if I will succeed it...

Statistics: Posted by Marus780 — Sun Sep 15, 2024 11:44 am



Viewing all articles
Browse latest Browse all 1241

Trending Articles