loader: build_module_importation invalidates the mmodule on errors
[nit.git] / contrib / physical_interface_for_mpd_on_rpi / physical_interface_for_mpd_on_rpi.nit
index 79b52ee..335444a 100644 (file)
@@ -29,23 +29,29 @@ import mpd
 import privileges
 
 class PhysicalInterface
-       var mpd = new MPDConnection("localhost", 6600, "password")
+       var mpd = new MPDConnection(server, port, password)
+       protected fun password: String do return "password"
+       fun server: String do return "localhost"
+       fun port: Int do return 6600
 
-       var debug_led: RPiPin
+       var but_play: Switch is noinit
+       var but_playlist_a: Switch is noinit
 
-       var but_play: Switch
-       var but_playlist_a: Switch
-
-       var vol: RotaryEncoder
+       var vol: RotaryEncoder is noinit
        var vol_step = 2
 
-       var lcd: HD44780
+       var lcd: HD44780 is noinit
+
+       var lcd_backlight: RPiPin is noinit
+       var lcd_backlight_delay = 1000
+
+       var buzzer: Buzzer is noinit
 
        init
        do
                # commandline options for privileges drop
                var opts = new OptionContext
-               var opt_ug = new OptionDropPrivileges
+               var opt_ug = new OptionUserAndGroup.for_dropping_privileges
                #opt_ug.mandatory = true
                opts.add_option(opt_ug)
 
@@ -64,11 +70,6 @@ class PhysicalInterface
                var user_group = opt_ug.value
                if user_group != null then user_group.drop_privileges
 
-               # Debug LED
-               debug_led = new RPiPin.p1_11
-               debug_led.fsel = new FunctionSelect.outp
-               debug_led.write(false)
-
                # Play button
                but_play = new Switch(new RPiPin.p1_13, new PUDControl.down)
 
@@ -97,12 +98,20 @@ class PhysicalInterface
                lcd = new HD44780(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7)
                lcd.setup
                lcd.clear
+
+               lcd_backlight = new RPiPin.p1_18
+               lcd_backlight.fsel = new FunctionSelect.outp
+
+               # Buzzer
+               var buzzer_pin = new RPiPin.p1_11
+               buzzer_pin.fsel = new FunctionSelect.outp
+               buzzer = new Buzzer(buzzer_pin)
        end
 
        fun run
        do
-               var led_on = false
                var tick = 0
+               var last_event = 0
                loop
                        var force_lcd_update = false
 
@@ -143,6 +152,7 @@ class PhysicalInterface
                                        lcd.text = "No song playing"
                                else
                                        if status.playing then
+                                               last_event = tick
                                                status_char = ">"
                                        else status_char = "#"
 
@@ -154,6 +164,16 @@ class PhysicalInterface
                                end
                        end
 
+                       # manage backlight
+                       if force_lcd_update then last_event = tick
+
+                       var diff_with_last_event = tick - last_event
+                       if diff_with_last_event == 0 then
+                               lcd_backlight.write(true)
+                       else if diff_with_last_event == lcd_backlight_delay then
+                               lcd_backlight.write(false)
+                       end
+
                        10.bcm2835_delay
                        tick += 1
                end
@@ -179,12 +199,16 @@ class PhysicalInterface
                        print "stopped -> play"
                        mpd.play
                end
+
+               bell
        end
 
        fun play_playlist_a
        do
                mpd.load_playlist("alexis")
        end
+
+       fun bell do buzzer.buzz(1.5, 20)
 end
 
 var phy = new PhysicalInterface