一个旨在为初学者抽象出使用gpio和picamera的复杂性的模块。
easyaspi的Python项目详细描述
easyaspi
这个项目的目的是要消除处理GPIO和PICROMAA的复杂程度,因为初级程序员与树莓Pi合作。这个项目最初是为了在我自己教的课程中使用而开发的,但我希望它也能为其他人找到用处。
项目主页
开始
这假设您有一个覆盆子pi(和一个picamera使用该功能)。所有代码都在Raspberry PI Model 3、Picamera Model 2上测试,运行Raspbian 2018-06-29。
安装
pip install easyaspi
用法
目前,该项目支持LED、按钮、超声波和Picamera。随着需求的增加,这个工具列表预计会不断增加。
注意:此库将默认GPIO管脚布局模式设置为bcm。
发光二极管
- 创建LED对象
importeasyaspiled=easyaspi.LED(pin_number)
- 打开LED
led.set(True)
- 关闭LED
led.set(False)
按钮
- 创建按钮对象
importeasyaspibutton=easyaspi.Button(pin_number)
- 如果按下按钮,则检索
button_state=button.get()
- 为按键设置事件回调
# Note: The function definition for the callback requires the state parameter even though it should always be set to True to indicate the button is currently pressed.defwas_pressed(state):print("Button was pressed!")button.on_press(was_pressed)
- 删除事件回调
button.remove_on_press()# Remove any event listener for this button
超声波
- 创建超声波变量
importeasyaspiultra=easyaspi.Ultrasonic(trigger_pin_number,echo_pin_number)
- 以厘米为单位检索距离
distance=ultra.get_distance()
皮卡梅拉
- 创建相机对象
importeasyaspicamera=easyaspi.Camera()
- 拍照(无需留言)
camera.photo("myphoto.png")
- 拍照(带信息)
camera.photo("myphoto.png","my message")
- 开始录像
camera.record("myvideo.h264","my message")
- 检查摄像头是否正在录制视频
recording_state=camera.recording# Returns True or False
- 停止录像
camera.stop()
- 完成后关闭“相机预览”窗口
camera.preview(False)
作者
许可证
麻省理工学院许可证(C)2018 Paul Baumgarten