[Help needed] text and song info on lcd display

Hello everybody,
I’m trying to customize my volumio project: a net streamer with a 1tb hdd, 4 lines i2c lcd display and external psu.
My goal is to have a simple text while not playing and song info like artist/album, bitrate (e.g. 24/196) and filetype (flac, dsd, mp3).
I’m really noob in python scripting; I’ve just found a couple of them working good, but i’d like to improve them with the information required.
Could someone of you help me ?

I post the two files I have; LCD_Display.py (general information such as ip address, cpu temperature, date/time) --> I’d like to have just text to customize like “Volumio mediaplayer”
and MPD_Display.py (with album, artist, track name and time) —> i’d like to have Artist and Track title, filetype, bitrate and time elapsed/remaining

credist to markwheeler

lcd _display

[code]#!/usr/bin/env python3

Project Tutorial Url:http://www.markwheeler.com

import smbus
import time
import os
import glob
import subprocess
import psutil
from datetime import datetime
#import mpd
#from socket import error as SocketError

#client = mpd.MPDClient()

Define some device parameters

I2C_ADDR = 0x27 # I2C device address, if any error, change this address to 0x3f
LCD_WIDTH = 20 # Maximum characters per line

Define some device constants

LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

LCD_BACKLIGHT = 0x08 # On
#LCD_BACKLIGHT = 0x00 # Off

ENABLE = 0b00000100 # Enable bit

Timing constants

E_PULSE = 0.0005
E_DELAY = 0.0005

#Open I2C interface
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1

def lcd_init():

Initialise display

lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)

def lcd_byte(bits, mode):

Send byte to data pins

bits = the data

mode = 1 for data

0 for command

bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT

High bits

bus.write_byte(I2C_ADDR, bits_high)
lcd_toggle_enable(bits_high)

Low bits

bus.write_byte(I2C_ADDR, bits_low)
lcd_toggle_enable(bits_low)

def lcd_toggle_enable(bits):

Toggle enable

time.sleep(E_DELAY)
bus.write_byte(I2C_ADDR, (bits | ENABLE))
time.sleep(E_PULSE)
bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
time.sleep(E_DELAY)

def lcd_string(message,line,style):

Send string to display

style=1 Left justified

style=2 Centered

style=3 Right justified

