Advertisement
Guest User

Untitled

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