-
Notifications
You must be signed in to change notification settings - Fork 0
/
oled.py
211 lines (174 loc) · 6.83 KB
/
oled.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -----------------------------------------------------------
# oled.py
# created 30.04.2021
# Thomas Kaulke, kaulketh@gmail.com
# https://github.com/kaulketh
# -----------------------------------------------------------
import subprocess
from io import BytesIO
from sys import stderr
from time import sleep
from PIL import ImageFont, Image
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306
from dht import get_values
serial = i2c(port=1, address=0x3C)
display = ssd1306(serial)
SPACE = " "
left = 0
top = 0
# Load fonts
# font = ImageFont.load_default()
# Alternatively load a TTF font.
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
__font_folder = "/home/pi/oled/fonts/"
def __load_font(font_file, size):
"""
To avoid issue related to how Unicode file paths are handled on platforms,
read the font file in binary form.
"""
with open(font_file, "rb") as f:
bytes_font = BytesIO(f.read())
return ImageFont.truetype(bytes_font, size)
tahoma = __load_font(f"{__font_folder}tahoma.ttf", 30)
dejavu = __load_font(f"{__font_folder}dejavu.ttf", 28)
arial_10 = __load_font(f"{__font_folder}arial.ttf", 10)
arial_12 = __load_font(f"{__font_folder}arial.ttf", 12)
free_sans_10 = __load_font(f"{__font_folder}FreeSans.ttf", 10)
free_sans_12 = __load_font(f"{__font_folder}FreeSans.ttf", 12)
free_sans_16 = __load_font(f"{__font_folder}FreeSans.ttf", 16)
def __get_core_temp():
temp = open('/sys/class/thermal/thermal_zone0/temp').read()
temp_str = f"{temp[0]}{temp[1]},{temp[2]}{temp[3]}{temp[4]}°C"
return temp_str
def __show_core_temperature(x=left, font=ImageFont.load_default(),
showtime=5.0):
temp = __get_core_temp()
with canvas(display) as draw:
draw.text((x, top),
"RasPi Temperature",
font=ImageFont.load_default(), fill=255)
draw.text((x, display.height / 3 - 3),
temp, font=font, fill=255)
sleep(showtime)
def __show_pi(showtime=5.0):
with canvas(display) as draw:
draw.rectangle((32, top - 3, 95, 63), outline=1, fill=1)
draw.bitmap((32, top - 3), Image.open('pi_logo.png'), fill=0)
sleep(showtime)
def __show_humidity(x=left, font=ImageFont.load_default(), showtime=5.0):
hum = get_values()[0]
with canvas(display) as draw:
draw.text((x, top),
"Humidity", font=ImageFont.load_default(), fill=255)
draw.text((x, display.height / 3 - 3),
hum, font=font, fill=255)
sleep(showtime)
def __show_temperature(x=left, font=ImageFont.load_default(), showtime=5.0):
temp = get_values()[1]
with canvas(display) as draw:
draw.text((x, top),
"Temperature", font=ImageFont.load_default(), fill=255)
draw.text((x, display.height / 3 - 3),
temp, font=font, fill=255)
sleep(showtime)
def __show_hum_temp(line1, line2, x=left,
font=ImageFont.load_default(), showtime=5.0):
hum, temp = get_values()[0:2]
with canvas(display) as draw:
draw.text((x, line1), f"H {hum}", font=font, fill=255)
draw.text((x, line2), f"T {temp}", font=font, fill=255)
sleep(showtime)
def __show_separate_hum_temp_(x=left, font=ImageFont.load_default(),
showtime=5.0):
hum, temp = get_values()[0:2]
with canvas(display) as draw:
draw.text((x, top),
"Humidity", font=ImageFont.load_default(), fill=255)
draw.text((x, display.height / 3 - 3),
hum, font=font, fill=255)
sleep(showtime)
with canvas(display) as draw:
draw.text((x, top),
"Temperature", font=ImageFont.load_default(), fill=255)
draw.text((x, display.height / 3 - 3),
temp, font=font, fill=255)
sleep(showtime)
def __show_states(font=ImageFont.load_default(), single_line=False,
showtime=5.0):
"""
Shell scripts for system monitoring from here :
https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
"""
# IP
cmd = "hostname -I | cut -d\' \' -f1"
ip = subprocess.check_output(cmd, shell=True).decode('utf-8')
# CPU load
cmd = "top - bn1 | grep \"Cpu(s)\" | sed \"s/.*, *\\([0-9.]*\\)%* " \
"id.*/\\1/\" | " \
"awk '{print \"CPU Load : \"100 - $1\"%\"}'"
# cmd = "top -bn1 | grep load | awk '{printf \"CPU Load : %.2f\",
# $(NF-2)}'"
cpu = subprocess.check_output(cmd, shell=True).decode('utf-8')
# Memory usage
cmd = "free -m | awk 'NR==2{printf \"Mem : %s / %s MB %.0f%%\", $3,$2," \
"$3*100/$2 }' "
mem_usage = subprocess.check_output(cmd, shell=True).decode('utf-8')
# HDD usage
cmd = "df -h | awk '$NF==\"/\"{printf \"Disk : %d / %d GB %s\", $3,$2,$5}'"
disk = subprocess.check_output(cmd, shell=True).decode('utf-8')
if single_line:
with canvas(display) as draw:
draw.text((left, display.height / 3),
f"IP: {ip}", font=font, fill=255)
sleep(showtime)
with canvas(display) as draw:
draw.text((left, display.height / 3),
cpu, font=font, fill=255)
sleep(showtime)
with canvas(display) as draw:
draw.text((left, display.height / 3),
mem_usage, font=font, fill=255)
sleep(showtime)
with canvas(display) as draw:
draw.text((left, display.height / 3),
mem_usage, font=font, fill=255)
sleep(showtime)
else:
with canvas(display) as draw:
draw.text((left, top),
f"IP: {ip}", font=font, fill="white")
draw.text((left, top + 15),
cpu, font=font, fill="white")
draw.text((left, top + 30),
mem_usage, font=font, fill="white")
draw.text((left, top + 45),
disk, font=font, fill="white")
sleep(showtime)
def run_at_128x64():
i = 0
while i < 40: # about 15 sec?
__show_states(font=free_sans_10, showtime=0.01)
i += 1
i = 0
while i < 4:
__show_hum_temp(top + 3, top + 35, x=5, font=tahoma, showtime=4)
i += 1
__show_core_temperature(x=5, font=tahoma)
def run_at_128x32():
__show_states(font=free_sans_10, single_line=True)
__show_hum_temp(top + 5, top + 37, font=tahoma)
if __name__ == '__main__':
while True:
try:
run_at_128x64()
except KeyboardInterrupt:
stderr.write("Oled interrupted.\n")
exit()
except Exception as e:
stderr.write(f"OLED: Any error or exception occurred! , {e}\n")
finally:
display.clear()