From a1621fd1eb96dc4f7620abfde90b76d42bddfadc Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Mon, 30 Jul 2018 23:36:48 +0200 Subject: [PATCH] cleanup correctly --- README.md | 3 ++- raspberry-environment/display.py | 44 ++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 1e9cdd5..6bfcfe0 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,8 @@ echo "dtoverlay=dwc2" >> /boot/config.txt echo "dtoverlay=gpio-poweroff,gpiopin=5,active_low" >> /boot/config.txt -sed -i /boot/config.txt -e "s/^hdmi_force_hotplug=1$/hdmi_force_hotplug=1$/" +sed -i /boot/config.txt -e "s/^#hdmi_force_hotplug=1$/hdmi_force_hotplug=1$/" +sed -i /boot/config.txt -e "s/^#disable_overscan=1$/disable_overscan=1$/" echo "libcomposite" >> /etc/modules diff --git a/raspberry-environment/display.py b/raspberry-environment/display.py index fc5a365..fb89680 100644 --- a/raspberry-environment/display.py +++ b/raspberry-environment/display.py @@ -7,6 +7,7 @@ __author__ = "Hendrik Langer " import multiprocessing import os.path +import signal import sys import threading import time @@ -125,6 +126,31 @@ def os_echo_bin(filename, string): with open(filename, 'wb') as f: f.write(string) +def cleanup(): + g="/sys/kernel/config/usb_gadget/freezer/" + GPIO.output(pin_led_busy, GPIO.LOW) + GPIO.output(pin_led_ready, GPIO.LOW) + subprocess.run(['killall', 'fim'], shell=False, check=False) + subprocess.run(['umount', '/mnt'], shell=False, check=False) + os_echo(g+"UDC", "") + time.sleep(0.5) + subprocess.run(['rmdir', g+"configs/c.1/strings/0x409"], shell=False, check=False) + subprocess.run(['rm', g+"configs/c.1/mass_storage.usb0"], shell=False, check=False) + subprocess.run(['rm', g+"configs/c.1/hid.usb0"], shell=False, check=False) + subprocess.run(['rmdir', g+"configs/c.1"], shell=False, check=False) + subprocess.run(['rmdir', g+"functions/mass_storage.usb0"], shell=False, check=False) + subprocess.run(['rmdir', g+"functions/hid.usb0"], shell=False, check=False) + subprocess.run(['rmdir', g+"strings/0x409"], shell=False, check=False) + subprocess.run(['rmdir', g], shell=False, check=False) + subprocess.run(['modprobe', '-r', 'usb_f_mass_storage'], shell=False, check=False) + subprocess.run(['modprobe', '-r', 'usb_f_hid'], shell=False, check=False) + subprocess.run(['modprobe', '-r', 'libcomposite'], shell=False, check=False) + +def signal_term_handler(signal, frame): + print('got SIGTERM') + cleanup() + sys.exit(0) + def send_keypress(): with open('/dev/hidg0', 'wb') as f: @@ -155,6 +181,8 @@ if __name__ == '__main__': subprocess.run('cp /opt/freezer/freeze.exe /mnt/', shell=True, check=True) subprocess.run(['umount', '/mnt'], shell=False, check=True) + signal.signal(signal.SIGTERM, signal_term_handler) + # http://irq5.io/2016/12/22/raspberry-pi-zero-as-multiple-usb-gadgets/ # http://isticktoit.net/?p=1383 subprocess.run(['modprobe', 'libcomposite'], shell=False, check=True) @@ -223,7 +251,7 @@ if __name__ == '__main__': p = subprocess.Popen(['tvservice', '-s'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) _stdout, _stderr = p.communicate() if (len(_stdout) > 0 and _stdout == fallback_str): - subprocess.run(['tvservice', '--explicit="CEA 16 HDMI"'], shell=False, check=False) + subprocess.run('tvservice --explicit="CEA 16 HDMI"', shell=True, check=False) subprocess.run(['fbset', '-fb', '/dev/fb0', '-depth', '16'], shell=False, check=False) subprocess.run(['fbset', '-fb', '/dev/fb0', '-depth', '32'], shell=False, check=False) @@ -264,18 +292,6 @@ if __name__ == '__main__': watch_delay_call('/tmp/usbdisk.img', callback, 0.2, True, pyinotify.IN_MODIFY) finally: - GPIO.output(pin_led_busy, GPIO.LOW) - GPIO.output(pin_led_ready, GPIO.LOW) - subprocess.run(['killall', 'fim'], shell=False, check=False) - subprocess.run(['umount', '/mnt'], shell=False, check=False) - os_echo(g+"UDC", "") - time.sleep(0.5) - subprocess.run(['rmdir', g+"configs/c.1/strings/0x409"], shell=False, check=False) - subprocess.run(['rmdir', g+"configs/c.1"], shell=False, check=False) - subprocess.run(['rmdir', g+"functions/mass_storage.usb0"], shell=False, check=False) - subprocess.run(['rmdir', g+"strings/0x409"], shell=False, check=False) - subprocess.run(['rmdir', g], shell=False, check=False) - subprocess.run(['modprobe', '-r', 'usb_f_mass_storage'], shell=False, check=False) - subprocess.run(['modprobe', '-r', 'libcomposite'], shell=False, check=False) + cleanup() sys.exit(0)