akellyirl

radio_jazzi_p.py

Aug 12th, 2015
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.85 KB | None | 0 0
  1. from datetime              import datetime
  2. from subprocess            import *
  3. from time                  import sleep, strftime
  4. from Queue                 import Queue
  5. from threading             import Thread
  6. import os
  7. from nanpy import Arduino, Lcd
  8.  
  9. Arduino.pinMode(14, input)
  10.  
  11. lcd = Lcd([8,9,4,5,6,7],[16,2])            # Setup the LCD pins for the Sainsmart Shield
  12. lcd.printString("Jess's Web Radio",0,0)
  13. lcd.printString("Loading" + "."*3,0,1)
  14. sleep(5)
  15. max_trax = 74
  16. x = 1
  17. loop_menu = 1
  18. loop_radio = 1
  19.  
  20. def display_ipaddr():
  21.    show_wlan0 = "ip addr show wlan0 | cut -d/ -f1 | awk '/inet/ {printf \"w%15.15s\", $2}'"
  22.    show_eth0  = "ip addr show eth0  | cut -d/ -f1 | awk '/inet/ {printf \"e%15.15s\", $2}'"
  23.    ipaddr = run_cmd(show_eth0)
  24.    if ipaddr == "":
  25.       ipaddr = run_cmd(show_wlan0)
  26.    lcd.printString('IP Address:',0,0)
  27.    lcd.printString(ipaddr,0,1)
  28.    sleep(2)
  29.  
  30. def displaymenu():    
  31.     if x==1:
  32.         lcd.printString("1. Display      ",0,0)
  33.         lcd.printString("   IP Address   ",0,1)
  34.     elif x==2:
  35.         lcd.printString("2. Audio Output ",0,0)
  36.         lcd.printString("   to hdmi Port ",0,1)
  37.     elif x==3:
  38.         lcd.printString("3. Audio Output ",0,0)
  39.         lcd.printString("  to Analog port",0,1)
  40.     elif x==4:
  41.         lcd.printString("4. Audio Output ",0,0)
  42.         lcd.printString("  Auto Sel. Port",0,1)
  43.     elif x==5:
  44.         lcd.printString("5. Reload the   ",0,0)
  45.         lcd.printString("   Playlist     ",0,1)
  46.     elif x==6:
  47.         lcd.printString("6. ShutDown     ",0,0)
  48.         lcd.printString("   the System   ",0,1)
  49.     else:
  50.         lcd.printString("7. Exit to      ",0,0)
  51.         lcd.printString("   Main Menu    ",0,1)
  52.  
  53. def load_playlist():
  54.    output = run_cmd("mpc clear")
  55.    output = run_cmd("/home/pi/radio_playlist.sh")
  56.                              
  57. def run_cmd(cmd):
  58.    p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
  59.    output = p.communicate()[0]
  60.    return output
  61.  
  62. def getKey():                            
  63.    val = Arduino.analogRead(14)
  64.    if val == 1023:
  65.       return "NONE"
  66.    elif val < 100:
  67.       return "RIGHT"
  68.    elif val < 150:
  69.       return "UP"
  70.    elif val < 330:
  71.       return "DOWN"
  72.    elif val < 510:
  73.       return "LEFT"
  74.    elif val < 750:
  75.       return "SEL"
  76.    else:
  77.       return "KBD_FAULT"
  78.  
  79. load_playlist()
  80. def getTrack():
  81.    #L= [S.strip('\n') for S in os.popen('mpc').readlines()]        # Get the Track info from the stdout of the mpc command
  82.    output = run_cmd("mpc current")
  83.    station = output [0:16]                                           # Pick out the Station and Track info
  84.    track =  output [-17:-1]
  85.    lcd.printString(station + " "*(16 - len(station)), 0, 0)
  86.    lcd.printString(track + " "*(16 - len(track)), 0, 1)
  87.  
  88. track_num = 1                                                     # Start off on Track number 1
  89. os.system("mpc play "+str(track_num))                             # Tell the OS to Play it
  90.  
  91. while loop_radio == 1:
  92.    getTrack()
  93.    loop_menu = 1
  94.    x = 1
  95.    key = getKey()
  96.    if key == "UP":
  97.       track_num += 1
  98.       if track_num > max_trax:
  99.          track_num = max_trax
  100.       os.system("mpc play " + str(track_num))
  101.       getTrack()
  102.    elif key == "DOWN":
  103.       track_num -= 1
  104.       if track_num < 1:
  105.          track_num = 1
  106.       os.system("mpc play " + str(track_num))
  107.       getTrack()
  108.    elif key == "SEL":
  109.       while loop_menu == 1:
  110.            displaymenu()
  111.            key = getKey()
  112.            if key == "RIGHT":
  113.                   os.system("mpc volume +2")
  114.                   lcd.printString(16*" ", 0, 0)
  115.                   lcd.printString(16*" ", 0, 1)
  116.                   output = run_cmd("mpc volume")
  117.                   lcd.printString("VOLUME UP:", 0, 0)
  118.                   lcd.printString(output, 5, 1)
  119.                   sleep(.25)
  120.                   lcd.printString(16*" ",0,0)
  121.                   lcd.printString(16*" ",0,1)
  122.            elif key == "LEFT":
  123.                   os.system("mpc volume -2")
  124.                   lcd.printString(16*" ",0,0)
  125.                   lcd.printString(16*" ", 0,1)
  126.                   output = run_cmd("mpc volume")
  127.                   lcd.printString("VOLUME DOWN:", 0, 0)
  128.                   lcd.printString(output, 5, 1)
  129.                   sleep(.25)
  130.                   lcd.printString(16*" ",0,0)
  131.                   lcd.printString(16*" ",0,1)
  132.            elif key == "UP":
  133.                  if x <= 1:
  134.                      x = 7
  135.                  else:
  136.                     x = x - 1
  137.            elif key == "DOWN":
  138.                  if x >= 7:
  139.                       x = 1
  140.                  else:
  141.                      x = x + 1      
  142.            elif key == "SEL":
  143.                  if x == 1:
  144.                     display_ipaddr()
  145.                     sleep(1)
  146.                  elif x == 2:
  147.                     output = run_cmd("amixer -q cset numid=3 2")
  148.                     lcd.printString("Audio OUT-->HDMI", 0, 0)
  149.                     lcd.printString("output ", 0, 1)
  150.                     sleep(.5)
  151.                  elif x == 3:
  152.                     output = run_cmd("amixer -q cset numid=3 1")
  153.                     lcd.printString("Audio OUT->Analog", 0, 0)
  154.                     lcd.printString("output ", 0, 1)
  155.                     sleep(.5)
  156.                  elif x == 4:
  157.                     output = run_cmd("amixer -q cset numid=3 0")
  158.                     lcd.printString("Audio OUT->  Auto", 0, 0)
  159.                     lcd.printString("output ", 0, 1)
  160.                     sleep(.5)
  161.                  elif x == 5:
  162.                     load_playlist()      
  163.                     os.system('mpc play 1')
  164.                  elif x == 6:
  165.                     lcd.printString("Good Bye         ", 0, 0)
  166.                     lcd.printString("Have a Nice Day  ", 0, 1)
  167.                     output = run_cmd("mpc clear")
  168.                     output = run_cmd("sudo shutdown now")
  169.                  elif x == 7:
  170.                      loop_menu = 0
  171.                      getTrack()
  172.                  break
  173.            elif key == "RIGHT":
  174.                   os.system("mpc volume +2")
  175.                   lcd.printString(16*" ",0,0)
  176.                   lcd.printString(16*" ", 0,1)
  177.                   output = run_cmd("mpc volume")
  178.                   lcd.printString("VOLUME UP:", 0, 0)
  179.                   lcd.printString(output, 5, 1)
  180.                   sleep(.25)
  181.                   lcd.printString(16*" ",0,0)
  182.                   lcd.printString(16*" ",0,1)
  183.            elif key == "LEFT":
  184.                   os.system("mpc volume -2")
  185.                   lcd.printString(16*" ", 0, 0)
  186.                   lcd.printString(16*" ", 0, 1)
  187.                   output = run_cmd("mpc volume")
  188.                   lcd.printString("VOLUME DOWN:", 0, 0)
  189.                   lcd.printString(output, 5, 1)
  190.                   sleep(.25)
  191.                   lcd.printString(16*" ",0,0)
  192.                   lcd.printString(16*" ",0,1)
Advertisement
Add Comment
Please, Sign In to add comment