RPi.GPIO是通过Python/C API实现的,C代码操作底层寄存器, python通过Python/C API调用这些C接口。

这是关于RPi.GPIO项目的介绍。 其中提到了有python 垃圾回收机制,并不适合于实时要求的应用。

https://pypi.org/project/RPi.GPIO/

This package provides a class to control the GPIO on a Raspberry Pi.

Note that this module is unsuitable for real-time or timing critical applications. This is because you can not predict when Python will be busy garbage collecting. It also runs under the Linux kernel which is not suitable for real time applications - it is multitasking O/S and another process may be given priority over the CPU, causing jitter in your program.

关于Python/C API,有点类似于JNI,PyObject  作为common object,用于参数和return

https://docs.python.org/3/c-api/intro.html

https://docs.python.org/3/c-api/structures.html#c.PyObjectPython

在树莓派中之前编译的系统,python, 以及RPi.GPIO module都已经预装了,

pi@raspberrypi:~/python $ python led.py 

执行python led.py脚本,LED闪烁。

#!/usr/bin/python
# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import time LED = 26 GPIO.setmode(GPIO.BCM)
GPIO.setup(LED,GPIO.OUT)
try:
while True:
GPIO.output(LED,GPIO.HIGH)
time.sleep(1)
GPIO.output(LED,GPIO.LOW)
time.sleep(1)
except:
print("except")
GPIO.cleanup()

下面是RPi.GPIO中 setmode的C代码

// python function setmode(mode)
static PyObject *py_setmode(PyObject *self, PyObject *args)
{
int new_mode; if (!PyArg_ParseTuple(args, "i", &new_mode))
return NULL; if (gpio_mode != MODE_UNKNOWN && new_mode != gpio_mode)
{
PyErr_SetString(PyExc_ValueError, "A different mode has already been set!");
return NULL;
} if (setup_error)
{
PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!");
return NULL;
} if (new_mode != BOARD && new_mode != BCM)
{
PyErr_SetString(PyExc_ValueError, "An invalid mode was passed to setmode()");
return NULL;
} if (rpiinfo.p1_revision == 0 && new_mode == BOARD)
{
PyErr_SetString(PyExc_RuntimeError, "BOARD numbering system not applicable on compute module");
return NULL;
} gpio_mode = new_mode;
Py_RETURN_NONE;
}

