#!/usr/bin/python3
# -*- coding: utf-8 -*-
# example gpiozero code that could be used to have a reboot
#  and a shutdown function on one GPIO button
# scruss - 2017-10

use_button=27                       # lowest button on PiTFT+

from gpiozero import Button
from signal import pause
import os
import time
import subprocess
import sys

held_for=0.0

def rls():
        global held_for
        if (held_for > 10.0):
                #do something before shutdown here
                #restart networking
                os.system('sudo /etc/init.d/networking restart')
                time.sleep(2)
                os.system('sudo wpa_supplicant -c /etc/wpa_supplicant.conf -i wlan0')
                #10 seconds for authentication
                time.sleep(10)
                #upload logs
                subprocess.call("/home/pi/bin/kismetupload.sh", shell=True)
                #shutdown
                os.system('sudo shutdown now')
       #removed any contingencies for button holds shorter than 10seconds, reboots etc..

def hld():
        # callback for when button is held
        #  is called every hold_time seconds
        global held_for
        # need to use max() as held_time resets to zero on last callback
        held_for = max(held_for, button.held_time + button.hold_time)
        # added call to rls() function to shutdown while button is held, ignoring release.
        rls()

button=Button(use_button, hold_time=1.0, hold_repeat=True)
button.when_held = hld
button.when_released = rls

pause() # wait forever