python 3电子工作库
pyelectronics的Python项目详细描述
这是一个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)