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

    From Chris Green@21:1/5 to All on Thu Aug 31 22:15:36 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
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to All on Thu Aug 31 22:25:02 2023
    Chris Green <cl@isbd.net> wrote:

    [snip code and question]

    Sorry folks, it was a caching problem, I wasn't running the code I
    thought I was running! When I made sure I had cleared everything out
    and tried again it all worked as I expected.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Larry Martell@21:1/5 to python-list@python.org on Thu Aug 31 15:34:55 2023
    On Thu, Aug 31, 2023 at 3:19 PM Chris Green via Python-list <python-list@python.org> wrote:

    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?

    Change the return to a break

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alan Gauld@21:1/5 to Chris Green via Python-list on Fri Sep 1 08:57:22 2023
    On 31/08/2023 22:15, Chris Green via Python-list wrote:

    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 + "'")

    You don't store the line anywhere.
    You need to use self.line
    self.line = chip.get_line(l)
    if 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)

    As you do here.

    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to Alan Gauld on Fri Sep 1 10:04:37 2023
    Alan Gauld <learn2program@gmail.com> wrote:
    On 31/08/2023 22:15, Chris Green via Python-list wrote:

    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 + "'")

    You don't store the line anywhere.
    You need to use self.line
    self.line = chip.get_line(l)
    if 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)

    As you do here.

    Yes, OK, absolutely. However that wasn't my original rather basic
    problem which was, as I said, that I wasn't running the code I was
    looking at.

    The above was just a quick hack from some even cruder code doing the
    same job, trying to develop it into something better and more general.

    It's all on a headless Beaglebone Black (bit like a Raspberry Pi) so
    I'm doing everything via multiple ssh connections and sometimes this
    results in "the left hand not knowing what the right hand is doing"!

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Chris Green on Fri Sep 1 12:17:06 2023
    Chris Green <cl@isbd.net> writes:
    I'm obviously doing something very silly here but at the
    moment I can't see what.

    not adding debug print statements to see what exactly happens

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)