if style==1:
message = message.ljust(LCD_WIDTH," “)
elif style==2:
message = message.center(LCD_WIDTH,” “)
elif style==3:
message = message.rjust(LCD_WIDTH,” ")

lcd_byte(line, LCD_CMD)

for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)

Is MPD playing a song?

def mpc_status():
playing = (os.popen(‘mpc status | grep playing’)).readline()
if playing=="":
return(0)
elif playing != “”:
return(1)

Stuff to Display

def measure_temp():
temp = os.popen(‘vcgencmd measure_temp’).readline()
return(temp.replace(“temp=”,"").replace("'C\n",""))

def ip_address():
ip = os.popen(‘host localhost | grep “address” | cut -d" " -f4’).readline()
return(ip.strip())

def cpu_load():
CPU_Pct=str(round(float(os.popen(’’'grep ‘cpu ’ /proc/stat | awk ‘{usage=($2+$4)*100/($2+$4+$5)} END {print usage }’ ‘’’).readline()),2))
return(str(CPU_Pct))

def ram_used():
mem_used = (psutil.virtual_memory().used / (1024.0 ** 2))
return(str(round(mem_used,2)))

def main():

Main program block

Initialise display

lcd_init()

Display Method

while True:

Call MPD Display if Running

client.connect(“localhost”, 6600)

if client.status()[‘state’] == “play”:

lcd_string(“MPD is PLAYING”,LCD_LINE_2,1)

client.disconnect()

os.system(’/home/volumio/scripts/mpd_display.py’)

else:

client.disconnect()

Call MPD Display if Running - Comment these next two lines if not using MPD

if mpc_status() == 1:
    os.system('/home/volumio/scripts/mpd_display.py')
	
# Display Data
lcd_string(datetime.now().strftime('%m/%d/%Y %H:%M'),LCD_LINE_1,2)
lcd_string(ip_address(),LCD_LINE_2,2)
lcd_string("CPU: "+measure_temp()+"c "+cpu_load()+"%",LCD_LINE_3,2)

lcd_string(" ",LCD_LINE_3,1)

lcd_string("RAM FREE: "+ram_used()+"Mgb",LCD_LINE_4,2)
time.sleep(1)

if name == ‘main’:

try:
main()
except KeyboardInterrupt:
pass
finally:
lcd_byte(0x01, LCD_CMD)[/code]

mpd_display

[code]#!/usr/bin/env python3

Project Tutorial Url:http://www.markwheeler.com

import smbus
import time
import os
import glob
import subprocess
#import mpd
#from socket import error as SocketError
from datetime import datetime

#client = mpd.MPDClient()

Define some device parameters

I2C_ADDR = 0x27 # I2C device address, if any error, change this address to 0x3f
LCD_WIDTH = 20 # Maximum characters per line

Define some device constants

LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

LCD_BACKLIGHT = 0x08 # On
#LCD_BACKLIGHT = 0x00 # Off

ENABLE = 0b00000100 # Enable bit

Timing constants

E_PULSE = 0.0005
E_DELAY = 0.0005

#Open I2C interface
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1

def lcd_init():

Initialise display

lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)

def lcd_byte(bits, mode):

Send byte to data pins

bits = the data

mode = 1 for data

0 for command

bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT

High bits

bus.write_byte(I2C_ADDR, bits_high)
lcd_toggle_enable(bits_high)

Low bits

bus.write_byte(I2C_ADDR, bits_low)
lcd_toggle_enable(bits_low)

def lcd_toggle_enable(bits):

Toggle enable

time.sleep(E_DELAY)
bus.write_byte(I2C_ADDR, (bits | ENABLE))
time.sleep(E_PULSE)
bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
time.sleep(E_DELAY)

def lcd_string(message,line,style):

Send string to display

style=1 Left justified

style=2 Centred

style=3 Right justified

if style==1:
message = message.ljust(LCD_WIDTH)
elif style==2:
message = message.center(LCD_WIDTH)
elif style==3:
message = message.rjust(LCD_WIDTH," ")

lcd_byte(line, LCD_CMD)

for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)

Is MPD playing a song?

def mpc_status():
playing = (os.popen(‘mpc status | grep playing’)).readline()
if playing=="":
return(0)
else:
return(1)

Stuff to Display

def current_song():
song = os.popen(‘mpc -f %title%’).readline()
return(song.strip())

def current_artist():
title = os.popen(‘mpc -f %artist%’).readline()
return(title.strip())

def current_time():
stime = os.popen(‘mpc -f %time%’).readline()
return(stime.strip())

def current_album():
album = os.popen(‘mpc -f %album%’).readline()
return(album.strip())

MPD connection times out

#def song_elapsed():

elapsed = client.status()[‘elapsed’]

m, s = divmod(int(float(elapsed)), 60)

h, m = divmod(m, 60)

elapsed_min = ("%02d:%02d" % (m, s))

return(elapsed_min)

def song_elapsed():
elapsed = os.popen(‘mpc status | awk ‘NR==2 { split($3, a, “/”); print a[1]}’’).readline()
return(elapsed.strip())

#This scrolls the song title if it exceeds 20 characters. Not implemented in display here.
def long_song():
str_song = " " * 20
long_song = str_song + current_song()
while True:
for i in range (0, len(long_song)):
lcd_text = long_song[i:(i+20)]
lcd_string(lcd_text,LCD_LINE_3,1)
time.sleep(0.4)
lcd_string(str_song,LCD_LINE_3,1)
continue

def main():

Main program block

Initialise display

lcd_init()

Display Method

while True:

Call Default Display if Stopped

client.connect(“localhost”, 6600)

if client.status()[‘state’] != “play”:

lcd_string(“MPD is STOPPED”,LCD_LINE_2,1)

client.disconnect()

os.system(’/home/volumio/scripts/lcd_display.py’)

else:

client.disconnect()

Call Default Display if Stopped

if mpc_status() == 0:
    os.system('/home/volumio/scripts/lcd_display.py')

Display Data

lcd_string(datetime.now().strftime(’%m/%d/%Y %H:%M’),LCD_LINE_1,2)

lcd_string(current_artist(),LCD_LINE_1,1)
lcd_string(current_album(),LCD_LINE_2,1)
lcd_string(current_song(),LCD_LINE_3,1)
lcd_string(song_elapsed()+"/"+current_time(),LCD_LINE_4,3)
time.sleep(0.1)

if name == ‘main’:

try:
main()
except KeyboardInterrupt:
pass
finally:
lcd_byte(0x01, LCD_CMD)
[/code]

Any help will be truly appreciated

Hi I dont know if you are having success I hope so. I spoke to Saiyato about getting the lcd screen to work. he sent me this link

github.com/Saiyato/volumio-plugin-helper

it made it possible to install and run the lcd good luck.

Hi roscoed, thank you!
My display works fine, I just want to show on it something different than what I’ve found on internet.

I will try the script you’ve linked, and than contact sayato.

Thank you again