简体   繁体   中英

Add two while loop with cwiid module - Raspberry Pi

I would appreciate if someone is able to assist me on my script. I'm planning to write a script which is able to switch between loop. Currently, I have TWO(2) while loop. Thus, I add an 'if' statement to create a condition which I hope the program is able to differentiate which loop to run. BUT when I run the program, I'm just able to connect the Wiimote but unable run the conditions and the while loop. I would appreciate if someone could point out or assist me to make my script right. I'm kinda new with programming language. Still experimenting myself.

import cwiid

print("Press and hold 1+2 buttons on your Wiimote simultaneously")
wii = cwiid.Wiimote()
print("Connection established")

wii.rpt_mode = cwiid.RPT_BTN

while True:

    buttons = wii.state["buttons"]

    if (buttons & cwiid.BTN_MINUS):
        wii.rpt_mode = cwiid.RPT_BTN
        condition_1 = buttons & cwiid.BTN_MINUS
        
        while condition_1 == buttons & cwiid.BTN_MINUS:
            if (buttons & cwiid.BTN_LEFT):
                print("LEFT")

            if (buttons & cwiid.BTN_RIGHT):
                print("RIGHT")

            if (buttons & cwiid.BTN_UP):
                print("UP")

            if (buttons & cwiid.BTN_DOWN):
                print("DOWN")

            if (buttons & cwiid.BTN_B):
                print("B")

    if (buttons & cwiid.BTN_PLUS):
        wii.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACCC
        condition_2 = buttons & cwiid.BTN_PLUS

        while condition_2 == buttons & cwiid.BTN_PLUS:
            print(wii.state['acc'])
            time.sleep(0.01)

After digging and do some research. Manage to reach the goal. Below are the code I write and manage to run.

import cwiid
import time

delay = 0.2

print("Press and hold 1+2 buttons on your Wiimote simultaneously")
wii = cwiid.Wiimote()
print("Connection established")

wii.rpt_mode = cwiid.RPT_BTN

while True:
    buttons = wii.state["buttons"]

    if (buttons & cwiid.BTN_MINUS):
        print("Button - pressed: Joypad Mode")
        
        condition_1 = True
        
        while condition_1:
            if (buttons & cwiid.BTN_LEFT):
                print("LEFT")

            if (buttons & cwiid.BTN_RIGHT):
                print("RIGHT")

            if (buttons & cwiid.BTN_UP):
                print("UP")

            if (buttons & cwiid.BTN_DOWN):
                print("DOWN")

            if (buttons & cwiid.BTN_B):
                print("B")

            buttons = wii.state["buttons"]

            if (buttons & cwiid.BTN_PLUS):
                print("Button + pressed: Switch condition")
                condition_1 = False

        time.sleep(delay)


    if (buttons & cwiid.BTN_PLUS):
        print("Button + pressed: Accelerometer Mode")
        wii.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC
        condition_2 = True

        while condition_2:
            print(wii.state['acc'])
            time.sleep(0.01)

            buttons = wii.state["buttons"]

            if (buttons & cwiid.BTN_MINUS):
                print("Button - pressed: Switch condition")
                condition_2 = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM