#!/usr/bin/python
#by SQ6QV
#required: https://github.com/gizuone/sh1106.git

import time

from oled.device import sh1106
from oled.render import canvas

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

import subprocess


disp =  sh1106(port=1, address=0x3C)
width = 128
height = 64

padding = -2
top = padding
bottom = height-padding
x = 1

font = ImageFont.truetype('pm.ttf', 10)
#font = ImageFont.load_default()

while True:
  with canvas(disp) as draw:
    try:
        draw.rectangle((0,0,width,height), outline=0, fill=0)
        IP = subprocess.check_output("hostname -I", shell = True )
        IP = IP.split(" ")[0]
        draw.text((x, top),"IP: " + str(IP),  font=font, fill=255)

        LINES = subprocess.check_output("php /var/www/html/spdxl/display.php", shell = True )
        lines = LINES.splitlines()

        draw.text((x, top+11),lines[1],  font=font, fill=255)
        draw.text((x, top+22),lines[2],  font=font, fill=255)
        draw.text((x, top+33),lines[3],  font=font, fill=255)
        draw.text((x, top+44),lines[4],  font=font, fill=255)
        draw.text((x, top+55),lines[5],  font=font, fill=255)
        time.sleep(1)
    except:
        draw.text((110, top),"E",  font=font, fill=255)

