<?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>GPIO &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/gpio/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 27 Apr 2020 17:37:17 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] 树莓派：这个世界是你们的，是我们的，但终究是Python的！</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e6%a0%91%e8%8e%93%e6%b4%be%ef%bc%9a%e8%bf%99%e4%b8%aa%e4%b8%96%e7%95%8c%e6%98%af%e4%bd%a0%e4%bb%ac%e7%9a%84%ef%bc%8c%e6%98%af%e6%88%91%e4%bb%ac%e7%9a%84%ef%bc%8c%e4%bd%86/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e6%a0%91%e8%8e%93%e6%b4%be%ef%bc%9a%e8%bf%99%e4%b8%aa%e4%b8%96%e7%95%8c%e6%98%af%e4%bd%a0%e4%bb%ac%e7%9a%84%ef%bc%8c%e6%98%af%e6%88%91%e4%bb%ac%e7%9a%84%ef%bc%8c%e4%bd%86/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sat, 30 Nov 2019 18:13:05 +0000</pubDate>
				<category><![CDATA[Raspberry Pi/树莓派]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[GPIO]]></category>
		<category><![CDATA[pigpio]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[树莓派]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=11141</guid>

					<description><![CDATA[<p>
要查看更多树莓派相关文章，请点击<a href="https://www.codelast.com/?p=5013" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>很多年前我刚开始玩树莓派的时候，Python还是个&#8220;很不火&#8221;的编程语言，而如今Python借着深度学习(Deep Learning)的东风，已经毫无疑问地成了世界上最耀眼的明日之星；想当年我刚开始玩树莓派的时候，使用<a href="http://wiringpi.com/" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">WiringPi</span></a>开发库、在C++下开发GPIO应用是一个比较明智的选择，而如今树莓派上的Pyhton GPIO库已经百花齐放，并且多个树莓派的OS都已经内置了一或多个Python GPIO开发包，真正做到了：你只要装好系统，就马上能用Python写程序操作GPIO，立等可用。<br />
<span id="more-11141"></span><br />
C++编译一个简单的GPIO程序都要写Makefile，修改一点东西也得重新编译；而Python直接在交互模式下就可以测试GPIO，方便得让你想哭。相比之下，对一般的GPIO应用而言，我真的找不到什么理由再去用C++ &#38; WiringPi来做。此外还有最重要的一点：树莓派在教育领域的广泛应用，使得Python成为了最合适的教育工具。儿童是祖国的花朵，如果让小孩子们在树莓派上用C++，这花朵还能好好成长吗？<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
下面就来看看，在Ubuntu MATE 18.04 系统下，用Python来开发一个GPIO应用有多么简单。</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);">▶▶</span></span>&#160;选择哪个GPIO库<br />
之前我已经<a href="https://www.codelast.com/?p=11133" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">装好了Miniconda环境</span></a>，而知名Python GPIO库 RPi.GPIO 在Anaconda上没有Python 3.6的版本，因此我安装了另一个<a href="http://abyz.me.uk/rpi/pigpio/index.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">pigpio</span></a>库：</p>
<blockquote>
<div>
		conda install -c cbetters pigpio</div>
</blockquote>
<div>
	如果你装的是Raspbian系统，那么它已经自带了&#160;RPi.GPIO，你啥都不需要做就可以开始上台表演了。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);">▶▶</span></span>&#160;启动 pigpio&#160;的daemon进程<br />
	这是基于 pigpio 的Python程序能work的必要条件：</div>
<blockquote>
<div>
		sudo&#160;pigpiod</div>
</blockquote>
<div>
	如果没有启动它，那么调用了 pigpio 的Python程序运行的时候会报错：
<blockquote>
<div>
			Can&#39;t connect to pigpio at localhost(8888)</div>
<div>
			&#160;</div>
<div>
			Did you start the pigpio daemon?</div></blockquote></div>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e6%a0%91%e8%8e%93%e6%b4%be%ef%bc%9a%e8%bf%99%e4%b8%aa%e4%b8%96%e7%95%8c%e6%98%af%e4%bd%a0%e4%bb%ac%e7%9a%84%ef%bc%8c%e6%98%af%e6%88%91%e4%bb%ac%e7%9a%84%ef%bc%8c%e4%bd%86/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
要查看更多树莓派相关文章，请点击<a href="https://www.codelast.com/?p=5013" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>很多年前我刚开始玩树莓派的时候，Python还是个&ldquo;很不火&rdquo;的编程语言，而如今Python借着深度学习(Deep Learning)的东风，已经毫无疑问地成了世界上最耀眼的明日之星；想当年我刚开始玩树莓派的时候，使用<a href="http://wiringpi.com/" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">WiringPi</span></a>开发库、在C++下开发GPIO应用是一个比较明智的选择，而如今树莓派上的Pyhton GPIO库已经百花齐放，并且多个树莓派的OS都已经内置了一或多个Python GPIO开发包，真正做到了：你只要装好系统，就马上能用Python写程序操作GPIO，立等可用。<br />
<span id="more-11141"></span><br />
C++编译一个简单的GPIO程序都要写Makefile，修改一点东西也得重新编译；而Python直接在交互模式下就可以测试GPIO，方便得让你想哭。相比之下，对一般的GPIO应用而言，我真的找不到什么理由再去用C++ &amp; WiringPi来做。此外还有最重要的一点：树莓派在教育领域的广泛应用，使得Python成为了最合适的教育工具。儿童是祖国的花朵，如果让小孩子们在树莓派上用C++，这花朵还能好好成长吗？<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
下面就来看看，在Ubuntu MATE 18.04 系统下，用Python来开发一个GPIO应用有多么简单。</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;选择哪个GPIO库<br />
之前我已经<a href="https://www.codelast.com/?p=11133" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">装好了Miniconda环境</span></a>，而知名Python GPIO库 RPi.GPIO 在Anaconda上没有Python 3.6的版本，因此我安装了另一个<a href="http://abyz.me.uk/rpi/pigpio/index.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">pigpio</span></a>库：</p>
<blockquote>
<div>
		conda install -c cbetters pigpio</div>
</blockquote>
<div>
	如果你装的是Raspbian系统，那么它已经自带了&nbsp;RPi.GPIO，你啥都不需要做就可以开始上台表演了。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;启动 pigpio&nbsp;的daemon进程<br />
	这是基于 pigpio 的Python程序能work的必要条件：</div>
<blockquote>
<div>
		sudo&nbsp;pigpiod</div>
</blockquote>
<div>
	如果没有启动它，那么调用了 pigpio 的Python程序运行的时候会报错：</p>
<blockquote>
<div>
			Can&#39;t connect to pigpio at localhost(8888)</div>
<div>
			&nbsp;</div>
<div>
			Did you start the pigpio daemon? E.g. sudo pigpiod</div>
<div>
			&nbsp;</div>
<div>
			Did you specify the correct Pi host/port in the environment</div>
<div>
			variables PIGPIO_ADDR/PIGPIO_PORT?</div>
<div>
			E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888</div>
<div>
			&nbsp;</div>
<div>
			Did you specify the correct Pi host/port in the</div>
<div>
			pigpio.pi() function? E.g. pigpio.pi(&#39;soft&#39;, 8888)</div>
</blockquote>
<p>	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;树莓派硬件接线<br />
	这里做一个最简单的实验：用树莓派的GPIO口点亮LED。</p>
<div>
		<a href="https://pinout.xyz" rel="noopener noreferrer" target="_blank">https://pinout.xyz</a>&nbsp;这个网站上有树莓派的引脚分配图。这里我使用树莓派的物理编号为&nbsp;<span style="color:#ff0000;">11&nbsp;</span>的GPIO口，也就是Broadcom命名方式下的 <span style="color:#ff0000;">GPIO 17&nbsp;</span>口。<br />
		<img decoding="async" alt="raspberry pi gpio 17" src="https://www.codelast.com/wp-content/uploads/2019/11/rpi_gpio_17.jpg" style="width: 516px; height: 900px;" /><br />
		<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
		<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;Python程序<br />
		程序这么简单我还能说什么：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;pigpio

pi&nbsp;=&nbsp;pigpio.pi()
pi.set_mode(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">17</span>,&nbsp;pigpio.OUTPUT)&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;把GPIO&nbsp;17设置成输出模式</span>
pi.write(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">17</span>,&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1</span>)&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;把GPIO&nbsp;17设置成高电平</span>
</code></pre>
</section>
<p>这代码完全不用做任何解释。<br />
		<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
		<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;效果实拍<br />
		为了兼容性，MP4视频被我转成了体积巨大的gif(我容易吗我)，且看且珍惜：<br />
		<img decoding="async" alt="pigpio control led" src="https://www.codelast.com/wp-content/uploads/2019/11/pigpio_control_led.gif" /><br />
		<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
		<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;总结<br />
		<span style="color:#ff0000;">对树莓派来说：</span><br />
		<span style="background-color:#ffff00;">☞</span> 人生苦短，请你用Python！<br />
		<span style="background-color: rgb(255, 255, 0);">☞</span>&nbsp;在树莓派上折腾GPIO，用Python！<br />
		<span style="background-color: rgb(255, 255, 0);">☞</span>&nbsp;For kids，毫无疑问地选Python！<br />
		<span style="background-color: rgb(255, 255, 0);">☞</span>&nbsp;这个世界是你们的，是我们的，但终究是Python的！</p>
<p>		<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
		<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
		转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
		感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
			<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
</p></div>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e6%a0%91%e8%8e%93%e6%b4%be%ef%bc%9a%e8%bf%99%e4%b8%aa%e4%b8%96%e7%95%8c%e6%98%af%e4%bd%a0%e4%bb%ac%e7%9a%84%ef%bc%8c%e6%98%af%e6%88%91%e4%bb%ac%e7%9a%84%ef%bc%8c%e4%bd%86/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
