Improve button responsiveness (WIP)

This commit is contained in:
pangduckwai
2019-04-20 15:19:26 +08:00
parent 322bdea68e
commit cf080daab9
+30 -26
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,34 +49,14 @@ 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
start = time.time()
try:
while True:
stamp = time.time() stamp = time.time()
start = time.time()
if GPIO.event_detected(JS_P_PIN): def main_process(channel):
press = 1
if GPIO.event_detected(BTN1_PIN):
press = 2
if GPIO.event_detected(BTN2_PIN):
press = 4
if GPIO.event_detected(BTN3_PIN):
press = 8
if state == 0: # Display is off if state == 0: # Display is off
if press > 0: if channel > 0:
press = 0
state = 1 state = 1
start = time.time() start = time.time()
@@ -88,9 +69,8 @@ try:
else: else:
pass pass
elif state == 1: elif state == 1:
if (press > 0) or ((stamp - start) > SCREEN_SAVER): # Signal to turn off display if (channel > 0) or ((stamp - start) > SCREEN_SAVER): # Signal to turn off display
GPIO.output(RST_PIN,GPIO.LOW) GPIO.output(RST_PIN,GPIO.LOW)
press = 0
state = 0 state = 0
else: else:
pass pass
@@ -112,8 +92,32 @@ try:
else: else:
pass 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:
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
time.sleep(1) time.sleep(1)
except: except:
print "Stopped" print "Stopped", sys.exc_info()[0]
raise
GPIO.cleanup() GPIO.cleanup()