Add timer to turn off screen

This commit is contained in:
pangduckwai
2019-04-19 13:07:10 +08:00
parent ac03b3df64
commit d1b2353e60
+51 -41
View File
@@ -47,52 +47,62 @@ top = padding
bottom = height-padding
xpos = 0
SCREEN_SAVER = 30.0
press = 1
state = 0
press = 0
start = time.time()
drawn = time.time()
try:
while 1:
stamp = time.time()
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):
# button is pressed
if press == 1:
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
else: # button is released:
if press == 1:
press = 0
state = 1
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(KEY1_PIN) == 0:
press = 1
elif GPIO.input(KEY2_PIN) == 0:
press = 1
elif GPIO.input(KEY3_PIN) == 0:
press = 1
else:
pass
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:
with canvas(device) as draw:
cmd = "hostname -I | cut -d\' \' -f1"
IP = subprocess.check_output(cmd, shell = True )
cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
CPU = subprocess.check_output(cmd, shell = True )
cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
MemUsage = subprocess.check_output(cmd, shell = True )
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
draw.text((xpos, top), str(DTTM), font=font, fill=255)
draw.text((xpos, top+12), "IP : " + str(ADDR), font=font, fill=255)
draw.text((xpos, top+20), str(CPUL), font=font, fill=255)
draw.text((xpos, top+28), str(MEMU), font=font, fill=255)
draw.text((xpos, top+36), str(DISK), font=font, fill=255)
draw.text((xpos, top+45), "Temp:" + str(TEMP), font=font, fill=255)
else:
pass
except:
print("except")
GPIO.cleanup()
print("Stopped")
GPIO.cleanup()