NoPaste

raspberry pi GIPO LCD-Anzeige für mpc

von pinguin2008
SNIPPET_DESC:
internet-radio-mpc.py
SNIPPET_CREATION_TIME:
27.10.2013 21:31:44
SNIPPET_PRUNE_TIME:
Unendlich

SNIPPET_TEXT:
  1. #!/usr/bin/python
  2.  
  3. import time
  4. import RPi.GPIO as GPIO
  5. import re
  6. import subprocess
  7. import os
  8. import datetime
  9. from time import localtime
  10.  
  11. def bytestomb(b):
  12.     mb = round(float(b) / (1024*1024),2)
  13.     return mb
  14.  
  15. def get_ram():
  16.     "Returns a tuple (total ram, available ram) in megabytes. See www.linuxatemyram.com"
  17.     try:
  18.         s = subprocess.check_output(["free","-m"])
  19.         lines = s.split('\n')      
  20.         return ( int(lines[1].split()[1]), int(lines[2].split()[3]) )
  21.     except:
  22.         return 0
  23.  
  24. def get_up_stats():
  25.     "Returns a tuple (uptime, 5 min load average)"
  26.     try:
  27.         s = subprocess.check_output(["uptime"])
  28.         load_split = s.split('load average: ')
  29.         load_five = float(load_split[1].split(',')[1])
  30.         up = load_split[0]
  31.         up_pos = up.rfind(',',0,len(up)-4)
  32.         up = up[:up_pos].split('up ')[1]
  33.         return ( up , load_five )      
  34.     except:
  35.         return ( "" , 0 )
  36.  
  37. def get_network_bytes(interface):
  38.     output = subprocess.Popen(['ifconfig', interface], stdout=subprocess.PIPE).communicate()[0]
  39.     rx_bytes = re.findall('RX bytes:([0-9]*) ', output)[0]
  40.     tx_bytes = re.findall('TX bytes:([0-9]*) ', output)[0]
  41.     return (bytestomb(rx_bytes), bytestomb(tx_bytes))
  42.  
  43. def get_temperature():
  44.     "Returns the temperature in degrees C"
  45.     try:
  46.         s = subprocess.check_output(["/opt/vc/bin/vcgencmd","measure_temp"])
  47.         return float(s.split('=')[1][:-3])
  48.     except:
  49.         return 0
  50.  
  51. def get_cpu_speed():
  52.     "Returns the current CPU speed"
  53.     f = os.popen('/opt/vc/bin/vcgencmd get_config arm_freq')
  54.     cpu = f.read()
  55.     return cpu[9:]
  56.  
  57. ############################################################
  58. # read radio station
  59. def get_mpc():
  60.         f=os.popen("mpc current -f [%name%]")
  61.         station = ""
  62.         station +='       '
  63.         for i in f.readlines():
  64.                 station+= i
  65.  
  66. ############################################################
  67.  
  68. ############################################################
  69. #localtime = time.strftime("%a  %H:%M:%S", time.localtime())
  70. localtime = time.strftime("%H:%M:%S ", time.localtime())
  71. ############################################################
  72.  
  73. # Zuordnung der GPIO Pins (ggf. anpassen)
  74. DISPLAY_RS = 7
  75. DISPLAY_E  = 8
  76. DISPLAY_DATA4 = 25
  77. DISPLAY_DATA5 = 24
  78. DISPLAY_DATA6 = 23
  79. DISPLAY_DATA7 = 18
  80.  
  81. DISPLAY_WIDTH = 16      # Zeichen je Zeile
  82. DISPLAY_LINE_1 = 0x80   # Adresse der ersten Display Zeile
  83. DISPLAY_LINE_2 = 0xC0   # Adresse der zweiten Display Zeile
  84. DISPLAY_LINE_3 = 0x90   # Adresse der dritten Display Zeile
  85. DISPLAY_LINE_4 = 0xD0   # Adresse der vierten Display Zeile
  86. DISPLAY_CHR = True
  87. DISPLAY_CMD = False
  88. E_PULSE = 0.00005
  89. E_DELAY = 0.00005
  90.  
  91. def main():
  92.         while True:
  93.                 GPIO.setmode(GPIO.BCM)
  94.                 GPIO.setup(DISPLAY_E, GPIO.OUT)
  95.                 GPIO.setup(DISPLAY_RS, GPIO.OUT)
  96.                 GPIO.setup(DISPLAY_DATA4, GPIO.OUT)
  97.                 GPIO.setup(DISPLAY_DATA5, GPIO.OUT)
  98.                 GPIO.setup(DISPLAY_DATA6, GPIO.OUT)    
  99.                 GPIO.setup(DISPLAY_DATA7, GPIO.OUT)
  100.  
  101.                 display_init()
  102.  
  103.                 #rx_bytes, tx_bytes = get_network_bytes('eth0')
  104.                 rx1_bytes, tx1_bytes = get_network_bytes('wlan0')
  105.        
  106.                 lcd_byte(DISPLAY_LINE_1, DISPLAY_CMD)
  107.                 lcd_string('ONLINE -'+get_up_stats()[0])
  108.                 #lcd_string(get_mpc()[0])
  109.                
  110.                 lcd_byte(DISPLAY_LINE_2, DISPLAY_CMD)
  111.                 lcd_string('RAM: '+str(get_ram()[1])+'/'+str(get_ram()[0])+'MB')
  112.                
  113.                 #time.sleep(5)
  114.                
  115.                 #lcd_byte(DISPLAY_LINE_3, DISPLAY_CMD)
  116.                 #lcd_string('ETH0 send/rec.')
  117.  
  118.                 #lcd_byte(DISPLAY_LINE_4, DISPLAY_CMD)
  119.                 #lcd_string(str(rx_bytes)+'/'+str(tx1_bytes)+' MB')
  120.  
  121.                 #time.sleep(5)
  122.  
  123.                 lcd_byte(DISPLAY_LINE_3, DISPLAY_CMD)
  124.                 lcd_string('WLAN0 send/rec.')
  125.  
  126.                 lcd_byte(DISPLAY_LINE_4, DISPLAY_CMD)
  127.                 lcd_string(str(rx1_bytes)+'/'+str(tx1_bytes)+' MB')
  128.                
  129.                 time.sleep(10)
  130.  
  131. def display_init():
  132.         lcd_byte(0x33,DISPLAY_CMD)
  133.         lcd_byte(0x32,DISPLAY_CMD)
  134.         lcd_byte(0x28,DISPLAY_CMD)
  135.         lcd_byte(0x0C,DISPLAY_CMD)  
  136.         lcd_byte(0x06,DISPLAY_CMD)
  137.         lcd_byte(0x01,DISPLAY_CMD)  
  138.  
  139. def lcd_string(message):
  140.         message = message.ljust(DISPLAY_WIDTH," ")  
  141.         for i in range(DISPLAY_WIDTH):
  142.           lcd_byte(ord(message[i]),DISPLAY_CHR)
  143.  
  144. def lcd_byte(bits, mode):
  145.         GPIO.output(DISPLAY_RS, mode)
  146.         GPIO.output(DISPLAY_DATA4, False)
  147.         GPIO.output(DISPLAY_DATA5, False)
  148.         GPIO.output(DISPLAY_DATA6, False)
  149.         GPIO.output(DISPLAY_DATA7, False)
  150.         if bits&0x10==0x10:
  151.           GPIO.output(DISPLAY_DATA4, True)
  152.         if bits&0x20==0x20:
  153.           GPIO.output(DISPLAY_DATA5, True)
  154.         if bits&0x40==0x40:
  155.           GPIO.output(DISPLAY_DATA6, True)
  156.         if bits&0x80==0x80:
  157.           GPIO.output(DISPLAY_DATA7, True)
  158.         time.sleep(E_DELAY)    
  159.         GPIO.output(DISPLAY_E, True)  
  160.         time.sleep(E_PULSE)
  161.         GPIO.output(DISPLAY_E, False)  
  162.         time.sleep(E_DELAY)      
  163.         GPIO.output(DISPLAY_DATA4, False)
  164.         GPIO.output(DISPLAY_DATA5, False)
  165.         GPIO.output(DISPLAY_DATA6, False)
  166.         GPIO.output(DISPLAY_DATA7, False)
  167.         if bits&0x01==0x01:
  168.           GPIO.output(DISPLAY_DATA4, True)
  169.         if bits&0x02==0x02:
  170.           GPIO.output(DISPLAY_DATA5, True)
  171.         if bits&0x04==0x04:
  172.           GPIO.output(DISPLAY_DATA6, True)
  173.         if bits&0x08==0x08:
  174.           GPIO.output(DISPLAY_DATA7, True)
  175.         time.sleep(E_DELAY)    
  176.         GPIO.output(DISPLAY_E, True)  
  177.         time.sleep(E_PULSE)
  178.         GPIO.output(DISPLAY_E, False)  
  179.         time.sleep(E_DELAY)  
  180.  
  181. if __name__ == '__main__':
  182.         main()

Quellcode

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