Ecco... Proprio lì andavo a parare... Allora... Googlando (sono ore che lo faccio) ho trovato questo script:
Codice:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
import textwrap
import time
import signal
import sys
import Adafruit_CharLCD as LCD
# Raspberry Pi gpio pin configuration
lcd_rs = 5
lcd_en = 6
lcd_d4 = 17
lcd_d5 = 23
lcd_d6 = 24
lcd_d7 = 27
lcd_backlight = 26
# define LCD column and row size for LCD
lcd_columns = 24
lcd_rows = 2
# init the LCD using the pins above
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)
def sigterm_handler(signal, frame):
lcd.set_cursor(0,0)
line1 = (' Volumio 1.55 ')[0:lcd_columns]
line2 = (' shutting down... ')[0:lcd_columns]
lcd.message(line1 + '\n' + line2)
sys.exit(0)
signal.signal(signal.SIGTERM, sigterm_handler)
def replace_specialchars(message):
try:
# message = message.encode('utf-8')
message = message.replace('ä', chr(228))
message = message.replace('ö', chr(246))
message = message.replace('ü', chr(252))
# message = message.replace('Ä', chr(196))
# message = message.replace('Ö', chr(214))
# message = message.replace('Ü', chr(220))
# message = message.replace('ß', chr(223))
# message = message.replace('°', chr(223))
# message = message.replace('µ', chr(228))
# message = message.replace('´', chr(96))
# message = message.replace('€', chr(227))
# message = message.replace('–', '-')
# message = message.replace('“', '"')
# message = message.replace('”', '"')
# message = message.replace('„', '"')
# message = message.replace('’', '\'')
# message = message.replace('‘', '\'')
# message = message.replace('è', '232');
# message = message.replace('é', '233');
# message = message.replace('ê', 'e');
# message = message.replace('á', '225');
# message = message.replace('à', '224');
# message = message.prelace('â', 'a');
except:
return message;
return message
def main():
# print welcome message
line1 = ' Volumio 1.55 '
line2 = ' initializing system '
lcd.message(line1 + '\n' + line2)
# create custom char (...) = three dots in one character
char_threeDots = [0b00000,0b00000,0b00000,0b00000,0b00000,0b00000,0b10101,0b00000]
# create in ram position 0
lcd.create_char(0, char_threeDots)
# wait 2 seconds
time.sleep(2.0)
# clear display and turn on backlight
lcd.clear()
# lcd.backlight(lcd.ON)
songArtistPrevious = ''
stoppedSet = 0
z = 0
while True:
# get current status and playtime
process = subprocess.Popen('mpc', shell=True, stdout=subprocess.PIPE)
status = process.communicate()[0]
statusLines = status.split('\n')
# check if mpc returns more that one line plus an extra, in that case we dont have stopped the music and can parse additional data
if len(statusLines) > 2:
# extract the song name / artist name (first line)
# web radio has station name before artist / song
songArtist = statusLines[0]
if ":" in songArtist:
songArtist = statusLines[0].split(':',1)[1].strip()
if ":" not in songArtist:
songArtist = statusLines[0]
# extract player status (playing/stopped)
infoPlayerStatus = statusLines[1].split(' ',1)[0].strip()
# check for song change; only write new data to display if there is a change cause this is nicer to cpu
if songArtist != songArtistPrevious:
songArtistPrevious = songArtist
stoppedSet = 0
z = 0
infoArtist = songArtist.split(' - ',1)[0].strip()
# shorten artist name if longer than lcd_columns
if len(infoArtist) > lcd_columns:
infoArtist = (infoArtist)[0:lcd_columns - 1] + '\x00'
# center
while len(infoArtist) < (lcd_columns -1):
infoArtist = ' ' + infoArtist + ' '
infoArtist = infoArtist + ' '
infoSong = songArtist.split(' - ',1)[1].strip()
# shorten song name if longer than lcd_columns
if len(infoSong) > lcd_columns:
infoSong = (infoSong)[0:lcd_columns - 1] + '\x00'
# center
while len(infoSong) < (lcd_columns -1):
infoSong = ' ' + infoSong + ' '
infoSong = infoSong + ' '
# extract a string like '2:01/3:43 (54%)' from the string '[playing] #2/8 2:01/4:38 (50%)'
infoLine = statusLines[1].split(']',1)[1].strip()
# remove first character ('#')
# format aftwerwards: 2/8 2:01/4:38 (50%)
infoLine = infoLine[1:]
infoTrack = infoLine.split(' ',1)[0].strip()
infoTimes = infoLine.split(' ',1)[1].strip()
# split of (50%)
infoTimes = infoTimes.split('(',1)[0].strip()
infoTotalPlaytime = infoTimes.split('/',1)[1].strip()
# toggle artist / track info
if z > 40:
z = 0
if z == 0:
# show song title and total play time / track
lcd.set_cursor(0,0)
line1 = (infoSong)[0:lcd_columns]
line2 = ('Track: '+infoTrack+' Time: '+infoTotalPlaytime)[0:lcd_columns]
while len(line2) < (lcd_columns -1):
line2 = ' ' + line2 + ' '
if infoPlayerStatus == '[paused]':
line2 = ' [playing paused] '
# clean special chars
line1 = replace_specialchars(line1)
# line2 = replace_specialchars(line2)
lcd.message(line1 + '\n' + line2)
if z == 10:
# show song title and artist
lcd.set_cursor(0,0)
line1 = (infoSong)[0:lcd_columns]
line2 = (infoArtist)[0:lcd_columns]
# clean special chars
line1 = replace_specialchars(line1)
line2 = replace_specialchars(line2)
lcd.message(line1 + '\n' + line2)
else:
# message when stopped
# set message only once, not in a loop (cpu friendly)
if stoppedSet != 1:
lcd.set_cursor(0,0)
line1 = (' Volumio 1.55 ')[0:lcd_columns]
line2 = (' [no music playing] ')[0:lcd_columns]
lcd.message(line1 + '\n' + line2)
songArtistPrevious = ''
stoppedSet = 1
# sleep 0.5s
time.sleep(0.5)
z += 1
if __name__ == '__main__':
che però sembra essere scritto per un
display NON i2c ma che avrebbe i requisiti che a me occorrono...
Ora chiedo a voi esperti:
girovagando in rete ho trovato questo script:
Codice:
# Credit for this code goes to "natbett" of the Raspberry Pi Forum 18/02/13
# https://www.raspberrypi.org/forums/viewtopic.php?t=34261&p=378524
# Before running this code make sure to run sudo i2cdetect -y 1
# and match the LCD address of your device
import smbus
from time import *
# LCD Address
ADDRESS = 0x27
# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
]LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80
# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00
# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00
# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00
# flags for backlight control
LCD_BACKLIGHT = 0x08
LCD_NOBACKLIGHT = 0x00
En = 0b00000100 # Enable bit
Rw = 0b00000010 # Read/Write bit
Rs = 0b00000001 # Register select bit
class i2c_device:
def __init__(self, addr, port=1):
self.addr = addr
self.bus = smbus.SMBus(port)
# Write a single command
def write_cmd(self, cmd):
self.bus.write_byte(self.addr, cmd)
sleep(0.0001)
class lcd:
#initializes objects and lcd
def __init__(self):
self.lcd_device = i2c_device(ADDRESS)
self.lcd_write(0x03)
self.lcd_write(0x03)
self.lcd_write(0x03)
self.lcd_write(0x02)
self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON)
self.lcd_write(LCD_CLEARDISPLAY)
self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
sleep(0.2)
# clocks EN to latch command
def lcd_strobe(self, data):
self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
sleep(.0005)
self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT))
sleep(.0001)
def lcd_write_four_bits(self, data):
self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
self.lcd_strobe(data)
# write a command to lcd
def lcd_write(self, cmd, mode=0):
self.lcd_write_four_bits(mode | (cmd & 0xF0))
self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))
# put string function
def lcd_display_string(self, string, line):
if line == 1:
self.lcd_write(0x80)
if line == 2:
self.lcd_write(0xC0)
if line == 3:
self.lcd_write(0x94)
if line == 4:
self.lcd_write(0xD4)
for char in string:
self.lcd_write(ord(char), Rs)
# clear lcd and set to home
def lcd_clear(self):
self.lcd_write(LCD_CLEARDISPLAY)
self.lcd_write(LCD_RETURNHOME)
display = lcd()
display.lcd_display_string("RaspiNews - 16x2", 1)
display.lcd_display_string("I2C LCD Demo..", 2)
mi scuso per il color ed il font, ma non riesco a cancellarli...
tel o ho ripulito io. Zzed
Poichè, però, io sono leggermente capra nella programmazione chiedo: è possibile "aprire" lo script per poterlo confrontare ed eventualmente editare per arrivare ad una cosa similare a quello riportato sopra? e come?
E per provare se lo script funziona, come faccio?
Grazie.