项目作者: David-Lor

项目描述 :
A very simple Python script for using Raspberry Pi GPIO
高级语言: Python
项目地址: git://github.com/David-Lor/Another-Python-RPi-GPIO-Lib.git
创建时间: 2018-02-20T10:52:36Z
项目社区:https://github.com/David-Lor/Another-Python-RPi-GPIO-Lib

开源协议:

下载


Another-Python-RPi-GPIO-Lib

A very simple Python tool for using Raspberry Pi GPIO inputs/outputs. This has been created for OpenWRT/LEDE, since these systems can’t compile the code required by other GPIO libraries like the common RPi-GPIO.

Pins are initialized as objects. You can read and write digital values, as well as attach some sort of “interrupts” to the read pins (it’s actually a looped polling over the pin value).

Examples

Write

  1. from GPIO import *
  2. p4 = Pin(4, OUTPUT)
  3. p4.write(HIGH)
  4. p4.write(LOW)
  5. p4.write(True)
  6. p4.write(False)
  7. p4.write(1)
  8. p4.write(0)

Read

  1. from GPIO import *
  2. p4 = Pin(4, INPUT)
  3. if p4.read():
  4. print("Pin is HIGH")
  5. else:
  6. print("Pin is LOW")

Interrupt

  1. from GPIO import *
  2. def mydef():
  3. print("Pin changed!")
  4. p4 = Pin(4, INPUT)
  5. int_falling = p4.attach_interrupt(mydef, FALLING)
  6. int_rising = p4.attach_interrupt(mydef, RISING)
  7. int_both = p4.attach_interrupt(mydef, BOTH)
  8. int_falling.start()
  9. #mydef() will be executed when pin changes HIGH -> LOW
  10. int_falling.stop()
  11. int_rising.start()
  12. #mydef() will be executed when pin changes LOW -> HIGH
  13. int_rising.stop()
  14. int_both.start()
  15. #mydef() will be executed when pin changes any state (HIGH -> LOW or LOW -> HIGH)
  16. int_both.stop()

Interrupt with args

Not implemented yet

  1. from GPIO import *
  2. def mydef(edgetype):
  3. print("Pin changed with edgetype:", edgetype)
  4. p4 = Pin(4, INPUT)
  5. int_falling = p4.attach_interrupt(mydef, FALLING, ("Falling (high to low)",))
  6. int_rising = p4.attach_interrupt(mydef, RISING, ("Rising (low to high)",))
  7. int_both = p4.attach_interrupt(mydef, BOTH, ("Both (pin changed value)",))
  8. int_falling.start()
  9. #mydef() will be executed when pin changes HIGH -> LOW
  10. int_falling.stop()
  11. int_rising.start()
  12. #mydef() will be executed when pin changes LOW -> HIGH
  13. int_rising.stop()
  14. int_both.start()
  15. #mydef() will be executed when pin changes any state (HIGH -> LOW or LOW -> HIGH)
  16. int_both.stop()