{"id":295,"date":"2013-11-11T14:39:00","date_gmt":"2013-11-11T14:39:00","guid":{"rendered":"http:\/\/thomas.w-p.me.uk\/blog\/2013\/11\/rpizebox-display-code\/"},"modified":"2013-11-16T21:42:26","modified_gmt":"2013-11-16T21:42:26","slug":"rpizebox-display-code","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2013\/11\/rpizebox-display-code\/","title":{"rendered":"RPizeBox Display code"},"content":{"rendered":"<p>Here is the code for now. It needs pylms downloaded and wiringpi2 to work.\u00a0 I made some changes to pylms player.py to allow for accented letters in the titles so I just load the code rather than importing it.<\/p>\n<pre>#####################################################################\n# SqueezeLite LCD Display                                           #\n# Thomas W-P - http:\/\/thomas.w-p.me.uk                              #\n# Needs to be run as sudo                                           #\n#                                                                   #\n# Known issues:                                                     #\n#      Code is a bit crap, but that's life.  It just about works.   #\n#                                                                   #\n#####################################################################\nimport wiringpi2 as wp      # to control display\nimport time, datetime           # for delays and date\nimport fcntl, socket, struct # to get mac address\nimport commands           # to get IP address\n#import subprocess              # for printing to console\nimport sys\nsys.path.append('\/home\/pi\/PyLMS\/')\n\nfrom pylms.server import Server # to talk to LMS server\nfrom pylms.player import Player # to talk to LMS player\n\n\n# variables\/constants\nloggedin = False      # to wait for the server\nscrollpos = 0       # for scrolling the title - needs to be remembered...\nwaitBeforeOffMinutes = 10   # time to wait before switching off in minutes\nwaitCount = 0               # counter for the suspend mode\nelapsed = float(0)          # to hold elapsed time\ntotal = float(0)            # to hold total time\nscPort = 9090               # default LMS port for CLI\nscUser = \"\"                 # CLI user\nscPassword = \"\"             # CLI password\nwhatToShow = 0              # 0 = Artist, 1 = Album, 2 = Position\/Length\n\n\n\n# to get the mac address\ndef getHwAddr(ifname):\n    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))\n    return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]\n\n# to scroll the title\ndef scrollTitle():\n    tracktitle = \"\"       # string to hold track title\n    global scrollpos\n    try:\n        tracktitle = sl.get_track_title()\n        if (len(tracktitle) &gt; 16):\n            tracktitle += \" --- \" + tracktitle[:16]\n            for x in range(0,7):\n                # exit if not in \"Play\" mode\n                if (sl.get_mode() != 'play'):\n                    return\n                # we're at the start - pause \n                if (scrollpos == 0):\n                    # scroll \n                    wp.lcdPosition(lcd, 0, 0)\n                    wp.lcdPuts(lcd,tracktitle[0:16])\n                    time.sleep(3)\n                if (len(tracktitle) &gt; 16+scrollpos):\n                    scrollpos += 1\n                    wp.lcdPosition(lcd, 0, 0)\n                    wp.lcdPuts(lcd,tracktitle[scrollpos:16+scrollpos])\n                    time.sleep(0.4)\n                elif (len(tracktitle) == 16+scrollpos): #we've got back to the start\n                    scrollpos = 0\n        else:   #standard title\n            for x in range(0,4):\n                if (sl.get_mode() != 'play'):\n                    return\n                wp.lcdPosition(lcd, 0, 0)\n                wp.lcdPuts(lcd,(tracktitle + \" \"*16)[:16])\n                time.sleep(1)\n\n    except:\n        wp.lcdPosition(lcd, 0, 0)\n        #cmd = [ 'echo', tracktitle ]\n        #output = subprocess.Popen( cmd, stdout=subprocess.PIPE ).communicate()[0]\n        #print output\n        wp.lcdPuts(lcd,\"(Title unknown)\")\n        time.sleep(5)  \n\n\n# get the IP address\nimport commands\nintf = 'eth0'\nintf_ip = commands.getoutput(\"ip address show dev \" + intf).split()\nintf_ip = intf_ip[intf_ip.index('inet') + 1].split('\/')[0]\n\n# set up the display - 4 bit to save pins - apparently slower but marginal\n# https:\/\/projects.drogon.net\/raspberry-pi\/wiringpi\/lcd-library\/\nwp.wiringPiSetup();\nlcd = wp.lcdInit (2, 16, 4,  11,10 , 0,1,2,3,0,0,0,0)\n\n# connect to the squeezebox\nsc = Server(hostname=intf_ip, port=scPort, username=scUser, password=scPassword)\n\n# initialising\nwp.lcdClear(lcd)      # clear the lcd\nwp.lcdPosition(lcd, 0, 0) # cursor to start of row\n\n\n# keep trying to connect until there is a response\nwhile loggedin != True:\n    try:\n        sc.connect()\n        loggedin = sc.logged_in\n        #wp.lcdPuts(lcd,str(loggedin))\n    except:\n        wp.lcdClear(lcd)\n        wp.lcdPosition(lcd, 0, 0)\n        wp.lcdPuts(lcd,\"Music server\"[:16])\n        wp.lcdPosition(lcd, 0, 1)\n        wp.lcdPuts(lcd,\"   not ready :(\"[:16])\n        time.sleep(3)\n\n# set up the player\nsl = sc.get_player(getHwAddr(\"eth0\")) # http:\/\/stackoverflow.com\/questions\/159137\/getting-mac-address\n\n# change the display\nwhile True:\n\n    # show something\n    if (sl.get_mode() == 'play'):\n        waitCount = 0 \n        try:\n            wp.lcdPosition(lcd, 0, 1)\n            # no switch in python!\n            if(whatToShow == 0):\n                wp.lcdPuts(lcd,(sl.get_track_artist() + \" \"*16)[:16])\n            elif(whatToShow == 1):\n                wp.lcdPuts(lcd,(sl.get_track_album() + \" \"*16)[:16])\n            elif(whatToShow == 2):\n                wp.lcdPosition(lcd, 0, 1)  \n                elapsed = sl.get_time_elapsed()\n                total = sl.get_track_duration()\n                progress = int(9 * elapsed \/ total)#\n                wp.lcdPuts(lcd,\"&gt;\" + \"&gt;\"*(progress+1)+\"-\"*(8-progress) + \" \" + str(datetime.timedelta(seconds=int(total)))[-5:])            \n            whatToShow += 1\n            if (whatToShow == 3):\n                whatToShow = 0\n        except:\n            wp.lcdPuts(lcd,\"      ~~~~      \"[:16])          \n        # title scrolling\n        scrollTitle()  \n\n\n    # deal with pause\/stop\n    if (sl.get_mode() == 'pause'):\n        wp.lcdPosition(lcd, 0, 0)\n        wp.lcdPuts(lcd,(sl.get_track_artist() + \" \"*16)[:16])\n        wp.lcdPosition(lcd, 0, 1)  \n        wp.lcdPuts(lcd,(\"     PAUSED\" + \" \"*16)[:16])\n        time.sleep(1)\n        waitCount += 1\n        if (waitCount &gt;= (waitBeforeOffMinutes * 60)): \n            # send \"OFF\" message to player\n            sl.set_power_state(False)\n\n    if (sl.get_mode() == 'stop'):\n        waitCount = 0\n        wp.lcdPosition(lcd, 0, 1)  \n        wp.lcdPuts(lcd,\"           \" + time.strftime(\"%H:%M\", time.gmtime())[:16])\n        wp.lcdPosition(lcd, 0, 0)\n        wp.lcdPuts(lcd,(datetime.datetime.now().strftime(\"%A\") + \" \" + datetime.datetime.now().strftime(\"%d\")+ \" \" + datetime.datetime.now().strftime(\"%B\")[:3] + \" \"*16)[:16])\n        time.sleep(1)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is the code for now. It needs pylms downloaded and wiringpi2 to work.\u00a0 I made some changes to pylms player.py to allow for accented letters in the titles so I just load the code rather than importing it. ##################################################################### # SqueezeLite LCD Display # # Thomas W-P &#8211; http:\/\/thomas.w-p.me.uk # # Needs to be &hellip; <a href=\"https:\/\/thomas.w-p.me.uk\/blog\/2013\/11\/rpizebox-display-code\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">RPizeBox Display code<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[33,32,34],"class_list":["post-295","post","type-post","status-publish","format-standard","hentry","category-geekiness","tag-raspberry-pi","tag-rpizebox","tag-squeezebox"],"_links":{"self":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":2,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"predecessor-version":[{"id":414,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/295\/revisions\/414"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}