signal handling for controlled exit

This commit is contained in:
Patrick Tschuchnig 2019-08-27 14:58:49 +02:00
parent f86cca398c
commit a46cf22eb1

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
#coding=utf-8 #coding=utf-8
import pygame, sys, time, os, RPi.GPIO as GPIO import pygame, signal, sys, time, os, RPi.GPIO as GPIO
from pygame.locals import * from pygame.locals import *
#GPIO #GPIO
@ -35,6 +35,10 @@ mainFontColor = redColor
#fonts #fonts
font = pygame.font.Font('freesansbold.ttf', 90) font = pygame.font.Font('freesansbold.ttf', 90)
def signal_handler(sig, frame):
print('Programm exiting')
exit_application()
def toggle_fullscreen(): def toggle_fullscreen():
screen = pygame.display.get_surface() screen = pygame.display.get_surface()
tmp = screen.convert() tmp = screen.convert()
@ -79,12 +83,16 @@ def handle_events():
def exit_application(): def exit_application():
pygame.quit() pygame.quit()
GPIO.cleanup()
sys.exit() sys.exit()
return return
def shutdown_raspberry(): def shutdown_raspberry():
os.system("sudo shutdown -h now") os.system("sudo shutdown -h now")
return return
#signal handing for controlled exit via ctrl+c
signal.signal(signal.SIGINT, signal_handler)
pygame.mouse.set_visible(False) # hide mouse pygame.mouse.set_visible(False) # hide mouse
@ -94,6 +102,8 @@ screen = pygame.display.set_mode((screenSizeX, screenSizeY), pygame.FULLSCREEN)
#screen = toggle_fullscreen() #screen = toggle_fullscreen()
while True: # main loop while True: # main loop
#global label storage #global label storage
time_surface = 0 time_surface = 0