Skip to content

Commit c04d2c7

Browse files
authored
Merge pull request #928 from caternuson/tft_sidekick
Add code for TFT Sidekick
2 parents 7669d0f + 03c83f4 commit c04d2c7

File tree

4 files changed

+536
-0
lines changed

4 files changed

+536
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import time
2+
from collections import deque
3+
import random
4+
import math
5+
# Blinka CircuitPython
6+
import board
7+
import digitalio
8+
import adafruit_rgb_display.ili9341 as ili9341
9+
# Matplotlib
10+
import matplotlib.pyplot as plt
11+
# Python Imaging Library
12+
from PIL import Image
13+
14+
#pylint: disable=bad-continuation
15+
#==| User Config |========================================================
16+
REFRESH_RATE = 0.5
17+
HIST_SIZE = 61
18+
PLOT_CONFIG = (
19+
#--------------------
20+
# PLOT 1 (upper plot)
21+
#--------------------
22+
{
23+
'line_config' : (
24+
{'color' : '#FF0000', 'width' : 2, 'style' : '--'},
25+
{ },
26+
)
27+
},
28+
#--------------------
29+
# PLOT 2 (lower plot)
30+
#--------------------
31+
{
32+
'title' : 'sin()',
33+
'ylim' : (-1.5, 1.5),
34+
'line_config' : (
35+
{'color' : '#00FF00', 'width' : 4},
36+
)
37+
}
38+
)
39+
40+
def update_data():
41+
''' Do whatever to update your data here. General form is:
42+
y_data[plot][line].append(new_data_point)
43+
'''
44+
# upper plot data
45+
for data in y_data[0]:
46+
data.append(random.random())
47+
48+
# lower plot data
49+
y_data[1][0].append(math.sin(0.5 * time.monotonic()))
50+
51+
#==| User Config |========================================================
52+
#pylint: enable=bad-continuation
53+
54+
# Setup X data storage
55+
x_time = [x * REFRESH_RATE for x in range(HIST_SIZE)]
56+
x_time.reverse()
57+
58+
# Setup Y data storage
59+
y_data = [ [deque([None] * HIST_SIZE, maxlen=HIST_SIZE) for _ in plot['line_config']]
60+
for plot in PLOT_CONFIG
61+
]
62+
63+
# Setup display
64+
disp = ili9341.ILI9341(board.SPI(), baudrate = 24000000,
65+
cs = digitalio.DigitalInOut(board.D4),
66+
dc = digitalio.DigitalInOut(board.D5),
67+
rst = digitalio.DigitalInOut(board.D6))
68+
69+
# Setup plot figure
70+
plt.style.use('dark_background')
71+
fig, ax = plt.subplots(2, 1, figsize=(disp.width / 100, disp.height / 100))
72+
73+
# Setup plot axis
74+
ax[0].xaxis.set_ticklabels([])
75+
for plot, a in enumerate(ax):
76+
# add grid to all plots
77+
a.grid(True, linestyle=':')
78+
# limit and invert x time axis
79+
a.set_xlim(min(x_time), max(x_time))
80+
a.invert_xaxis()
81+
# custom settings
82+
if 'title' in PLOT_CONFIG[plot]:
83+
a.set_title(PLOT_CONFIG[plot]['title'], position=(0.5, 0.8))
84+
if 'ylim' in PLOT_CONFIG[plot]:
85+
a.set_ylim(PLOT_CONFIG[plot]['ylim'])
86+
87+
# Setup plot lines
88+
#pylint: disable=redefined-outer-name
89+
plot_lines = []
90+
for plot, config in enumerate(PLOT_CONFIG):
91+
lines = []
92+
for index, line_config in enumerate(config['line_config']):
93+
# create line
94+
line, = ax[plot].plot(x_time, y_data[plot][index])
95+
# custom settings
96+
if 'color' in line_config:
97+
line.set_color(line_config['color'])
98+
if 'width' in line_config:
99+
line.set_linewidth(line_config['width'])
100+
if 'style' in line_config:
101+
line.set_linestyle(line_config['style'])
102+
# add line to list
103+
lines.append(line)
104+
plot_lines.append(lines)
105+
106+
def update_plot():
107+
# update lines with latest data
108+
for plot, lines in enumerate(plot_lines):
109+
for index, line in enumerate(lines):
110+
line.set_ydata(y_data[plot][index])
111+
# autoscale if not specified
112+
if 'ylim' not in PLOT_CONFIG[plot].keys():
113+
ax[plot].relim()
114+
ax[plot].autoscale_view()
115+
# draw the plots
116+
canvas = plt.get_current_fig_manager().canvas
117+
plt.tight_layout()
118+
canvas.draw()
119+
# transfer into PIL image and display
120+
image = Image.frombytes('RGB', canvas.get_width_height(),
121+
canvas.tostring_rgb())
122+
disp.image(image)
123+
124+
print("looping")
125+
while True:
126+
update_data()
127+
update_plot()
128+
time.sleep(REFRESH_RATE)
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
from collections import deque
2+
import psutil
3+
# Blinka CircuitPython
4+
import board
5+
import digitalio
6+
import adafruit_rgb_display.ili9341 as ili9341
7+
# Matplotlib
8+
import matplotlib.pyplot as plt
9+
# Python Imaging Library
10+
from PIL import Image
11+
12+
#pylint: disable=bad-continuation
13+
#==| User Config |========================================================
14+
REFRESH_RATE = 1
15+
HIST_SIZE = 61
16+
PLOT_CONFIG = (
17+
#--------------------
18+
# PLOT 1 (upper plot)
19+
#--------------------
20+
{
21+
'title' : 'LOAD',
22+
'ylim' : (0, 100),
23+
'line_config' : (
24+
{'color' : '#0000FF', 'width' : 2},
25+
{'color' : '#0060FF', 'width' : 2},
26+
{'color' : '#00FF60', 'width' : 2},
27+
{'color' : '#60FF00', 'width' : 2},
28+
)
29+
},
30+
#--------------------
31+
# PLOT 2 (lower plot)
32+
#--------------------
33+
{
34+
'title' : 'TEMP',
35+
'ylim' : (20, 50),
36+
'line_config' : (
37+
{'color' : '#FF0000', 'width' : 2},
38+
{'color' : '#FF3000', 'width' : 2},
39+
{'color' : '#FF8000', 'width' : 2},
40+
{'color' : '#Ff0080', 'width' : 2},
41+
)
42+
}
43+
)
44+
45+
CPU_COUNT = 4
46+
47+
def update_data():
48+
''' Do whatever to update your data here. General form is:
49+
y_data[plot][line].append(new_data_point)
50+
'''
51+
cpu_percs = psutil.cpu_percent(interval=REFRESH_RATE, percpu=True)
52+
for cpu in range(CPU_COUNT):
53+
y_data[0][cpu].append(cpu_percs[cpu])
54+
55+
cpu_temps = []
56+
for shwtemp in psutil.sensors_temperatures()['coretemp']:
57+
if 'Core' in shwtemp.label:
58+
cpu_temps.append(shwtemp.current)
59+
for cpu in range(CPU_COUNT):
60+
y_data[1][cpu].append(cpu_temps[cpu])
61+
62+
#==| User Config |========================================================
63+
#pylint: enable=bad-continuation
64+
65+
# Setup X data storage
66+
x_time = [x * REFRESH_RATE for x in range(HIST_SIZE)]
67+
x_time.reverse()
68+
69+
# Setup Y data storage
70+
y_data = [ [deque([None] * HIST_SIZE, maxlen=HIST_SIZE) for _ in plot['line_config']]
71+
for plot in PLOT_CONFIG
72+
]
73+
74+
# Setup display
75+
disp = ili9341.ILI9341(board.SPI(), baudrate = 24000000,
76+
cs = digitalio.DigitalInOut(board.D4),
77+
dc = digitalio.DigitalInOut(board.D5),
78+
rst = digitalio.DigitalInOut(board.D6))
79+
80+
# Setup plot figure
81+
plt.style.use('dark_background')
82+
fig, ax = plt.subplots(2, 1, figsize=(disp.width / 100, disp.height / 100))
83+
84+
# Setup plot axis
85+
ax[0].xaxis.set_ticklabels([])
86+
for plot, a in enumerate(ax):
87+
# add grid to all plots
88+
a.grid(True, linestyle=':')
89+
# limit and invert x time axis
90+
a.set_xlim(min(x_time), max(x_time))
91+
a.invert_xaxis()
92+
# custom settings
93+
if 'title' in PLOT_CONFIG[plot]:
94+
a.set_title(PLOT_CONFIG[plot]['title'], position=(0.5, 0.8))
95+
if 'ylim' in PLOT_CONFIG[plot]:
96+
a.set_ylim(PLOT_CONFIG[plot]['ylim'])
97+
98+
# Setup plot lines
99+
#pylint: disable=redefined-outer-name
100+
plot_lines = []
101+
for plot, config in enumerate(PLOT_CONFIG):
102+
lines = []
103+
for index, line_config in enumerate(config['line_config']):
104+
# create line
105+
line, = ax[plot].plot(x_time, y_data[plot][index])
106+
# custom settings
107+
if 'color' in line_config:
108+
line.set_color(line_config['color'])
109+
if 'width' in line_config:
110+
line.set_linewidth(line_config['width'])
111+
if 'style' in line_config:
112+
line.set_linestyle(line_config['style'])
113+
# add line to list
114+
lines.append(line)
115+
plot_lines.append(lines)
116+
117+
def update_plot():
118+
# update lines with latest data
119+
for plot, lines in enumerate(plot_lines):
120+
for index, line in enumerate(lines):
121+
line.set_ydata(y_data[plot][index])
122+
# autoscale if not specified
123+
if 'ylim' not in PLOT_CONFIG[plot].keys():
124+
ax[plot].relim()
125+
ax[plot].autoscale_view()
126+
# draw the plots
127+
canvas = plt.get_current_fig_manager().canvas
128+
plt.tight_layout()
129+
canvas.draw()
130+
# transfer into PIL image and display
131+
image = Image.frombytes('RGB', canvas.get_width_height(),
132+
canvas.tostring_rgb())
133+
disp.image(image)
134+
135+
print("looping")
136+
while True:
137+
update_data()
138+
update_plot()
139+
# update rate controlled by psutil.cpu_percent()

0 commit comments

Comments
 (0)