Advertisement
Godhunter74

recalbox_clcd

Mar 10th, 2017
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.26 KB | None | 0 0
  1. #!/usr/bin/python
  2. """
  3. recalbox_clcd.py
  4. Author       : Godhunter74 with  a lot of of inspiration and work from zzeromin, member of Raspberrypi Village
  5. Creation Date: Mar 08, 2017
  6. Blog         : https://forum.recalbox.com/topic/5777/relier-%C3%A0-un-%C3%A9cran-et-afficher-du-texte and original work : http://rasplay.org, http://forums.rasplay.org/, https://zzeromin.tumblr.com/
  7. Thanks to    : zzeromin smyani, zerocool, GreatKStar
  8.  
  9. Free and open for all to use. But put credit where credit is due.
  10.  
  11. #Reference:
  12. I2C_LCD_driver developed by: Denis Pleic ( https://gist.github.com/DenisFromHR/cc863375a6e19dce359d )
  13. IP_Script Developed by: AndyPi ( http://andypi.co.uk/ )
  14. lcdScroll Developed by: Eric Pavey ( https://bitbucket.org/AK_Eric/my-pi-projects/src/28302f8f5657599e29cb5d55573d192b9fa30265/Adafruit_CharLCDPlate/lcdScroll.py?at=master&fileviewer=file-view-default )
  15.  
  16. #Notice:
  17. recalbox_clcd.py require I2C_LCD_driver.py, lcdScroll.py
  18.  
  19. Small script written in Python for recalbox project (https://www.recalbox.com/)
  20. running on Raspberry Pi 1,2,3, which displays all neccessary info on a 16x2 LCD display
  21. #Features:
  22. 1. Current date and time, IP address of eth0, wlan0
  23. 2. CPU temperature and speed
  24. 3. Emulation and ROM information extracet from gamelist                      !!!!!!!!!!     YOU MUST SCRAPP YOUR ROMS        !!!!!!!!!!!!!
  25. """
  26.  
  27. import I2C_LCD_driver
  28. import os
  29. from os import popen
  30. from sys import exit
  31. from subprocess import *
  32. from time import *
  33. from datetime import datetime
  34. from lcdScroll import Scroller
  35. import string
  36.  
  37. def run_cmd(cmd):
  38.    # runs whatever is in the cmd variable in the terminal
  39.    p = Popen(cmd, shell=True, stdout=PIPE)
  40.    output = p.communicate()[0]
  41.    return output
  42.  
  43. def get_cpu_temp():
  44.    # get the cpu temp
  45.    tempFile = open("/sys/class/thermal/thermal_zone0/temp")
  46.    cpu_temp = tempFile.read()
  47.    tempFile.close()
  48.    return float(cpu_temp)/1000
  49.  
  50. def get_cpu_speed():
  51.    # get the cpu speed
  52.    tempFile = open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq")
  53.    cpu_speed = tempFile.read()
  54.    tempFile.close()
  55.    return float(cpu_speed)/1000
  56.  
  57. def getTextInside (fullText, textBefore, textAfter, index) :
  58.     begin = -1
  59.     end = -1
  60.     if index < len(fullText[index:]) - len(textBefore):
  61.         begin = fullText[index:].find(textBefore)   #on cherche le debut
  62.         if len(fullText[index:]) >= begin +len(textBefore)+len(textAfter):
  63.             end = fullText[index+begin +len(textBefore):].find(textAfter)       #on cherche la fin
  64.     if begin ==-1 or end ==-1 : #-1 = pas trouve, trouve est forcement entre 0 et len(fullText)-1
  65.         return (index, "pas_trouve")        # si on a pas retrouve le debut ou la fin on retourne une chaine vide
  66.     else:
  67.         return (index + begin+  end + len(textAfter), fullText[index + begin+ len(textBefore): index + begin+ len(textBefore)+ end])    # sinon on retourne ce qui se trouve entre les 2
  68.  
  69. mylcd = I2C_LCD_driver.lcd()
  70. mylcd.lcd_clear()
  71.  
  72. #get ip address of eth0 connection
  73. cmdeth = "ip addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -d/ -f1"
  74. #get ip address of wlan0 connection
  75. cmd = "ip addr show wlan0 | grep 'inet ' | awk '{print $2}' | cut -d/ -f1"
  76. #cmd = "ip addr show wlan1 | grep 'inet ' | awk '{print $2}' | cut -d/ -f1"
  77.  
  78. old_Temp = new_Temp = get_cpu_temp()
  79. old_Speed = new_Speed = get_cpu_speed()
  80. #draw icons not existing in [a-z]
  81. icons = [
  82.                 [ 0b00000, 0b11111, 0b11011, 0b10001, 0b10001, 0b10001, 0b11111, 0b00000 ], # Ethernet
  83.                 [ 0b00000, 0b00000, 0b00001, 0b00001, 0b00101, 0b00101, 0b10101, 0b00000 ] # Wireless
  84.         ]
  85.  
  86. # Load logo chars (icons)
  87. mylcd.lcd_load_custom_chars(icons)
  88.  
  89. #display first message on screen
  90. mylcd.lcd_display_string("PI STATION 2", 1, 1) #firstline
  91. mylcd.lcd_display_string("Pwd by RECALBOX", 2, 1) #secondline
  92. sleep(5) # 5 sec delay
  93. mylcd.lcd_clear()
  94. # display a second message on screen
  95. mylcd.lcd_display_string("www.recalbox.com", 1, 1) #firstline
  96. mylcd.lcd_display_string("RECALBOX", 2, 3) #secondline
  97. sleep(5) # 5 sec delay
  98. mylcd.lcd_clear() #delete strings on screen
  99.  
  100. while 1:
  101.    
  102.    mylcd.lcd_clear()
  103.    sec = 0
  104.    while ( sec < 5 ) :
  105.       # wlan ip address
  106.       ipaddr = run_cmd(cmd).replace("\n","")
  107.  
  108.       # selection of wlan or eth address
  109.       length = len(ipaddr)
  110.       space = ""
  111.  
  112.       if length == 0 :
  113.          ipaddr = run_cmd(cmdeth).replace("\n","")
  114.  
  115.          if len(ipaddr) == 0 :
  116.             ipaddr = unichr(0)+unichr(1)+" Not connected"
  117.          else:
  118.             if len(ipaddr) == 15 :
  119.                ipaddr = unichr(0)+run_cmd(cmdeth)
  120.             else :
  121.                for i in range( 15-len(ipaddr) ) :
  122.                   space = space + " "
  123.                ipaddr = unichr(0)+space+run_cmd(cmdeth)
  124.    
  125.       else :
  126.          if len(ipaddr) == 15 :
  127.             ipaddr = unichr(1)+run_cmd(cmd)
  128.          else :
  129.             for i in range( 15-len(ipaddr) ) :
  130.                 space = space + " "
  131.             ipaddr = unichr(1)+space+run_cmd(cmd)
  132.  
  133.       #print datetime.now().strftime( "%b %d  %H:%M:%S" )
  134.       #print "IP " + str( ipaddr )
  135.       #display the third message
  136.       mylcd.lcd_display_string( datetime.now().strftime( "%b %d  %H:%M:%S" ), 1, 0 )
  137.       mylcd.lcd_display_string( ipaddr, 2, 0 )
  138.       sec = sec + 1
  139.       sleep(1)
  140.  
  141.    mylcd.lcd_clear()
  142.    sec = 0
  143.    while ( sec < 5 ) :
  144.       space = ""
  145.       # cpu Temp & Speed information
  146.       new_Temp = get_cpu_temp()
  147.       new_Speed = int( get_cpu_speed() )
  148.  
  149.       if old_Temp != new_Temp or old_Speed != new_Speed :
  150.          old_Temp = new_Temp
  151.          old_Speed = new_Speed
  152.          #print "CPU Temp: " + str( new_Temp )
  153.          #print "CPU Speed: " + str( new_Speed )
  154.      for i in range( 5 - len( str(new_Speed) ) ) :
  155.              space = space + " "
  156.          mylcd.lcd_display_string( "CPU Temp :" + str( new_Temp ), 1, 0 )
  157.          mylcd.lcd_display_string( "CPU Speed: " + space + str( new_Speed ), 2, 0 )
  158.          sec = sec + 1  
  159.          sleep(1)
  160.  
  161.    mylcd.lcd_clear()
  162.    sec = 0
  163.    while ( sec < 1 ) :
  164.       # show system & rom file information
  165.  
  166.       #   system = f.readline()
  167.          result = run_cmd("ps | grep emulatorlauncher.py | grep -v 'c python' | grep -v grep")
  168.          (index, systeme) = getTextInside( result, "-system ", " -rom ",0) 
  169.          #~ index = 0
  170.          (index,rom ) = getTextInside( result, "-rom ", " -emulator ",0)
  171.          #print "systeme [" + systeme + "]"
  172.          #print "rom [" + rom + "]"
  173.          nom_rom= os.path.basename(rom)
  174.          nom_rom= "./"+nom_rom
  175.          #print "nom_rom ["+nom_rom +"]"
  176.          f=open("/recalbox/share/roms/"+ systeme + "/gamelist.xml", 'r')    # on ouvre le fichier
  177.          # ici on considere que le fichier est dans le meme repertoire mais tu peux aller le chercher ou tu veux avec un chemin absolu ex= "/mondossier/gamelist.xml" ou relatif ex= "../mondossier/gamelist.xml"
  178.          buf = f.read()             # on lit tout ce qu'il y a dedans (stocke dans un buffer en RAM)
  179.          f.close()                  #on ferme le fichier          
  180.          (index,gameData) = getTextInside( buf, "<path>" + nom_rom, "</game>",0)
  181.          #print gameData
  182.          #initialize all the <balise> to extract from gamelist
  183.          name = ""
  184.          desc = ""
  185.          image = ""
  186.          rating = ""
  187.          releasedate = ""
  188.          developer = ""
  189.          publisher = ""
  190.          genre = ""
  191.          players = ""
  192.  
  193.          if gameData != "pas_trouve": # on a trouve le nom , on peut chercher aussi les autres
  194.             (index2,name) = getTextInside( gameData, "<name>","</name>",0)  # name
  195.             (index2,desc) = getTextInside( gameData, "<desc>","</desc>",0)  #desc
  196.             (index2,image) = getTextInside( gameData, "<image>","</image>",0)   #image
  197.             (index2,rating) =  getTextInside( gameData, "<rating>","</rating>",0)   #rating        
  198.             (index2,releasedate) = getTextInside( gameData, "<releasedate>","</releasedate>",0) # releasedate
  199.             (index2,developer) = getTextInside( gameData, "<developer>","</developer>",0)   #developer
  200.             (index2,publisher) = getTextInside( gameData, "<publisher>","</publisher>",0)   #publisher
  201.             (index2,genre) =  getTextInside( gameData, "<genre>","</genre>",0)  #genre        
  202.             (index2,players) =  getTextInside( gameData, "<players>","</players>",0)    #players        
  203.             #print "name [" + name + "] description [" + desc + "] image [" + image + "] rating [" + str(rating) + "] releasedate [" + releasedate[:-11] + "] developer [" + developer + "] publisher [" + publisher + "] genre [" + genre + "] players [" + players + "]"
  204.             mylcd.lcd_clear()
  205.             firstline = name
  206.             systemMap = {
  207.             "Berrycade":"Berrycade",
  208.             "fba":"FinalBurn Alpha",
  209.             "gba":"GameBoy Advance",
  210.             "kodi":"KODI",
  211.             "mame-mame4all":"MAME4ALL",
  212.             "mame-advmame":"AdvanceMAME",
  213.             "mame-libretro":"MAME-libretro",
  214.             "msx":"MSX",
  215.             "nes":"Famicom",   # Nintendo Entertainment System
  216.             "psp":"PSPortable",    # PlayStation Portable
  217.             "psx":"Playstation",
  218.             "ports":"Ports",
  219.             "snes":"Super Famicom", # Super Nintendo Entertainment System
  220.             "notice":"TURN OFF",
  221.             }
  222.             system = name[:15]
  223.             plateforme = systemMap.get(systeme)            
  224.             rom = "Titre : " + name + " - Plateforme : " + plateforme + " - Genre : " + genre + " - " + "joueur(s) - Note : " + str(rating) + "% - Date de sortie : " + releasedate[:-11] + " par " + developer + " pour " + publisher + "."
  225.             #print "firstline : [" + firstline + "]"
  226.             #print "secondline : [" + secondline + "]"
  227.             mylcd.lcd_display_string( "%s" %(rom), 2 )
  228.             mylcd.lcd_display_string( "%s" %(system), 1, 0 )
  229.             sleep(3)
  230.             mylcd.lcd_clear()
  231.          flag = "TURN OFF"
  232.          lines = rom
  233.          wait = 0
  234.          speed = 0.1
  235.  
  236.          # Create scroller instance:
  237.          scroller = Scroller(lines=lines)
  238.          while True :
  239.             if wait < 10 and systeme != flag:
  240.                message = scroller.scroll()      
  241.                mylcd.lcd_display_string( "%s" %(system), 1, 0 )
  242.                mylcd.lcd_display_string( "%s" %(message), 2 )
  243.                sleep(speed)
  244.                wait = wait + 0.1
  245.             else :
  246.                break
  247.          sec = sec + 1
  248.          sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement