rpi_gpio 是一个 Dart 库,可在 Linux 系统上直接访问 Raspberry Pi 的 GPIO 引脚。它允许开发者控制数字输入和输出,从而与传感器、LED 和电机等物理硬件进行交互。该库专为物联网应用设计,为在 Raspberry Pi 上运行的 Flutter 或独立 Dart 项目提供了一种简单直观的 GPIO 功能管理接口。该包与 Raspberry Pi 运行的 Linux 环境无缝集成,非常适合嵌入式和实时硬件控制场景。
适合场景
控制LED和继电器
从GPIO读取传感器数据
构建家庭自动化系统
创建交互式物联网设备
开发机器人接口
主要特点
直接访问 GPIO 引脚
仅限 Linux 的兼容性
输入/输出控制的简单 API
支持基于中断的事件
轻量且依赖极少
适合对象
Raspberry Pi 开发者
物联网爱好者
硬件交互式 Flutter 应用
嵌入式系统项目
实时设备控制
使用建议
仅适用于运行 Linux 的树莓派
某些操作需要根权限
仅限 Linux 平台
不支持其他微控制器
可能需要额外设置GPIO权限
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
rpi_gpio.dart
rpi_gpio is a Dart package for accessing the Raspberry Pi GPIO pins.
Overview
Gpio provides the API
for accessing the various General Purpose I/O pins on the Raspberry Pi.
RpiGpio provides the implementation
for the Gpio API via the native libgpiod.so.2 library.
Setup
Installing libgpiod (should already be installed)
RpiGpio accesses the GPIO pins using the native libgpiod.so.2 library.
If you are running the latest Raspberry Pi OS (formerly Raspbian),
then this library should already be installed by default on your Raspberry Pi.
If you are unsure, see #2 below in the troubleshooting section.
If it is not already installed, open a terminal and run
Blinking an LED along with software based PWM on a second LED.
Responding to a button press by turning on an LED.
The example is structured such that the example test
can inject a mock gpio to facilitate testing and allow test execution on platforms
other than the Raspberry Pi.
Testing
Latest Raspberry Pi OS (2024-05-29)
Raspberry Pi Hardware
Dart version
Pi 5 Model B Rev 1.0
3.3.4 (stable) (Tue Apr 16 19:56:12 2024 +0000) on "linux_arm64"
Pi 3 Model B Rev 1.2
3.3.4 (stable) (Tue Apr 16 19:56:12 2024 +0000) on "linux_arm64"
Pi 2 Model B Rev 1.1
3.3.4 - 32 bit
Pi 1 Model B Rev 2
unsupported
Troubleshooting
Run test/src/native/show_hardware_and_os.dart and record the output
Run test/src/native/show_lib_version.dart to see if the libgpiod.so.2 library can be located
If running test/src/native/blink_led.dart crashes then there may be a 32 bit / 64 bit mismatch. Check that the board, the OS, and the Dart SDK are all either 32 bit or 64 bit.
If running test/src/native/blink_led.dart doesn't crash, but doesn't blink the LED
(where the LED and resistor are connected in series to GPIO 17 (pin 11) and ground) then
sudo apt-get install libgpiod-dev
gpioinfo
and and check to see that the gpiochip# listing all of the GPIO#
(for example "gpiochip4" below)