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

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

$
0
0
After all, it seems that this RPi.GPIO tragedy was in fact a good thing, because I found someting better. With gpiod I have more control over the GPIO and I can make my own threads for monitoring. I implemented it in my project, I tested it and it works perfectly ! Thank you again ! :D

I made a thread class for monitoring:

Code:

class GpioMonitor(threading.Thread):  def __init__(self, label, config, callbacks):    super().__init__(name='GPIO Monitor')    self.daemon = False    self.Label = label    self.LConfig = config    self.LCallbacks = callbacks    self.done_fd = os.eventfd(0)    self.start()  def Terminate(self):    if self.is_alive():      os.eventfd_write(self.done_fd, 1)      self.join()  def run(self):    with gpiod.request_lines(RPiChip, consumer=self.Label, config=self.LConfig) as request:      poll = select.poll()      poll.register(request.fd, select.POLLIN)      poll.register(self.done_fd, select.POLLIN)      while True:        for fd, event in poll.poll():          if fd == self.done_fd: return          for event in request.read_edge_events():            if event.line_offset in self.LCallbacks:              try: self.LCallbacks[event.line_offset](event)              except: pass
And I use it like this:

Code:

  GpioConfig = {    SDReqPin:  gpiod.LineSettings(direction=Direction.INPUT, bias=Bias.PULL_UP,   edge_detection=Edge.RISING, debounce_period=timedelta(milliseconds=10)),    RpmPin:    gpiod.LineSettings(direction=Direction.INPUT, bias=Bias.PULL_UP,   edge_detection=Edge.FALLING),    UAlertPin: gpiod.LineSettings(direction=Direction.INPUT, bias=Bias.PULL_DOWN, edge_detection=Edge.RISING) }  GpioCallbacks = {    SDReqPin:  SDRequest,    RpmPin:    CountImpulses,    UAlertPin: UPSAlert }  GpioMon = GpioMonitor('NAS-GpioMonitor', GpioConfig, GpioCallbacks)

Statistics: Posted by Marus780 — Mon Sep 16, 2024 6:54 pm



Viewing all articles
Browse latest Browse all 5827

Trending Articles