SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Copyright (c) 2017 Adafruit Industries
  2. # Author: Tony DiCola & James DeVito
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. import time
  22.  
  23. import Adafruit_SSD1306
  24.  
  25. from PIL import Image
  26. from PIL import ImageDraw
  27. from PIL import ImageFont
  28.  
  29.  
  30. import subprocess
  31.  
  32. # 128x32 display with hardware I2C:
  33. disp = Adafruit_SSD1306.SSD1306_128_32(rst=None, i2c_bus=1, gpio=1) # setting gpio to 1 is hack to avoid platform detection
  34.  
  35. # Initialize library.
  36. disp.begin()
  37.  
  38. # Clear display.
  39. disp.clear()
  40. disp.display()
  41.  
  42. # Create blank image for drawing.
  43. # Make sure to create image with mode '1' for 1-bit color.
  44. width = disp.width
  45. height = disp.height
  46. image = Image.new('1', (width, height))
  47.  
  48. # Get drawing object to draw on image.
  49. draw = ImageDraw.Draw(image)
  50.  
  51. # Draw a black filled box to clear the image.
  52. draw.rectangle((0,0,width,height), outline=0, fill=0)
  53.  
  54. # Draw some shapes.
  55. # First define some constants to allow easy resizing of shapes.
  56. padding = -4
  57. top = padding
  58. bottom = height-padding
  59. # Move left to right keeping track of the current x position for drawing shapes.
  60. x = 0
  61.  
  62. # Load default font.
  63. font = ImageFont.load_default()
  64.  
  65. def cpu_temp():
  66.      tempF = (((int(open('/sys/class/thermal/thermal_zone1/temp').read()) / 1000)*9/5)+32)
  67.      return "CPU TEMP: %sF" % str(tempF)
  68.  
  69.  
  70. def gpu_temp():
  71.      tempF = (((int(open('/sys/class/thermal/thermal_zone2/temp').read()) / 1000)*9/5)+32)
  72.      return "GPU TEMP: %sF" % str(tempF)
  73.    
  74.  
  75.  
  76. while True:
  77.  
  78.     # Draw a black filled box to clear the image.
  79.     draw.rectangle((0,0,width,height), outline=0, fill=0)
  80.  
  81.     # Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
  82.     cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
  83.     CPU = subprocess.check_output(cmd, shell = True )
  84.     cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s %.2f%%\", $3,$2,$3*100/$2 }'"
  85.     MemUsage = subprocess.check_output(cmd, shell = True )
  86.     cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
  87.     Disk = subprocess.check_output(cmd, shell = True )
  88.  
  89.  
  90.     draw.text((x, top+2), gpu_temp(), font=font, fill=255)
  91.     draw.text((x, top+11), cpu_temp(), font=font, fill=255)
  92.     draw.text((x, top+19),    str(MemUsage.decode('utf-8')),  font=font, fill=255)
  93.     draw.text((x, top+27),    str(CPU.decode('utf-8')),  font=font, fill=255)
  94.  
  95.     # Display image.
  96.     disp.image(image)
  97.     disp.display()
  98.     time.sleep(1)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top