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)