【问题标题】:How can I call these variables in main?如何在 main 中调用这些变量?
【发布时间】:2016-02-21 21:06:50
【问题描述】:

我有这段代码运行并将GPIOS 7,11,13,15设置为我的Raspberry Pi 2HIGHLOW,因此我可以相应地寻址16个多路复用器通道,然后通过MCP3002 SPI读取模拟电压,然后返回如果有一个按键或一个键被释放,加上那个键。代码如下:

def findpress(keypressed, keyreleased, key):
    x=0
    while True:
        binary_x="{0:04b}".format(x)
        GPIO.output(7, binary_x[0])
        GPIO.output(11, binary_x[1])
        GPIO.output(13, binary_x[2])
        GPIO.output(15, Binary_x[3])
        channeldata_1 = read_mcp3002(0) # get CH0 input
        channeldata_2 = read_mcp3002(0) # get CH0 input
        channeldata_3 = read_mcp3002(0) # get CH0 input
        channeldata = (channeldata_1+channeldata_2+channeldata_3)/3
        #
        # Voltage = (CHX data * (V-ref [= 3300 mV] * 2 [= 1:2 input divider]) / 1024 [= 10bit resolution]
        #
        voltage = int(round(((channeldata * vref * 2) / resolution),0))+ calibration
        if DEBUG : print("Data (bin)    {0:010b}".format(channeldata))
        if x==15 :      # some problem with this sensor so i had to go and twicked the thresshold
            voltage = voltage - 500
        if voltage<=2500 and keyreleased==True:
            return keypressed=True
            return key=x+1
        if voltage<=2500 and keyreleased==False
            return keypressed=True
            return key=x+1
        if voltage>2500 and keypressed==True:
            x=x+1
            return keyreleased==True
        if x == 15:
            x=0

如何在main 中调用这些变量?

【问题讨论】:

  • 如何在 main 中调用这些变量? main 过程或调用此过程的任何过程...
  • 这是我的问题!我有这个函数返回的 keypressed、keyreleased 和 key 变量。正确的 ?我怎样才能在 main 中问他们?
  • 假设如果 keypressed == True 这样做?
  • 为什么在return语句中有赋值?我认为你需要更清楚你想要从你的函数返回什么。 表示事件发生的值
  • 传达发生按键和按键释放的事件。键的值。

标签: python function python-3.x raspberry-pi2


【解决方案1】:

您将三个参数传递给这个函数,大概是来自主过程。

使用元组解包,只返回一个元组:

print(foo(1, 2, 3)) # should print: 2, 4, 0

# unpacking the tuple like so:
x, y, z = foo(1,2,3)
print x, y, z

def foo(x, y, z):
    x +=1
    y +=2
    z = 0

在您的代码中,您可以简单地返回一些组合,例如:

return keypressed, keyreleased, key

请注意,这样的多个return语句不会按预期执行,而不是按顺序返回两个值,它只会在第一个return之后停止:

        return keypressed=True
        return key=x+1           # This statement will NOT execute

另请注意,您当前正在实施的这种表示法将进行评估并返回真实值:

return keypressed=False # returns True if keypressed==False else False

如果这是您的意图,那么可以,否则您应该在返回语句之前进行分配。您的代码的工作原理,特别是您的 return 语句看起来没有意义,我不会真正解决这些问题。

【讨论】:

    猜你喜欢
    • 2022-12-31
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2023-04-03
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多