Author Topic: python code suggestions (wmdemo)  (Read 6180 times)

Offline linuxConvert

  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
on: March 06, 2008, 12:05:28 PM
Hello,

I am looking for some code review and suggestions for a port of wmdemo (from cwiid in linux) into python.

Code: [Select]
#!/usr/bin/env python
# By linuxConvert
# 3-6-08

# program emulates the wmdemo in linux with cwiid.



import cwiid,time
from math import log



def printMenu():
    print '''
1: toggle LED 1
2: toggle LED 2
3: toggle LED 3
4: toggle LED 4
5: toggle rumble
a: toggle accelerometer reporting
b: toggle button reporting
e: toggle extension reporting
i: toggle ir reporting
m: toggle messages
p: print this menu
r: request status message ((t) enables callback output)
s: print current state
t: toggle status reporting
x: exit
'''

def wmconnect():
    print "Please press 1&2 on the wiimote..."
    wm = cwiid.Wiimote()
    print "Connected..."
    wm.rumble=1
    time.sleep(.2)
    wm.rumble=0
    return wm



def main():

    wm = wmconnect()
    rumble = 0
    report=[0]*7
    printMenu()
    ch=raw_input(">")
    while (ch != "x"):
       
        if ch == "1":
            wm.led = cwiid.LED1_ON
       
        elif ch == "2":
            wm.led = cwiid.LED2_ON

        elif ch == "3":
            wm.led = cwiid.LED3_ON
       
        elif ch == "4":
            wm.led = cwiid.LED4_ON
       
        elif ch == "5":
            if rumble == 1:
                wm.rumble = 0
                rumble = 0
            else:
                wm.rumble = 1
                rumble = 1
       
        elif ch == "a":
            index = int(log(cwiid.RPT_ACC,2))
            if report[index] == 0:
                report[index] = cwiid.RPT_ACC
            else:
                report[index] = 0
            wm.rpt_mode = sum(report)
       
        elif ch == "b":
            index = int(log(cwiid.RPT_BTN,2))
            if report[index] == 0:
                report[index] = cwiid.RPT_BTN
            else:
                report[index] = 0
            wm.rpt_mode = sum(report)
       
        elif ch == "e":
            passindex = int(log(cwiid.RPT_EXT,2))
            if report[index] == 0:
                report[index] = cwiid.RPT_EXT
            else:
                report[index] = 0
            wm.rpt_mode = sum(report)
       
        elif ch == "i":
            passindex = int(log(cwiid.RPT_IR,2))
            if report[index] == 0:
                report[index] = cwiid.RPT_IR
            else:
                report[index] = 0
            wm.rpt_mode = sum(report)
       
        elif ch == "m":
            pass
       
        elif ch == "p":
            printMenu()
       
        elif ch == "r":
            pass
       
        elif ch == "s":
            state = wm.state
            print
            for item in state:
                print item,state[item]
            print

        elif ch == "t":
            pass

        elif ch == "":
            print
        else:
            print "Invalid Input"
            break

        ch=raw_input(">")


    wm.close()
    print "Exited Safely\nShutting Down Wiimote..."




main()



A few of the options are not implemented yet. They are designated by 'pass'. Input on those are welcome as well.
Thanks in advance!

----linuxConvert---