<?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>Programming Archives - G00</title>
	<atom:link href="https://g00.eu/category/prog/feed/" rel="self" type="application/rss+xml" />
	<link>https://g00.eu/category/prog/</link>
	<description>Rapid Prototyping</description>
	<lastBuildDate>Wed, 27 May 2020 11:28:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.1</generator>

<image>
	<url>https://g00.eu/wp-content/uploads/2016/06/cropped-text4185-150x150.png</url>
	<title>Programming Archives - G00</title>
	<link>https://g00.eu/category/prog/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Using Pythonista to query an Xbox controller on the iPad</title>
		<link>https://g00.eu/2020/05/27/using-pythonista-to-query-an-xbox-controller-on-the-ipad/</link>
					<comments>https://g00.eu/2020/05/27/using-pythonista-to-query-an-xbox-controller-on-the-ipad/#respond</comments>
		
		<dc:creator><![CDATA[istimat]]></dc:creator>
		<pubDate>Wed, 27 May 2020 11:28:42 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[controller_changed]]></category>
		<category><![CDATA[game controller]]></category>
		<category><![CDATA[get_controllers()]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[pythonista]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[xbox]]></category>
		<guid isPermaLink="false">http://g00.eu/?p=664</guid>

					<description><![CDATA[<p>Example code for using Pythonista to read button states of the Xbox controller.</p>
<p>The post <a rel="nofollow" href="https://g00.eu/2020/05/27/using-pythonista-to-query-an-xbox-controller-on-the-ipad/">Using Pythonista to query an Xbox controller on the iPad</a> appeared first on <a rel="nofollow" href="https://g00.eu">G00</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>It seems I need to add another category of posts. Programming. Now, you need to take into account the fact that I am a mechanical engineer and have no serious programming experience or education for that matter, and have only done scripting in Python and Arduino code for my electronics projects. Never take anything I write under this category seriously.</p>



<p>Nevertheless, I had the idea of using the iPad as a remote control for a future robotics project. Having Pythonista installed and knowing that the Xbox controller is supported by iOS, I thought I could research if it has been done before, and how.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="1024" height="768" src="http://g00.eu/wp-content/uploads/2020/05/CB262CB6-8469-43D9-9D44-87A789A87B2D-1024x768.jpeg" alt="" class="wp-image-667" srcset="https://g00.eu/wp-content/uploads/2020/05/CB262CB6-8469-43D9-9D44-87A789A87B2D-1024x768.jpeg 1024w, https://g00.eu/wp-content/uploads/2020/05/CB262CB6-8469-43D9-9D44-87A789A87B2D-300x225.jpeg 300w, https://g00.eu/wp-content/uploads/2020/05/CB262CB6-8469-43D9-9D44-87A789A87B2D-768x576.jpeg 768w, https://g00.eu/wp-content/uploads/2020/05/CB262CB6-8469-43D9-9D44-87A789A87B2D-1536x1152.jpeg 1536w, https://g00.eu/wp-content/uploads/2020/05/CB262CB6-8469-43D9-9D44-87A789A87B2D-2048x1536.jpeg 2048w, https://g00.eu/wp-content/uploads/2020/05/CB262CB6-8469-43D9-9D44-87A789A87B2D-520x390.jpeg 520w, https://g00.eu/wp-content/uploads/2020/05/CB262CB6-8469-43D9-9D44-87A789A87B2D-740x555.jpeg 740w, https://g00.eu/wp-content/uploads/2020/05/CB262CB6-8469-43D9-9D44-87A789A87B2D-600x450.jpeg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>It seemed it could be done, as outlined in the Pythonista documentation, but there were no examples available and I have trouble reading documentations as I have a very limited OOP backgound and I probably only wrote like two classes in my entire life. </p>



<p>After a couple of days of messing around, I managed to create an extremely simple example that reads the value of each button when its state changes. My example only uses one button (the left trigger) and can be easily extended to all the other buttons.</p>



<p>No matter how much I googled for an example, I couldn&#8217;t find one, so I thought I would publish mine here.</p>



<pre class="wp-block-code"><code>from scene import *

class XboxContr(Scene):
	
	def setup(self):
		self.score_label = LabelNode('0', font=('Avenir Next', 40), position=(self.size.w/2, self.size.h-50), parent=self)
		self.controller_val = 0
		self.changed_key = ''
		self.left_bar = SpriteNode('shp:RoundRect')
		self.left_bar.position = 150, 150
		self.left_bar.size = 50, 150
		self.add_child(self.left_bar)
		
	def update(self):
		self.update_text()
		self.update_bar()
	
	def controller_changed(self, controller, key, value):
		self.controller_val = value
		self.changed_key = key
		
	def update_text(self):
		self.score_label.text = self.changed_key + ' ' + str(self.controller_val)
		
	def update_bar(self):
		if self.changed_key == 'trigger_left':
			self.left_bar.size = 50, self.controller_val*1000 + 150
			self.left_bar.position = 150, self.controller_val*500 + 150
				
run(XboxContr())


#Here is a list of all the buttons and their values. This is returned by the get_controllers() method:
#&#91;{'controller_id': '0x282afca80', 'shoulder_left': 0.0, 'shoulder_right': 0.0, 'dpad': Point(0.00, 0.00), 'button_a': 0.0, 'button_b': 0.0, 'button_x': 0.0, 'button_y': 0.0, 'trigger_left': 0.0, 'trigger_right': 0.0, 'thumb_stick_left': Point(0.00, 0.00), 'thumb_stick_right': Point(0.00, 0.00)}]
﻿</code></pre>



<p>Here is the interface it generates. The upper text block changes in real time and shows what button is changing and what value it has. The lower left bar increases in height according to how hard you pull on the left trigger.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="1024" height="768" src="http://g00.eu/wp-content/uploads/2020/05/57AF49DB-5513-4ACB-8A83-4F6E40B086FF-1024x768.png" alt="" class="wp-image-668" srcset="https://g00.eu/wp-content/uploads/2020/05/57AF49DB-5513-4ACB-8A83-4F6E40B086FF-1024x768.png 1024w, https://g00.eu/wp-content/uploads/2020/05/57AF49DB-5513-4ACB-8A83-4F6E40B086FF-300x225.png 300w, https://g00.eu/wp-content/uploads/2020/05/57AF49DB-5513-4ACB-8A83-4F6E40B086FF-768x576.png 768w, https://g00.eu/wp-content/uploads/2020/05/57AF49DB-5513-4ACB-8A83-4F6E40B086FF-1536x1152.png 1536w, https://g00.eu/wp-content/uploads/2020/05/57AF49DB-5513-4ACB-8A83-4F6E40B086FF-520x390.png 520w, https://g00.eu/wp-content/uploads/2020/05/57AF49DB-5513-4ACB-8A83-4F6E40B086FF-740x555.png 740w, https://g00.eu/wp-content/uploads/2020/05/57AF49DB-5513-4ACB-8A83-4F6E40B086FF-600x450.png 600w, https://g00.eu/wp-content/uploads/2020/05/57AF49DB-5513-4ACB-8A83-4F6E40B086FF.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>I would love to see more finalized interfaces from you guys. Also, let me know if I made some dumb-ass mistakes in the code.</p>
<p>The post <a rel="nofollow" href="https://g00.eu/2020/05/27/using-pythonista-to-query-an-xbox-controller-on-the-ipad/">Using Pythonista to query an Xbox controller on the iPad</a> appeared first on <a rel="nofollow" href="https://g00.eu">G00</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://g00.eu/2020/05/27/using-pythonista-to-query-an-xbox-controller-on-the-ipad/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
