1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # This programs interprets the input of a physical interface thought the
18 # GPIO pins of a Raspberry Pi to control an MPD server.
20 # It suppot two inputs: a play/pause button and a rotary encoder to adjust
23 # The each data output of the volume control are connected to the board
25 module physical_interface_for_mpd_on_rpi
31 class PhysicalInterface
32 var mpd
= new MPDConnection("localhost", 6600, "password")
37 var but_playlist_a
: Switch
39 var vol
: RotaryEncoder
44 var lcd_backlight
: RPiPin
45 var lcd_backlight_delay
= 1000
49 # commandline options for privileges drop
50 var opts
= new OptionContext
51 var opt_ug
= new OptionDropPrivileges
52 #opt_ug.mandatory = true
53 opts
.add_option
(opt_ug
)
55 # parse and check command line options
57 if not opts
.errors
.is_empty
then
59 print
"Usage: {sys.program_name} [options]"
64 assert bcm2835_init
else print
"Failed to init"
67 var user_group
= opt_ug
.value
68 if user_group
!= null then user_group
.drop_privileges
71 debug_led
= new RPiPin.p1_11
72 debug_led
.fsel
= new FunctionSelect.outp
73 debug_led
.write
(false)
76 but_play
= new Switch(new RPiPin.p1_13
, new PUDControl.down
)
79 but_playlist_a
= new Switch(new RPiPin.p1_15
, new PUDControl.down
)
82 var vol3
= new RPiPin.p1_03
83 vol3
.fsel
= new FunctionSelect.inpt
84 vol3
.pud
= new PUDControl.up
87 var vol5
= new RPiPin.p1_05
88 vol5
.fsel
= new FunctionSelect.inpt
89 vol5
.pud
= new PUDControl.up
91 vol
= new RotaryEncoder(vol3
,vol5
)
94 var lcd_rs
= new RPiPin.p1_23
95 var lcd_en
= new RPiPin.p1_21
96 var lcd_d4
= new RPiPin.p1_19
97 var lcd_d5
= new RPiPin.p1_26
98 var lcd_d6
= new RPiPin.p1_24
99 var lcd_d7
= new RPiPin.p1_22
100 lcd
= new HD44780(lcd_rs
, lcd_en
, lcd_d4
, lcd_d5
, lcd_d6
, lcd_d7
)
104 lcd_backlight
= new RPiPin.p1_18
105 lcd_backlight
.fsel
= new FunctionSelect.outp
114 var force_lcd_update
= false
117 if but_play
.changed
and but_play
.is_down
then
120 force_lcd_update
= true
123 if but_playlist_a
.changed
and but_playlist_a
.is_down
then
125 force_lcd_update
= true
133 mpd
.relative_volume
= -vol_step
136 mpd
.relative_volume
= vol_step
138 force_lcd_update
= true
142 if tick
% 100 == 0 or force_lcd_update
then
143 var status
= mpd
.status
144 var song
= mpd
.current_song
147 if status
== null then
148 lcd
.text
= "Unknown status"
149 else if song
== null then
150 lcd
.text
= "No song playing"
152 if status
.playing
then
155 else status_char
= "#"
157 var tr
= status
.time_ratio
159 if tr
!= null then pos
= (status
.time_ratio
*10.0).to_i
.to_s
161 lcd
.text
= "{status_char} {song.artist}\n{pos} {song.title}"
166 if force_lcd_update
then last_event
= tick
168 var diff_with_last_event
= tick
- last_event
169 if diff_with_last_event
== 0 then
170 lcd_backlight
.write
(true)
171 else if diff_with_last_event
== lcd_backlight_delay
then
172 lcd_backlight
.write
(false)
183 var status
= mpd
.status
185 if status
!= null then
186 playing
= status
.playing
188 print
"Cannot get state"
194 print
"playing -> stop"
197 print
"stopped -> play"
204 mpd
.load_playlist
("alexis")
208 var phy
= new PhysicalInterface