zoukankan      html  css  js  c++  java
  • 树莓派

    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
    channel        - either board pin number or BCM number depending on which mode is set.
    direction      - IN or OUT
    [pull_up_down] - PUD_OFF (default), PUD_UP or PUD_DOWN
    [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
    [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
    channel - either board pin number or BCM number depending on which mode is set.
    value   - 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
    channel - 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.
    BOARD - Use Raspberry Pi board numbers
    BCM   - Use Broadcom GPIO 00..nn numbers"},
       {"getmode", py_getmode, METH_VARARGS, "Get numbering mode used for channel numbers.
    Returns 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.
    channel      - either board pin number or BCM number depending on which mode is set.
    edge         - RISING, FALLING or BOTH
    [callback]   - A callback function for the event (optional)
    [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
    channel - 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.
    channel - 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()
    channel      - either board pin number or BCM number depending on which mode is set.
    callback     - 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.
    channel      - either board pin number or BCM number depending on which mode is set.
    edge         - RISING, FALLING or BOTH
    [bouncetime] - time allowed between calls to allow for switchbounce
    [timeout]    - timeout in ms"},
       {"gpio_function", py_gpio_function, METH_VARARGS, "Return the current GPIO function (IN, OUT, PWM, SERIAL, I2C, SPI)
    channel - 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}
    };



  • 相关阅读:
    Docker系统知识整理(从安装到熟练操作)
    Dockerfile 文件介绍
    Cmake命令之add_subdirectory介绍
    Cmake实战指南
    cmake的四个命令:add_compile_options、add_definitions、target_compile_definitions、build_command
    cmake:选择编译器及设置编译器选项
    Task异常
    单元测试误区
    网络的核心概念
    java~使用枚举来实现接口的多态
  • 原文地址:https://www.cnblogs.com/feiwatson/p/9478217.html
Copyright © 2011-2022 走看看