-
Notifications
You must be signed in to change notification settings - Fork 5
/
display.py
60 lines (50 loc) · 1.87 KB
/
display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Imports the necessary libraries...
import math
import board
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
import config
class display:
def __init__(self):
# init I2C
self.i2c = board.I2C()
self.oled = adafruit_ssd1306.SSD1306_I2C(128, 64, self.i2c, addr=0x3c)
# Clear display.
self.clear()
# Load font and size.
self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", config.font_size)
def clear(self):
# Create blank image for drawing.
self.image = Image.new("1", (self.oled.width, self.oled.height))
self.draw = ImageDraw.Draw(self.image)
self.oled.fill(0)
self.oled.show()
def showtext(self,row):
self.clear()
r = math.ceil(self.draw.textsize(row,self.font)[0]/128) #calculate number of rows
pos = int(128*len(row)/self.draw.textsize(row,self.font)[0])-1 #calculate number of characters in one row
rows = [] #create list of rows
for i in range(r) :
rows.append(row[:pos])
row=row[pos:]
row="\n".join(rows)
self.draw.text((0, 0), row, font=self.font, fill=255)
self.oled.image(self.image)
self.oled.show()
def showimage(self,image):
self.clear()
self.oled.image(image)
self.oled.show()