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(server
, port
, password
)
33 protected fun password
: String do return "password"
34 fun server
: String do return "localhost"
35 fun port
: Int do return 6600
38 var but_playlist_a
: Switch
40 var vol
: RotaryEncoder
45 var lcd_backlight
: RPiPin
46 var lcd_backlight_delay
= 1000
52 # commandline options for privileges drop
53 var opts
= new OptionContext
54 var opt_ug
= new OptionUserAndGroup.for_dropping_privileges
55 #opt_ug.mandatory = true
56 opts
.add_option
(opt_ug
)
58 # parse and check command line options
60 if not opts
.errors
.is_empty
then
62 print
"Usage: {sys.program_name} [options]"
67 assert bcm2835_init
else print
"Failed to init"
70 var user_group
= opt_ug
.value
71 if user_group
!= null then user_group
.drop_privileges
74 but_play
= new Switch(new RPiPin.p1_13
, new PUDControl.down
)
77 but_playlist_a
= new Switch(new RPiPin.p1_15
, new PUDControl.down
)
80 var vol3
= new RPiPin.p1_03
81 vol3
.fsel
= new FunctionSelect.inpt
82 vol3
.pud
= new PUDControl.up
85 var vol5
= new RPiPin.p1_05
86 vol5
.fsel
= new FunctionSelect.inpt
87 vol5
.pud
= new PUDControl.up
89 vol
= new RotaryEncoder(vol3
,vol5
)
92 var lcd_rs
= new RPiPin.p1_23
93 var lcd_en
= new RPiPin.p1_21
94 var lcd_d4
= new RPiPin.p1_19
95 var lcd_d5
= new RPiPin.p1_26
96 var lcd_d6
= new RPiPin.p1_24
97 var lcd_d7
= new RPiPin.p1_22
98 lcd
= new HD44780(lcd_rs
, lcd_en
, lcd_d4
, lcd_d5
, lcd_d6
, lcd_d7
)
102 lcd_backlight
= new RPiPin.p1_18
103 lcd_backlight
.fsel
= new FunctionSelect.outp
106 var buzzer_pin
= new RPiPin.p1_11
107 buzzer_pin
.fsel
= new FunctionSelect.outp
108 buzzer
= new Buzzer(buzzer_pin
)
116 var force_lcd_update
= false
119 if but_play
.changed
and but_play
.is_down
then
122 force_lcd_update
= true
125 if but_playlist_a
.changed
and but_playlist_a
.is_down
then
127 force_lcd_update
= true
135 mpd
.relative_volume
= -vol_step
138 mpd
.relative_volume
= vol_step
140 force_lcd_update
= true
144 if tick
% 100 == 0 or force_lcd_update
then
145 var status
= mpd
.status
146 var song
= mpd
.current_song
149 if status
== null then
150 lcd
.text
= "Unknown status"
151 else if song
== null then
152 lcd
.text
= "No song playing"
154 if status
.playing
then
157 else status_char
= "#"
159 var tr
= status
.time_ratio
161 if tr
!= null then pos
= (status
.time_ratio
*10.0).to_i
.to_s
163 lcd
.text
= "{status_char} {song.artist}\n{pos} {song.title}"
168 if force_lcd_update
then last_event
= tick
170 var diff_with_last_event
= tick
- last_event
171 if diff_with_last_event
== 0 then
172 lcd_backlight
.write
(true)
173 else if diff_with_last_event
== lcd_backlight_delay
then
174 lcd_backlight
.write
(false)
185 var status
= mpd
.status
187 if status
!= null then
188 playing
= status
.playing
190 print
"Cannot get state"
196 print
"playing -> stop"
199 print
"stopped -> play"
208 mpd
.load_playlist
("alexis")
211 fun bell
do buzzer
.buzz
(1.5, 20)
214 var phy
= new PhysicalInterface