From Hentschel
Jump to: navigation, search

Beaglebone black

3,183 bytes added, 07:06, 7 December 2019
/* lcd + button script */
=== lcd + button script ===
==== Raspi ====
<pre>
#!/usr/bin/python
 
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_GPIO.GPIO as GPIO
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import subprocess
import socket
import psutil
import os
 
from gpiozero import CPUTemperature
from datetime import datetime
 
button_off = 4
gpio = GPIO.get_platform_gpio()
 
while True:
try:
gpio.setup(button_off, GPIO.IN, GPIO.PUD_UP)
button_oldstate = gpio.input(button_off)
#
RST = 0
#
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
disp.begin()
disp.clear()
disp.display()
#
width = disp.width
height = disp.height
#
image1 = Image.new('1', (width, height))
draw = ImageDraw.Draw(image1)
draw.rectangle((0,0,width,height), outline=0, fill=0)
#
padding = -2
top = padding
#
bottom = height-padding
x = 0
font = ImageFont.load_default()
#
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("192.168.1.1", 80))
hostname = socket.gethostname()
ip = s.getsockname()[0]
firstline = ip + " " + hostname
#
while True:
time.sleep(1)
#
image1 = Image.new('1', (width, height))
draw = ImageDraw.Draw(image1)
draw.rectangle((0,0,width,height), outline=0, fill=0)
cputemp = CPUTemperature()
memstr = '{0: >5}'.format(str(int(psutil.virtual_memory().percent))) + "%|"
statvfs = os.statvfs('/')
disktotal = statvfs.f_frsize * statvfs.f_blocks # Size of filesystem in bytes
diskfree = statvfs.f_frsize * statvfs.f_bavail # Number of free bytes that ordinary users
diskpercent = round((disktotal - diskfree) * 100.0 / float(disktotal),0)
diskstr = 'SD:' + '{0: >3}'.format(str(int(diskpercent))) + "%"
now = datetime.now()
tempstr ='{0: >5}'.format(str(round(cputemp.temperature,))) + u"\u00b0" + "C|"
cpustr = '{0: >6}'.format(str(round(psutil.cpu_percent(),1))) + "%"
secondline = "CPU:" + tempstr + cpustr
thirdline = "MEM: " + memstr + diskstr
fourthline = now.strftime("%d/%m/%Y %H:%M:%S")
disp.clear()
draw.text((x, top), firstline, font=font, fill=255)
draw.text((x, top+8), secondline, font=font, fill=255)
draw.text((x, top+16), thirdline, font=font, fill=255)
draw.text((x, top+25), fourthline, font=font, fill=255)
disp.image(image1)
disp.display()
#
#
button_state = gpio.input(button_off)
if( button_state != button_oldstate ):
if( button_state ):
print("Button OFF")
else:
print("Button ON -> reboot")
disp.clear()
image1 = Image.new('1', (width, height))
draw = ImageDraw.Draw(image1)
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((x, top + 8), "Rebooting...", font=font, fill=255)
disp.image(image1)
disp.display()
os.system("reboot now")
# exit()
button_oldstate = button_state
#
#
except Exception as e:
print("LCD Error: %s" % str(e) )
 
</pre>
 
==== standard BBB ====
<pre>