LoginSignup
25

More than 5 years have passed since last update.

Raspberry Pi + Ruby で GPIO

Last updated at Posted at 2014-04-12

Raspberry Pi の上で、Ruby から GPIO をいじるのに、pi_piper を使おうと思ったら、依存関係の ffi gem がネイティブパッケージなのでコンパイルをしようとし始めます。コンパイル環境メンドクセなのでバイナリでどうにかする方法。

debianパッケージでffiをインストール

ちょっとバージョンが古いけど、dpkgにバイナリがあるので apt-get でインストールします。

  • rubyのバージョン確認

1.8 か 1.9 かでパスが変わるので確認。

$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [arm-linux-eabihf]

この場合は 1.9 系ですね。

  • パッケージのインストール

$ sudo apt-get install ruby-ffi

  • gemに登録

パッケージでやってくれても良さそうなんだけど、手動です。

$ cd /var/lib/gems/1.9.1/specifications/
$ sudo ln -s /usr/share/rubygems-integration/1.9.1/specifications/ffi-1.0.11.gemspec .

1.8系はパスを読み替えてください。

  • 登録されたか確認
$ gem list

*** LOCAL GEMS ***

ffi (1.0.11)

pi_piper をインストール

これはそのまま。ただし、rdocとか言いだすとメンドウなので省略。

$ sudo gem install pi_piper --no-rdoc --no-ri

あとはよしなに...

たとえば irb で使えます

# irb
irb(main):001:0> require 'pi_piper'
=> true
irb(main):002:0> pin_led = PiPiper::Pin.new :pin => 22, :direction => :out
=> #<PiPiper::Pin:0x20a9780 @pin=22, @direction=:out, @invert=false, @trigger=:both, @last_value=nil, @value=0>
irb(main):003:0> loop do
irb(main):004:1* pin_led.on
irb(main):005:1> sleep 1
irb(main):006:1> pin_led.off
irb(main):007:1> sleep 1
irb(main):008:1> end
chikachika.rb
# irb
require 'pi_piper'
pin_led = PiPiper::Pin.new :pin => 22, :direction => :out
loop do
  pin_led.on
  sleep 1
  pin_led.off
  sleep 1
end

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25