Why do I always get an exception raised in this __init__()?

Chris Green cl at isbd.net
Thu Aug 31 17:15:36 EDT 2023


I'm obviously doing something very silly here but at the moment I
can't see what.

Here's the code:-

    #!/usr/bin/python3 
    # 
    # 
    # GPIO  
    # 
    import gpiod
    # 
    # 
    # Simple wrapper class for gpiod to make set and clearing outputs
    easier 
    # 
    class Gpiopin:

        def __init__(self, pin):
            # 
            #  
            # scan through the GPIO chips to find the line/pin we want 
            # 
            for c in ['gpiochip0', 'gpiochip1', 'gpiochip2', 'gpiochip3']:
     
                chip = gpiod.Chip(c)                
                for l in range(32):
                    line = chip.get_line(l)                
                    if pin in line.name():
                        print("Found: ", line.name())
                        return
            else:
                raise ValueError("Can't find pin '" + pin + "'")
     
        def print_name(self): 
            print (self.line.name()) 
     
        def set(self): 
            self.line.set_value(1) 
     
        def clear(self): 
            self.line.set_value(0) 


This is by no means the final code, the print() in the __init__() is
just a diagnostic for example. However I really can't understand why I
see the following when I try it:-

    >>> import ngp
    >>> ngp.Gpiopin("P9_23")
    Found:  P9_23
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/chris/.cfg/hosts/bbb/bin/ngp.py", line 24, in __init__
        return
    ValueError: Can't find pin 'P9_23'
    >>> 

Does a return in __init__() not do what I think it does?

How else could/should I do this?





-- 
Chris Green
·


More information about the Python-list mailing list