Use better way to improve button responsiveness

This commit is contained in:
pangduckwai
2019-04-21 02:10:09 +08:00
parent cf080daab9
commit ae15f458b6
+40 -39
View File
@@ -28,7 +28,16 @@ BTN1_PIN = 21
BTN2_PIN = 20
BTN3_PIN = 16
#init GPIO
# Some constants
font = ImageFont.load_default()
width = 128
height = 64
padding = -2
top = padding
bottom = height-padding
xpos = 2
# init GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(JS_U_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(JS_D_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
@@ -39,22 +48,39 @@ GPIO.setup(BTN1_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(BTN2_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(BTN3_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
font = ImageFont.load_default()
width = 128
height = 64
padding = -2
top = padding
bottom = height-padding
xpos = 0
# 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
draw = ImageDraw.Draw(Image.new('1', (width, height)))
draw.rectangle((0,0,width,height), outline=0, fill=0)
SCREEN_SAVER = 20.0
press = 1
state = 0
stamp = time.time()
start = time.time()
def draw_process(channel):
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)
def main_process(channel):
global serial
global device
global draw
global state
global start
if state == 0: # Display is off
if channel > 0:
state = 1
@@ -66,6 +92,7 @@ def main_process(channel):
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw_process(channel)
else:
pass
elif state == 1:
@@ -75,20 +102,7 @@ def main_process(channel):
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)
draw_process(channel)
else:
pass
@@ -98,23 +112,10 @@ GPIO.add_event_detect(BTN2_PIN, GPIO.RISING, callback=main_process, bouncetime=2
GPIO.add_event_detect(BTN3_PIN, GPIO.RISING, callback=main_process, bouncetime=200)
try:
main_process(1)
while True:
stamp = time.time()
if GPIO.event_detected(JS_P_PIN):
press = JS_P_PIN
if GPIO.event_detected(BTN1_PIN):
press = BTN1_PIN
if GPIO.event_detected(BTN2_PIN):
press = BTN2_PIN
if GPIO.event_detected(BTN3_PIN):
press = BTN3_PIN
main_process(press)
press = 0
main_process(0)
time.sleep(1)
except: