RPi.GPIO : NanoPi NEO/NEO2/Air GPIO Programming with Python

From FriendlyELEC WiKi
Revision as of 13:09, 7 June 2017 by Yftan (Talk | contribs) (RPi.GPIO_NP安装)

Jump to: navigation, search

查看中文

1 Introduction to RPi.GPIO_NP

For users to easily access GPIO with python FriendlyElec integrated RPi.GPIO in the UbuntuCore image for the NanoPi NEO/NEO2.
RPi.GPIO is a famous library in python for Raspberry Pi. FriendlyElec ported it to the NanoPi NEO/NEO2's UbuntuCore and renamed it as RPi.GPIO_NP.
Most RPi.GPIO_NP's APIs are the same as those of RPi.GPIO and you can refer to https://pypi.python.org/pypi/RPi.GPIO for more details.

2 Install RPi.GPIO_NP

The RPi.GPIO_NP library is integrated in all the UbuntuCore images that are and will be released after June 5, 2017. Therefore for images released after this date no installation is needed.

3 支持RPi.GPIO_NP的开发板型号及固件列表

下表中列出了当前已支持RPi.GPIO_NP的开发板型号,以及对应的固件文件,固件文件位于下载链接的officail-ROMs目录:

开发板型号 固件文件名 下载地址1 下载地址2
NanoPi NEO2 nanopi-neo2_ubuntu-core-xenial_4.11.0_YYYYMMDD.img.zip 百度盘 MediaFire
NanoPi NEO nanopi-neo_ubuntu-core-xenial_4.11.0_YYYYMMDD.img.zip 百度盘 MediaFire
NanoPi NEO Air nanopi-neo-air_ubuntu-core-xenial_4.11.0_YYYYMMDD.img.zip 百度盘 MediaFire

4 RPi.GPIO_NP示例

以NanoPi NEO2为例,将一个Matrix - LED通过连接至NanoPi NEO2,像下图这样:
WiringNP-LED-Demo
其中,引脚的连接对应如下:

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灯一闪一闪的,就表示成功了。