MythPyWii (yes, I’m not very good at names, better suggestions welcome
in the comments!) is born!
I love the Wiimote
(Wii Remote) so much,
I’ve just been gagging for a way to hook it up to my computer and do
something useful. I started by hooking it up to
Neverball and that
was cool, but I wanted something better. I’ve always thought it would
make a great remote control for Mythfrontend (from the
MythTV package) - but
those that exist only seem to use the Wiimote as a keyboard - they
ignore it’s accelerometers and other such things. (And I want one that
doesn’t require a wii sensor
bar, because I don’t have a second one!)
I wanted better. But I never
seemed to have the time to make it. That is, until Jof told me “go and
learn
Python “Python (programming language)”)“.
This was the perfect project for starting python. That is a lie, it was
way too complex, but I thought “why bother if it isn’t challenging” - it
turned out to be a kind of baptism by fire.
If you are in a rush, or hate nerdy stuff, skip to the next title “How To Install”.
Having had
PHP as my main programming
language for such a long long long time, switching to Python sounded
like fun. It has got a very nice syntax, and is a very clear language…
except for it’s major overuse of references. For example:
A comprehension issue with Python’s references
123456789
a=[2,3]b=[1,a,4]printb# Outputs [1, [2, 3], 4]b[1][1]=“x”printb# Outputs [1, [2, 'x'], 4]printa# Outputs [2, 'x'], not [2, 3] as I expect, coming from PHP.
Still this is “easily” got around by making sure you copy objects
rather than just assigning them. And checking your code thoroughly.
This
was my first time interfacing with mythfrontend in any way, and I chose
to try and script mythfrontend’s telnet socket interface. It was also my
first time programming an interface to the wiimote, so I chose to use
the cwiid package, as that is what I used to control neverball, and it
seemed to work well. A few days of reading python tutorials, hacking and
swearing, I finally acheived what I had set out to do - fastforwarding
using the accelerometers. A couple of hours later and I had a fully
working wiimote interface to mythtv…
My thoughts on the mythtv telnet
socket interface: its very basic, and quite slow, but definitely better
than nothing. I think a few iterations down the line and it could be
awesome. My biggest problem with it currently is how slowly it does
“query location” - it takes almost a second to get back to you with an
answer, which means you can’t do location based buttons easily. (For
example, I wanted A to be “p” (play/pause) when playing back a video,
and “enter” (accept, OK, …) when not doing so.) I found the best way
to do things in the end was to get the program to emulate the keyboard
after all, admittedly sometimes with macros.
How To Install
You should definitely keep in mind that this project is not even
alpha stage.
Its my first real forray into the world of Python, my first real forray
into programming with the wiimote, AND my first real forray with using
mythfrontend’s telnet interface - all in all it is very new to me. It
seems to work, just about, so I thought I would release what I have so
far, and then set about tidying it up. I had intended to release a video
at this point too, but I am just too excited! You can download the code
here:
MythPyWii first release (myth_py_wii.r12.py)download
#!/usr/bin/env python"""Copyright (c) 2008, Benjie GillamAll rights reserved.Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of MythPyWii nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""# By Benjie Gillam http://www.benjiegillam.com/mythpywii/importcwiid,time,StringIO,sys,asyncore,socketfrommathimportlog,floor,atan,sqrt,cos,exp# Note to self - list of good documentation:# cwiid: http://flx.proyectoanonimo.com/proyectos/cwiid/# myth telnet: http://www.mythtv.org/wiki/index.php/Telnet_socketclassMythSocket(asyncore.dispatcher):firstData=Truedata=""prompt="\n# "owner=Nonebuffer=""callbacks=[]oktosend=Truedef__init__(self,owner):self.owner=ownerasyncore.dispatcher.__init__(self)self.create_socket(socket.AF_INET,socket.SOCK_STREAM)self.connect(("localhost",6546))defhandle_connect(self):print"Connected"defhandle_close(self):print"Closed"self.close()defhandle_read(self):self.data=self.data+self.recv(8192)whilelen(self.data)>0:a=self.data.find(self.prompt)ifa>-1:self.oktosend=Trueresult=self.data[:a]self.data=self.data[a+len(self.prompt):]ifnotself.firstData:print"<<<",resultcb=self.callbacks.pop(0)ifcb:cb(result)else:print"Logged in to MythFrontend"self.firstData=Falseelse:break;defwritable(self):return(self.oktosend)and(len(self.buffer)>0)and(self.buffer.find("\n")>0)defhandle_write(self):a=self.buffer.find("\n")sent=self.send(self.buffer[:a+1])print">>>",self.buffer[:sent-1]self.buffer=self.buffer[sent:]self.oktosend=Falsedefcmd(self,data,cb=None):self.buffer+=data+"\n"self.callbacks.append(cb)defraw(self,data):cmds=data.split("\n")forcmdincmds:iflen(cmd.strip())>0:self.cmd(cmd)defok(self):returnlen(self.callbacks)==len(self.buffer)==0classWiiMyth:wii_calibration=Falsewm=Nonems=Nonewii_calibration=None#Initialize variablesreportvals={"accel":cwiid.RPT_ACC,"button":cwiid.RPT_BTN,"ext":cwiid.RPT_EXT,"status":cwiid.RPT_STATUS}report={"accel":True,"button":True}state={"acc":[0,0,1]}lasttime=0.0laststate={}responsiveness=0.5#wii_rel = lambda v, axis: float(v - self.wii_calibration[0][axis]) / (# self.wii_calibration[1][axis] - self.wii_calibration[0][axis])defwii_rel(self,v,axis):returnfloat(v-self.wii_calibration[0][axis])/(self.wii_calibration[1][axis]-self.wii_calibration[0][axis])defwmconnect(self):print"Please press 1&2 on the wiimote..."try:self.wm=cwiid.Wiimote()except:self.wm=Noneifself.msisnotNone:self.ms.close()self.ms=NonereturnNoneself.ms=MythSocket(self)print"Connected..."self.wm.rumble=1time.sleep(.2)self.wm.rumble=0# Wiimote calibration data (cache this)self.wii_calibration=self.wm.get_acc_cal(cwiid.EXT_NONE)returnself.wmdefwmcb(self,messages):state=self.stateformessageinmessages:ifmessage[0]==cwiid.MESG_BTN:state["buttons"]=message[1]#elif message[0] == cwiid.MESG_STATUS:# print "\nStatus: ", message[1]elifmessage[0]==cwiid.MESG_ERROR:ifmessage[1]==cwiid.ERROR_DISCONNECT:self.wm=Noneifself.msisnotNone:self.ms.close()self.ms=Nonecontinueelse:print"ERROR: ",message[1]elifmessage[0]==cwiid.MESG_ACC:state["acc"]=message[1]else:print"Unknown message!",messagelaststate=self.laststateif('buttons'inlaststate)and(laststate['buttons']<>state['buttons']):iflaststate['buttons']&cwiid.BTN_Bandnotstate['buttons']&cwiid.BTN_B:delstate['BTN_B']self.ms.cmd('play speed normal')ifself.ms.ok()and(self.wmisnotNone)and(state["buttons"]>0)and(time.time()>self.lasttime+self.responsiveness):self.lasttime=time.time()# Stuff that doesn't need roll/etc calculationsifstate["buttons"]&cwiid.BTN_HOME:self.ms.cmd('key escape')ifstate["buttons"]&cwiid.BTN_A:self.ms.cmd('key enter')ifstate["buttons"]&cwiid.BTN_MINUS:self.ms.cmd('key d')ifstate["buttons"]&cwiid.BTN_UP:self.ms.cmd('key up')ifstate["buttons"]&cwiid.BTN_DOWN:self.ms.cmd('key down')ifstate["buttons"]&cwiid.BTN_LEFT:self.ms.cmd('key left')ifstate["buttons"]&cwiid.BTN_RIGHT:self.ms.cmd('key right')ifstate["buttons"]&cwiid.BTN_PLUS:self.ms.cmd('key p')ifstate["buttons"]&cwiid.BTN_1:self.ms.cmd('key i')ifstate["buttons"]&cwiid.BTN_2:self.ms.cmd('key m')# Do we need to calculate roll, etc?# Currently only BTN_B needs this.calcAcc=state["buttons"]&cwiid.BTN_BifcalcAcc:# Calculate the roll/etc.X=self.wii_rel(state["acc"][cwiid.X],cwiid.X)Y=self.wii_rel(state["acc"][cwiid.Y],cwiid.Y)Z=self.wii_rel(state["acc"][cwiid.Z],cwiid.Z)if(Z==0):Z=0.00000001# Hackishly prevents divide by zerosroll=atan(X/Z)if(Z<=0.0):if(X>0):roll+=3.14159else:roll-=3.14159pitch=atan(Y/Z*cos(roll))#print "X: %f, Y: %f, Z: %f; R: %f, P: %f; B: %d \r" % (X, Y, Z, roll, pitch, state["buttons"]),sys.stdout.flush()ifstate["buttons"]&cwiid.BTN_B:speed=roll/3.14159if(speed>1):speed=1if(speed<-1):speed=-1speed=int(speed*13)ifabs(speed)>9:ifspeed>0:speed=9else:speed=-9state['BTN_B']=speedifnot'BTN_B'inlaststate:# # query location# Playback Recorded 00:04:20 of 00:25:31 1x 30210 2008-09-10T09:18:00 6523 /video/30210_20080910091800.mpg 25cmd=""#"play speed normal\n"ifspeed>0:cmd+="key .\n"elifspeed<0:cmd+="key ,\n"ifspeed<>0:cmd+="key "+str(abs(speed)-1)+"\n"#print cmdeliflaststate['BTN_B']<>speed:self.wm.rumble=1time.sleep(.05)self.wm.rumble=0ifspeed==0:cmd="play speed normal"elif((laststate['BTN_B']>0)and(speed>0))or((laststate['BTN_B']<0)and(speed<0)):cmd="key "+str(abs(speed)-1)+"\n"elifspeed>0:cmd="key .\nkey "+str(abs(speed)-1)+"\n"else:cmd="key ,\nkey "+str(abs(speed)-1)+"\n"else:cmd=NoneifcmdisnotNone:self.ms.raw(cmd)self.laststate=state.copy()#NOTE TO SELF: REMEMBER .copy() !!!defmythLocation(self,data):#Playback Recorded 00:00:49 of 00:25:31 1x 30210 2008-09-10T09:18:00 1243 /video/30210_20080910091800.mpg 25#PlaybackBoxtemp=data.split(" ")output={}output['mode']=temp[0]ifoutput['mode']=="Playback":output['position']=temp[2]output['max']=temp[4]returnoutputdefmain(self):whileTrue:ifself.wmisNone:#Connect wiimoteself.wmconnect()ifself.wm:#Tell Wiimote to display rock signself.wm.led=cwiid.LED1_ON|cwiid.LED4_ONself.wm.rpt_mode=sum(self.reportvals[a]forainself.reportifself.report[a])self.wm.enable(cwiid.FLAG_MESG_IFC|cwiid.FLAG_REPEAT_BTN)self.wm.mesg_callback=self.wmcbasyncore.loop(timeout=0,count=1)time.sleep(0.05)print"Exited Safely"# Instantiate our class, and start.inst=WiiMyth()inst.main()
First, load up
mythfrontend. Then run the script using “python
myth_py_wii.r12.py”. Once it is running it will prompt you to press
1+2 on the Wiimote. Doing so should make the LEDs flash at the bottom of
the wiimote, and then a good few seconds later (up to 30) the wiimote
should vibrate to let you know it is activated, and LED1+LED4 should be
turned on (my Wiimote version of rock-hands). Then navigate using the
controls below.
Unfortunately I have not tested this on any computer but
my own. Hopefully in a few days time I can write some decent install
instructions. However for now you will have to try your best, with the
following hopefully helpful hints:
You need (some of and probably more
than) the following installed (
Ubuntu Hardy):
GNU/Linux
working bluetooth connectivity (bluetooth keyfobs are really cheap
now, and most work out of the box with Hardy)
a Wiimote (duh!)
python-cwiid, libcwiid1, libcwiid1-dev
python (I’m using 2.5)
a working mythfrontend
patience
You also need to set mythfrontend up to accept remote connections on
port 6546 (this took a couple of attempts to activate for me - try
restarting mythfrontend once you have modified and saved the settings).
You can find this under something similar to Mythfrontend Main Menu >
Utilities/Setup > Setup > General > page 4 > “Enable Network Remote
Control interface”, “Network Remote Control Port: 6546”
Hopefully
thats enough to get you started. I aim to release a video soon to show
it in action. One last thing - the controls!
Controls
These are liable to change, but for now, here is how they are mapped:
Keypad : same as keypad on keyboard
A : Enter (Accept, OK, next, …)
Minus (-) : d (Delete)
Home : escape (Exit to previous menu/exit mythfrontend)
Plus (+) : p (Play/pause)
1 : Info
2 : Menu
B + twist wiimote : rewind (if twisted to the left) or fastforward
(otherwise) with speed dependant on twist amount.
A comment on twisting:
Point the wii remote at the screen, and twist from the elbow so that it
continues to point at the screen.
The maximum fastforward/rewind speed
is 180x. The speeds are dictated by mythfrontend itself. When you rotate
the wiimote, you will feel a slight vibration (0.05 seconds) to let you
know you have gone up or down a speed segment.
To stop
fastforwarding/rewinding, simply let go of B.
Beware: there is no power
saving built in - however you should be able to turn the wiimote off
(power button) when not in use, and turn it back on by holding down 1
and 2 to make it sync.
I know this post is a bit of an info burst, I
just want to get this out there so other people can hack with it and
give me some feedback. Let me know what you think!
Known bugs:
Everything! This is pre-alpha software, don’t blame me if it messes up
your computer! (It should be fine though…) Biggest known bug at the
moment is with key repeats being really slow/unreliable.
ENJOY! (and let me know what you think in the comments)