python 3电子工作库

pyelectronics的Python项目详细描述


https://travis-ci.org/MartijnBraam/pyElectronics.svg?branch=masterhttps://badge.fury.io/py/pyelectronics.svg

这是一个python库,用于使用具有统一接口的电子设备(如i2c或spi设备)。它目前支持 使用i2c内核驱动程序通过树莓pi gpio连接到数据,并使用总线盗版。

支持的网关

网关是linux和硬件之间的桥梁。当前支持:

  • 总线海盗v3
  • Raspberry PI/其他Linux i2c_dev支持的接口

支持的芯片/设备

Read the contributing guide添加设备

  • 博世BMP180数字压力传感器
  • 霍尼韦尔3轴数字罗盘IC HMC583L 六轴(陀螺+加速度计)MEMS器件
  • NXP LM75A数字温度传感器
  • 微芯片MCP23017 16位I/O扩展器
  • 7段显示不复用

安装

从pypi安装:

$ pip3 install pyelectronics

各种网关都有自己的依赖关系。安装所需网关的依赖项:

# Requirements for the Raspberry Pi
$ pip3 install pysmbus

# Requirements for the Bus Pirate
$ pip3 install pyserial

要运行doctests,您需要所有依赖项。您可以使用doctest需求文件安装它们:

$ pip3 install -r docs/doctest-requirements.txt

用法

阅读pythonhosted上的完整文档。

首先创建网关实例:

from electronics.gateways import BusPirate
from electronics.gateways import LinuxDevice

# Use a BusPirate to connect to a bus
gw = BusPirate('/dev/ttyUSB0')

# Use a i2c bus with a linux driver (like the raspberry pi)
gw = LinuxDevice(1) # /dev/i2c-1

为连接到网关的组件创建实例:

from electronics.devices import BMP180
from electronics.devices import MPU6050I2C

barometer = BMP180(gw, address=0x77) # Address is optional
inertia = MPU6050(gw)

# Do chip specific initialisation
barometer.load_calibration()
inertia.wakeup()

从传感器读取值:

temperature = barometer.temperature()
pressure = barometer.pressure()
acceleration = inertia.acceleration()
rotation = inertia.angular_rate()

GPIO示例:

# Red led conected to the aux pin of the bus pirate
red_led = gw.get_aux_pin()
# Turn the led on
red_led.write(True)

# An Microchip port expander connected through I2C
expander = MCP23017I2C(gw, address=0x21)

# Get the pins for the two other leds that are connected
# to the port expander
green_led = expander.get_pin('A0')
blue_led = expander.get_pin('A1')

# Bundle the leds to a bus so you can address 8 colours as an int
# the pins on the port expander are open drain, they are inverted with the invert operator (~)
bus = GPIOBus([red_led, ~green_led, ~blue_led])
bus.write(0) # Black
bus.write(7) # White

# Disco!
while True:
    for i in range(0,8):
        bus.write(i)
        sleep(0.2)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Spring数据JPA+Hibernate在不首先找到父实体的情况下保存子实体   php Java:如何从CLI接收命令   spring为java中的导出数据创建访问文件   java在Windows 8.1上安装Play Framework   java Spring启动白标签错误页面(类型=未找到,状态=404)   java如何在单击时从数组中绘制?   java fn:substringAfter()上次出现   java在IFR语句中使用方法返回   java onPause()或onStop()的名称   对关联对象的关联对象具有条件的java HQL查询   java只打印一次总值,无需迭代   java如何使用抽象Uri buildOn()方法?   如何在Java中执行sudo命令并获得错误输出?   java反射:避免对getConstructor(类<?>…)的未经检查的警告调用作为原始类型类的成员   Java:如何从类中创建的对象调用类方法?   java如何在电子邮件中嵌入图像?   java如何在Android上启用详细GC?   java什么是串行版本id?