#!/usr/bin/python
# coding=utf-8
# Temperatur von Sensor auslesen auf OLED Display anzeigen
# (c) Sebastian Hemel, www.shemel.de
#-----------------------------------------------------------------------------------
import time
import datetime
import re
import os, sys, threading

# 0,96 Zoll OLED Display 128x64 Pixel
import Adafruit_SSD1306
from PIL import Image, ImageDraw, ImageFont

a=u"°" # damit Sonderzeichen korrekt dargestellt wird 
 
#Quelle: http://www.schnatterente.net/code/raspberrypi/temperature.py
############################################################################################
# Temperatursensor Name ermitteln
SensorPfad = "/sys/bus/w1/devices/" # Pfad
SensorNames = open("/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves" ,"r")
Sensor = SensorNames.readline()
if Sensor:
	SensorNames.close()	
	
# Temperatur ermitteln und Wert value zurckgeben
def GetTemp():
	a = open(SensorPfad+Sensor.strip()+'/w1_slave', 'r') # Ermittelten Sensor lesen
	Temp = a.readline()

	if re.match(r"([0-9a-f]{2} ){9}: crc=[0-9a-f]{2} YES", Temp): # Erste Zeile auf "YES" am Ende uberprufen
		Temp = a.readline()
		m = re.match(r"([0-9a-f]{2} ){9}t=([+-]?[0-9]+)", Temp) # Wert aus zweiter Zeile prufen 
	if m:
		value = str(float(m.group(2)) / 1000.0) # Wert nach t= auslesen und umwandeln
	a.close()
	return value # Ruckgabewert "value" definieren
# Temperatur zurueckgeben
############################################################################################
	
# Display einrichten
 
# Raspberry Pi pin configuration:
RST = 24
 
# Display 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
 
# Initialize library.
disp.begin()
 
# Clear display.
disp.clear()
disp.display()
 
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
 
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
 
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# First define some constants to allow easy resizing of shapes.
padding = 2
shape_width = 20
top = padding
bottom = height-padding
 
# Move left to right keeping track of the current x position for drawing shapes.
x = padding
 
# Load default font.
#font = ImageFont.load_default() #
# Alternatively load a TTF font. => https://www.dafont.com/bitmap.php
font12 = ImageFont.truetype("font/arial.ttf", 12) # Schriftart, Schriftgröße
font20 = ImageFont.truetype("font/arial.ttf", 20)
font15 = ImageFont.truetype("font/arial.ttf", 14)

# Write two lines of text.
draw.text((x, top+15), ' (c) Sebastian Hemel',  font=font12, fill=255)
draw.text((x, top+30), '     www.shemel.de  ',  font=font12, fill=255)
 
# Display image.
disp.image(image)
disp.display()

time.sleep(5) # 5sec warten, damit Sensor ausgelesen werden kann

while True: # endlos Schleife
	try: # Fehler abfangen?
	
		now=datetime.datetime.now()
		Zeit = now.strftime("%d.%m.%Y - %H:%M:%S") # Aktuelles Datum + Uhrzeit
		Temp = GetTemp() # Temperatur messen....
		draw.rectangle((0,0,width,height), outline=0, fill=0) #clear Display
		draw.text((x, top), Zeit,  font=font12, fill=255)
		draw.text((x, top+15), '-------------------------------',  font=font12, fill=255)
		draw.text((x, top+30), Temp+' '+a+'C',  font=font20, fill=255)
		disp.image(image)
		disp.display()
		
		time.sleep(5) # 5sec warten, bis Sensor erneut abgefragt wird
	
	except KeyboardInterrupt: # STR+C = Abbruch
		draw.rectangle((0,0,width,height), outline=0, fill=0) #clear Display
		disp.image(image)
		disp.display()