1

我正在编写一个 python 脚本,它将使用 cwiid 库通过 Wiimote 控制 LED。该程序识别 wiimote 并能够启动循环,但是当用户按遥控器上的“B”时它不会让我停止循环。这是相关代码,如果需要,我可以提供脚本的其余部分。谢谢。

  buttons = wii.state['buttons']

...

  if (buttons & cwiid.BTN_A):
    print 'Button A pressed'
    print 'Press B to cancel loop'
    keepRunning = True
    while keepRunning:
        blink(32)#5v green
        blink(38)#5v yellow
        blink(36)#5v blue
        blink(40)#5v red
        blink(37)#3v3 green
        blink(35)#3v3 yellow
        blink(33)#3v3 blue
        blink(31)#3v3 red
        if (buttons & cwiid.BTN_B):
            keepRunning  = False
    time.sleep(button_delay)

这是每个斯图尔特答案的固定循环

  if (buttons & cwiid.BTN_A):
    print 'Button A pressed'
    print 'Press B to cancel loop'
    keepRunning = True
    while keepRunning:
        blink(32)#5v green
        blink(38)#5v yellow
        blink(36)#5v blue
        blink(40)#5v red
        blink(37)#3v3 green
        blink(35)#3v3 yellow
        blink(33)#3v3 blue
        blink(31)#3v3 red
        buttons = wii.state['buttons']#added in this line
        if (buttons & cwiid.BTN_B):
            keepRunning  = False
    time.sleep(button_delay)
4

1 回答 1

1

wii.state['buttons']看起来是一个数字,因此将其存储在变量中buttons意味着当用户按下不同的按钮组合时它不再更新。

要解决此问题,只需替换buttonswii.state['buttons']每次的直接引用。

您可能需要考虑检查是否在 each 之后按下 B blink,如下所示:

from itertools import cycle
...
if wii.state['buttons'] & cwiid.BTN_A:
    print 'Button A pressed'
    print 'Press B to cancel loop'
    for colour in cycle([32, 38, 36, 40, 37, 35, 33, 31]):
        blink(colour)
        if wii.state['buttons'] & cwiid.BTN_B:
            break
    time.sleep(button_delay)
于 2016-01-06T18:57:24.717 回答