|
| 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