PyMethodDef rpi_gpio_methods[] = {
{"setup", (PyCFunction)py_setup_channel, METH_VARARGS | METH_KEYWORDS, "Set up a GPIO channel or list of channels with a direction and (optional) pull/up down control\nchannel - either board pin number or BCM number depending on which mode is set.\ndirection - IN or OUT\n[pull_up_down] - PUD_OFF (default), PUD_UP or PUD_DOWN\n[initial] - Initial value for an output channel"},
{"cleanup", (PyCFunction)py_cleanup, METH_VARARGS | METH_KEYWORDS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection\n[channel] - individual channel or list/tuple of channels to clean up. Default - clean every channel that has been used."},
{"output", py_output_gpio, METH_VARARGS, "Output to a GPIO channel or list of channels\nchannel - either board pin number or BCM number depending on which mode is set.\nvalue - 0/1 or False/True or LOW/HIGH"},
{"input", py_input_gpio, METH_VARARGS, "Input from a GPIO channel. Returns HIGH=1=True or LOW=0=False\nchannel - either board pin number or BCM number depending on which mode is set."},
{"setmode", py_setmode, METH_VARARGS, "Set up numbering mode to use for channels.\nBOARD - Use Raspberry Pi board numbers\nBCM - Use Broadcom GPIO 00..nn numbers"},
{"getmode", py_getmode, METH_VARARGS, "Get numbering mode used for channel numbers.\nReturns BOARD, BCM or None"},
{"add_event_detect", (PyCFunction)py_add_event_detect, METH_VARARGS | METH_KEYWORDS, "Enable edge detection events for a particular GPIO channel.\nchannel - either board pin number or BCM number depending on which mode is set.\nedge - RISING, FALLING or BOTH\n[callback] - A callback function for the event (optional)\n[bouncetime] - Switch bounce timeout in ms for callback"},
{"remove_event_detect", py_remove_event_detect, METH_VARARGS, "Remove edge detection for a particular GPIO channel\nchannel - either board pin number or BCM number depending on which mode is set."},
{"event_detected", py_event_detected, METH_VARARGS, "Returns True if an edge has occured on a given GPIO. You need to enable edge detection using add_event_detect() first.\nchannel - either board pin number or BCM number depending on which mode is set."},
{"add_event_callback", (PyCFunction)py_add_event_callback, METH_VARARGS | METH_KEYWORDS, "Add a callback for an event already defined using add_event_detect()\nchannel - either board pin number or BCM number depending on which mode is set.\ncallback - a callback function"},
{"wait_for_edge", (PyCFunction)py_wait_for_edge, METH_VARARGS | METH_KEYWORDS, "Wait for an edge. Returns the channel number or None on timeout.\nchannel - either board pin number or BCM number depending on which mode is set.\nedge - RISING, FALLING or BOTH\n[bouncetime] - time allowed between calls to allow for switchbounce\n[timeout] - timeout in ms"},
{"gpio_function", py_gpio_function, METH_VARARGS, "Return the current GPIO function (IN, OUT, PWM, SERIAL, I2C, SPI)\nchannel - either board pin number or BCM number depending on which mode is set."},
{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"},
{NULL, NULL, 0, NULL}
};

树莓派 - RPi.GPIO的更多相关文章

  1. 树莓派RPi.GPIO+Flask构建WebApi实现远程控制

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- import RPi.GPIO as GPIO from flask import Flask, requ ...

  2. 树莓派高级GPIO库,wiringpi2 for python使用笔记(一)安装

    网上的教程,一般Python用RPi.GPIO来控制树莓派的GPIO,而C/C++一般用wringpi库来操作GPIO,RPi.GPIO过于简单,很多高级功能不支持,比如i2c/SPI库等,也缺乏高精 ...

  3. 【玩转开源】BananaPi R2——移植RPi.GPIO 到 R2

    1. 首先给大家介绍一下什么是RPi.GPIO. 简单去讲,RPi.GPIO就是一个运行在树莓派开发板上可以通过Python去控制GPIO的一个中间件. 现在我这边做了一个基础功能的移植,接下来大家可 ...

  4. 关于RPi.GPIO、BCM2835 c library、WiringPi、Gertboard

    1.RPi.GPIO//RPi.GPIO-0.5.5.tar.gz 开发者:python官网:https://www.python.org/ 官网:https://pypi.python.org/py ...

  5. nanopi NEO2 学习笔记 3:python 安装 RPi.GPIO

    如果我要用python控制NEO2的各种引脚,i2c 或 spi ,RPi.GPIO模块是个非常好的选择 这个第三方模块是来自树莓派的,好像友善之臂的工程师稍作修改移植到了NEO2上,就放在 /roo ...

  6. RPi.GPIO 和 HM

    后续笔记不再记录导入的模块和硬件的连接方法,请根据关键词自行搜索. RPi.GPIO模块 GPIO:General Purpose Input Output 即 通用输入/输出 RPi.GPIO是一个 ...

  7. 树莓派的GPIO编程

    作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁转载. 树莓派除了提供常见的网口和USB接口 ,还提供了一组GPIO(General Purpose Input/ ...

  8. 树莓派控制GPIO(Python)

    如果你的raspi没有安装python那么先   sudo apt-get update sudo apt-get install python-dev   例如想要控制35管脚的亮灭: 先建一个文本 ...

  9. 树莓派高级GPIO库,wiringpi2 for python使用笔记(四)实战DHT11解码

    DHT11是一款有已校准数字信号输出的温湿度传感器. 精度湿度+-5%RH, 温度+-2℃,量程湿度20-90%RH, 温度0~50℃. 我买的封装好的模块,上边自带了上拉电阻,直接查到树莓派上即可灰 ...

随机推荐

  1. 【插件开发】—— 5 SWT控件以及布局使用

    前文回顾: 1 插件学习篇 2 简单的建立插件工程以及模型文件分析 3 利用扩展点,开发透视图 4 SWT编程须知 经过前几篇的介绍,多少对SWT又有了一些认识,那么这篇继续来看一下一些控件的组合使用 ...

  2. 安装privoxy后curl 操作

    如果讲privoxy服务关掉 sudo /etc/init.d/privoxy restart 这时候发现 curl www.baidu.com 也没法正常工作,直接显示的是无法连接到相应端口还是 主 ...

  3. 【原创】《从0开始学Elasticsearch》—集群健康和索引管理

    内容目录 1.搭建Kibana2.集群健康3.索引操作 1.搭建Kibana 正如<Kibana 用户手册>中所介绍,Kibana 是一款开源的数据分析和可视化平台,因此我们可以借助 Ki ...

  4. 实现简单版的LinkedList

    相比ArrayList,双链表的数据结构就复杂多了,想要弄清代码的意思还是要搞清数据结构层面的变化. package cn.sp.chapter03; import java.util.Concurr ...

  5. Hdu 5379 Mahjong tree (dfs + 组合数)

    题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号 ...

  6. _bzoj1059 [ZJOI2007]矩阵游戏【二分图匹配】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1059 保存匈牙利模板. #include <cstdio> #include & ...

  7. clock()函数的返回值精度问题

    clock()函数返回值为1毫秒,就是0.001秒.clock函数功 能: 返回处理器调用某个进程或函数所花费的时间.用 法: clock_t clock(void);说明:clock_t其实就是lo ...

  8. 贪心+枚举/哈希表 HDOJ Trouble

    题目传送门 题意:5个集合,每个集合最多200个数字,问是否每个集合挑一个数加起来和为0. 分析:显然n^5的程序果断超时,甚至n^3logn的二分也过不了.想n^3的方法,既然判断有没有,那么可以将 ...

  9. 题解报告:hdu 1062 Text Reverse

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 Problem Description Ignatius likes to write word ...

  10. 2、IO流的分类和IO流体系