Skip to content

Instantly share code, notes, and snippets.

@CLCL
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CLCL/bdc0392bc1cad4bac6dc to your computer and use it in GitHub Desktop.
Save CLCL/bdc0392bc1cad4bac6dc to your computer and use it in GitHub Desktop.
Raspberry Pi直結型 LED/ボタン/ブザーアドオンボード MS100A(BerryClip)用Perlアクセスサンプル
#!/usr/bin/env perl
# Raspberry Pi直結型 LED/ボタン/ブザーアドオンボード
# MS100A用Perlアクセスサンプル
# CPANのDevice::BCM2835 が必要で、それをインストールする
# には http://www.airspayce.com/mikem/bcm2835/ から
# bcm2835-1.xx.tar.gz をダウンロードしてビルドする必要が
# あるよ。これを実行するときはroot権限が必要だよ。
# Raspbian にplenv+cpanm+cartonでPerl 5.20.0入れた環境で
# テストしたよ。
use strict;
use warnings;
my $ms = MS100A->new();
while (1) {
# Turn it on
$ms->led(5, 1);
$ms->delay(500); # Milliseconds
$ms->led(4, 1);
$ms->delay(500); # Milliseconds
$ms->led(3, 1);
$ms->delay(500); # Milliseconds
$ms->led(2, 1);
$ms->delay(500); # Milliseconds
$ms->led(1, 1);
$ms->delay(500); # Milliseconds
$ms->led(0, 1);
$ms->beep(1);
$ms->delay(500); # Milliseconds
print "ボタン押されているなう\n" if $ms->button();
# Turn it off
$ms->beep(0);
$ms->led(5, 0);
$ms->delay(500); # Milliseconds
$ms->led(4, 0);
$ms->delay(500); # Milliseconds
$ms->led(3, 0);
$ms->delay(500); # Milliseconds
$ms->led(2, 0);
$ms->delay(500); # Milliseconds
$ms->led(1, 0);
$ms->delay(500); # Milliseconds
$ms->led(0, 0);
$ms->delay(500); # Milliseconds
}
package MS100A;
# メソッド:
# my $ms = MS100A->new() 初期化
# $ms->led( $led_id, $turn ) MS100Aの指定したLED No.(0-5)を点灯:1 消灯:0
# $ms->beep( $turn ) MS100Aブザーを 鳴らす:1 止める:0
# $ms->button() 戻り値 MS100Aのスイッチが押されていなければundef
use Device::BCM2835;
sub new {
my $class = shift;
my %args = ( @_ );
my $self = {};
Device::BCM2835::init() || die "Could not init library";
# アドオンボード | 対応GPIO | D:BCM2835
# MS100A | BCMGPIO | 指定PIN
# -----------------------------------
# LED1 緑 | GPIO 4 | PIN 07
# LED2 緑 | GPIO 17 | PIN 11
# LED3 赤 | GPIO 22 | PIN 15
# LED4 黄 | GPIO 10 | PIN 19
# LED5 赤 | GPIO 9 | PIN 21
# LED6 赤 | GPIO 11 | PIN 23
# ボタン | GPIO 7 | PIN 26
# ブザー | GPIO 8 | PIN 24
my $LED_PINS = [07, 11, 15, 19, 21, 23, ];
my $BUTTON_PIN = 26;
my $BEEP_PIN = 24;
my $OUTP = &Device::BCM2835::BCM2835_GPIO_FSEL_OUTP;
my $INPT = &Device::BCM2835::BCM2835_GPIO_FSEL_INPT;
# LED(6個)の初期化と、LED番号とGPIO指定ピンの紐づけ
my $LEDS = [];
foreach my $pin_no (@$LED_PINS) {
my $gpio=_gpio( pin => $pin_no );
Device::BCM2835::gpio_fsel( $gpio, $OUTP );
push @$LEDS, $gpio;
}
# ブザーの初期化(出力)
Device::BCM2835::gpio_fsel( _gpio( pin => $BEEP_PIN ), $OUTP );
# ボタンの初期化(入力)
Device::BCM2835::gpio_fsel( _gpio( pin => $BUTTON_PIN ), $INPT );
$self->{LED} = $LEDS;
$self->{BEEP} = _gpio( pin => $BEEP_PIN ); # ブザーのピン
$self->{BUTTON} = _gpio( pin => $BUTTON_PIN ); # ボタンのピン
return bless $self, $class;
}
sub _gpio {
my $hash = {@_};
return eval('&Device::BCM2835::RPI_GPIO_P1_' . sprintf "%02d", $hash->{pin});
}
sub led {
my $self = shift;
my $n = shift;
my $t = shift;
Device::BCM2835::gpio_write($self->{LED}->[$n], $t);
}
sub beep {
my $self = shift;
my $t = shift;
Device::BCM2835::gpio_write($self->{BEEP}, $t);
}
sub button {
my $self = shift;
return Device::BCM2835::gpio_lev($self->{BUTTON});
}
sub delay {
my $self = shift;
my $msec = shift;
Device::BCM2835::delay($msec);
}
1;
requires 'Device::BCM2835';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment