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
32 fun mpd
: MPDConnection do return once
new MPDConnection("localhost", 6600, "password")
38 var status
= mpd
.status
40 if status
!= null then
41 playing
= status
.playing
43 print
"Cannot get state"
49 print
"playing -> stop"
52 print
"stopped -> play"
57 # commandline options for privileges drop
58 var opts
= new OptionContext
59 var opt_ug
= new OptionDropPrivileges
60 #opt_ug.mandatory = true
61 opts
.add_option
(opt_ug
)
63 # parse and check command line options
65 if not opts
.errors
.is_empty
then
67 print
"Usage: {program_name} [options]"
72 assert bcm2835_init
else print
"Failed to init"
75 var user_group
= opt_ug
.value
76 if user_group
!= null then user_group
.drop_privileges
79 var out
= new RPiPin.p1_11
80 out
.fsel
= new FunctionSelect.outp
84 var inp
= new RPiPin.p1_13
85 inp
.fsel
= new FunctionSelect.inpt
86 inp
.pud
= new PUDControl.down
89 var vol3
= new RPiPin.p1_03
90 vol3
.fsel
= new FunctionSelect.inpt
91 vol3
.pud
= new PUDControl.up
94 var vol5
= new RPiPin.p1_05
95 vol5
.fsel
= new FunctionSelect.inpt
96 vol5
.pud
= new PUDControl.up
98 var vol
= new RotaryEncoder(vol3
,vol5
)
102 var lcd_rs
= new RPiPin.p1_23
103 var lcd_en
= new RPiPin.p1_21
104 var lcd_d4
= new RPiPin.p1_19
105 var lcd_d5
= new RPiPin.p1_26
106 var lcd_d6
= new RPiPin.p1_24
107 var lcd_d7
= new RPiPin.p1_22
108 var lcd
= new HD44780(lcd_rs
, lcd_en
, lcd_d4
, lcd_d5
, lcd_d6
, lcd_d7
)
116 var force_lcd_update
= false
120 if lev
!= last_in
then
122 if lev
then hit_play_stop
123 force_lcd_update
= true
131 mpd
.relative_volume
= -vol_step
134 mpd
.relative_volume
= vol_step
136 force_lcd_update
= true
140 if tick
% 100 == 0 or force_lcd_update
then
141 var status
= mpd
.status
142 var song
= mpd
.current_song
145 if status
== null then
146 lcd
.text
= "Unknown status"
147 else if song
== null then
148 lcd
.text
= "No song playing"
150 if status
.playing
then
152 else status_char
= "#"
154 var tr
= status
.time_ratio
156 if tr
!= null then pos
= (status
.time_ratio
*10.0).to_i
.to_s
158 lcd
.text
= "{status_char} {song.artist}\n{pos} {song.title}"