MythPyWii – now on Google Code
Monday, January 4th, 2010Fancy submitting to MythPyWii? Let me know! It’s now on Google Code.
Code, documentation, bugs, bugfixes, etc all welcome!
Fancy submitting to MythPyWii? Let me know! It’s now on Google Code.
Code, documentation, bugs, bugfixes, etc all welcome!

Thanks to Matthew Zimmerman for sending me his modified version of MythPyWii, it now has power-saving – after 35 minutes of inactivity the Wiimote turns off. You can download the latest version, as always, here; or you can get this specific version (v17) here.
I love open source!
I’ve just updated MythPyWii to handle the connection to Mythfrontend more smoothly, and to give feedback via the wiimote on error/disconnect. (i.e. if myth closes, the wiimote shakes, and the LED pattern changes to [ . # # . ] instead of [ # . . # ] just before the wiimote turns itself off). This means if there are issues connecting to Mythfrontend you can retry again simply by pressing 1&2 once more (fix any issues first though – e.g. closed mythfrontend/no remote interface enabled).
I’ve fixed a little bug with timestretching, and have also improved the timestretch responsiveness by emulating not just the left/right keys but also the up/down keys (which increase/decrease timestretch by 0.25 instead of just 0.05).
As always, download the latest version of MythPyWii here.
Ah hah, an update to Zemanta has just been released! Integration with Facebook and Flickr, eh? I best sign up for a Flickr account then, I suppose… Argh! They seem to have broken images with my theme – I fixed it by adding this CSS to my theme (copied from firebug):
.alignright {
float:right;
}
.wp-caption {
-moz-border-radius-bottomleft:3px;
-moz-border-radius-bottomright:3px;
-moz-border-radius-topleft:3px;
-moz-border-radius-topright:3px;
background-color:#F3F3F3;
border:1px solid #DDDDDD;
margin:10px;
padding-top:4px;
text-align:center;
}
Thanks to a heads up from Sam, I have updated the MythPyWii script with better error handling and more informative feedback messages. Hopefully now when you run the script you will know what to do! I’ve also fixed a few minor bugs in the documentation.
As always, you can download the latest version of MythPyWii here.
I should probably make a GUI for MythPyWii at some point… though it seems a little pointless at the moment. I could allow you to change the controls to your liking, I spose…
UPDATE: If you’re getting errors like:
$ myth_py_wii.py
Please open Mythfrontend and then press 1&2 on the wiimote…
Connected to a wiimote
Logged in to MythFrontend
TypeError: wmcb() takes exactly 2 arguments (3 given)
TypeError: wmcb() takes exactly 2 arguments (3 given)
TypeError: wmcb() takes exactly 2 arguments (3 given)
TypeError: wmcb() takes exactly 2 arguments (3 given)
Then you have a more up to date version of cwiid than me. The fix is simple – change line 141:
def wmcb(self, messages):
to:
def wmcb(self, messages, timeIgnore):
Thanks to Mike H for pointing out this issue.
Here’s the short version:
Despite having got up at 3:30am this morning and being extremely tired, it’s been quite a good day. I finished setting up the media centre, hooked it up to the TV and got it working, sound and all. It seems the PC doesn’t run video smoothly at 1080p (not suprisingly, it was bought a good few years ago on a budget, it’s an onboard graphics card too!) – but that’s OK. I dropped it down to ~720p and it seems to run great.
It’s completely diskless, it’s only purpose being to play MythTV videos from my main mythbackend. It might find itself playing some DVDs at some point also, we’ll see.
I installed MythPyWii on it, using my new MythPyWii Install Instructions, which I wrote today and you can find here. (I also made a page detailing the controls, here. Both links can also be found in my sidebar.) Everything went smoothly, so I made the video I had been promising. This is my first time *ever* doing any video editing, and this video was all filmed in one take with no rehersals, so please bear with me! The longer version is better if you need help during the install/etc, and is linked to on the instructions page.
Here goes, I know it’s a bit long… perhaps I will make a scripted version sometime which is shorter:
For anyone wondering, I used “Kino” to edit the video. It’s OK, took me a little while to figure it out. “Add text to” is a filter under FX called “Tilter” – Text fILTER, I guess… How intuitive…
I can’t sleep, so I thought it would be wise to give you an update on my previous post about MythPyWii, despite feeling a bit like the living dead must feel. (And having to frequently remove Artie from the keyboard, bless her.)
Unfortunately I have not been able to get a video of MythPyWii yet because – minor technical hitch – I can’t find a VGA cable to connect my PC to my TV! (Wanted to show off the TV at the same time, it’s a good excuse!) I have, however, set up completely diskless booting on the old PC – and it’s working great! Took me a little bit of hacking (2 hours) to get it working well with my setup mind, not bad for my first ever diskless box! Hopefully it gives me a chance to outline every required piece of software on mythbuntu too.
I’ve also been hacking away at MythPyWii, it now has the following improvements:
Here’s the latest code:
#!/usr/bin/env python
"""
Copyright (c) 2008, Benjie Gillam
All 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/
import cwiid, time, StringIO, sys, asyncore, socket
from math import log, 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_socket
def do_scale(input, max, divisor=None):
if divisor is None: divisor = max
if (input > 1): input = 1
if (input < -1): input = -1
input = int(input * divisor)
if input>max: input = max
elif input < -max: input = -max
return input
class MythSocket(asyncore.dispatcher):
firstData = True
data = ""
prompt="\n# "
owner = None
buffer = ""
callbacks = []
oktosend = True
def __init__(self, owner):
self.owner = owner
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect(("localhost", 6546))
def handle_connect(self):
print "Connected"
def handle_close(self):
print "Closed"
self.close()
def handle_read(self):
self.data = self.data + self.recv(8192)
while len(self.data)>0:
a = self.data.find(self.prompt)
if a>-1:
self.oktosend = True
result = self.data[:a]
self.data = self.data[a+len(self.prompt):]
if not self.firstData:
print "<<<", result
cb = self.callbacks.pop(0)
if cb:
cb(result)
else:
print "Logged in to MythFrontend"
self.firstData = False
else:
break;
def writable(self):
return (self.oktosend) and (len(self.buffer) > 0) and (self.buffer.find("\n") > 0)
def handle_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 = False
def cmd(self, data, cb = None):
self.buffer += data + "\n"
self.callbacks.append(cb)
def raw(self, data):
cmds = data.split("\n")
for cmd in cmds:
if len(cmd.strip())>0:
self.cmd(cmd)
def ok(self):
return len(self.callbacks) == len(self.buffer) == 0
class WiiMyth:
wii_calibration = False
wm = None
ms = None
wii_calibration = None
#Initialize variables
reportvals = {"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.0
laststate = {}
responsiveness = 0.15
firstPress = True
firstPressDelay = 0.5
maxButtons = 0
#wii_rel = lambda v, axis: float(v - self.wii_calibration[0][axis]) / (
# self.wii_calibration[1][axis] - self.wii_calibration[0][axis])
def wii_rel(self, v, axis):
return float(v - self.wii_calibration[0][axis]) / (
self.wii_calibration[1][axis] - self.wii_calibration[0][axis])
def wmconnect(self):
print "Please press 1&2 on the wiimote..."
try:
self.wm = cwiid.Wiimote()
except:
self.wm = None
if self.ms is not None:
self.ms.close()
self.ms = None
return None
self.ms = MythSocket(self)
print "Connected..."
self.wm.rumble=1
time.sleep(.2)
self.wm.rumble=0
# Wiimote calibration data (cache this)
self.wii_calibration = self.wm.get_acc_cal(cwiid.EXT_NONE)
return self.wm
def wmcb(self, messages):
state = self.state
for message in messages:
if message[0] == cwiid.MESG_BTN:
state["buttons"] = message[1]
#elif message[0] == cwiid.MESG_STATUS:
# print "\nStatus: ", message[1]
elif message[0] == cwiid.MESG_ERROR:
if message[1] == cwiid.ERROR_DISCONNECT:
self.wm = None
if self.ms is not None:
self.ms.close()
self.ms = None
continue
else:
print "ERROR: ", message[1]
elif message[0] == cwiid.MESG_ACC:
state["acc"] = message[1]
else:
print "Unknown message!", message
laststate = self.laststate
if ('buttons' in laststate) and (laststate['buttons'] <> state['buttons']):
if state['buttons'] == 0:
self.maxButtons = 0
elif state['buttons'] < self.maxButtons:
continue
else:
self.maxButtons = state['buttons']
self.lasttime = 0
self.firstPress = True
if laststate['buttons'] == cwiid.BTN_B and not state['buttons'] == cwiid.BTN_B:
del state['BTN_B']
self.ms.cmd('play speed normal')
if (laststate['buttons'] & cwiid.BTN_A and laststate['buttons'] & cwiid.BTN_B) and not (state['buttons'] & cwiid.BTN_A and state['buttons'] & cwiid.BTN_B):
del state['BTN_AB']
#self.ms.cmd('play speed normal')
if self.ms.ok() and (self.wm is not None) and (state["buttons"] > 0) and (time.time() > self.lasttime+self.responsiveness):
self.lasttime = time.time()
wasFirstPress = False
if self.firstPress:
wasFirstPress = True
self.lasttime = self.lasttime + self.firstPressDelay
self.firstPress = False
# Stuff that doesn't need roll/etc calculations
if state["buttons"] == cwiid.BTN_HOME:
self.ms.cmd('key escape')
if state["buttons"] == cwiid.BTN_A:
self.ms.cmd('key enter')
if state["buttons"] == cwiid.BTN_MINUS:
self.ms.cmd('key d')
if state["buttons"] == cwiid.BTN_UP:
self.ms.cmd('key up')
if state["buttons"] == cwiid.BTN_DOWN:
self.ms.cmd('key down')
if state["buttons"] == cwiid.BTN_LEFT:
self.ms.cmd('key left')
if state["buttons"] == cwiid.BTN_RIGHT:
self.ms.cmd('key right')
if state["buttons"] == cwiid.BTN_PLUS:
self.ms.cmd('key p')
if state["buttons"] == cwiid.BTN_1:
self.ms.cmd('key i')
if state["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_B
if calcAcc:
# 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 zeros
roll = atan(X/Z)
if (Z <= 0.0):
if (X>0): roll += 3.14159
else: roll -= 3.14159
pitch = 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()
if state["buttons"] & cwiid.BTN_B and state["buttons"] & cwiid.BTN_LEFT:
self.ms.cmd('play seek beginning')
if state["buttons"] & cwiid.BTN_B and state["buttons"] & cwiid.BTN_A:
speed=do_scale(roll/3.14159, 20, 25)
if (speed<-10): speed = -10
state['BTN_AB'] = speed
cmd = ""
# on first press, press a,
# after then use the diff to press left/right
if not 'BTN_AB' in laststate:
# # query location
# Playback Recorded 00:04:20 of 00:25:31 1x 30210 2008-09-10T09:18:00 6523 /video/30210_20080910091800.mpg 25
cmd += "play speed normal\nkey a\n"#"play speed normal\n"
else:
speed = speed - laststate['BTN_AB']
if speed > 0:
cmd += abs(speed)*"key right\n"
elif speed < 0:
cmd += abs(speed)*"key left\n"
if speed <> 0:
self.wm.rumble=1
time.sleep(.05)
self.wm.rumble=0
if cmd is not None and cmd:
self.ms.raw(cmd)
if state["buttons"] == cwiid.BTN_B:
speed=do_scale(roll/3.14159, 8, 13)
state['BTN_B'] = speed
if not 'BTN_B' in laststate:
# # query location
# Playback Recorded 00:04:20 of 00:25:31 1x 30210 2008-09-10T09:18:00 6523 /video/30210_20080910091800.mpg 25
cmd = ""#"play speed normal\n"
if speed > 0:
cmd += "key .\n"
elif speed < 0:
cmd += "key ,\n"
if speed <> 0:
cmd += "key "+str(abs(speed)-1)+"\n"
#print cmd
elif laststate['BTN_B']<>speed:
self.wm.rumble=1
time.sleep(.05)
self.wm.rumble=0
if speed == 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"
elif speed>0:
cmd = "key .\nkey "+str(abs(speed)-1)+"\n"
else:
cmd = "key ,\nkey "+str(abs(speed)-1)+"\n"
else:
cmd = None
if cmd is not None:
self.ms.raw(cmd)
self.laststate = state.copy() #NOTE TO SELF: REMEMBER .copy() !!!
def mythLocation(self, data):
#Playback Recorded 00:00:49 of 00:25:31 1x 30210 2008-09-10T09:18:00 1243 /video/30210_20080910091800.mpg 25
#PlaybackBox
temp = data.split(" ")
output = {}
output['mode'] = temp[0]
if output['mode'] == "Playback":
output['position'] = temp[2]
output['max'] = temp[4]
return output
def main(self):
while True:
if self.wm is None:
#Connect wiimote
self.wmconnect()
if self.wm:
#Tell Wiimote to display rock sign
self.wm.led = cwiid.LED1_ON | cwiid.LED4_ON
self.wm.rpt_mode = sum(self.reportvals[a] for a in self.report if self.report[a])
self.wm.enable(cwiid.FLAG_MESG_IFC | cwiid.FLAG_REPEAT_BTN)
self.wm.mesg_callback = self.wmcb
asyncore.loop(timeout=0, count=1)
time.sleep(0.05)
print "Exited Safely"
# Instantiate our class, and start.
inst = WiiMyth()
inst.main()
Download this code: /code/myth_py_wii.r14.py
Hopefully tomorrow I can have it up and running, get a video up, and decent install instructions. Hopefully.
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“. 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 = [2, 3]
b = [1, a, 4]
print b
# Outputs [1, [2, 3], 4]
b[1][1] = “x”
print b
# Outputs [1, [2, 'x'], 4]
print a
# 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 equating 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.
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:
#!/usr/bin/env python
"""
Copyright (c) 2008, Benjie Gillam
All 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/
import cwiid, time, StringIO, sys, asyncore, socket
from math import log, 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_socket
class MythSocket(asyncore.dispatcher):
firstData = True
data = ""
prompt="\n# "
owner = None
buffer = ""
callbacks = []
oktosend = True
def __init__(self, owner):
self.owner = owner
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect(("localhost", 6546))
def handle_connect(self):
print "Connected"
def handle_close(self):
print "Closed"
self.close()
def handle_read(self):
self.data = self.data + self.recv(8192)
while len(self.data)>0:
a = self.data.find(self.prompt)
if a>-1:
self.oktosend = True
result = self.data[:a]
self.data = self.data[a+len(self.prompt):]
if not self.firstData:
print "<<<", result
cb = self.callbacks.pop(0)
if cb:
cb(result)
else:
print "Logged in to MythFrontend"
self.firstData = False
else:
break;
def writable(self):
return (self.oktosend) and (len(self.buffer) > 0) and (self.buffer.find("\n") > 0)
def handle_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 = False
def cmd(self, data, cb = None):
self.buffer += data + "\n"
self.callbacks.append(cb)
def raw(self, data):
cmds = data.split("\n")
for cmd in cmds:
if len(cmd.strip())>0:
self.cmd(cmd)
def ok(self):
return len(self.callbacks) == len(self.buffer) == 0
class WiiMyth:
wii_calibration = False
wm = None
ms = None
wii_calibration = None
#Initialize variables
reportvals = {"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.0
laststate = {}
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])
def wii_rel(self, v, axis):
return float(v - self.wii_calibration[0][axis]) / (
self.wii_calibration[1][axis] - self.wii_calibration[0][axis])
def wmconnect(self):
print "Please press 1&2 on the wiimote..."
try:
self.wm = cwiid.Wiimote()
except:
self.wm = None
if self.ms is not None:
self.ms.close()
self.ms = None
return None
self.ms = MythSocket(self)
print "Connected..."
self.wm.rumble=1
time.sleep(.2)
self.wm.rumble=0
# Wiimote calibration data (cache this)
self.wii_calibration = self.wm.get_acc_cal(cwiid.EXT_NONE)
return self.wm
def wmcb(self, messages):
state = self.state
for message in messages:
if message[0] == cwiid.MESG_BTN:
state["buttons"] = message[1]
#elif message[0] == cwiid.MESG_STATUS:
# print "\nStatus: ", message[1]
elif message[0] == cwiid.MESG_ERROR:
if message[1] == cwiid.ERROR_DISCONNECT:
self.wm = None
if self.ms is not None:
self.ms.close()
self.ms = None
continue
else:
print "ERROR: ", message[1]
elif message[0] == cwiid.MESG_ACC:
state["acc"] = message[1]
else:
print "Unknown message!", message
laststate = self.laststate
if ('buttons' in laststate) and (laststate['buttons'] <> state['buttons']):
if laststate['buttons'] & cwiid.BTN_B and not state['buttons'] & cwiid.BTN_B:
del state['BTN_B']
self.ms.cmd('play speed normal')
if self.ms.ok() and (self.wm is not None) and (state["buttons"] > 0) and (time.time() > self.lasttime+self.responsiveness):
self.lasttime = time.time()
# Stuff that doesn't need roll/etc calculations
if state["buttons"] & cwiid.BTN_HOME:
self.ms.cmd('key escape')
if state["buttons"] & cwiid.BTN_A:
self.ms.cmd('key enter')
if state["buttons"] & cwiid.BTN_MINUS:
self.ms.cmd('key d')
if state["buttons"] & cwiid.BTN_UP:
self.ms.cmd('key up')
if state["buttons"] & cwiid.BTN_DOWN:
self.ms.cmd('key down')
if state["buttons"] & cwiid.BTN_LEFT:
self.ms.cmd('key left')
if state["buttons"] & cwiid.BTN_RIGHT:
self.ms.cmd('key right')
if state["buttons"] & cwiid.BTN_PLUS:
self.ms.cmd('key p')
if state["buttons"] & cwiid.BTN_1:
self.ms.cmd('key i')
if state["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_B
if calcAcc:
# 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 zeros
roll = atan(X/Z)
if (Z <= 0.0):
if (X>0): roll += 3.14159
else: roll -= 3.14159
pitch = 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()
if state["buttons"] & cwiid.BTN_B:
speed = roll/3.14159
if (speed > 1): speed = 1
if (speed < -1): speed = -1
speed = int(speed * 13)
if abs(speed)>9:
if speed>0: speed = 9
else: speed = -9
state['BTN_B'] = speed
if not 'BTN_B' in laststate:
# # query location
# Playback Recorded 00:04:20 of 00:25:31 1x 30210 2008-09-10T09:18:00 6523 /video/30210_20080910091800.mpg 25
cmd = ""#"play speed normal\n"
if speed > 0:
cmd += "key .\n"
elif speed < 0:
cmd += "key ,\n"
if speed <> 0:
cmd += "key "+str(abs(speed)-1)+"\n"
#print cmd
elif laststate['BTN_B']<>speed:
self.wm.rumble=1
time.sleep(.05)
self.wm.rumble=0
if speed == 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"
elif speed>0:
cmd = "key .\nkey "+str(abs(speed)-1)+"\n"
else:
cmd = "key ,\nkey "+str(abs(speed)-1)+"\n"
else:
cmd = None
if cmd is not None:
self.ms.raw(cmd)
self.laststate = state.copy() #NOTE TO SELF: REMEMBER .copy() !!!
def mythLocation(self, data):
#Playback Recorded 00:00:49 of 00:25:31 1x 30210 2008-09-10T09:18:00 1243 /video/30210_20080910091800.mpg 25
#PlaybackBox
temp = data.split(" ")
output = {}
output['mode'] = temp[0]
if output['mode'] == "Playback":
output['position'] = temp[2]
output['max'] = temp[4]
return output
def main(self):
while True:
if self.wm is None:
#Connect wiimote
self.wmconnect()
if self.wm:
#Tell Wiimote to display rock sign
self.wm.led = cwiid.LED1_ON | cwiid.LED4_ON
self.wm.rpt_mode = sum(self.reportvals[a] for a in self.report if self.report[a])
self.wm.enable(cwiid.FLAG_MESG_IFC | cwiid.FLAG_REPEAT_BTN)
self.wm.mesg_callback = self.wmcb
asyncore.loop(timeout=0, count=1)
time.sleep(0.05)
print "Exited Safely"
# Instantiate our class, and start.
inst = WiiMyth()
inst.main()
Download this code: /code/myth_py_wii.r12.py
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):
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!
These are liable to change, but for now, here is how they are mapped:
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!
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)
… is great! For those of you who don’t know, I got married last month (and then spent 2 weeks on honeymoon in Athens!) – it was fantastic, though very very hot. During the trip we kept a moblog – which you can view here (little point repeating the content on this blog!). We’ve now moved into our new flat, unpacked, got a kitten, Artemis Gillam (seen here curled up next to my keyboard), and FINALLY have internet again, and now are both back to work. Yay.
Hopefully soon we’ll have our new TV (a 42″ LG 42LG6000 LCD 1080p TV, John Lewis are currently price-matching it for us with Currys (£749 -> £699.99)) which I am sure will be the subject of many blog posts in the future whilst I try and build the perfect HTPC for it (running MythTV of course!) and pimp it out with compiz, allow controlling of it from a Wiimote, and all that jazz. Sounds like fun! Hopefully I can get a little more hardcore in my hacking of the OS (as it doesn’t have to be stable in the short term, unlike my work PC) – and this is also the perfect opportunity for me to brush up on my python skills, rather than doing everything in PHP! (When you know one language so well, its hard not to neglect all the others, even if they are technically better in many ways…)
Whilst we’re on the subject of the TV – though I have not received it yet, there is one thing I was surprised to find when I went and viewed it – the TV, when on it’s stand, is *very* wobbly. A gentle touch of the finger is enough to move it up and down (though not rotate it on it’s stand) – and it wobbles for a good few seconds before coming back to stationary. Compared to other similarly sized TVs of other brands it is very noticeable – however it does not seem to be about to topple over, even when you are a little more vigorous with it, so I suppose it is nothing to worry too much about… The kitten should not be able to do it too much damage!
It has bothered me a little for a while that the MythWeb (part of the fantastic MythTV package for Linux) aspect ratio is hard-coded to 4:3. Most of the TV that I watch (received over Freeview (DVB-T) in the UK) is in 16:9, so watching it back on 4:3 is a bit of a pain. Thus I was motivated to change the hard coding to 16:9. The process is quite simple:
There is a minor bug now where the player does not show the control bar at the bottom properly initially, but a click on the preview picture solves this.
I’m currently working on modifications to stream the video in 3gp format to my mobile (a Nokia 6120 Classic), however this seems a lot harder as I have to implement a RTSP server, and have to re-encode all jobs in advance (by using a MythTV User Job) which is not quite what I am after. There is a page about it in the MythTV wiki. I wonder if I can find a cunning way around it…
If this helps you, please let me know in the comments!