著者:米田聡
シェルスクリプトマガジンでは、小型コンピュータボード「Raspberry Pi」(ラズパイ)のプログラミングが楽しめる拡張ボード「ラズパイ入門ボード」を制作しました。本特集では、このラズパイ入門ボードに関する基本的な使い方の一部を紹介します。
記事本文掲載のシェルスクリプトマガジン vol.52は以下のリンク先でご購入できます。
![]()
![]()
| 
					 1 2 3 4 5 6 7 8 9  | 
						LED1 = 14 LED2 = 15 LED3 = 12 LED4 = 16 SW1 = 4 SW2 = 17 SW3 = 5 SW4 = 6  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  | 
						# -*- coding: utf-8 -*- # etnryboard.pyを読み込む from entryboard import * import time import RPi.GPIO as GPIO # 動作モード設定 GPIO.setmode(GPIO.BCM) # GPIOモード設定 GPIO.setup(LED1, GPIO.OUT, initial=GPIO.LOW) # LED1に1を出力 GPIO.output(LED1, GPIO.HIGH) # 2秒待つ time.sleep(2) # LED1に0を出力 GPIO.output(LED1, GPIO.LOW) # 終了処理 GPIO.cleanup(LED1)  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  | 
						# -*- coding: utf-8 -*- from entryboard import * import time import RPi.GPIO as GPIO # LEDが接続されているGPIOのリスト leds = [LED1, LED2, LED3 ,LED4] GPIO.setmode(GPIO.BCM) GPIO.setup(leds, GPIO.OUT, initial=GPIO.LOW) for i in range(3):   for l in leds:     GPIO.output(l, 1)     time.sleep(1)     GPIO.output(l, 0) GPIO.cleanup(leds)  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  | 
						# -*- coding: utf-8 -*- from entryboard import * import time import sys import RPi.GPIO as GPIO switches = [SW1, SW2, SW3, SW4] leds = [LED1, LED2, LED3, LED4] GPIO.setmode(GPIO.BCM) # スイッチのGPIOを入力に設定 GPIO.setup(switches, GPIO.IN) GPIO.setup(leds, GPIO.OUT, initial=GPIO.LOW) try:   while True:     for i in range(4):       if GPIO.input(switches[i]) == GPIO.LOW:         GPIO.output(leds[i], GPIO.HIGH)       else:         GPIO.output(leds[i], GPIO.LOW) except KeyboardInterrupt:   GPIO.cleanup(switches)   GPIO.cleanup(leds)   sys.exit()  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41  | 
						# -*- coding: utf-8 -*- from entryboard import * import time import sys import RPi.GPIO as GPIO switches = [SW1, SW2, SW3, SW4] leds = [LED1, LED2, LED3, LED4] # スイッチのGPIOが変化したらこの関数が呼ばれる def switch_event(channel):   global leds   global switches   for i in range(4):     if channel == switches[i]:       if GPIO.input(channel) == GPIO.LOW:         GPIO.output(leds[i], GPIO.HIGH)       else:         GPIO.output(leds[i], GPIO.LOW) # プログラムはここから if __name__ == "__main__":   GPIO.setmode(GPIO.BCM)   GPIO.setup(switches, GPIO.IN)   GPIO.setup(leds, GPIO.OUT, initial=GPIO.LOW)   for i in range(4):     # イベントを登録する     GPIO.add_event_detect(switches[i], GPIO.BOTH, bouncetime=60)     GPIO.add_event_callback(switches[i], switch_event)   try:     # 1時間何もしない     time.sleep(3600)   except KeyboardInterrupt:     GPIO.cleanup(switches)     GPIO.cleanup(leds)     sys.exit()  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13  | 
						# -*- coding: utf-8 -*- from entryboard import * import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(BUZZER, GPIO.OUT) bz = GPIO.PWM(BUZZER, 1000) bz.start(50) time.sleep(2) bz.stop() GPIO.cleanup(BUZZER)  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11  | 
						# -*- coding: utf-8 -*- from entryboard import * import time import pigpio gpio = pigpio.pi() gpio.set_mode(BUZZER, pigpio.OUTPUT) gpio.hardware_PWM(BUZZER, 1000, 500000) # 1kHz, 50% time.sleep(2) gpio.hardware_PWM(BUZZER,0 ,0) gpio.stop()  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42  | 
						# -*- coding: utf-8 -*- from entryboard import * import time import pigpio # 1オクターブの音階周波数リスト scale = [ 523.251, 587.33, 659.255, 698.456, 783.991, 880, 987.767, 1046.502 ] switches = [SW1, SW2, SW3, SW4] bouncetime = 5 *1000 # 5ms prev_tick = 0 # コールバック def switch_event(channel, edge, tick):   global bouncetime   global prev_tick   global scale   global switches   if (tick-prev_tick) < bouncetime:      return   prev_tick = tick   for i in range(4):     if switches[i] == channel:       if edge == pigpio.LOW:         gpio.hardware_PWM(BUZZER, scale[i], 500000)       else:         gpio.hardware_PWM(BUZZER, 0, 0) if __name__ == "__main__":   gpio = pigpio.pi()   gpio.set_mode(BUZZER, pigpio.OUTPUT)   for i in range(4):35     # モードとコールバック     gpio.set_mode(switches[i], pigpio.INPUT)     gpio.callback(switches[i], pigpio.EITHER_EDGE, switch_event)   try:     time.sleep(3600)   except KeyboardInterrupt:     gpio.hardware_PWM(BUZZER, 0, 0)     gpio.stop()  | 
					
| 
					 1 2 3 4 5 6 7  | 
						LED5_R = 20 LED5_G = 22 LED5_B = 27 LED6_R = 26 LED6_G = 19 LED6_B = 13  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34  | 
						# -*- coding: utf-8 -*- from entryboard import * import time import RPi.GPIO as GPIO led6 = [LED6_R, LED6_G, LED6_B] duty = [50.0, 50.0, 50.0] switches = [SW1, SW2, SW3, SW4] pwm = [] GPIO.setmode(GPIO.BCM) GPIO.setup(led6, GPIO.OUT) GPIO.setup(switches, GPIO.IN) for i in range(3):   # 100Hzに設定   pwm.append(GPIO.PWM(led6[i], 100))   pwm[i].start(duty[i]) try:   while True:     for i in range(3):       if GPIO.input(switches[i]) == GPIO.LOW:         if GPIO.input(SW4) == GPIO.LOW:           duty[i] = duty[i] - 5.0           if duty[i] < 0.0:             duty[i] = 0.0           else:             duty[i] = duty[i] + 5.0             if duty[i] > 100.0:               duty[i] = 100.0       pwm[i].ChangeDutyCycle(duty[i])       time.sleep(0.1) except KeyboardInterrupt:   GPIO.cleanup()  |