Improve button responsiveness (WIP)

This commit is contained in:
pangduckwai
2019-04-20 15:19:26 +08:00
parent 322bdea68e
commit cf080daab9
+54 -50
View File
@@ -7,6 +7,7 @@ from luma.core import lib
from luma.oled.device import sh1106 from luma.oled.device import sh1106
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import sys
import time import time
import subprocess import subprocess
@@ -48,72 +49,75 @@ bottom = height-padding
xpos = 0 xpos = 0
SCREEN_SAVER = 20.0 SCREEN_SAVER = 20.0
GPIO.add_event_detect(JS_P_PIN, GPIO.RISING)
GPIO.add_event_detect(BTN1_PIN, GPIO.RISING)
GPIO.add_event_detect(BTN2_PIN, GPIO.RISING)
GPIO.add_event_detect(BTN3_PIN, GPIO.RISING)
press = 1 press = 1
state = 0 state = 0
stamp = time.time()
start = time.time() start = time.time()
def main_process(channel):
if state == 0: # Display is off
if channel > 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)
else:
pass
elif state == 1:
if (channel > 0) or ((stamp - start) > SCREEN_SAVER): # Signal to turn off display
GPIO.output(RST_PIN,GPIO.LOW)
state = 0
else:
pass
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 | awk 'NR==3{printf \"CPU : %.1f%% idle\", $8}'", 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 )
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
GPIO.add_event_detect(JS_P_PIN, GPIO.RISING, callback=main_process, bouncetime=200)
GPIO.add_event_detect(BTN1_PIN, GPIO.RISING, callback=main_process, bouncetime=200)
GPIO.add_event_detect(BTN2_PIN, GPIO.RISING, callback=main_process, bouncetime=200)
GPIO.add_event_detect(BTN3_PIN, GPIO.RISING, callback=main_process, bouncetime=200)
try: try:
while True: while True:
stamp = time.time() stamp = time.time()
if GPIO.event_detected(JS_P_PIN): if GPIO.event_detected(JS_P_PIN):
press = 1 press = JS_P_PIN
if GPIO.event_detected(BTN1_PIN): if GPIO.event_detected(BTN1_PIN):
press = 2 press = BTN1_PIN
if GPIO.event_detected(BTN2_PIN): if GPIO.event_detected(BTN2_PIN):
press = 4 press = BTN2_PIN
if GPIO.event_detected(BTN3_PIN): if GPIO.event_detected(BTN3_PIN):
press = 8 press = BTN3_PIN
if state == 0: # Display is off
if press > 0:
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)
else:
pass
elif state == 1:
if (press > 0) or ((stamp - start) > SCREEN_SAVER): # Signal to turn off display
GPIO.output(RST_PIN,GPIO.LOW)
press = 0
state = 0
else:
pass
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 | awk 'NR==3{printf \"CPU : %.1f%% idle\", $8}'", 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 )
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
main_process(press)
press = 0
time.sleep(1) time.sleep(1)
except: except:
print "Stopped" print "Stopped", sys.exc_info()[0]
raise
GPIO.cleanup() GPIO.cleanup()