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?
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)
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.
I'm obviously doing something very silly here but at the
moment I can't see what.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 07:41:57 |
Calls: | 10,386 |
Calls today: | 1 |
Files: | 14,058 |
Messages: | 6,416,644 |