python3 如何通过传递参数更改属性 .P0 以调用
python3 how to change the atribute .P0 with passing argument to call
我正在尝试在创建通道时使用将引脚选择传递给覆盆子 py,并且在调用该方法时只想更改 .P(value)。因为如果我在另一个 class 中调用 class,我目前必须按照现在的方式再次导入所有库。下面是代码。
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
从 adafruit_mcp3xxx.analog_in 导入 AnalogIn
def createChannel(self, channelNumber):
# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D22)
# create the mcp object
mcp = MCP.MCP3008(spi, cs)
self.channelNumber = channelNumber
chan = self.channelNumber
chan = AnalogIn(mcp, self.channelNumber)
rawValue = chan.voltage
return rawValue
那我就这样称呼它
sensor = createChannel()
rawValue = sensor.createChannel(MCP.P0)
因此,当我创建另一个 class 以使用传感器检索的数据并调用该函数时,我需要再次导入与 MCP 一起使用的所有库。我想这样称呼它
sensor = createChannel()
rawValue = sensor.createChannel(P0)
但我找不到通过在有效的调用中传递参数来更改最后一部分 'MCP.P0') 的方法。
所以当我创建另一个 class 我必须这样做并再次导入所有库
def sensorOne(self):
# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D22)
# create the mcp object
mcp = MCP.MCP3008(spi, cs)
#get date and time
outTime = str(datetime.now())
# instance of createSpi class. so if tds sensor is connected to pin 0 you do as below. I will also do ph on pin 2 but comment it out for I am not sure if anyting is connected there yet.
sensor = createChannel()
#get data from sensor on pin 1
outData = sensor.createChannel(MCP.P1)
return outTime, outData
如果间距不是百分百请原谅我看不见,但代码工作只需要尝试并能够通过将参数传递给调用来将 .P0 更改为例如 P1 .
谢谢
我认为您可以在定义 createChannel
的模块中定义常量 P0
到 P7
,然后其他文件可以从该模块导入这些常量,而不是从 MCP 获取它们直接地。另外,您可以只指定一个 0 到 7 之间的整数的频道。
我在网上找到了一些 documentation for adafruit_mcp3xxx.mcp3008。我认为这意味着像 MCP.P0 和 MCP.P1 这样的通道名称实际上只是分别像 0 和 1 这样的整数值。
The ADC chips’ input pins (AKA “channels”) are aliased in this library
as integer variables whose names start with “P” (eg MCP3008.P0 is
channel 0 on the MCP3008 chip). Each module that contains a driver
class for a particular ADC chip has these aliases predefined
accordingly. This is done for code readability and prevention of
erroneous SPI commands.
您可以通过在定义 createChannel
的模块中定义常量 P0
、P1
等,使频道名称可供 createChannel
方法的用户使用。
## File createChannel.py
import adafruit_mcp3xxx.mcp3008 as MCP
# Alias the channel constants for convenience.
P0 = MCP.P0
P1 = MCP.P1
# etc.
P7 = MCP.P7
class createChannel():
def createChannel(self, channelNumber):
# ... Do stuff with channelNumber.
return channelNumber # Or whatever you need to return.
在另一个想要使用的文件中 createChannel
您可以导入通道常量和方法。或者,我认为您可以通过指定 0 到 7 之间的整数来访问频道。
## Another file.
from createChannel import createChannel, P0, P1, P7
sensor = createChannel()
# Access a single pin.
rawValue = sensor.createChannel(P0)
# Access each pin.
rawBus = [sensor.createChannel(pin) for pin in range(8)]
我正在尝试在创建通道时使用将引脚选择传递给覆盆子 py,并且在调用该方法时只想更改 .P(value)。因为如果我在另一个 class 中调用 class,我目前必须按照现在的方式再次导入所有库。下面是代码。
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
从 adafruit_mcp3xxx.analog_in 导入 AnalogIn
def createChannel(self, channelNumber):
# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D22)
# create the mcp object
mcp = MCP.MCP3008(spi, cs)
self.channelNumber = channelNumber
chan = self.channelNumber
chan = AnalogIn(mcp, self.channelNumber)
rawValue = chan.voltage
return rawValue
那我就这样称呼它
sensor = createChannel()
rawValue = sensor.createChannel(MCP.P0)
因此,当我创建另一个 class 以使用传感器检索的数据并调用该函数时,我需要再次导入与 MCP 一起使用的所有库。我想这样称呼它
sensor = createChannel()
rawValue = sensor.createChannel(P0)
但我找不到通过在有效的调用中传递参数来更改最后一部分 'MCP.P0') 的方法。
所以当我创建另一个 class 我必须这样做并再次导入所有库
def sensorOne(self):
# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D22)
# create the mcp object
mcp = MCP.MCP3008(spi, cs)
#get date and time
outTime = str(datetime.now())
# instance of createSpi class. so if tds sensor is connected to pin 0 you do as below. I will also do ph on pin 2 but comment it out for I am not sure if anyting is connected there yet.
sensor = createChannel()
#get data from sensor on pin 1
outData = sensor.createChannel(MCP.P1)
return outTime, outData
如果间距不是百分百请原谅我看不见,但代码工作只需要尝试并能够通过将参数传递给调用来将 .P0 更改为例如 P1 .
谢谢
我认为您可以在定义 createChannel
的模块中定义常量 P0
到 P7
,然后其他文件可以从该模块导入这些常量,而不是从 MCP 获取它们直接地。另外,您可以只指定一个 0 到 7 之间的整数的频道。
我在网上找到了一些 documentation for adafruit_mcp3xxx.mcp3008。我认为这意味着像 MCP.P0 和 MCP.P1 这样的通道名称实际上只是分别像 0 和 1 这样的整数值。
The ADC chips’ input pins (AKA “channels”) are aliased in this library as integer variables whose names start with “P” (eg MCP3008.P0 is channel 0 on the MCP3008 chip). Each module that contains a driver class for a particular ADC chip has these aliases predefined accordingly. This is done for code readability and prevention of erroneous SPI commands.
您可以通过在定义 createChannel
的模块中定义常量 P0
、P1
等,使频道名称可供 createChannel
方法的用户使用。
## File createChannel.py
import adafruit_mcp3xxx.mcp3008 as MCP
# Alias the channel constants for convenience.
P0 = MCP.P0
P1 = MCP.P1
# etc.
P7 = MCP.P7
class createChannel():
def createChannel(self, channelNumber):
# ... Do stuff with channelNumber.
return channelNumber # Or whatever you need to return.
在另一个想要使用的文件中 createChannel
您可以导入通道常量和方法。或者,我认为您可以通过指定 0 到 7 之间的整数来访问频道。
## Another file.
from createChannel import createChannel, P0, P1, P7
sensor = createChannel()
# Access a single pin.
rawValue = sensor.createChannel(P0)
# Access each pin.
rawBus = [sensor.createChannel(pin) for pin in range(8)]