NoPaste

raspberry pi GIPO LCD-Anzeige für mpc

von pinguin2008
SNIPPET_DESC:
Python Code
SNIPPET_CREATION_TIME:
04.11.2013 22:08:48
SNIPPET_PRUNE_TIME:
Unendlich

SNIPPET_TEXT:
  1. #!/usr/bin/python
  2. # -*- coding: utf-8
  3. import time
  4. import RPi.GPIO as GPIO
  5. import re
  6. import subprocess
  7. import os
  8.  
  9. #######RADIO-CODE##########################################################
  10. #
  11. # read radio station
  12. f=os.popen("mpc current -f [%name%]")
  13. station = ""
  14. station+=''
  15. for i in f.readlines():
  16.         station += i
  17.  
  18. f=os.popen("mpc current -f [%title%]")
  19. station1 = ""
  20. station1+=''
  21. for i in f.readlines():
  22.         station1 += i
  23.  
  24. # Zuordnung der GPIO Pins (ggf. anpassen)
  25. DISPLAY_RS = 7
  26. DISPLAY_E  = 8
  27. DISPLAY_DATA4 = 25
  28. DISPLAY_DATA5 = 24
  29. DISPLAY_DATA6 = 23
  30. DISPLAY_DATA7 = 18
  31.  
  32. DISPLAY_WIDTH = 16      # Zeichen je Zeile
  33. DISPLAY_LINE_1 = 0x80   # Adresse der ersten Display Zeile
  34. DISPLAY_LINE_2 = 0xC0   # Adresse der zweiten Display Zeile
  35. DISPLAY_LINE_3 = 0x90   # Adresse der dritten Display Zeile
  36. DISPLAY_LINE_4 = 0xD0   # Adresse der vierten Display Zeile
  37. DISPLAY_CHR = True
  38. DISPLAY_CMD = False
  39. E_PULSE = 0.00005
  40. E_DELAY = 0.00005
  41.  
  42. def main():
  43.         while True:
  44.                 GPIO.setmode(GPIO.BCM)
  45.                 GPIO.setup(DISPLAY_E, GPIO.OUT)
  46.                 GPIO.setup(DISPLAY_RS, GPIO.OUT)
  47.                 GPIO.setup(DISPLAY_DATA4, GPIO.OUT)
  48.                 GPIO.setup(DISPLAY_DATA5, GPIO.OUT)
  49.                 GPIO.setup(DISPLAY_DATA6, GPIO.OUT)    
  50.                 GPIO.setup(DISPLAY_DATA7, GPIO.OUT)
  51.  
  52.                 display_init()
  53.        
  54.                 lcd_byte(DISPLAY_LINE_1, DISPLAY_CMD)
  55.                 lcd_string(station)
  56.  
  57.                 lcd_byte(DISPLAY_LINE_2, DISPLAY_CMD)
  58.                 lcd_string(station1)
  59.  
  60.                 time.sleep(20)
  61.        
  62. def display_init():
  63.         lcd_byte(0x33,DISPLAY_CMD)
  64.         lcd_byte(0x32,DISPLAY_CMD)
  65.         lcd_byte(0x28,DISPLAY_CMD)
  66.         lcd_byte(0x0C,DISPLAY_CMD)  
  67.         lcd_byte(0x06,DISPLAY_CMD)
  68.         lcd_byte(0x01,DISPLAY_CMD)  
  69.  
  70. def lcd_string(message):
  71.         message = message.ljust(DISPLAY_WIDTH," ")  
  72.         for i in range(DISPLAY_WIDTH):
  73.           lcd_byte(ord(message[i]),DISPLAY_CHR)
  74.  
  75. def lcd_byte(bits, mode):
  76.         GPIO.output(DISPLAY_RS, mode)
  77.         GPIO.output(DISPLAY_DATA4, False)
  78.         GPIO.output(DISPLAY_DATA5, False)
  79.         GPIO.output(DISPLAY_DATA6, False)
  80.         GPIO.output(DISPLAY_DATA7, False)
  81.         if bits&0x10==0x10:
  82.           GPIO.output(DISPLAY_DATA4, True)
  83.         if bits&0x20==0x20:
  84.           GPIO.output(DISPLAY_DATA5, True)
  85.         if bits&0x40==0x40:
  86.           GPIO.output(DISPLAY_DATA6, True)
  87.         if bits&0x80==0x80:
  88.           GPIO.output(DISPLAY_DATA7, True)
  89.         time.sleep(E_DELAY)    
  90.         GPIO.output(DISPLAY_E, True)  
  91.         time.sleep(E_PULSE)
  92.         GPIO.output(DISPLAY_E, False)  
  93.         time.sleep(E_DELAY)      
  94.         GPIO.output(DISPLAY_DATA4, False)
  95.         GPIO.output(DISPLAY_DATA5, False)
  96.         GPIO.output(DISPLAY_DATA6, False)
  97.         GPIO.output(DISPLAY_DATA7, False)
  98.         if bits&0x01==0x01:
  99.           GPIO.output(DISPLAY_DATA4, True)
  100.         if bits&0x02==0x02:
  101.           GPIO.output(DISPLAY_DATA5, True)
  102.         if bits&0x04==0x04:
  103.           GPIO.output(DISPLAY_DATA6, True)
  104.         if bits&0x08==0x08:
  105.           GPIO.output(DISPLAY_DATA7, True)
  106.         time.sleep(E_DELAY)    
  107.         GPIO.output(DISPLAY_E, True)  
  108.         time.sleep(E_PULSE)
  109.         GPIO.output(DISPLAY_E, False)  
  110.         time.sleep(E_DELAY)  
  111.  
  112. if __name__ == '__main__':
  113.         main()

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN