Skip to content

Commit

Permalink
A lil better buttons handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pskowronek committed Sep 24, 2018
1 parent 6eb4641 commit 0c505e0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ install:
- pip install -r requirements.txt
- pip install flake8
script:
- "flake8 --show-source --ignore=W293,E201,E202,E501,W291,E221,E231,E203,W391,E303,E251 --exclude=\"epd*,venv/*\""
- "flake8 --show-source --ignore=W293,E201,E202,E501,W291,E221,E231,E203,W391,E303,E251,E731 --exclude=\"epd*,venv/*\""

23 changes: 11 additions & 12 deletions buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ class Buttons(object):
action_in_progress = False


def __init__(self, key1, key1_action, key2, key2_action, key3, key3_action, key4, key4_action, close_action):
def __init__(self, keys, button_handler):
GPIO.setmode(GPIO.BCM)
GPIO.setup(key1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(key2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(key3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(key4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(keys[0], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(keys[1], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(keys[2], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(keys[3], GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.add_event_detect(key1, GPIO.FALLING, callback=lambda pin: self.button_pressed(1, key1_action, close_action), bouncetime=200)
GPIO.add_event_detect(key2, GPIO.FALLING, callback=lambda pin: self.button_pressed(2, key2_action, close_action), bouncetime=200)
GPIO.add_event_detect(key3, GPIO.FALLING, callback=lambda pin: self.button_pressed(3, key3_action, close_action), bouncetime=200)
GPIO.add_event_detect(key4, GPIO.FALLING, callback=lambda pin: self.button_pressed(4, key4.action, close_action), bouncetime=200)
GPIO.add_event_detect(keys[0], GPIO.FALLING, callback=lambda pin: self.button_pressed(1, button_handler), bouncetime=200)
GPIO.add_event_detect(keys[1], GPIO.FALLING, callback=lambda pin: self.button_pressed(2, button_handler), bouncetime=200)
GPIO.add_event_detect(keys[2], GPIO.FALLING, callback=lambda pin: self.button_pressed(3, button_handler), bouncetime=200)
GPIO.add_event_detect(keys[3], GPIO.FALLING, callback=lambda pin: self.button_pressed(4, button_handler), bouncetime=200)


def set_busy(self):
Expand All @@ -34,15 +34,14 @@ def busy(self):
return self.action_in_progress


def button_pressed(self, buttonNo, open_action, close_action):
def button_pressed(self, buttonNo, open_action):
if self.busy():
logging.info("Button #{} ignored".format(buttonNo))
return
try:
self.set_busy()
logging.info("Button #{} pressed".format(buttonNo))
open_action()
open_action(buttonNo)
finally:
logging.info("Finishing button key press handling")
close_action()

4 changes: 2 additions & 2 deletions epaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def display_system_details(self):
self.display_buffer(black_frame, red_frame, 'system')


def display_main_screen(self, dt):
def display_main_screen(self, dt, force = False):
time_format = "%H%M"
formatted = dt.strftime(time_format)

Expand All @@ -171,7 +171,7 @@ def display_main_screen(self, dt):
if int(h) in dead_range:
formatted = "{} ".format(h)

if formatted != self._str_time:
if force or formatted != self._str_time:

weather_data = self.weather.get()
logging.info("--- weather: " + json.dumps(weather_data))
Expand Down
92 changes: 53 additions & 39 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@

DEBUG_MODE = os.environ.get("EPAPER_DEBUG_MODE", "false") == "true"
shutting_down = False
after_details = False
details_to_display = None
epaper = None


def main():
global epaper
global shutting_down
global after_details
global details_to_display

epaper = EPaper(debug_mode=DEBUG_MODE)

Expand All @@ -63,62 +63,76 @@ def main():
if not DEBUG_MODE and (os.environ.get("EPAPER_BUTTONS_ENABLED", "true") == "true"):
from buttons import Buttons
buttons = Buttons(
int(os.environ.get("EPAPER_GPIO_PIN_FOR_KEY1", "5")),
lambda: epaper.display_gmaps_details(),
int(os.environ.get("EPAPER_GPIO_PIN_FOR_KEY2", "6")),
lambda: epaper.display_airly_details(),
int(os.environ.get("EPAPER_GPIO_PIN_FOR_KEY3", "13")),
lambda: epaper.display_weather_details(),
int(os.environ.get("EPAPER_GPIO_PIN_FOR_KEY4", "19")),
lambda: epaper.display_system_details(),
lambda: action_after_details()
[
int(os.environ.get("EPAPER_GPIO_PIN_FOR_KEY1", "5")),
int(os.environ.get("EPAPER_GPIO_PIN_FOR_KEY2", "6")),
int(os.environ.get("EPAPER_GPIO_PIN_FOR_KEY3", "13")),
int(os.environ.get("EPAPER_GPIO_PIN_FOR_KEY4", "19"))
],
lambda key: action_button(key, epaper)
)

notifier = sdnotify.SystemdNotifier()
notifier.notify("READY=1")

while True:
notifier.notify("WATCHDOG=1")
logging.info("Going to refresh the main screen...")

if shutting_down:
logging.info("... or not - app is shutting down.")
logging.info("App is shutting down.....")
break
if buttons is None or not buttons.busy():
if buttons:
buttons.set_busy()
refresh_main_screen(epaper)
if buttons:
buttons.set_not_busy()

notifier.notify("WATCHDOG=1")

if details_to_display is not None:
logging.info("Going to refresh the main screen with details view...")
details_to_display()
details_to_display = None
buttons.set_not_busy()
for i in range(10):
time.sleep(0.5)
if details_to_display is not None:
logging.info("Got button pressed while in details!")
break
if details_to_display is not None:
continue
logging.info("Ok, enough - going back to standard view")
refresh_main_screen(epaper, force = True)
else:
logging.info("Ignoring main screen refresh due to button's orignating action")

for i in range(30):
if after_details:
logging.info("State after button press - wait a while before refreshing the main window (to keep the info on display)...")
time.sleep(5)
after_details = False
buttons.set_not_busy()
logging.info("Going to refresh the main screen...")
refresh_main_screen(epaper)

for i in range(120):
if shutting_down:
logging.info("App is shutting down...")
break
if details_to_display is not None:
logging.info("Got button pressed!")
break
time.sleep(2)
time.sleep(0.5)


def action_button(key, epaper):
global details_to_display
if key == 1:
details_to_display = lambda: epaper.display_gmaps_details()
elif key == 2:
details_to_display = lambda: epaper.display_airly_details()
elif key == 3:
details_to_display = lambda: epaper.display_weather_details()
elif key == 4:
details_to_display = lambda: epaper.display_system_details()
else:
details_to_display = None


def refresh_main_screen(epaper):
def refresh_main_screen(epaper, force = False):
utc_dt = datetime.now(timezone('UTC')) # time readings should be done in epaper itself (probably using acquire.py w/o caching)
epaper.display_main_screen(utc_dt.astimezone(get_localzone()))
epaper.display_main_screen(utc_dt.astimezone(get_localzone()), force)
if DEBUG_MODE:
epaper.display_weather_details()
epaper.display_airly_details()
epaper.display_gmaps_details()


def action_after_details():
global after_details

logging.info("Informing main thread that button action has just finished")
after_details = True


def signal_hook(*args):
if shutdown_hook():
logging.info("calling exit 0")
Expand Down

0 comments on commit 0c505e0

Please sign in to comment.