-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpd_demo.rb
executable file
·148 lines (127 loc) · 2.38 KB
/
mpd_demo.rb
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env ruby
require 'bundler/setup'
require 'eventmachine'
require 'serialport'
require_relative 'mpd_idle'
require_relative 'denon'
require_relative 'mpd_operations'
module MyOperations
def enable_airplay
EM.defer { system "service shairplay start" }
end
def disable_airplay
EM.defer { system "pgrep shairplay && sudo service shairplay stop" }
end
def enable_music
@mpd.noidle do |mpd|
mpd.enableoutput 0
mpd.play
end
end
def disable_music
@mpd.noidle do |mpd|
mpd.disableoutput 0
end
end
end
class MPDDenon < Denon
include MPDOperations
include MyOperations
def initialize(mpd)
super()
@mpd = mpd
end
def on_display_brightness(brigtness)
super
end
def on_network_button(button)
super
case button
when :next
mpd :next
when :previous
mpd :previous
when :forward
mpd_next_album
when :rewind
mpd_prev_album
when :up
nil
when :down
nil
when :left
nil
when :right
nil
when :enter
nil
when :mode
nil
when :play_pause
mpd_pause
when :play
mpd :play
when :stop
mpd :stop
when :repeat
mpd_toggle :repeat
when :random
mpd_toggle :random
when :num1
load_playlist "Anathema"
when :num2
load_playlist "Riverside"
when :num3
load_playlist "Sigur Rós".force_encoding('UTF-8')
when :num4
load_playlist "Soundtrack"
when :num5
load_playlist "Kizomba"
when :clear
send_keys "c"
when :info
nil
when :program
nil
end
end
def on_cd_button(button)
super
# Same button choice as in on_network_button
end
def on_network_function(function)
super
case function
when :online_music
disable_airplay
enable_music
when :music_server
disable_music
enable_airplay
end
end
def on_source(source)
super
case source
when :network
enable_music
mpd :play
else
disable_music
disable_airplay
end
end
def on_amp_off
super
disable_music
disable_airplay
end
end
EventMachine.run do
mpd = MPD.new
mpd.connect
puts "Connected to MPD"
mpd.async_idle
sp = SerialPort.open("/dev/ttyAMA0", 115200, 8, 1, SerialPort::NONE)
denon = EventMachine.attach sp, MPDDenon, mpd
end