/ TFT_Sidekick_With_FT232H / tft_sidekick_net.py
tft_sidekick_net.py
1 # SPDX-FileCopyrightText: 2019 Carter Nelson for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 from collections import deque 7 import psutil 8 # Blinka CircuitPython 9 import board 10 import digitalio 11 import adafruit_rgb_display.ili9341 as ili9341 12 # Matplotlib 13 import matplotlib.pyplot as plt 14 # Python Imaging Library 15 from PIL import Image 16 17 #pylint: disable=bad-continuation 18 #==| User Config |======================================================== 19 REFRESH_RATE = 1 20 HIST_SIZE = 61 21 PLOT_CONFIG = ( 22 #-------------------- 23 # PLOT 1 (upper plot) 24 #-------------------- 25 { 26 'title' : 'RATE (MBPS)', 27 'ylim' : (0, 1), 28 'line_config' : ( 29 {'color' : '#AAFF00', 'width' : 2}, # sent 30 {'color' : '#00AAFF', 'width' : 2}, # recv 31 ) 32 }, 33 #-------------------- 34 # PLOT 2 (lower plot) 35 #-------------------- 36 { 37 'title' : 'TOTAL (GB)', 38 'ylim' : (0, 1), 39 'line_config' : ( 40 {'color' : '#AAFF00', 'width' : 2}, # sent 41 {'color' : '#00AAFF', 'width' : 2}, # recv 42 ) 43 } 44 ) 45 46 def update_data(): 47 ''' Do whatever to update your data here. General form is: 48 y_data[plot][line].append(new_data_point) 49 ''' 50 # get two data points 51 net_start = psutil.net_io_counters() 52 time.sleep(REFRESH_RATE) 53 net_finish = psutil.net_io_counters() 54 55 # rate is d()/dt 56 BPS_sent = (net_finish.bytes_sent - net_start.bytes_sent) / REFRESH_RATE 57 BPS_recv = (net_finish.bytes_recv - net_start.bytes_recv) / REFRESH_RATE 58 y_data[0][0].append(BPS_sent / 1e6) 59 y_data[0][1].append(BPS_recv / 1e6) 60 61 # total is just last 62 y_data[1][0].append(net_finish.bytes_sent / 1e9) 63 y_data[1][1].append(net_finish.bytes_recv / 1e9) 64 65 #==| User Config |======================================================== 66 #pylint: enable=bad-continuation 67 68 # Setup X data storage 69 x_time = [x * REFRESH_RATE for x in range(HIST_SIZE)] 70 x_time.reverse() 71 72 # Setup Y data storage 73 y_data = [ [deque([None] * HIST_SIZE, maxlen=HIST_SIZE) for _ in plot['line_config']] 74 for plot in PLOT_CONFIG 75 ] 76 77 # Setup display 78 disp = ili9341.ILI9341(board.SPI(), baudrate = 24000000, 79 cs = digitalio.DigitalInOut(board.D4), 80 dc = digitalio.DigitalInOut(board.D5), 81 rst = digitalio.DigitalInOut(board.D6)) 82 83 # Setup plot figure 84 plt.style.use('dark_background') 85 fig, ax = plt.subplots(2, 1, figsize=(disp.width / 100, disp.height / 100)) 86 87 # Setup plot axis 88 ax[0].xaxis.set_ticklabels([]) 89 for plot, a in enumerate(ax): 90 # add grid to all plots 91 a.grid(True, linestyle=':') 92 # limit and invert x time axis 93 a.set_xlim(min(x_time), max(x_time)) 94 a.invert_xaxis() 95 # custom settings 96 if 'title' in PLOT_CONFIG[plot]: 97 a.set_title(PLOT_CONFIG[plot]['title'], position=(0.5, 0.8)) 98 if 'ylim' in PLOT_CONFIG[plot]: 99 a.set_ylim(PLOT_CONFIG[plot]['ylim']) 100 101 # Setup plot lines 102 #pylint: disable=redefined-outer-name 103 plot_lines = [] 104 for plot, config in enumerate(PLOT_CONFIG): 105 lines = [] 106 for index, line_config in enumerate(config['line_config']): 107 # create line 108 line, = ax[plot].plot(x_time, y_data[plot][index]) 109 # custom settings 110 if 'color' in line_config: 111 line.set_color(line_config['color']) 112 if 'width' in line_config: 113 line.set_linewidth(line_config['width']) 114 if 'style' in line_config: 115 line.set_linestyle(line_config['style']) 116 # add line to list 117 lines.append(line) 118 plot_lines.append(lines) 119 120 def update_plot(): 121 # update lines with latest data 122 for plot, lines in enumerate(plot_lines): 123 for index, line in enumerate(lines): 124 line.set_ydata(y_data[plot][index]) 125 # autoscale if not specified 126 if 'ylim' not in PLOT_CONFIG[plot].keys(): 127 ax[plot].relim() 128 ax[plot].autoscale_view() 129 # draw the plots 130 canvas = plt.get_current_fig_manager().canvas 131 plt.tight_layout() 132 canvas.draw() 133 # transfer into PIL image and display 134 image = Image.frombytes('RGB', canvas.get_width_height(), 135 canvas.tostring_rgb()) 136 disp.image(image) 137 138 print("looping") 139 while True: 140 update_data() 141 update_plot() 142 # update rate controlled in update_data()