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

Chris Green cl at isbd.net
Fri Sep 1 05:04:37 EDT 2023


Alan Gauld <learn2program at 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
·


More information about the Python-list mailing list