153 lines
5.2 KiB
Python
153 lines
5.2 KiB
Python
# import math
|
|
# import time
|
|
|
|
# import Adafruit_GPIO.SPI as SPI
|
|
import Adafruit_SSD1306
|
|
|
|
from PIL import Image
|
|
from PIL import ImageFont
|
|
from PIL import ImageDraw
|
|
|
|
|
|
# Raspberry Pi pin configuration:
|
|
RST = 24
|
|
I2C_ADDRESS = 0x3C # 011110+SA0+RW - 0x3C or 0x3D
|
|
|
|
|
|
class StabDisplay:
|
|
|
|
def __init__(self, i2c_address=I2C_ADDRESS):
|
|
|
|
print('__init__(self, ' + str(i2c_address))
|
|
|
|
self.i2c_address = i2c_address
|
|
|
|
self.disp = None
|
|
self.width = None
|
|
self.height = None
|
|
self.image = None
|
|
self.draw = None
|
|
|
|
self.init_hw()
|
|
self.volt = ''
|
|
self.cur = ''
|
|
self.watt = ''
|
|
self.daily = ''
|
|
self.sum = ''
|
|
self.wtemp = ''
|
|
self.gtemp = ''
|
|
self.state = 0
|
|
|
|
# Load default font.
|
|
# self.font = ImageFont.load_default()
|
|
# Load font
|
|
self.font_s = ImageFont.truetype('Paul-le1V.ttf', 13)
|
|
self.font_vs = ImageFont.truetype('Paul-le1V.ttf', 10)
|
|
# self.font_b = ImageFont.truetype('bigshot.ttf', 20)
|
|
self.font_b = ImageFont.truetype('gotham-black.ttf', 19)
|
|
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as this python script!
|
|
# Some nice fonts to try: http://www.dafont.com/bitmap.php
|
|
# font = ImageFont.truetype('Minecraftia.ttf', 8)
|
|
|
|
def init_hw(self):
|
|
|
|
try:
|
|
# 128x64 display with hardware I2C:
|
|
self.disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_address=self.i2c_address)
|
|
|
|
# Initialize library.
|
|
self.disp.begin()
|
|
|
|
# Get display width and height.
|
|
self.width = self.disp.width
|
|
self.height = self.disp.height
|
|
|
|
# Clear display.
|
|
self.disp.clear()
|
|
self.disp.display()
|
|
|
|
# Create image buffer.
|
|
# Make sure to create image with mode '1' for 1-bit color.
|
|
self.image = Image.new('1', (self.width, self.height))
|
|
|
|
# Create drawing object.
|
|
self.draw = ImageDraw.Draw(self.image)
|
|
|
|
except Exception as e:
|
|
self.online = False
|
|
else:
|
|
self.online = True
|
|
|
|
def set_values(self, v, c, w, d, s, wt, gt):
|
|
|
|
# print('StabDisplay::set_values(' + v + ', ' + c + ', ' + w + ', ' + d + ', ' + s + ', ' + wt + ', ' + gt + ' )')
|
|
self.volt = v
|
|
self.cur = c
|
|
self.watt = w
|
|
self.daily = d
|
|
self.sum = s
|
|
self.wtemp = wt
|
|
self.gtemp = gt
|
|
|
|
def update(self):
|
|
|
|
# print('StabDisplay::update()', self.volt, self.cur, self.watt, self.daily, self.sum, self.wtemp, self.gtemp)
|
|
|
|
if self.online:
|
|
|
|
# Clear image buffer by drawing a black filled box.
|
|
self.draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
|
|
|
|
# #################################
|
|
# # 'aktuelle Leistung' #
|
|
# # \ 'Spannung' 'Strom' #
|
|
# # 'Gesammt kWh' 'Tag Wh' #
|
|
# # 'Wassertemp' 'GeraeteTemp' #
|
|
# #################################
|
|
|
|
char_left, char_top, char_right, char_bottom = self.font_b.getbbox(self.watt + ' W')
|
|
self.draw.text((self.width - char_right - 1, 1), self.watt + ' W', font=self.font_b, fill=255)
|
|
|
|
rest = char_bottom
|
|
progress_list = ['|', '/', '-', '\\']
|
|
self.draw.text((5, rest + 10), progress_list[self.state], font=self.font_s, fill=255)
|
|
self.state = self.state + 1
|
|
if self.state > 3:
|
|
self.state = 0
|
|
|
|
char_left, char_top, char_right, char_bottom = self.font_s.getbbox(self.volt + ' V')
|
|
self.draw.text((self.width / 2 - char_right - 7, rest + 5), self.volt + ' V', font=self.font_s, fill=255)
|
|
|
|
char_left, char_top, char_right, char_bottom = self.font_s.getbbox(self.cur + ' A')
|
|
self.draw.text((self.width - char_right - 7, rest + 5), self.cur + ' A', font=self.font_s, fill=255)
|
|
|
|
rest = rest + char_bottom
|
|
lsum = len(self.sum)
|
|
ssum = self.sum[:lsum-3]
|
|
|
|
char_left, char_top, char_right, char_bottom = self.font_s.getbbox(ssum + ' kWh')
|
|
self.draw.text((self.width / 2 - char_right - 7, rest + 10), ssum + ' kWh', font=self.font_s, fill=255)
|
|
|
|
char_left, char_top, char_right, char_bottom = self.font_s.getbbox(self.daily + ' Wh')
|
|
self.draw.text((self.width - char_right - 7, rest + 10), self.daily + ' Wh', font=self.font_s, fill=255)
|
|
|
|
rest = rest + char_bottom
|
|
lwt = len(self.wtemp)
|
|
char_left, char_top, char_right, char_bottom = self.font_vs.getbbox('W: ' + self.wtemp[:lwt-1] + ' °C')
|
|
self.draw.text((self.width / 2 - char_right - 7, rest + 15), 'W: ' + self.wtemp[:lwt-1] + ' °C', font=self.font_vs, fill=255)
|
|
|
|
char_left, char_top, char_right, char_bottom = self.font_vs.getbbox('G: ' + self.gtemp + ' °C')
|
|
self.draw.text((self.width - char_right - 7, rest + 15), 'G: ' + self.gtemp + ' °C', font=self.font_vs, fill=255)
|
|
|
|
self.disp.image(self.image)
|
|
self.disp.display()
|
|
|
|
else:
|
|
self.init_hw()
|
|
if self.online:
|
|
self.update()
|
|
|
|
|
|
|
|
|