#!/usr/bin/python

import os
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

f = open("/home/pi/events/eventoincorso.txt","r") #legge il file eventoincorso

Testo = f.read()
print Testo
text = (("                    > > > > > > > > > >     ", ( 255, 0, 0,)),(Testo, (0, 255, 0)),("    < < < < < < < < < < <   ", ( 255, 0, 0,)))

font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 16)
all_text = ""
for text_color_pair in text:
    t = text_color_pair[0]
    all_text = all_text + t

#print(all_text)
width, ignore = font.getsize(all_text)
tempo = ((width) + 30)/32 * 4
print(width)


im = Image.new("RGB", (width + 30, 16), "black")
draw = ImageDraw.Draw(im)

x = 0;
for text_color_pair in text:
    t = text_color_pair[0]
    c = text_color_pair[1]
    print("t=" + t + " " + str(c) + " " + str(x))
    draw.text((x, 0), t, c, font=font)
    x = x + font.getsize(t)[0]


im.save("/home/pi/ppmfile/eventoincorso.ppm")

os.system("while pgrep -u root led-matrix > /dev/null; do sleep 1; done")

os.system("mpg123 -m --gain 70 /home/pi/suoni/campanello.mp3 ;" + " bash /home/pi/gvoice.sh & ")
os.system("sudo /home/pi/scrollingtext/rpi-rgb-led-matrix/led-matrix -t " + str(tempo) + " -D 1 /home/pi/ppmfile/eventoincorso.ppm &")
