#!/usr/bin/env python
#
# 30.07.2014 - AKR - Runs in the background and detects if a
# button on the TFT display was pressed. Used to switch on/off
# the backlight.
#

import RPi.GPIO as GPIO
import os
from time import sleep

GPIO.setmode(GPIO.BCM)

#button23 = 23 # button 1 poweroff (leftmost)
#button22 = 22
#button27 = 27
button_backlight = 18 # button 4 (rightmost)

# prepare to control the TFT backlight
os.system("echo 252 > /sys/class/gpio/export")
os.system("echo 'out' > /sys/class/gpio/gpio252/direction")
os.system("echo '1' > /sys/class/gpio/gpio252/value")


tfton = True

def switchdebounce(button_backlight):
	global tfton
	if tfton == True:
		tfton = False
		os.system("echo '0' > /sys/class/gpio/gpio252/value")
	else:
		tfton = True
		os.system("echo '1' > /sys/class/gpio/gpio252/value")

GPIO.setup(button_backlight, GPIO.IN, GPIO.PUD_UP) # pull_up_down
# callback thread
GPIO.add_event_detect(button_backlight, GPIO.RISING, callback=switchdebounce, bouncetime=300)

# main thread
while True:
	#if GPIO.event_detected(button_backlight):
		#print(button_backlight)
	# do something
	sleep(5)

GPIO.remove_event_detect(button_backlight)
GPIO.cleanup()

