<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>about:benjie &#187; Wii Remote</title>
	<atom:link href="http://www.benjiegillam.com/tag/wii-remote/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.benjiegillam.com</link>
	<description>Benjie Gillam's blog, and home of MythPyWii - probably the best Wii remote (Wiimote) interface to MythTV - see sidebar.</description>
	<lastBuildDate>Tue, 06 Apr 2010 14:14:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MythPyWii &#8211; A Wiimote Interface To MythTV Using Python</title>
		<link>http://www.benjiegillam.com/2008/09/mythpywii-a-wiimote-interface-to-mythtv-using-python/</link>
		<comments>http://www.benjiegillam.com/2008/09/mythpywii-a-wiimote-interface-to-mythtv-using-python/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 17:45:51 +0000</pubDate>
		<dc:creator>Benjie</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MythTV]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MythPyWii]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Wii]]></category>
		<category><![CDATA[Wii Remote]]></category>
		<category><![CDATA[Wiimote]]></category>

		<guid isPermaLink="false">http://www.benjiegillam.com/?p=117</guid>
		<description><![CDATA[Image via Wikipedia 
MythPyWii (yes, I&#8217;m not very good at names, better suggestions welcome in the comments!) is born!
I love the Wiimote (Wii Remote) so much, I&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; float: right; display: block;"><a href="http://commons.wikipedia.org/wiki/Image:Wii_Remote_Image.jpg"><img style="border: medium none ; display: block;" src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Wii_Remote_Image.jpg/202px-Wii_Remote_Image.jpg" alt="An image of the Wii remote (with wrist strap) ..."></a><span class="zemanta-img-attribution">Image via <a href="http://commons.wikipedia.org/wiki/Image:Wii_Remote_Image.jpg">Wikipedia</a> </span></div>
<p>MythPyWii (yes, I&#8217;m not very good at names, better suggestions welcome in the comments!) is born!</p>
<p>I love the Wiimote (<a class="zem_slink" title="Wii Remote" rel="wikipedia" href="http://en.wikipedia.org/wiki/Wii_Remote">Wii Remote</a>) so much, I&#8217;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 <a class="zem_slink" title="Neverball" rel="wikipedia" href="http://en.wikipedia.org/wiki/Neverball">Neverball</a> and that was cool, but I wanted something better. I&#8217;ve always thought it would make a great remote control for Mythfrontend (from the <a class="zem_slink" title="MythTV" rel="wikipedia" href="http://en.wikipedia.org/wiki/MythTV">MythTV</a> package) &#8211; but those that exist only seem to use the Wiimote as a keyboard &#8211; they ignore it&#8217;s accelerometers and other such things. (And I want one that doesn&#8217;t require a <a class="zem_slink" title="Wii" rel="wikipedia" href="http://en.wikipedia.org/wiki/Wii">wii</a> sensor bar, because I don&#8217;t have a second one!)</p>
<p>I wanted better. But I never seemed to have the time to make it. That is, until Jof told me &#8220;go and learn <a class="zem_slink" title="Python (programming language)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>&#8220;. This was the perfect project for starting python. That is a lie, it was way too complex, but I thought &#8220;why bother if it isn&#8217;t challenging&#8221; &#8211; it turned out to be a kind of baptism by fire.</p>
<p>If you are in a rush, or hate nerdy stuff, skip to the next title &#8220;How To Install&#8221;.</p>
<p>Having had <a class="zem_slink" title="PHP" rel="wikipedia" href="http://en.wikipedia.org/wiki/PHP">PHP</a> 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&#8230; except for it&#8217;s major overuse of references. For example:</p>
<blockquote><p>a = [2, 3]<br />
b = [1, a, 4]<br />
print b<br />
# Outputs [1, [2, 3], 4]<br />
b[1][1] = &#8220;x&#8221;<br />
print b<br />
# Outputs [1, [2, 'x'], 4]<br />
print a<br />
# Outputs [2, 'x'], not [2, 3] as I expect, coming from PHP.</p></blockquote>
<p>Still this is &#8220;easily&#8221; got around by making sure you <strong>copy</strong> objects rather than just equating them. And checking your code thoroughly.</p>
<p>This was my first time interfacing with mythfrontend in any way, and I chose to try and script mythfrontend&#8217;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 &#8211; fastforwarding using the accelerometers. A couple of hours later and I had a fully working wiimote interface to mythtv&#8230;</p>
<p>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 &#8220;query location&#8221; &#8211; it takes almost a second to get back to you with an answer, which means you can&#8217;t do location based buttons easily. (For example, I wanted A to be &#8220;p&#8221; (play/pause) when playing back a video, and &#8220;enter&#8221; (accept, OK, &#8230;) 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.</p>
<h3>How To Install</h3>
<p>You should definitely keep in mind that this project is not even <a class="zem_slink" title="Software release life cycle" rel="wikipedia" href="http://en.wikipedia.org/wiki/Software_release_life_cycle">alpha stage</a>. 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&#8217;s telnet interface &#8211; 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:</p>
<p><code class="source">
<span class="enscript-comment">#!/usr/bin/env python
</span><span class="enscript-string">&quot;&quot;&quot;
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 &quot;AS IS&quot; 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.
&quot;&quot;&quot;</span>
<span class="enscript-comment"># By Benjie Gillam http://www.benjiegillam.com/mythpywii/
</span>
<span class="enscript-keyword">import</span> cwiid, time, StringIO, sys, asyncore, socket
<span class="enscript-keyword">from</span> math <span class="enscript-keyword">import</span> log, floor, atan, sqrt, cos, exp

<span class="enscript-comment"># Note to self - list of good documentation:
</span><span class="enscript-comment"># cwiid: http://flx.proyectoanonimo.com/proyectos/cwiid/
</span><span class="enscript-comment"># myth telnet: http://www.mythtv.org/wiki/index.php/Telnet_socket
</span>
<span class="enscript-keyword">class</span> MythSocket(asyncore.dispatcher):
	firstData = True
	data = <span class="enscript-string">&quot;&quot;</span>
	prompt=<span class="enscript-string">&quot;\n# &quot;</span>
	owner = None
	buffer = <span class="enscript-string">&quot;&quot;</span>
	callbacks = []
	oktosend = True
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">__init__</span>(self, owner):
		self.owner = owner
		asyncore.dispatcher.__init__(self)
		self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
		self.connect((<span class="enscript-string">&quot;localhost&quot;</span>, 6546))
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">handle_connect</span>(self):
		<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Connected&quot;</span>
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">handle_close</span>(self):
		<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Closed&quot;</span>
		self.close()
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">handle_read</span>(self):
		self.data = self.data + self.recv(8192)
		<span class="enscript-keyword">while</span> len(self.data)&gt;0:
			a = self.data.find(self.prompt)
			<span class="enscript-keyword">if</span> a&gt;-1:
				self.oktosend = True
				result = self.data[:a]
				self.data = self.data[a+len(self.prompt):]
				<span class="enscript-keyword">if</span> <span class="enscript-keyword">not</span> self.firstData:
					<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;&lt;&lt;&lt;&quot;</span>, result
					cb = self.callbacks.pop(0)
					<span class="enscript-keyword">if</span> cb:
						cb(result)
				<span class="enscript-keyword">else</span>:
					<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Logged in to MythFrontend&quot;</span>
					self.firstData = False
			<span class="enscript-keyword">else</span>:
				<span class="enscript-keyword">break</span>;
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">writable</span>(self):
		<span class="enscript-keyword">return</span> (self.oktosend) <span class="enscript-keyword">and</span> (len(self.buffer) &gt; 0) <span class="enscript-keyword">and</span> (self.buffer.find(<span class="enscript-string">&quot;\n&quot;</span>) &gt; 0)
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">handle_write</span>(self):
		a = self.buffer.find(<span class="enscript-string">&quot;\n&quot;</span>)
		sent = self.send(self.buffer[:a+1])
		<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;&gt;&gt;&gt;&quot;</span>, self.buffer[:sent-1]
		self.buffer = self.buffer[sent:]
		self.oktosend = False
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">cmd</span>(self, data, cb = None):
		self.buffer += data + <span class="enscript-string">&quot;\n&quot;</span>
		self.callbacks.append(cb)
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">raw</span>(self, data):
		cmds = data.split(<span class="enscript-string">&quot;\n&quot;</span>)
		<span class="enscript-keyword">for</span> cmd <span class="enscript-keyword">in</span> cmds:
			<span class="enscript-keyword">if</span> len(cmd.strip())&gt;0:
				self.cmd(cmd)
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">ok</span>(self):
		<span class="enscript-keyword">return</span> len(self.callbacks) == len(self.buffer) == 0


<span class="enscript-keyword">class</span> WiiMyth:
	wii_calibration = False
	wm = None
	ms = None
	wii_calibration = None
	<span class="enscript-comment">#Initialize variables
</span>	reportvals = {<span class="enscript-string">&quot;accel&quot;</span>:cwiid.RPT_ACC, <span class="enscript-string">&quot;button&quot;</span>:cwiid.RPT_BTN, <span class="enscript-string">&quot;ext&quot;</span>:cwiid.RPT_EXT,  <span class="enscript-string">&quot;status&quot;</span>:cwiid.RPT_STATUS}
	report={<span class="enscript-string">&quot;accel&quot;</span>:True, <span class="enscript-string">&quot;button&quot;</span>:True}
	state = {<span class="enscript-string">&quot;acc&quot;</span>:[0, 0, 1]}
	lasttime = 0.0
	laststate = {}
	responsiveness = 0.5
	<span class="enscript-comment">#wii_rel = lambda v, axis: float(v - self.wii_calibration[0][axis]) / (
</span>	<span class="enscript-comment">#	self.wii_calibration[1][axis] - self.wii_calibration[0][axis])
</span>	<span class="enscript-keyword">def</span> <span class="enscript-function-name">wii_rel</span>(self, v, axis):
		<span class="enscript-keyword">return</span> float(v - self.wii_calibration[0][axis]) / (
		self.wii_calibration[1][axis] - self.wii_calibration[0][axis])
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">wmconnect</span>(self):
		<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Please press 1&amp;2 on the wiimote...&quot;</span>
		try:
			self.wm = cwiid.Wiimote()
		<span class="enscript-keyword">except</span>:
			self.wm = None
			<span class="enscript-keyword">if</span> self.ms <span class="enscript-keyword">is</span> <span class="enscript-keyword">not</span> None:
				self.ms.close()
				self.ms = None
			<span class="enscript-keyword">return</span> None
		self.ms = MythSocket(self)
		<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Connected...&quot;</span>
		self.wm.rumble=1
		time.sleep(.2)
		self.wm.rumble=0
		<span class="enscript-comment"># Wiimote calibration data (cache this)
</span>		self.wii_calibration = self.wm.get_acc_cal(cwiid.EXT_NONE)
		<span class="enscript-keyword">return</span> self.wm
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">wmcb</span>(self, messages):
		state = self.state
		<span class="enscript-keyword">for</span> message <span class="enscript-keyword">in</span> messages:
			<span class="enscript-keyword">if</span> message[0] == cwiid.MESG_BTN:
				state[<span class="enscript-string">&quot;buttons&quot;</span>] = message[1]
			<span class="enscript-comment">#elif message[0] == cwiid.MESG_STATUS:
</span>			<span class="enscript-comment">#	print &quot;\nStatus: &quot;, message[1]
</span>			<span class="enscript-keyword">elif</span> message[0] == cwiid.MESG_ERROR:
				<span class="enscript-keyword">if</span> message[1] == cwiid.ERROR_DISCONNECT:
					self.wm = None
					<span class="enscript-keyword">if</span> self.ms <span class="enscript-keyword">is</span> <span class="enscript-keyword">not</span> None:
						self.ms.close()
						self.ms = None
					<span class="enscript-keyword">continue</span>
				<span class="enscript-keyword">else</span>:
					<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;ERROR: &quot;</span>, message[1]
			<span class="enscript-keyword">elif</span> message[0] == cwiid.MESG_ACC:
				state[<span class="enscript-string">&quot;acc&quot;</span>] = message[1]
			<span class="enscript-keyword">else</span>:
				<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Unknown message!&quot;</span>, message
			laststate = self.laststate
			<span class="enscript-keyword">if</span> (<span class="enscript-string">'buttons'</span> <span class="enscript-keyword">in</span> laststate) <span class="enscript-keyword">and</span> (laststate[<span class="enscript-string">'buttons'</span>] &lt;&gt; state[<span class="enscript-string">'buttons'</span>]):
				<span class="enscript-keyword">if</span> laststate[<span class="enscript-string">'buttons'</span>] &amp; cwiid.BTN_B <span class="enscript-keyword">and</span> <span class="enscript-keyword">not</span> state[<span class="enscript-string">'buttons'</span>] &amp; cwiid.BTN_B:
					del state[<span class="enscript-string">'BTN_B'</span>]
					self.ms.cmd(<span class="enscript-string">'play speed normal'</span>)
			<span class="enscript-keyword">if</span> self.ms.ok() <span class="enscript-keyword">and</span> (self.wm <span class="enscript-keyword">is</span> <span class="enscript-keyword">not</span> None) <span class="enscript-keyword">and</span> (state[<span class="enscript-string">&quot;buttons&quot;</span>] &gt; 0) <span class="enscript-keyword">and</span> (time.time() &gt; self.lasttime+self.responsiveness):
				self.lasttime = time.time()
				<span class="enscript-comment"># Stuff that doesn't need roll/etc calculations
</span>				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_HOME:
					self.ms.cmd(<span class="enscript-string">'key escape'</span>)
				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_A:
					self.ms.cmd(<span class="enscript-string">'key enter'</span>)
				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_MINUS:
					self.ms.cmd(<span class="enscript-string">'key d'</span>)
				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_UP:
					self.ms.cmd(<span class="enscript-string">'key up'</span>)
				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_DOWN:
					self.ms.cmd(<span class="enscript-string">'key down'</span>)
				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_LEFT:
					self.ms.cmd(<span class="enscript-string">'key left'</span>)
				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_RIGHT:
					self.ms.cmd(<span class="enscript-string">'key right'</span>)
				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_PLUS:
					self.ms.cmd(<span class="enscript-string">'key p'</span>)
				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_1:
					self.ms.cmd(<span class="enscript-string">'key i'</span>)
				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_2:
					self.ms.cmd(<span class="enscript-string">'key m'</span>)
				<span class="enscript-comment"># Do we need to calculate roll, etc?
</span>				<span class="enscript-comment"># Currently only BTN_B needs this.
</span>				calcAcc = state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_B
				<span class="enscript-keyword">if</span> calcAcc:
					<span class="enscript-comment"># Calculate the roll/etc.
</span>					X = self.wii_rel(state[<span class="enscript-string">&quot;acc&quot;</span>][cwiid.X], cwiid.X)
					Y = self.wii_rel(state[<span class="enscript-string">&quot;acc&quot;</span>][cwiid.Y], cwiid.Y)
					Z = self.wii_rel(state[<span class="enscript-string">&quot;acc&quot;</span>][cwiid.Z], cwiid.Z)
					<span class="enscript-keyword">if</span> (Z==0): Z=0.00000001 <span class="enscript-comment"># Hackishly prevents divide by zeros
</span>					roll = atan(X/Z)
					<span class="enscript-keyword">if</span> (Z &lt;= 0.0):
						<span class="enscript-keyword">if</span> (X&gt;0): roll += 3.14159
						<span class="enscript-keyword">else</span>: roll -= 3.14159
					pitch = atan(Y/Z*cos(roll))
					<span class="enscript-comment">#print &quot;X: %f, Y: %f, Z: %f; R: %f, P: %f; B: %d    \r&quot; % (X, Y, Z, roll, pitch, state[&quot;buttons&quot;]),
</span>					sys.stdout.flush()
				<span class="enscript-keyword">if</span> state[<span class="enscript-string">&quot;buttons&quot;</span>] &amp; cwiid.BTN_B:
					speed = roll/3.14159
					<span class="enscript-keyword">if</span> (speed &gt; 1): speed = 1
					<span class="enscript-keyword">if</span> (speed &lt; -1): speed = -1
					speed = int(speed * 13)
					<span class="enscript-keyword">if</span> abs(speed)&gt;9:
						<span class="enscript-keyword">if</span> speed&gt;0: speed = 9
						<span class="enscript-keyword">else</span>: speed = -9
					state[<span class="enscript-string">'BTN_B'</span>] = speed
					<span class="enscript-keyword">if</span> <span class="enscript-keyword">not</span> <span class="enscript-string">'BTN_B'</span> <span class="enscript-keyword">in</span> laststate:
						<span class="enscript-comment"># # query location
</span>						<span class="enscript-comment"># Playback Recorded 00:04:20 of 00:25:31 1x 30210 2008-09-10T09:18:00 6523 /video/30210_20080910091800.mpg 25
</span>						cmd = <span class="enscript-string">&quot;&quot;</span><span class="enscript-comment">#&quot;play speed normal\n&quot;
</span>						<span class="enscript-keyword">if</span> speed &gt; 0:
							cmd += <span class="enscript-string">&quot;key .\n&quot;</span>
						<span class="enscript-keyword">elif</span> speed &lt; 0:
							cmd += <span class="enscript-string">&quot;key ,\n&quot;</span>
						<span class="enscript-keyword">if</span> speed &lt;&gt; 0:
							cmd += <span class="enscript-string">&quot;key &quot;</span>+str(abs(speed)-1)+<span class="enscript-string">&quot;\n&quot;</span>
						<span class="enscript-comment">#print cmd
</span>					<span class="enscript-keyword">elif</span> laststate[<span class="enscript-string">'BTN_B'</span>]&lt;&gt;speed:
						self.wm.rumble=1
						time.sleep(.05)
						self.wm.rumble=0
						<span class="enscript-keyword">if</span> speed == 0:
							cmd = <span class="enscript-string">&quot;play speed normal&quot;</span>
						<span class="enscript-keyword">elif</span> ((laststate[<span class="enscript-string">'BTN_B'</span>] &gt; 0) <span class="enscript-keyword">and</span> (speed &gt; 0)) <span class="enscript-keyword">or</span> ((laststate[<span class="enscript-string">'BTN_B'</span>] &lt; 0) <span class="enscript-keyword">and</span> (speed &lt; 0)):
							cmd = <span class="enscript-string">&quot;key &quot;</span>+str(abs(speed)-1)+<span class="enscript-string">&quot;\n&quot;</span>
						<span class="enscript-keyword">elif</span> speed&gt;0:
							cmd = <span class="enscript-string">&quot;key .\nkey &quot;</span>+str(abs(speed)-1)+<span class="enscript-string">&quot;\n&quot;</span>
						<span class="enscript-keyword">else</span>:
							cmd = <span class="enscript-string">&quot;key ,\nkey &quot;</span>+str(abs(speed)-1)+<span class="enscript-string">&quot;\n&quot;</span>
					<span class="enscript-keyword">else</span>:
						cmd = None
					<span class="enscript-keyword">if</span> cmd <span class="enscript-keyword">is</span> <span class="enscript-keyword">not</span> None:
						self.ms.raw(cmd)
			self.laststate = state.copy() <span class="enscript-comment">#NOTE TO SELF: REMEMBER .copy() !!!
</span>	<span class="enscript-keyword">def</span> <span class="enscript-function-name">mythLocation</span>(self, data):
		<span class="enscript-comment">#Playback Recorded 00:00:49 of 00:25:31 1x 30210 2008-09-10T09:18:00 1243 /video/30210_20080910091800.mpg 25
</span>		<span class="enscript-comment">#PlaybackBox
</span>		temp = data.split(<span class="enscript-string">&quot; &quot;</span>)
		output = {}
		output[<span class="enscript-string">'mode'</span>] = temp[0]
		<span class="enscript-keyword">if</span> output[<span class="enscript-string">'mode'</span>] == <span class="enscript-string">&quot;Playback&quot;</span>:
			output[<span class="enscript-string">'position'</span>] = temp[2]
			output[<span class="enscript-string">'max'</span>] = temp[4]
		<span class="enscript-keyword">return</span> output
	<span class="enscript-keyword">def</span> <span class="enscript-function-name">main</span>(self):
		<span class="enscript-keyword">while</span> True:
			<span class="enscript-keyword">if</span> self.wm <span class="enscript-keyword">is</span> None:
				<span class="enscript-comment">#Connect wiimote
</span>				self.wmconnect()
				<span class="enscript-keyword">if</span> self.wm:
					<span class="enscript-comment">#Tell Wiimote to display rock sign
</span>					self.wm.led = cwiid.LED1_ON | cwiid.LED4_ON
					self.wm.rpt_mode = sum(self.reportvals[a] <span class="enscript-keyword">for</span> a <span class="enscript-keyword">in</span> self.report <span class="enscript-keyword">if</span> 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)
		<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Exited Safely&quot;</span>

<span class="enscript-comment"># Instantiate our class, and start.
</span>inst = WiiMyth()
inst.main()
</code>
<span style="display: block; text-align: center;">Download this code: <a href="http://www.benjiegillam.com/code/myth_py_wii.r12.py" title="Download the above code as a text file.">/code/myth_py_wii.r12.py</a></span></p>
<p>First, load up mythfrontend. Then run the script using "<strong>python myth_py_wii.r12.py</strong>". 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.</p>
<p>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:</p>
<p>You need (some of and probably more than) the following installed (<a class="zem_slink" title="Ubuntu" rel="wikipedia" href="http://en.wikipedia.org/wiki/Ubuntu">Ubuntu Hardy</a>):</p>
<ul>
<li>GNU/Linux</li>
<li>working bluetooth connectivity (bluetooth keyfobs are really cheap now, and most work out of the box with Hardy)</li>
<li>a Wiimote (duh!)</li>
<li>python-cwiid, libcwiid1, libcwiid1-dev</li>
<li>python (I'm using 2.5)</li>
<li>a working mythfrontend</li>
<li>patience</li>
</ul>
<p>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 <strong>Mythfrontend Main Menu &gt; Utilities/Setup &gt; Setup &gt; General &gt; page 4 &gt; "Enable Network Remote Control interface", "Network Remote Control Port: 6546"</strong></p>
<p>Hopefully thats enough to get you started. I aim to release a video soon to show it in action. One last thing - the controls!</p>
<h3>Controls</h3>
<p>These are liable to change, but for now, here is how they are mapped:</p>
<ul>
<li>Keypad : same as keypad on keyboard</li>
<li>A : Enter (Accept, OK, next, ...)</li>
<li>Minus (-) : d (Delete)</li>
<li>Home : escape (Exit to previous menu/exit mythfrontend)</li>
<li>Plus (+) : p (Play/pause)</li>
<li>1 : Info</li>
<li>2 : Menu</li>
<li>B + twist wiimote : rewind (if twisted to the left) or fastforward (otherwise) with speed dependant on twist amount.</li>
</ul>
<h3>A comment on twisting:</h3>
<p>Point the wii remote at the screen, and twist from the elbow so that it continues to point at the screen.</p>
<p>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.</p>
<p>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.</p>
<p>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!</p>
<h3>Known bugs:</h3>
<p>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.</p>
<p style="text-align: center;"><strong>ENJOY!<br />
(and let me know what you think in the comments)</strong></p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/a8dcc3d0-8dce-4fdc-a906-fe3028caae05/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_c.png?x-id=a8dcc3d0-8dce-4fdc-a906-fe3028caae05" alt="Reblog this post [with Zemanta]"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.benjiegillam.com/2008/09/mythpywii-a-wiimote-interface-to-mythtv-using-python/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
