#!/usr/bin/python

import os
import time
import datetime
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
		

f = open("/home/pi/events/oggiA.txt","r")
g = open("/home/pi/events/domaniA.txt","r") 

Testo1 = f.read()
Testo2 = g.read()
today = str(datetime.date.today().strftime("%d/%m"))
tomorrow = str((datetime.date.today() + datetime.timedelta(days=1)).strftime("%d/%m"))
print Testo1 + Testo2

text = (("                   > > > > > > > > > >    Today events  ", ( 255, 0, 0,)),(today,(255,255,0)),("    ",(0,0,0)),(Testo1, (0, 255, 0)),(" < < < < < < < < < < < ", ( 255, 0, 0,)),("> > > > > > > > > >    Tomorrow events  ", ( 255, 0, 0,)),(tomorrow, (255,255,0)),("    ",(0,0,0)),(Testo2, (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)/31 * 2
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/events.ppm")

os.system("while pgrep -u root led-matrix > /dev/null; do sleep 1; done")
os.system("mpg123 -m --gain 20 /home/pi/suoni/sonar.mp3 &")
os.system("sudo /home/pi/stilltext/rpi-rgb-led-matrix/led-matrix -r 16 -c 4 -t 4 -D 1 /home/pi/ppmfile/calendar.ppm ;")
os.system("sudo /home/pi/scrollingtext/rpi-rgb-led-matrix/led-matrix -t " + str(tempo) + " -D 1 /home/pi/ppmfile/events.ppm")

