Writing station and song information to the LCD-display has not really worked out for me. I used the example from https://github.com/adafruit/Adafruit_Python_CharLCD.git to have a as a starting point.
Adafruits Adafruit_CharLCD library has a scroll function that seemed to be a good solution if a string is longer then 16 characters. I kind of worked. It displays the text, but it looks like shit if the string is longer than about 40 characters.
After some fiddling around I decided to write my own routine to scroll text on the display. Another annoying issue is Swedish characters like “åäö”. I can’t find a way to make them show on the display. The quick and dirty solution is a function that replaces them. Å becomes Aa. Ä becomes Ae and Ö becomes Oe. There are still characters that don’t display as expected. For example: ‘ becomes ? on the display. So there is still work to be done.
Here is my source code!
I am sure that there is better and more efficient ways of doing this. If you have a better solution or have a question please feel free to make comment.
#!/usr/bin/python # -*- coding: utf-8 -*- # Examplecode from Adafruit that I used as a template # for displaying info from mpc/mpd streaming import time import Adafruit_CharLCD as LCD from subprocess import * from datetime import datetime # Raspberry Pi pin configuration: lcd_rs = 27 # Note this might need to be changed to 21 for older revision Pi's. lcd_en = 22 lcd_d4 = 25 lcd_d5 = 24 lcd_d6 = 23 lcd_d7 = 18 lcd_backlight = 4 # Define LCD column and row size for 16x2 LCD. lcd_columns = 16 lcd_rows = 2 lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight) def convertoutString(inString): # Replaces åäö and ÅÄÖ with aa ae oe outString = inString outString = outString.replace("Å", "Aa") outString = outString.replace("å", "aa") outString = outString.replace("Ä", "Ae") outString = outString.replace("ä", "ae") outString = outString.replace("Ö", "Oe") outString = outString.replace("ö", "oe") return outString def scrollDisplay(text, intervall): # preparing the text so the lenght is a multiple of 16. # The extra blank is for to be shure that there is a space between # the begging and end of the text when scrolling. # To ensure that strings longer than 40 char is displayed correct # I only display the first 16 char message = text message = message + " " raknare = len(message) + ((16-len(message) % 16)) lcd.message(datetime.now().strftime('%y%m%d %H:%M:%S \n')) # Adding blanks to make the string a multiple of 16 # This assures that the LCD shows only blanks before # iterating again message = message + " " * raknare lcd.message(message[:16]) time.sleep(1) for i in range(raknare-1): # Moving first charachter to the end of the string and then # writes the first 16 chars to LCD lcd.clear() primus = message[:1] message = message[1:] + primus lcd.message(datetime.now().strftime('%y%m%d %H:%M:%S \n')) lcd.message(message[:16]) time.sleep(intervall) return def run_cmd(cmd): #Preparing to rund shell commands p = Popen(cmd, shell=True, stdout=PIPE) output = p.communicate() return output lcd.clear() #replacing åäö message = convertoutString(" Internetradio! \n Busk ") #print(message) lcd.message(message) time.sleep(1) lcd.clear() #Getting information from the MPD server cmd = "mpc current" lcd.clear() while 1: #Sending the mpc information to the LCD mpcstatus = run_cmd(cmd) mpcstatus = convertoutString(mpcstatus) scrollDisplay(mpcstatus, 0.3)