[아이씨뱅큐 ICbanQ 파워블로거 2기] Volumio 로터리엔코더( GPIO )를 이용하여 음량제어하기

[본 컨텐츠는 ICbanQ (아이씨뱅큐)에서 진행하는 파워블로거 활동의 일환으로아이씨뱅큐의 지원을 받아 작성되었습니다]

 

 


[ 사용한 품목 ]

 

라즈베리파이 B+, 로터리 엔코더 


 

[ CONTENTS ]

 

[참고]

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::[ Hardware ]::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::



::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::[ Software ]::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


1. GPIO설치
sudo dpkg -i python-rpi.gpio_0.5.11-1_armhf.deb

2. Rotary Class
  아래의 2.1과 2.2모두 /home/volumio/에 위치한다.

2.1 rotary_class.py

[참고]  Bob Rathbone source: http://www.bobrathbone.com/raspberrypi_rotary.htm 

  sudo nano rotary_class.py
  아래내용 복사-붙여넣기
  Ctrl + X선택 후 y 선택 후 Enter

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 
#!/usr/bin/env python
#
# Raspberry Pi Rotary Encoder Class
# $Id: rotary_class.py,v 1.2 2014/01/31 13:34:48 bob Exp $
#
# Author : Bob Rathbone
#
# This class uses standard rotary encoder with push switch
#
 
import RPi.GPIO as GPIO
 
class RotaryEncoder:
 
    CLOCKWISE=1
    ANTICLOCKWISE=2
    BUTTONDOWN=3
    BUTTONUP=4
 
    rotary_a = 0
    rotary_b = 0
    rotary_c = 0
    last_state = 0
    direction = 0
 
    # Initialise rotary encoder object
    def __init__(self,pinA,pinB,button,callback):
        self.pinA = pinA
        self.pinB = pinB
        self.button = button
        self.callback = callback
 
        GPIO.setmode(GPIO.BCM)
        
        # The following lines enable the internal pull-up resistors
        # on version 2 (latest) boards
        GPIO.setwarnings(False)
        GPIO.setup(self.pinA, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(self.pinB, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(self.button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
        # For version 1 (old) boards comment out the above four lines
        # and un-comment the following 3 lines
        #GPIO.setup(self.pinA, GPIO.IN)
        #GPIO.setup(self.pinB, GPIO.IN)
        #GPIO.setup(self.button, GPIO.IN)
 
        # Add event detection to the GPIO inputs
        GPIO.add_event_detect(self.pinA, GPIO.FALLING, callback=self.switch_event)
        GPIO.add_event_detect(self.pinB, GPIO.FALLING, callback=self.switch_event)
        GPIO.add_event_detect(self.button, GPIO.BOTH, callback=self.button_event, bouncetime=200)
        return
 
    # Call back routine called by switch events
    def switch_event(self,switch):
        if GPIO.input(self.pinA):
            self.rotary_a = 1
        else:
            self.rotary_a = 0
 
        if GPIO.input(self.pinB):
            self.rotary_b = 1
        else:
            self.rotary_b = 0
 
        self.rotary_c = self.rotary_a ^ self.rotary_b
        new_state = self.rotary_a * 4 + self.rotary_b * 2 + self.rotary_c * 1
        delta = (new_state - self.last_state) % 4
        self.last_state = new_state
        event = 0
 
        if delta == 1:
            if self.direction == self.CLOCKWISE:
                # print "Clockwise"
                event = self.direction
            else:
                self.direction = self.CLOCKWISE
        elif delta == 3:
            if self.direction == self.ANTICLOCKWISE:
                # print "Anticlockwise"
                event = self.direction
            else:
                self.direction = self.ANTICLOCKWISE
        if event > 0:
            self.callback(event)
        return
 
 
    # Push button up event
    def button_event(self,button):
        if GPIO.input(button): 
            event = self.BUTTONUP 
        else:
            event = self.BUTTONDOWN 
        self.callback(event)
        return
 
    # Get a switch state
    def getSwitchState(self, switch):
        return  GPIO.input(switch)
 
# End of RotaryEncoder class
 
 
cs


2.2 VolumioKnob.py 

  sudo nano VolumioKnob.py
  아래내용 복사-붙여넣기
  Ctrl + X선택 후 y 선택 후 Enter
  그리고 아래를 실행하여 확인
  sudo python /home/volumio/VolumioKnob.py 


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
#!/usr/bin/env python
#
# Raspberry Pi Rotary Test Encoder Class
#
# Author : Bob Rathbone
#
# This class uses a standard rotary encoder with push switch
#
import subprocess
import sys
import time
from rotary_class import RotaryEncoder
 
# Define GPIO inputs (BCM)
PIN_A = 23              # Pin 16
PIN_B = 24             # Pin 18
BUTTON = 25     # Pin 22
 
# This is the event callback routine to handle events
def switch_event(event):
        if event == RotaryEncoder.CLOCKWISE:
              subprocess.call(['mpc''volume''+2' ])
              time.sleep(.2)
        elif event == RotaryEncoder.ANTICLOCKWISE:
              subprocess.call(['mpc''volume''-2' ])
              time.sleep(.2)
        elif event == RotaryEncoder.BUTTONDOWN:
              subprocess.call(['mpc''toggle' ])
        #elif event == RotaryEncoder.BUTTONUP:
              #print "Button up"
        return
 
# Define the switch
rswitch = RotaryEncoder(PIN_A,PIN_B,BUTTON,switch_event)
 
while True:
        time.sleep(0.5)
 
cs


3. 자동스크립트 작성

sudo nano /etc/rc.local

exit 0: 위에 아래 내용 삽입

sudo python /home/volumio/VolumioKnob.py &



3. 성공 & 재실행

sudo reboot


4. 동영상 확인





 

 

5. 관련상품