Difference between revisions of "RPi.GPIO : NanoPi NEO/NEO2/Air GPIO Programming with Python"
From FriendlyELEC WiKi
(→RPi.GPIO_NP简介) |
(→支持RPi.GPIO_NP的开发板型号及固件列表) |
||
Line 22: | Line 22: | ||
|NanoPi NEO Air||nanopi-neo-air_ubuntu-core-xenial_4.11.0_YYYYMMDD.img.zip||[https://pan.baidu.com/s/1kUMRucf 百度盘]||[https://www.mediafire.com/folder/sr5d0qpz774cs/NanoPi-NEO_Air MediaFire] | |NanoPi NEO Air||nanopi-neo-air_ubuntu-core-xenial_4.11.0_YYYYMMDD.img.zip||[https://pan.baidu.com/s/1kUMRucf 百度盘]||[https://www.mediafire.com/folder/sr5d0qpz774cs/NanoPi-NEO_Air MediaFire] | ||
|} | |} | ||
+ | |||
+ | ==RPi.GPIO_NP示例== | ||
+ | 以NanoPi NEO2为例,将一个[[Matrix - LED]]通过连接至NanoPi NEO2,像下图这样:<br /> | ||
+ | [[File:WiringNP-LED-Demo.jpg|frameless|300px|WiringNP-LED-Demo]]<br /> | ||
+ | 其中,引脚的连接对应如下:<br /> | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | |Matrix-LED || NanoPi M1 | ||
+ | |- | ||
+ | |S || Pin7 | ||
+ | |- | ||
+ | |V || Pin4 | ||
+ | |- | ||
+ | |G || Pin6 | ||
+ | |} | ||
+ | |||
+ | 接下来我们用Python实现一个LED闪烁的例子, <br /> | ||
+ | 在代码中使用数字7来操作Pin7这个引脚,即引脚的编号直接使用物理编号: <br /> | ||
+ | |||
+ | === Python语言示例 === | ||
+ | 创建一个Python源文件: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | vi led.py | ||
+ | </syntaxhighlight> | ||
+ | 然后键入如下代码: | ||
+ | <syntaxhighlight lang="python"> | ||
+ | #!/usr/bin/env python | ||
+ | import RPi.GPIO as GPIO | ||
+ | import time | ||
+ | PIN_NUM = 7 | ||
+ | |||
+ | GPIO.setmode(GPIO.BOARD) | ||
+ | GPIO.setup(PIN_NUM,GPIO.OUT) | ||
+ | while True: | ||
+ | GPIO.output(PIN_NUM,True) | ||
+ | time.sleep(1) | ||
+ | GPIO.output(PIN_NUM,False) | ||
+ | time.sleep(1) | ||
+ | </syntaxhighlight> | ||
+ | 运行led.py: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | chmod +x led.py | ||
+ | sudo ./led.py | ||
+ | </syntaxhighlight> | ||
+ | 看到LED灯一闪一闪的,就表示成功了。 |
Revision as of 07:13, 5 June 2017
1 RPi.GPIO_NP简介
为了更方便地用python来控制GPIO,NanoPi NEO/NEO2的Ubuntu core系统中已经预装了RPi.GPIO。
RPi.GPIO是 RaspberryPi 知名的python类库,其作用主要是用来控制GPIO,我们将它移植到了 NanoPi NEO/NEO2上,并给它起了一个名字叫:RPi.GPIO_NP。
RPi.GPIO_NP的接口基本遵循原作RPi.GPIO,所以你可以参考RPi.GPIO的文档:https://pypi.python.org/pypi/RPi.GPIO。
2 RPi.GPIO_NP安装
RPi.GPIO_NP已经预装在 2017/06/05 之后的 Ubuntu core系统中,无需安装。
3 支持RPi.GPIO_NP的开发板型号及固件列表
下表中列出了当前已支持RPi.GPIO_NP的开发板型号,以及对应的固件文件,固件文件位于下载链接的officail-ROMs目录:
4 RPi.GPIO_NP示例
以NanoPi NEO2为例,将一个Matrix - LED通过连接至NanoPi NEO2,像下图这样:
其中,引脚的连接对应如下:
Matrix-LED | NanoPi M1 |
S | Pin7 |
V | Pin4 |
G | Pin6 |
接下来我们用Python实现一个LED闪烁的例子,
在代码中使用数字7来操作Pin7这个引脚,即引脚的编号直接使用物理编号:
4.1 Python语言示例
创建一个Python源文件:
vi led.py
然后键入如下代码:
#!/usr/bin/env python import RPi.GPIO as GPIO import time PIN_NUM = 7 GPIO.setmode(GPIO.BOARD) GPIO.setup(PIN_NUM,GPIO.OUT) while True: GPIO.output(PIN_NUM,True) time.sleep(1) GPIO.output(PIN_NUM,False) time.sleep(1)
运行led.py:
chmod +x led.py sudo ./led.py
看到LED灯一闪一闪的,就表示成功了。