-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholedshowstatus.py
executable file
·146 lines (128 loc) · 3.95 KB
/
oledshowstatus.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
#!/usr/bin/python3
import sys
import os
import re
import time
from PIL import Image, ImageDraw, ImageFont
import board
import busio
import adafruit_ssd1306
def load_font(sz):
s = os.getenv('OLED_FONT')
if s:
font = ImageFont.truetype(s, sz)
else:
font = ImageFont.load_default()
return font
def main(args):
i2c = busio.I2C(board.SCL, board.SDA)
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c)
if len(args) > 0 and args[0] == 'clear':
exit(0)
font = load_font(14)
base_loop = float(15) #30s
ratio = 60 # 1 ip, 59 info
oled.fill(0)
c = 0
img = None
while True:
if c == 0:
img = show_ip(oled, font, None)
c = ratio
else:
if c % 2 != 0:
show_info(oled, font, None)
else:
img = show_ip(oled, font, img)
# info
c -= 1
time.sleep(base_loop)
def show_ip(oled, font, image):
if image is None:
image = Image.new("1", (oled.width, oled.height))
draw = ImageDraw.Draw(image)
x = 2
y = 2
cy = y
data = get_inner_ip()
if len(data) == 0:
return None
for (k, v) in data.items():
text = k + ': ' + v
(font_width, font_height) = font.getsize(text)
draw.text((x, cy), text, font=font, fill=255)
cy += (font_height + y)
#oled.fill(0)
oled.image(image)
oled.show()
return image
def show_info(oled, font, image):
if image is None:
image = Image.new("1", (oled.width, oled.height))
draw = ImageDraw.Draw(image)
x = 2
y = 2
cy = y
t1 = 'T: %.1fC' % (get_temperature())
t2 = 'CPU: %.2f %.2f %.2f' % get_cpu_average_load()
t3 = get_memory_usage()
t3 = ((t3[0] - t3[1] - t3[2] - t3[3]) / 1024, t3[0] / 1024)
t3 = 'MEM: %d/%dMB' % t3
for text in (t1, t2, t3):
(font_width, font_height) = font.getsize(text)
draw.text((x, cy), text, font=font, fill=255)
cy += (font_height + y)
#oled.fill(0)
oled.image(image)
oled.show()
def prepare(w, h, font, texts):
image = Image.new("1", (w, h))
draw = ImageDraw.Draw(image)
y = 2
for text in texts:
(font_width, font_height) = font.getsize(text)
draw.text(
(2, y),
text,
font=font,
fill=255,
)
y = y + font_height + 2
return image
def get_temperature():
with open('/sys/class/thermal/thermal_zone0/temp', 'r') as ifile:
t = int(ifile.read(5))
return t / 1000
def get_cpu_average_load():
with open('/proc/loadavg', 'r') as ifile:
t = ifile.read()
t = t.split(' ')
return (float(t[0]), float(t[1]), float(t[2])) # 1min 5min 15min
def get_memory_usage():
pattern = re.compile(r'(\S+):\s*(\d+) kB')
items = ('MemTotal', 'MemFree', 'Buffers', 'Cached')
with open('/proc/meminfo', 'r') as ifile:
t = ifile.readline()
data = dict()
while t:
m = pattern.match(t)
if m is not None:
item = m.group(1)
value = int(m.group(2))
if item in items:
data[item] = value
if len(data) == len(items):
return (data[items[0]], data[items[1]], data[items[2]], data[items[3]])
t = ifile.readline()
def get_inner_ip():
import getifaddrs
from socket import AF_INET
data = dict()
for t in getifaddrs.getifaddrs():
if t.name != b'lo' and t.family == AF_INET:
name = t.name.decode()
ip = t.addr[0]
data[name] = ip
return data
if __name__ == '__main__':
main(sys.argv[1:])