/ TFT_Sidekick_With_FT232H / tft_sidekick_mem.py
tft_sidekick_mem.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' : 'VIRTUAL',
 27      'ylim' : (0, psutil.virtual_memory().total / 1e9),
 28      'line_config' : (
 29          {'color' : '#00FFFF', 'width' : 2}, # free
 30          {'color' : '#FF00FF', 'width' : 2}, # used
 31          )
 32      },
 33      #--------------------
 34      # PLOT 2 (lower plot)
 35      #--------------------
 36      {
 37      'title' : 'SWAP',
 38      'ylim' : (0, psutil.swap_memory().total / 1e9),
 39      'line_config' : (
 40          {'color' : '#00FF00', 'width' : 2}, # free
 41          {'color' : '#FF0000', 'width' : 2}, # used
 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      vir_mem = psutil.virtual_memory()
 51      y_data[0][0].append(vir_mem.free / 1e9)
 52      y_data[0][1].append(vir_mem.used / 1e9)
 53  
 54      swp_mem = psutil.swap_memory()
 55      y_data[1][0].append(swp_mem.free / 1e9)
 56      y_data[1][1].append(swp_mem.used / 1e9)
 57  
 58  #==| User Config |========================================================
 59  #pylint: enable=bad-continuation
 60  
 61  # Setup X data storage
 62  x_time = [x * REFRESH_RATE for x in range(HIST_SIZE)]
 63  x_time.reverse()
 64  
 65  # Setup Y data storage
 66  y_data = [ [deque([None] * HIST_SIZE, maxlen=HIST_SIZE) for _ in plot['line_config']]
 67             for plot in PLOT_CONFIG
 68           ]
 69  
 70  # Setup display
 71  disp = ili9341.ILI9341(board.SPI(), baudrate = 24000000,
 72                         cs  = digitalio.DigitalInOut(board.D4),
 73                         dc  = digitalio.DigitalInOut(board.D5),
 74                         rst = digitalio.DigitalInOut(board.D6))
 75  
 76  # Setup plot figure
 77  plt.style.use('dark_background')
 78  fig, ax = plt.subplots(2, 1, figsize=(disp.width / 100, disp.height / 100))
 79  
 80  # Setup plot axis
 81  ax[0].xaxis.set_ticklabels([])
 82  for plot, a in enumerate(ax):
 83      # add grid to all plots
 84      a.grid(True, linestyle=':')
 85      # limit and invert x time axis
 86      a.set_xlim(min(x_time), max(x_time))
 87      a.invert_xaxis()
 88      # custom settings
 89      if 'title' in PLOT_CONFIG[plot]:
 90          a.set_title(PLOT_CONFIG[plot]['title'], position=(0.5, 0.8))
 91      if 'ylim' in PLOT_CONFIG[plot]:
 92          a.set_ylim(PLOT_CONFIG[plot]['ylim'])
 93  
 94  # Setup plot lines
 95  #pylint: disable=redefined-outer-name
 96  plot_lines = []
 97  for plot, config in enumerate(PLOT_CONFIG):
 98      lines = []
 99      for index, line_config in enumerate(config['line_config']):
100          # create line
101          line, = ax[plot].plot(x_time, y_data[plot][index])
102          # custom settings
103          if 'color' in line_config:
104              line.set_color(line_config['color'])
105          if 'width' in line_config:
106              line.set_linewidth(line_config['width'])
107          if 'style' in line_config:
108              line.set_linestyle(line_config['style'])
109          # add line to list
110          lines.append(line)
111      plot_lines.append(lines)
112  
113  def update_plot():
114      # update lines with latest data
115      for plot, lines in enumerate(plot_lines):
116          for index, line in enumerate(lines):
117              line.set_ydata(y_data[plot][index])
118          # autoscale if not specified
119          if 'ylim' not in PLOT_CONFIG[plot].keys():
120              ax[plot].relim()
121              ax[plot].autoscale_view()
122      # draw the plots
123      canvas = plt.get_current_fig_manager().canvas
124      plt.tight_layout()
125      canvas.draw()
126      # transfer into PIL image and display
127      image = Image.frombytes('RGB', canvas.get_width_height(),
128                              canvas.tostring_rgb())
129      disp.image(image)
130  
131  print("looping")
132  while True:
133      update_data()
134      update_plot()
135      time.sleep(REFRESH_RATE)