Add timer to turn off screen
This commit is contained in:
+51
-41
@@ -47,52 +47,62 @@ top = padding
|
|||||||
bottom = height-padding
|
bottom = height-padding
|
||||||
xpos = 0
|
xpos = 0
|
||||||
|
|
||||||
|
SCREEN_SAVER = 30.0
|
||||||
|
press = 1
|
||||||
state = 0
|
state = 0
|
||||||
press = 0
|
start = time.time()
|
||||||
|
drawn = time.time()
|
||||||
try:
|
try:
|
||||||
while 1:
|
while 1:
|
||||||
|
stamp = time.time()
|
||||||
if state == 0:
|
if state == 0:
|
||||||
if (GPIO.input(KEY_PRESS_PIN) == 0) or (GPIO.input(KEY1_PIN) == 0) or (GPIO.input(KEY2_PIN) == 0) or (GPIO.input(KEY3_PIN) == 0):
|
if press == 1:
|
||||||
# button is pressed
|
press = 0
|
||||||
|
state = 1
|
||||||
|
start = time.time()
|
||||||
|
|
||||||
|
# Initialize the display...
|
||||||
|
serial = spi(device=0, port=0, bus_speed_hz = 8000000, transfer_size = 4096, gpio_DC = DC_PIN, gpio_RST = RST_PIN)
|
||||||
|
device = sh1106(serial, rotate=2) #sh1106
|
||||||
|
image = Image.new('1', (width, height))
|
||||||
|
draw = ImageDraw.Draw(image)
|
||||||
|
draw.rectangle((0,0,width,height), outline=0, fill=0)
|
||||||
|
elif GPIO.input(KEY_PRESS_PIN) == 0:
|
||||||
press = 1
|
press = 1
|
||||||
else: # button is released:
|
elif GPIO.input(KEY1_PIN) == 0:
|
||||||
if press == 1:
|
press = 1
|
||||||
press = 0
|
elif GPIO.input(KEY2_PIN) == 0:
|
||||||
state = 1
|
press = 1
|
||||||
serial = spi(device=0, port=0, bus_speed_hz = 8000000, transfer_size = 4096, gpio_DC = DC_PIN, gpio_RST = RST_PIN)
|
elif GPIO.input(KEY3_PIN) == 0:
|
||||||
device = sh1106(serial, rotate=2) #sh1106
|
press = 1
|
||||||
image = Image.new('1', (width, height))
|
else:
|
||||||
draw = ImageDraw.Draw(image)
|
pass
|
||||||
draw.rectangle((0,0,width,height), outline=0, fill=0)
|
else: #state == 1
|
||||||
|
if press == 1:
|
||||||
|
GPIO.output(RST_PIN,GPIO.LOW)
|
||||||
|
press = 0
|
||||||
|
state = 0
|
||||||
|
elif (GPIO.input(KEY_PRESS_PIN) == 0) or (GPIO.input(KEY1_PIN) == 0) or (GPIO.input(KEY2_PIN) == 0) or (GPIO.input(KEY3_PIN) == 0) or (stamp - start) > SCREEN_SAVER:
|
||||||
|
press = 1 # Button pressed again, turning off display
|
||||||
|
elif (stamp - drawn) > 1:
|
||||||
|
drawn = stamp
|
||||||
|
with canvas(device) as draw:
|
||||||
|
DTTM = subprocess.check_output("date +\"%Y-%m-%d %H:%M:%S\"", shell = True)
|
||||||
|
ADDR = subprocess.check_output("hostname -I | cut -d\' \' -f1", shell = True )
|
||||||
|
CPUL = subprocess.check_output("top -bn1 | grep load | awk '{printf \"CPU : %.2f\", $(NF-2)}'", shell = True )
|
||||||
|
MEMU = subprocess.check_output("free -m | awk 'NR==2{printf \"Mem : %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'", shell = True )
|
||||||
|
DISK = subprocess.check_output("df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'", shell = True )
|
||||||
|
TEMP = subprocess.check_output("/opt/vc/bin/vcgencmd measure_temp | awk '{gsub(/temp=/,\" \"); print}'", shell = True )
|
||||||
|
|
||||||
if state == 1:
|
draw.text((xpos, top), str(DTTM), font=font, fill=255)
|
||||||
with canvas(device) as draw:
|
draw.text((xpos, top+12), "IP : " + str(ADDR), font=font, fill=255)
|
||||||
cmd = "hostname -I | cut -d\' \' -f1"
|
draw.text((xpos, top+20), str(CPUL), font=font, fill=255)
|
||||||
IP = subprocess.check_output(cmd, shell = True )
|
draw.text((xpos, top+28), str(MEMU), font=font, fill=255)
|
||||||
cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
|
draw.text((xpos, top+36), str(DISK), font=font, fill=255)
|
||||||
CPU = subprocess.check_output(cmd, shell = True )
|
draw.text((xpos, top+45), "Temp:" + str(TEMP), font=font, fill=255)
|
||||||
cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
|
else:
|
||||||
MemUsage = subprocess.check_output(cmd, shell = True )
|
pass
|
||||||
cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
|
|
||||||
Disk = subprocess.check_output(cmd, shell = True )
|
|
||||||
cmd = "/opt/vc/bin/vcgencmd measure_temp"
|
|
||||||
Temp = subprocess.check_output(cmd, shell = True )
|
|
||||||
|
|
||||||
draw.text((xpos, top), "IP: " + str(IP), font=font, fill=255)
|
|
||||||
draw.text((xpos, top+8), str(CPU), font=font, fill=255)
|
|
||||||
draw.text((xpos, top+16), str(MemUsage), font=font, fill=255)
|
|
||||||
draw.text((xpos, top+24), str(Disk), font=font, fill=255)
|
|
||||||
draw.text((xpos, top+33), str(Temp), font=font, fill=255)
|
|
||||||
|
|
||||||
if (GPIO.input(KEY_PRESS_PIN) == 0) or (GPIO.input(KEY1_PIN) == 0) or (GPIO.input(KEY2_PIN) == 0) or (GPIO.input(KEY3_PIN) == 0):
|
|
||||||
# button is pressed
|
|
||||||
press = 1
|
|
||||||
else: # button is released
|
|
||||||
if press == 1:
|
|
||||||
GPIO.output(RST_PIN,GPIO.LOW)
|
|
||||||
press = 0
|
|
||||||
state = 0
|
|
||||||
|
|
||||||
except:
|
except:
|
||||||
print("except")
|
print("Stopped")
|
||||||
GPIO.cleanup()
|
GPIO.cleanup()
|
||||||
|
|||||||
Reference in New Issue
Block a user