Guest User

Raspberry Pi Internet Radio

a guest
Feb 12th, 2013
2,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.51 KB | None | 0 0
  1. #!/usr/bin/python
  2. #
  3. # Script for Raspberry Pi Internet Radio
  4. #
  5. # Updates   :   Peter Foster (2013/02/11)
  6. # Email :       [email protected]
  7. # Notes:
  8. #      - fixed a few bugs
  9. #      - allowed quicker breakout from scrolling after channel change
  10. #
  11. # Updates   :   James Bos (2013/01/18)
  12. # Email :   [email protected]
  13. #
  14. #       Notes:
  15. #   * Changed for 16x2 LCD
  16. #       * Scrolling Song name (not the best code since my python is quire poor but it works!)                      
  17. #       * Display Station Name on 2nd line of LCD
  18. #       * Added 3rd button to show IP address and Uptime
  19. #       * Silly starting animation!
  20. #
  21. #       TO-DO:
  22. #       * Learn some better code practices so for loop for scrolling LCD doesn't
  23. #           affect button pushes (need to hold your finger on button for sleeptime)
  24. #       * Volume control!!!
  25. #       * Volume control display!!!!!!!
  26. #
  27. # Original Author: Kyle Prier
  28. # Site: http://www.youtube.com/meistervision
  29. #  
  30. # Original LCD author : Matt Hawkins
  31. # Site   : http://www.raspberrypi-spy.co.uk/
  32. #  
  33. # Date   : 10/01/2012
  34. #
  35.  
  36. # The wiring for the LCD is as follows:
  37. # 1 : GND
  38. # 2 : 5V
  39. # 3 : Contrast (0-5V)*
  40. # 4 : RS (Register Select)
  41. # 5 : R/W (Read Write)       - GROUND THIS PIN! We do not want the LCD to send anything to the Pi @ 5v
  42. # 6 : Enable or Strobe
  43. # 7 : Data Bit 0             - NOT USED
  44. # 8 : Data Bit 1             - NOT USED
  45. # 9 : Data Bit 2             - NOT USED
  46. # 10: Data Bit 3             - NOT USED
  47. # 11: Data Bit 4
  48. # 12: Data Bit 5
  49. # 13: Data Bit 6
  50. # 14: Data Bit 7
  51. # 15: LCD Backlight +5V
  52. # 16: LCD Backlight GND (Red)
  53. # 17: LCD Backlight GND (Green)
  54. # 18: LCD Backlight GND (Blue)
  55.  
  56. #import
  57. import RPi.GPIO as GPIO
  58. import time
  59. import os
  60.  
  61. # Define GPIO to LCD mapping
  62.  
  63. LCD_RS = 7
  64. LCD_E  = 8
  65. LCD_D4 = 25
  66. LCD_D5 = 24
  67. LCD_D6 = 23
  68. LCD_D7 = 18
  69.  
  70. # Defomte GPIO for Radio Controls
  71. NEXT = 4
  72. PREV = 17
  73. INFO = 15
  74.  
  75. # Define some device constants
  76. LCD_WIDTH = 16    # Maximum characters per line (change to 20 for 20x4)
  77. LCD_CHR = True
  78. LCD_CMD = False
  79.  
  80. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  81. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  82. #LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
  83. #LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
  84.  
  85.  
  86. # Timing constants
  87. E_PULSE = 0.00005
  88. E_DELAY = 0.00005
  89.  
  90. def main():
  91.     # Main program block
  92.     GPIO.setwarnings(False)
  93.     GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  94.     GPIO.setup(LCD_E, GPIO.OUT)  # E
  95.     GPIO.setup(LCD_RS, GPIO.OUT) # RS
  96.     GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  97.     GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  98.     GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  99.     GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  100.        
  101.     GPIO.setup(NEXT, GPIO.IN) # Next Channel button
  102.     GPIO.setup(PREV, GPIO.IN) # Previous Channel button
  103.     #GPIO.setup(INFO, GPIO.IN) # INFO Button
  104.  
  105.     # Calls the start up animation function
  106.     start_up_anime()
  107.    
  108.     # Start playing mpc straight away    
  109.     os.system("mpc play")
  110.    
  111.     # Run our core function (scrolling and button code)
  112.     while True:
  113.         core()
  114.  
  115. # Define our core program  
  116. def core():
  117.     # Declare "f" as our mpc current song title var
  118.     f=os.popen("mpc -f %title% current")
  119.     # Declare "s" as our station name var
  120.     s=os.popen("mpc -f %name% current").read()
  121.     s=s.strip()
  122.     # Declare an empty var for station (for use further down)
  123.     station = ""
  124.    
  125.     # Declare our default sleeptime for how quickly our text scrolls
  126.     # This also affects how quickly our button presses are picked up (not good :()
  127.     sleeptime=0.3
  128.    
  129.     # Start reading our lines from out "f" var in order to break it down and scroll
  130.     for i in f.readlines():
  131.                 i=i.strip()
  132.         station += i # include station in our iterator (i)
  133.         str_pad = " " * 16 #  same as 16 spaces
  134.         station = str_pad + station # combine both together
  135.        
  136.         # iterates of the length of our station var (16)
  137.         for i in range (0, len(station)):
  138.             lcd_byte(LCD_LINE_1, LCD_CMD) # set the marker to the first line of our LCD
  139.             lcd_text = station[i:(i+16)] # var for each line of text for each position as it iterates over for loop
  140.             lcd_string(lcd_text,1) # send our text to the LCD
  141.             lcd_byte(LCD_LINE_2, LCD_CMD) # set the marker to the 2nd line of our LCD
  142.             lcd_string(s,2) # display our station (this one doesn't scroll since 2 scrolling at once is headache worthy!)
  143.            
  144.             # When our GPIO is high (button is pressed) calls mpc and moves to next track
  145.             if ( GPIO.input(NEXT) == False):    
  146.                 os.system("mpc next") # call mpc to move on to next track
  147.                 time.sleep(0.5) # sleep for half a second and give her some time!
  148.                 os.system("mpc play") # call mpc to play
  149.                
  150.                 # (sleeptime=0) changes sleep time to 0, this will cause previous track info on
  151.                 # the LCD to quickly move across the screen and move on to next track.
  152.                 # this isn't here for effect (since it kinda looks cool I know:)) but it is here
  153.                 # so the the new track info is shown more quickly (so the for loop completes more quickly
  154.                 # and moves on to pushing the new info on the screen)
  155.                 sleeptime=0
  156.                 time.sleep(1)
  157.  
  158.             # Same as above but for previous track,
  159.             if ( GPIO.input(PREV) == False):    
  160.                 os.system("mpc prev")
  161.                 time.sleep(0.5)
  162.                 os.system("mpc play")
  163.                 sleeptime=0
  164.                                 time.sleep(1)
  165.  
  166.             # This calls the uptime and ip for diognostic junk (eg: oops, whats the IP again?? :))
  167.             #if ( GPIO.input(INFO) == True):
  168.             #   osIp=os.popen("ip addr | awk 'NR==6'").read()
  169.             #   osIp=osIp[9:24]
  170.             #   osUptime=os.popen("uptime | awk -F'[,]' '{print $1 $2}'").read()
  171.             #   osUptime=osUptime[10:]
  172.             #   lcd_byte(LCD_LINE_1, LCD_CMD)
  173.             #   lcd_string(osUptime,1)
  174.             #   lcd_byte(LCD_LINE_2, LCD_CMD)
  175.             #   lcd_string(osIp,2)
  176.             #   time.sleep(5)
  177.            
  178.             # sleep time for iterating over the loop
  179.             if(sleeptime==0):
  180.                           break
  181.                         time.sleep(sleeptime)
  182.    
  183. def lcd_init():
  184.   # Initialise display
  185.   lcd_byte(0x33,LCD_CMD)
  186.   lcd_byte(0x32,LCD_CMD)
  187.   lcd_byte(0x28,LCD_CMD)
  188.   lcd_byte(0x0C,LCD_CMD)  
  189.   lcd_byte(0x06,LCD_CMD)
  190.   lcd_byte(0x01,LCD_CMD)  
  191.  
  192. def lcd_string(message,style):
  193.   # Send string to display
  194.   # style=1 Left justified
  195.   # style=2 Centred
  196.   # style=3 Right justified
  197.  
  198.   if style==1:
  199.     message = message.ljust(LCD_WIDTH," ")  
  200.   elif style==2:
  201.     message = message.center(LCD_WIDTH," ")
  202.   elif style==3:
  203.     message = message.rjust(LCD_WIDTH," ")
  204.  
  205.   for i in range(LCD_WIDTH):
  206.     lcd_byte(ord(message[i]),LCD_CHR)
  207.  
  208. def start_up_anime():
  209.     # Initialise display
  210.     lcd_init()
  211.     # Send some test
  212.     lcd_byte(LCD_LINE_1, LCD_CMD)
  213.     lcd_string("Raspberry Pi",2)
  214.     lcd_byte(LCD_LINE_2, LCD_CMD)
  215.     lcd_string("      RADIO     ",1)
  216.     time.sleep(0.1)
  217.     lcd_byte(LCD_LINE_2, LCD_CMD)
  218.     lcd_string("    R A D I O   ",1)
  219.     time.sleep(0.1)
  220.     lcd_byte(LCD_LINE_2, LCD_CMD)
  221.     lcd_string("  R  A  D  I  O ",1)
  222.     time.sleep(0.1)
  223.     lcd_byte(LCD_LINE_2, LCD_CMD)
  224.     lcd_string("R   A   D   I  O",1)
  225.     time.sleep(0.1)
  226.     lcd_byte(LCD_LINE_2, LCD_CMD)
  227.     lcd_string("R    A    D    I",1)
  228.     time.sleep(0.1)
  229.     lcd_byte(LCD_LINE_2, LCD_CMD)
  230.     lcd_string("R     A     D   ",1)
  231.     time.sleep(0.1)
  232.     lcd_byte(LCD_LINE_2, LCD_CMD)
  233.     lcd_string("   R     A     D",1)
  234.     time.sleep(0.1)
  235.     lcd_byte(LCD_LINE_2, LCD_CMD)
  236.     lcd_string("    R     A     ",1)
  237.     time.sleep(0.1)
  238.     lcd_byte(LCD_LINE_2, LCD_CMD)
  239.     lcd_string("     R     A    ",1)
  240.     time.sleep(0.1)
  241.     lcd_byte(LCD_LINE_2, LCD_CMD)
  242.     lcd_string("      R     A   ",1)
  243.     time.sleep(0.1)
  244.     lcd_byte(LCD_LINE_2, LCD_CMD)
  245.     lcd_string("       R     A  ",1)
  246.     time.sleep(0.1)
  247.     lcd_byte(LCD_LINE_2, LCD_CMD)
  248.     lcd_string("        R     A ",1)
  249.     time.sleep(0.1)
  250.     lcd_byte(LCD_LINE_2, LCD_CMD)
  251.     lcd_string("         R     A",1)
  252.     time.sleep(0.1)
  253.     lcd_byte(LCD_LINE_2, LCD_CMD)
  254.     lcd_string("          R     ",1)
  255.     time.sleep(0.1)
  256.     lcd_byte(LCD_LINE_2, LCD_CMD)
  257.     lcd_string("           R    ",1)
  258.     time.sleep(0.1)
  259.     lcd_byte(LCD_LINE_2, LCD_CMD)
  260.     lcd_string("            R   ",1)
  261.     time.sleep(0.1)
  262.     lcd_byte(LCD_LINE_2, LCD_CMD)
  263.     lcd_string("             R  ",1)
  264.     time.sleep(0.1)
  265.     lcd_byte(LCD_LINE_2, LCD_CMD)
  266.     lcd_string("              R ",1)
  267.     time.sleep(0.1)
  268.     lcd_byte(LCD_LINE_2, LCD_CMD)
  269.     lcd_string("               R",1)
  270.     time.sleep(0.1)
  271.     lcd_byte(LCD_LINE_2, LCD_CMD)
  272.     lcd_string("                ",1)
  273.     time.sleep(.2)  
  274.  
  275. def lcd_byte(bits, mode):
  276.   # Send byte to data pins
  277.   # bits = data
  278.   # mode = True  for character
  279.   #        False for command
  280.  
  281.   GPIO.output(LCD_RS, mode) # RS
  282.  
  283.   # High bits
  284.   GPIO.output(LCD_D4, False)
  285.   GPIO.output(LCD_D5, False)
  286.   GPIO.output(LCD_D6, False)
  287.   GPIO.output(LCD_D7, False)
  288.   if bits&0x10==0x10:
  289.     GPIO.output(LCD_D4, True)
  290.   if bits&0x20==0x20:
  291.     GPIO.output(LCD_D5, True)
  292.   if bits&0x40==0x40:
  293.     GPIO.output(LCD_D6, True)
  294.   if bits&0x80==0x80:
  295.     GPIO.output(LCD_D7, True)
  296.  
  297.   # Toggle 'Enable' pin
  298.   time.sleep(E_DELAY)    
  299.   GPIO.output(LCD_E, True)  
  300.   time.sleep(E_PULSE)
  301.   GPIO.output(LCD_E, False)  
  302.   time.sleep(E_DELAY)      
  303.  
  304.   # Low bits
  305.   GPIO.output(LCD_D4, False)
  306.   GPIO.output(LCD_D5, False)
  307.   GPIO.output(LCD_D6, False)
  308.   GPIO.output(LCD_D7, False)
  309.   if bits&0x01==0x01:
  310.     GPIO.output(LCD_D4, True)
  311.   if bits&0x02==0x02:
  312.     GPIO.output(LCD_D5, True)
  313.   if bits&0x04==0x04:
  314.     GPIO.output(LCD_D6, True)
  315.   if bits&0x08==0x08:
  316.     GPIO.output(LCD_D7, True)
  317.  
  318.   # Toggle 'Enable' pin
  319.   time.sleep(E_DELAY)    
  320.   GPIO.output(LCD_E, True)  
  321.   time.sleep(E_PULSE)
  322.   GPIO.output(LCD_E, False)  
  323.   time.sleep(E_DELAY)    
  324.  
  325.  
  326. if __name__ == '__main__':
  327.   main()
Advertisement
Add Comment
Please, Sign In to add comment