Created
January 31, 2015 02:59
-
-
Save mamemomonga/1df143258f3928b943ef to your computer and use it in GitHub Desktop.
Raspberry Pi 直結したボタンが押されたときに反応する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Raspberry Pi | |
# Perl(IO::Poll)でGPIO 22に直結したボタンが押されたときに反応する | |
# プルアップはBCM2835の機能を使う、Device::BCM2835 を使ってこの設定をする | |
# 回路: | |
# [ GPIO 22 ] - [ 押しボタンスイッチ ] - [ GND ] | |
# | |
# プルアップされているので普段は 1 | |
# 押しボタンが押されたときにグランドと繋がるので 0 になる。 | |
# 参考: | |
# Raspberry Pi のGPIOをいじる | |
# http://d.hatena.ne.jp/penkoba/20131006/1381071209 | |
# IO::Poll | |
# https://metacpan.org/pod/IO::Poll | |
# poll - システムコールの説明 - Linux コマンド集 一覧表 | |
# http://kazmax.zpp.jp/cmd/p/poll.2.html | |
use strict; | |
use warnings; | |
use feature 'say'; | |
use IO::Poll qw(POLLPRI); | |
use Device::BCM2835; | |
Device::BCM2835::init() || die $!; | |
my $gpio='22'; | |
system("echo '$gpio' > /sys/class/gpio/export"); | |
system("echo 'in' > /sys/class/gpio/gpio$gpio/direction"); | |
system("echo 'falling' > /sys/class/gpio/gpio$gpio/edge"); | |
Device::BCM2835::gpio_set_pud($gpio,Device::BCM2835::BCM2835_GPIO_PUD_UP); | |
# CTRL+Cが押されたときの挙動 | |
$SIG{INT}=sub { | |
system("echo '$gpio' > /sys/class/gpio/unexport"); | |
say 'interrupt.'; | |
exit(1); | |
}; | |
open(my $fh,'<','/sys/class/gpio/gpio'.$gpio.'/value'); | |
my $poll=IO::Poll->new(); | |
$poll->mask($fh => POLLPRI); | |
<$fh>; | |
while(1) { | |
seek($fh,0,0); | |
$poll->poll(); | |
chomp( my $value=<$fh>); | |
say "GPIO $gpio: $value"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment