我正在努力了解如何使用变量来指定ADS1115上的引脚号。通常情况下,人们会通过指定类似的内容来读取模拟输入
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
chan0 = AnalogIn(ADS.ADS1115(i2c, address = 0x48), ADS.P0)
我想做的是使用一个变量作为;P0";
我可以把它做成这样的物体
p_object = ADS.P0
chan0 = AnalogIn(ADS.ADS1115(i2c, address = 0x48), p_object)
但是当我初始化我的类时,这并没有帮助。
这是我正在使用的完整代码。我想把P0换成"pin_number",我知道它可能不是一个数字,但它是一个字符串吗?
class Voltage_sensor:
'''
Given an ADS1115 address and a corresponding pin number
will read voltage values from an AC715
'''
def __init__(self, address, pin_number) -> None:
self.address = int(address)
self.pin_number = pin_number
try:
self.chan = AnalogIn(ADS.ADS1115(i2c, address = self.address), ADS.P0) #<- wanting to replace this with the pin number.
self.chan = AnalogIn(ADS.ADS1115(i2c, address = self.address), ADS.self.pin_number) # something like this, but this doesn't work
print(f"Adding ADS1115 at address {hex(self.address)}")
except:
print(f"Voltage Sensor not found at {hex(self.address)}")
我最终创建了一个引脚的字典
self.pins = {'0' : ADS.P0, '1' : ADS.P1, '2' : ADS.P2, '3' : ADS.P3}
然后我只是用一个整数来调用相应的pin。