-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathcarousel.py
More file actions
executable file
·112 lines (86 loc) · 3.41 KB
/
carousel.py
File metadata and controls
executable file
·112 lines (86 loc) · 3.41 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-18 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK
"""
Showcase viewport and hotspot functionality.
Loosely based on poster_demo by @bjerrep
https://github.com/bjerrep/ssd1306/blob/master/examples/poster_demo.py
Needs psutil (+ dependencies) installed::
$ sudo apt-get install python-dev
$ sudo pip install psutil
"""
import psutil
from demo_opts import get_device
from luma.core.virtual import viewport, snapshot
from hotspot import memory, uptime, cpu_load, clock, network, disk
def position(max):
forwards = range(0, max)
backwards = range(max, 0, -1)
while True:
for x in forwards:
yield x
for x in backwards:
yield x
def pause_every(interval, generator):
try:
while True:
x = next(generator)
if x % interval == 0:
for _ in range(20):
yield x
else:
yield x
except StopIteration:
pass
def intersect(a, b):
return list(set(a) & set(b))
def first(iterable, default=None):
if iterable:
for item in iterable:
return item
return default
def main():
if device.rotate in (0, 2):
# Horizontal
widget_width = device.width // 2
widget_height = device.height
else:
# Vertical
widget_width = device.width
widget_height = device.height // 2
# Either function or subclass
# cpuload = hotspot(widget_width, widget_height, cpu_load.render)
# cpuload = cpu_load.CPU_Load(widget_width, widget_height, interval=1.0)
utime = snapshot(widget_width, widget_height, uptime.render, interval=1.0)
mem = snapshot(widget_width, widget_height, memory.render, interval=2.0)
dsk = snapshot(widget_width, widget_height, disk.render, interval=2.0)
cpuload = snapshot(widget_width, widget_height, cpu_load.render, interval=0.5)
clk = snapshot(widget_width, widget_height, clock.render, interval=1.0)
network_ifs = psutil.net_if_stats().keys()
wlan = first(intersect(network_ifs, ["wlan0", "wl0"]), "wlan0")
eth = first(intersect(network_ifs, ["eth0", "en0"]), "eth0")
lo = first(intersect(network_ifs, ["lo", "lo0"]), "lo")
net_wlan = snapshot(widget_width, widget_height, network.stats(wlan), interval=2.0)
net_eth = snapshot(widget_width, widget_height, network.stats(eth), interval=2.0)
net_lo = snapshot(widget_width, widget_height, network.stats(lo), interval=2.0)
widgets = [cpuload, utime, clk, net_wlan, net_eth, net_lo, mem, dsk]
if device.rotate in (0, 2):
virtual = viewport(device, width=widget_width * len(widgets), height=widget_height)
for i, widget in enumerate(widgets):
virtual.add_hotspot(widget, (i * widget_width, 0))
for x in pause_every(widget_width, position(widget_width * (len(widgets) - 2))):
virtual.set_position((x, 0))
else:
virtual = viewport(device, width=widget_width, height=widget_height * len(widgets))
for i, widget in enumerate(widgets):
virtual.add_hotspot(widget, (0, i * widget_height))
for y in pause_every(widget_height, position(widget_height * (len(widgets) - 2))):
virtual.set_position((0, y))
if __name__ == "__main__":
try:
device = get_device()
main()
except KeyboardInterrupt:
pass